Symphony Of Empires
progress_bar.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/progress_bar.cpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #include "eng3d/ui/progress_bar.hpp"
26 #include "eng3d/ui/widget.hpp"
27 #include "eng3d/ui/input.hpp"
28 #include "eng3d/ui/ui.hpp"
29 #include "eng3d/texture.hpp"
30 #include "eng3d/rectangle.hpp"
31 #include "eng3d/state.hpp"
32 #include "eng3d/primitive.hpp"
33 
34 using namespace UI;
35 
36 ProgressBar::ProgressBar(int _x, int _y, unsigned w, unsigned h, const float _min, const float _max, Widget* _parent)
37  : Widget(_parent, _x, _y, w, h, UI::WidgetType::PROGRESS_BAR),
38  min{ _min },
39  max{ _max }
40 {
41 
42 }
43 
46  g_ui_context->obj_shader->set_texture(0, "diffuse_map", *g_ui_context->button);
47  g_ui_context->obj_shader->set_uniform("diffuse_color", glm::vec4(1.f, 1.f, 1.f, 1.f));
48  Eng3D::Square(0, 0, width, height).draw();
49  auto ratio = (value == 0 || max == 0) ? 0.f : value / max;
50  Eng3D::Rect pos_rect(0, 0, width, height);
51  switch(direction) {
53  pos_rect.right = width * ratio;
54  break;
56  pos_rect.left = width * (1.f - ratio);
57  break;
59  pos_rect.bottom = height * ratio;
60  break;
62  pos_rect.top = height * (1.f - ratio);
63  break;
64  }
65 
66  g_ui_context->obj_shader->set_texture(0, "diffuse_map", *g_ui_context->window_top);
67  g_ui_context->obj_shader->set_uniform("diffuse_color", glm::vec4(1.f, 1.f, 1.f, 1.f));
68  Eng3D::Square(pos_rect.left, pos_rect.top, pos_rect.right, pos_rect.bottom).draw();
69 
70  if(text_texture.get() != nullptr) {
71  g_ui_context->obj_shader->set_uniform("diffuse_color", glm::vec4(text_color.r, text_color.g, text_color.b, text_color.a));
72  draw_rectangle(4, 2, text_texture->width, text_texture->height, viewport, text_texture.get());
73  }
74 }
The UI context that handles all the ui widgets.
Definition: ui.hpp:63
std::shared_ptr< Eng3D::Texture > button
Definition: ui.hpp:150
std::unique_ptr< Eng3D::OpenGL::Program > obj_shader
Definition: ui.hpp:159
std::shared_ptr< Eng3D::Texture > window_top
Definition: ui.hpp:149
virtual void on_render(UI::Context &ctx, Eng3D::Rect viewport) override
ProgressBar(int x, int y, unsigned w, unsigned h, float min, float max, Widget *_parent=nullptr)
UI::Direction direction
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_rectangle(int x, int y, unsigned w, unsigned h, Eng3D::Rect viewport, const Eng3D::Texture *tex)
Definition: widget.cpp:221
std::shared_ptr< Eng3D::Texture > text_texture
Definition: widget.hpp:328
size_t width
Definition: widget.hpp:325
Eng3D::Color text_color
Definition: widget.hpp:332
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
float g
Definition: color.hpp:33
float b
Definition: color.hpp:33
float r
Definition: color.hpp:33
float a
Definition: color.hpp:34
void draw(int instances=0) const
Definition: mesh.hpp:205