Symphony Of Empires
checkbox.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/checkbox.cpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #include "eng3d/ui/checkbox.hpp"
26 #include "eng3d/ui/div.hpp"
27 #include "eng3d/texture.hpp"
28 #include "eng3d/state.hpp"
29 
30 using namespace UI;
31 
32 //
33 // Checkbox
34 //
36  : Widget(_parent, 0, 0, 24, 24, UI::WidgetType::CHECKBOX)
37 {
38  init_checkbox(24);
39 }
40 
41 Checkbox::Checkbox(int size, Widget* _parent)
42  : Widget(_parent, 0, 0, size, size, UI::WidgetType::CHECKBOX)
43 {
44  init_checkbox(size);
45 }
46 
47 Checkbox::Checkbox(int _x, int _y, unsigned w, unsigned h, Widget* _parent)
48  : Widget(_parent, _x, _y, w, h, UI::WidgetType::CHECKBOX)
49 {
50  auto min_size = glm::min<int>(w, h);
51  init_checkbox(min_size);
52 }
53 
54 void Checkbox::init_checkbox(int size) {
55  this->unchecked_texture = Eng3D::State::get_instance().tex_man.load(Eng3D::State::get_instance().package_man.get_unique("gfx/checkbox_false.png"));
56  this->checked_texture = Eng3D::State::get_instance().tex_man.load(Eng3D::State::get_instance().package_man.get_unique("gfx/checkbox_true.png"));
57 
58  this->box = new Div(0, 0, size, size, this);
59  this->box->current_texture = this->unchecked_texture;
60  this->on_click = ([this](UI::Widget& w) {
61  this->set_value(!this->value);
62  if(this->outside_on_click)
63  this->outside_on_click(w);
64  });
65  this->text_offset_x = size + 4;
66  this->clickable_effect = false;
67 }
68 
69 bool Checkbox::get_value() const {
70  return this->value;
71 }
72 
73 void Checkbox::set_value(bool checked) {
74  this->value = checked;
75  if(this->value) {
76  this->box->current_texture = this->checked_texture;
77  } else {
78  this->box->current_texture = this->unchecked_texture;
79  }
80 }
81 
82 void Checkbox::set_on_click(std::function<void(Widget&)> _on_click) {
83  this->outside_on_click = _on_click;
84 }
85 
86 void Checkbox::set_text(const std::string& _text) {
87  UI::Widget::set_text(_text);
88  this->width = this->text_texture->width + this->text_offset_x;
89  this->height = glm::max(this->text_texture->height, this->box->height);
90 }
static State & get_instance()
Definition: state.cpp:514
Eng3D::TextureManager tex_man
Definition: state.hpp:124
std::shared_ptr< Eng3D::Texture > load(const std::string &path, TextureOptions options=default_options)
Finds a texture in the list of a texture manager if the texture is already in the list we load the sa...
Definition: texture.cpp:432
The master widget all the other widgets inherit from, do not use directly instead use one of the many...
Definition: widget.hpp:176
std::function< void(UI::Widget &)> on_click
Definition: widget.hpp:350
std::shared_ptr< Eng3D::Texture > text_texture
Definition: widget.hpp:328
bool clickable_effect
Definition: widget.hpp:199
int text_offset_x
Definition: widget.hpp:329
size_t width
Definition: widget.hpp:325
std::shared_ptr< Eng3D::Texture > current_texture
Definition: widget.hpp:327
virtual void set_text(const std::string &text)
Generates text for the widget and overrides the current text texture.
Definition: widget.cpp:445
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
void set_value(bool checked)
Definition: checkbox.cpp:73
bool get_value() const
Definition: checkbox.cpp:69
void set_text(const std::string &_text) override
Generates text for the widget and overrides the current text texture.
Definition: checkbox.cpp:86
Checkbox(Widget *parent=nullptr)
Definition: checkbox.cpp:35
void set_on_click(std::function< void(Widget &)> on_click) override
Definition: checkbox.cpp:82
A basic widget without any presets.
Definition: div.hpp:38