Symphony Of Empires
chart.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 <algorithm>
29 
30 #include <glm/vec2.hpp>
31 
32 #include "eng3d/ui/widget.hpp"
33 #include "eng3d/ui/ui.hpp"
34 #include "eng3d/ui/chart.hpp"
35 #include "eng3d/texture.hpp"
36 #include "eng3d/rectangle.hpp"
37 #include "eng3d/state.hpp"
38 
39 UI::Chart::Chart(int _x, int _y, unsigned w, unsigned h, UI::Widget* _parent)
40  : UI::Widget(_parent, _x, _y, w, h, UI::WidgetType::LABEL)
41 {
42  auto& s = Eng3D::State::get_instance();
43  this->current_texture = s.tex_man.load(s.package_man.get_unique("gfx/top_win_chart.png"));
44 }
45 
46 void UI::Chart::set_data(std::vector<float> new_data) {
47  this->data = new_data;
48  this->min = this->max = glm::epsilon<float>();
49  if(!this->data.empty())
50  {
51  const auto [it1, it2] = std::minmax_element(this->data.cbegin(), this->data.cend());
52  this->min = *it1;
53  this->max = *it2;
54  }
55 }
56 
58  ctx.obj_shader->set_uniform("diffuse_color", glm::vec4(1.f));
59  if(current_texture != nullptr)
60  draw_rectangle(0, 0, width, height, viewport, current_texture.get());
61 
62  if(text_texture.get() != nullptr) {
63  ctx.obj_shader->set_uniform("diffuse_color", glm::vec4(text_color.r, text_color.g, text_color.b, 1.f));
64  draw_rectangle(4, 2, text_texture->width, text_texture->height, viewport, text_texture.get());
65  }
66 
67  ctx.obj_shader->set_uniform("diffuse_color", glm::vec4(1.f, 0.f, 0.f, 1.f));
68  ctx.obj_shader->set_texture(0, "diffuse_map", *Eng3D::State::get_instance().tex_man.get_white());
69 
70  // Draw chart itself
72  mesh.buffer.resize(this->data.size());
73 
74  const glm::vec2 scaling{ (1.f / (this->data.size() - 1)) * this->width, (1.f / this->max) * this->height };
75  for(size_t i = 0; i < this->data.size(); i++)
76  {
77  const auto& slice = this->data[i];
78  auto slice_pos = glm::vec2{ i, slice } * scaling;
79  slice_pos.y = this->height - slice_pos.y;
80  mesh.buffer[i] = Eng3D::MeshData(slice_pos, glm::vec2(0.f));
81  }
82  mesh.upload();
83  mesh.draw();
84 }
static State & get_instance()
Definition: state.cpp:514
Chart(int x, int y, unsigned w, unsigned h, Widget *_parent=nullptr)
Definition: chart.cpp:39
virtual void on_render(Context &ctx, Eng3D::Rect viewport) override
Definition: chart.cpp:57
void set_data(std::vector< float > data)
Definition: chart.cpp:46
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
std::shared_ptr< Eng3D::Texture > current_texture
Definition: widget.hpp:327
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