Symphony Of Empires
waffle.cpp
Go to the documentation of this file.
1 // Symphony of Empires
2 // Copyright (C) 2021, Symphony of Empires contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // eng3d/ui/chart.cpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #include <cstdlib>
26 #include <cstring>
27 #include <string>
28 #include <numeric>
29 #include <algorithm>
30 
31 #include <glm/vec2.hpp>
32 #include <glm/gtc/integer.hpp>
33 
34 #include "eng3d/ui/widget.hpp"
35 #include "eng3d/ui/ui.hpp"
36 #include "eng3d/ui/waffle.hpp"
37 #include "eng3d/texture.hpp"
38 #include "eng3d/rectangle.hpp"
39 #include "eng3d/state.hpp"
40 
41 UI::WaffleChart::WaffleChart(int _x, int _y, unsigned w, unsigned h, UI::Widget* _parent)
42  : UI::Widget(_parent, _x, _y, w, h, UI::WidgetType::LABEL)
43 {
44 
45 }
46 
47 void UI::WaffleChart::set_data(std::vector<UI::ChartData> new_data) {
48  this->data = new_data;
49  this->max = std::accumulate(this->data.begin(), this->data.end(), 0.f, [](const auto a, const auto& e) {
50  return a + e.num;
51  });
52  this->cols = this->width / 8.f;
53  this->rows = this->height / 8.f;
54 }
55 
57  ctx.obj_shader->set_uniform("diffuse_color", glm::vec4(1.f));
58  if(current_texture != nullptr)
59  draw_rectangle(0, 0, width, height, viewport, current_texture.get());
60 
61  if(text_texture.get() != nullptr) {
62  ctx.obj_shader->set_uniform("diffuse_color", glm::vec4(text_color.r, text_color.g, text_color.b, 1.f));
63  draw_rectangle(4, 2, text_texture->width, text_texture->height, viewport, text_texture.get());
64  }
65 
66  ctx.obj_shader->set_texture(0, "diffuse_map", *Eng3D::State::get_instance().tex_man.get_white());
67 
68  // Draw chart itself
69  const glm::vec2 scaling{ (1.f / this->cols) * this->width, (1.f / this->rows) * this->height };
70  const glm::vec2 square_size{ this->width / this->cols, this->height / this->rows };
71  const glm::vec2 aspect_ratio{ this->width / this->height, this->height / this->width };
72 
73  size_t total_squares = this->cols * this->rows, rem_squares = 0;
74  auto it = this->data.cbegin();
75  for(size_t sy = 0; sy < this->rows; sy++) {
76  for(size_t sx = 0; sx < this->cols; sx++) {
77  if(!rem_squares && it != this->data.cend()) {
78  const auto& slice = *it++;
79  ctx.obj_shader->set_uniform("diffuse_color", glm::vec4(slice.color.r, slice.color.g, slice.color.b, 1.f));
80  rem_squares = (slice.num / this->max) * total_squares;
81  }
82 
83  const auto w = 0.8f, h = 0.8f;
85  mesh.buffer.resize(4);
86  mesh.buffer[0] = Eng3D::MeshData(glm::vec2{ sx, sy + h } * scaling, glm::vec2(0.f));
87  mesh.buffer[1] = Eng3D::MeshData(glm::vec2{ sx, sy } * scaling, glm::vec2(0.f));
88  mesh.buffer[2] = Eng3D::MeshData(glm::vec2{ sx + w, sy + h } * scaling, glm::vec2(0.f));
89  mesh.buffer[3] = Eng3D::MeshData(glm::vec2{ sx + w, sy } * scaling, glm::vec2(0.f));
90  mesh.upload();
91  mesh.draw();
92  rem_squares--;
93  }
94  }
95 }
static State & get_instance()
Definition: state.cpp:514
The UI context that handles all the ui widgets.
Definition: ui.hpp:63
std::unique_ptr< Eng3D::OpenGL::Program > obj_shader
Definition: ui.hpp:159
The master widget all the other widgets inherit from, do not use directly instead use one of the many...
Definition: widget.hpp:176
WidgetType
The type of the widget, some widgets share types between them to keep simplicity.
Definition: widget.hpp:76
Packed model - packs both vertices and texcoords into the same buffer.
Definition: mesh.hpp:144
void set_data(std::vector< UI::ChartData > data)
Definition: waffle.cpp:47
virtual void on_render(Context &ctx, Eng3D::Rect viewport) override
Definition: waffle.cpp:56
WaffleChart(int x, int y, unsigned w, unsigned h, Widget *_parent=nullptr)
Definition: waffle.cpp:41