Symphony Of Empires
candle.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/candle.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/candle.hpp"
35 #include "eng3d/texture.hpp"
36 #include "eng3d/rectangle.hpp"
37 #include "eng3d/primitive.hpp"
38 #include "eng3d/state.hpp"
39 
40 UI::CandleChart::CandleChart(int _x, int _y, unsigned w, unsigned h, UI::Widget* _parent)
41  : UI::Widget(_parent, _x, _y, w, h, UI::WidgetType::LABEL)
42 {
43  auto& s = Eng3D::State::get_instance();
44  this->current_texture = s.tex_man.load(s.package_man.get_unique("gfx/top_win_chart.png"));
45 }
46 
47 void UI::CandleChart::set_data(std::vector<UI::CandleData> new_data) {
48  this->data = new_data;
49  this->min = this->max = glm::epsilon<float>();
50  if(!this->data.empty())
51  {
52  for(const auto& candle : this->data)
53  {
54  this->max = glm::max(this->max, candle.max);
55  this->min = glm::min(this->min, candle.min);
56  }
57  }
58 }
59 
61  ctx.obj_shader->set_uniform("diffuse_color", glm::vec4(1.f));
62  if(current_texture != nullptr)
63  draw_rectangle(0, 0, width, height, viewport, current_texture.get());
64 
65  if(text_texture.get() != nullptr) {
66  ctx.obj_shader->set_uniform("diffuse_color", glm::vec4(text_color.r, text_color.g, text_color.b, 1.f));
67  draw_rectangle(4, 2, text_texture->width, text_texture->height, viewport, text_texture.get());
68  }
69  ctx.obj_shader->set_texture(0, "diffuse_map", *Eng3D::State::get_instance().tex_man.get_white());
70 
71  // Draw chart itself
72  const glm::vec2 scaling{ (1.f / this->data.size()) * this->width, (1.f / this->max) * this->height };
73  for(size_t i = 0; i < this->data.size(); i++)
74  {
75  const auto& slice = this->data[i];
76  auto start = glm::vec2{ i, slice.open } * scaling;
77  start.y = this->height - start.y;
78  auto end = glm::vec2{ i + 1.f, slice.close } * scaling;
79  end.y = this->height - end.y;
80 
81  // The center to draw the candle from
82  const auto center = start + ((end - start) / 2.f);
83 
84  // Red = closing at lower than open
85  // Green = closing at higher than open
86  ctx.obj_shader->set_uniform("diffuse_color", glm::vec4(0.75f * (slice.close <= slice.open), 0.75f * (slice.close >= slice.open), 0.25f, 1.f));
87 
88  // Line of the candle
90  line_mesh.buffer.resize(2);
91  line_mesh.buffer[0] = Eng3D::MeshData(glm::vec2{ center.x, this->height - slice.max * scaling.y }, glm::vec2{});
92  line_mesh.buffer[1] = Eng3D::MeshData(glm::vec2{ center.x, this->height - slice.min * scaling.y }, glm::vec2{});
93  line_mesh.upload();
94  line_mesh.draw();
95 
96  // Candlebox
98  candle_mesh.buffer.resize(4);
99  candle_mesh.buffer[0] = Eng3D::MeshData(glm::vec2{ start.x, start.y }, glm::vec2{});
100  candle_mesh.buffer[1] = Eng3D::MeshData(glm::vec2{ start.x, end.y }, glm::vec2{});
101  candle_mesh.buffer[2] = Eng3D::MeshData(glm::vec2{ end.x, start.y }, glm::vec2{});
102  candle_mesh.buffer[3] = Eng3D::MeshData(glm::vec2{ end.x, end.y }, glm::vec2{});
103  candle_mesh.upload();
104  candle_mesh.draw();
105  }
106 }
static State & get_instance()
Definition: state.cpp:514
void set_data(std::vector< UI::CandleData > data)
Definition: candle.cpp:47
CandleChart(int x, int y, unsigned w, unsigned h, Widget *_parent=nullptr)
Definition: candle.cpp:40
virtual void on_render(Context &ctx, Eng3D::Rect viewport) override
Definition: candle.cpp:60
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