Symphony Of Empires
barchart.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/barchart.cpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #include <cstdlib>
26 #include <cstring>
27 #include <string>
28 #include <algorithm>
29 #include <glm/vec2.hpp>
30 
31 #include "eng3d/ui/barchart.hpp"
32 #include "eng3d/ui/widget.hpp"
33 #include "eng3d/ui/ui.hpp"
34 #include "eng3d/ui/tooltip.hpp"
35 #include "eng3d/texture.hpp"
36 #include "eng3d/rectangle.hpp"
37 #include "eng3d/state.hpp"
38 
39 using namespace UI;
40 
41 BarChart::BarChart(int _x, int _y, unsigned w, unsigned h, std::vector<ChartData> chart_data, Widget* _parent)
42  : Widget(_parent, _x, _y, w, h, UI::WidgetType::BAR_CHART),
43  data{ chart_data },
44  max{ 0 }
45 {
46 
47 }
48 
49 BarChart::BarChart(int _x, int _y, unsigned w, unsigned h, Widget* _parent)
50  : Widget(_parent, _x, _y, w, h, UI::WidgetType::BAR_CHART),
51  data{ std::vector<ChartData>() },
52  max{ 0 }
53 {
54 
55 }
56 
57 void BarChart::set_data(std::vector<ChartData> new_data) {
58  data = new_data;
59  max = 0;
60  for(const auto& slice : data)
61  max += slice.num;
62 }
63 
65  float counter = 0.f;
66  float last_ratio = 0.f;
67  for(auto& slice : data) {
68  counter += slice.num;
69  const float ratio = counter / max;
70 
71  Eng3D::Rect pos_rect(last_ratio * width, 0u, ratio * width, height);
72  Eng3D::Rect tex_rect(0u, 0u, 1u, 1u);
73  g_ui_context->obj_shader->set_uniform("diffuse_color", glm::vec4(slice.color.r, slice.color.g, slice.color.b, 1.f));
74  draw_rect(0, pos_rect, tex_rect, viewport);
75 
76  last_ratio = ratio;
77  }
78 }
virtual void on_render(Context &ctx, Eng3D::Rect viewport) override
Definition: barchart.cpp:64
void set_data(std::vector< ChartData > data)
Definition: barchart.cpp:57
BarChart(int x, int y, unsigned w, unsigned h, std::vector< ChartData > data=std::vector< ChartData >(), Widget *_parent=nullptr)
Definition: barchart.cpp:41
Generalized chart data, used mostly by chart widgets, however it's not specific to any widget.
Definition: widget.hpp:129
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
void draw_rect(const Eng3D::Texture *tex, Eng3D::Rect rect_pos, Eng3D::Rect rect_tex, Eng3D::Rect viewport)
Definition: widget.cpp:90
size_t width
Definition: widget.hpp:325
size_t height
Definition: widget.hpp:325
WidgetType
The type of the widget, some widgets share types between them to keep simplicity.
Definition: widget.hpp:76
Context * g_ui_context
Definition: ui.cpp:63
Definition: utils.hpp:35