lxgui
Loading...
Searching...
No Matches
gui_status_bar.cpp
1#include "lxgui/gui_status_bar.hpp"
2
3#include "lxgui/gui_alive_checker.hpp"
4#include "lxgui/gui_frame.hpp"
5#include "lxgui/gui_manager.hpp"
6#include "lxgui/gui_out.hpp"
7#include "lxgui/gui_region_tpl.hpp"
8#include "lxgui/gui_texture.hpp"
9
10#include <sstream>
11
12namespace lxgui::gui {
13
14std::array<float, 4> select_uvs(const std::array<float, 8>& uvs) {
15 return {uvs[0], uvs[1], uvs[4], uvs[5]};
16}
17
19 utils::control_block& block, manager& mgr, const frame_core_attributes& attr) :
20 frame(block, mgr, attr) {
21
22 initialize_(*this, attr);
23}
24
25std::string status_bar::serialize(const std::string& tab) const {
26 std::ostringstream str;
27
28 str << base::serialize(tab);
29 str << tab << " # Orientation: " << utils::to_string(orientation_) << "\n";
30 str << tab << " # Reversed : " << is_reversed_ << "\n";
31 str << tab << " # Value : " << value_ << "\n";
32 str << tab << " # Min value : " << min_value_ << "\n";
33 str << tab << " # Max value : " << max_value_ << "\n";
34
35 return str.str();
36}
37
38bool status_bar::can_use_script(const std::string& script_name) const {
39 return base::can_use_script(script_name) || script_name == "OnValueChanged";
40}
41
42void status_bar::copy_from(const region& obj) {
43 base::copy_from(obj);
44
45 const status_bar* bar_obj = down_cast<status_bar>(&obj);
46 if (!bar_obj)
47 return;
48
49 this->set_min_value(bar_obj->get_min_value());
50 this->set_max_value(bar_obj->get_max_value());
51 this->set_value(bar_obj->get_value());
52 this->set_bar_draw_layer(bar_obj->get_bar_draw_layer());
53 this->set_orientation(bar_obj->get_orientation());
54 this->set_reversed(bar_obj->is_reversed());
55
56 if (const texture* bar = bar_obj->get_bar_texture().get()) {
58 attr.name = bar->get_raw_name();
59 attr.inheritance = {bar_obj->get_bar_texture()};
60
61 auto bar_texture =
62 this->create_layered_region<texture>(bar->get_draw_layer(), std::move(attr));
63
64 if (bar_texture) {
65 bar_texture->set_manually_inherited(true);
66 bar_texture->notify_loaded();
67 this->set_bar_texture(bar_texture);
68 }
69 }
70}
71
72void status_bar::set_min_value(float min_value) {
73 if (min_value == min_value_)
74 return;
75
76 min_value_ = min_value;
79
80 const auto old_value = value_;
81 value_ = std::clamp(value_, min_value_, max_value_);
82
83 if (value_ != old_value) {
84 alive_checker checker(*this);
85 fire_script("OnValueChanged", {value_});
86 if (!checker.is_alive())
87 return;
88 }
89
91}
92
93void status_bar::set_max_value(float max_value) {
94 if (max_value == max_value_)
95 return;
96
97 max_value_ = max_value;
100
101 const auto old_value = value_;
102 value_ = std::clamp(value_, min_value_, max_value_);
103
104 if (value_ != old_value) {
105 alive_checker checker(*this);
106 fire_script("OnValueChanged", {value_});
107 if (!checker.is_alive())
108 return;
109 }
110
112}
113
114void status_bar::set_min_max_values(float min_value, float max_value) {
115 if (min_value == min_value_ && max_value == max_value_)
116 return;
117
118 min_value_ = std::min(min_value, max_value);
119 max_value_ = std::max(min_value, max_value);
120
121 const auto old_value = value_;
122 value_ = std::clamp(value_, min_value_, max_value_);
123
124 if (value_ != old_value) {
125 alive_checker checker(*this);
126 fire_script("OnValueChanged", {value_});
127 if (!checker.is_alive())
128 return;
129 }
130
132}
133
134void status_bar::set_value(float value) {
135 value = std::clamp(value, min_value_, max_value_);
136
137 if (value == value_)
138 return;
139
140 value_ = value;
141
142 alive_checker checker(*this);
143 fire_script("OnValueChanged", {value_});
144 if (!checker.is_alive())
145 return;
146
148}
149
151 bar_layer_ = bar_layer;
152
153 if (bar_texture_) {
154 bar_texture_->set_draw_layer(bar_layer_);
155 }
156}
157
158void status_bar::set_bar_texture(utils::observer_ptr<texture> bar_texture) {
159 bar_texture_ = std::move(bar_texture);
160 if (!bar_texture_)
161 return;
162
163 bar_texture_->set_draw_layer(bar_layer_);
164 bar_texture_->clear_all_anchors();
165
166 std::string parent = bar_texture_->get_parent().get() == this ? "$parent" : name_;
167
168 if (is_reversed_) {
169 bar_texture_->set_anchor(point::top_right, parent);
170 } else {
171 bar_texture_->set_anchor(point::bottom_left, parent);
172 }
173
174 initial_text_coords_ = select_uvs(bar_texture_->get_tex_coord());
176}
177
178void status_bar::set_bar_color(const color& bar_color) {
180
181 bar_color_ = bar_color;
182 bar_texture_->set_solid_color(bar_color_);
183}
184
186 if (orient == orientation_)
187 return;
188
189 orientation_ = orient;
191}
192
193void status_bar::set_reversed(bool reversed) {
194 if (reversed == is_reversed_)
195 return;
196
197 is_reversed_ = reversed;
198
199 if (bar_texture_) {
200 if (is_reversed_) {
201 bar_texture_->set_anchor(point::top_right);
202 } else {
203 bar_texture_->set_anchor(point::bottom_left);
204 }
205
206 if (!is_virtual_) {
207 bar_texture_->notify_borders_need_update();
208 }
209 }
210}
211
213 return min_value_;
214}
215
217 return max_value_;
218}
219
221 return value_;
222}
223
227
229 return bar_color_;
230}
231
235
237 return is_reversed_;
238}
239
241 if (bar_texture_)
242 return;
243
244 auto bar_texture = create_layered_region<texture>(bar_layer_, "$parentBarTexture");
245 if (!bar_texture)
246 return;
247
248 bar_texture->set_manually_inherited(true);
249 bar_texture->notify_loaded();
250 set_bar_texture(bar_texture);
251}
252
254 if (!bar_texture_)
255 return;
256
257 float coef = (value_ - min_value_) / (max_value_ - min_value_);
258
260 bar_texture_->set_relative_dimensions(vector2f(coef, 1.0f));
261 } else {
262 bar_texture_->set_relative_dimensions(vector2f(1.0f, coef));
263 }
264
265 std::array<float, 4> uvs = initial_text_coords_;
267 if (is_reversed_) {
268 uvs[0] = (uvs[0] - uvs[2]) * coef + uvs[2];
269 } else {
270 uvs[2] = (uvs[2] - uvs[0]) * coef + uvs[0];
271 }
272 } else {
273 if (is_reversed_) {
274 uvs[3] = (uvs[3] - uvs[1]) * coef + uvs[1];
275 } else {
276 uvs[1] = (uvs[1] - uvs[3]) * coef + uvs[3];
277 }
278 }
279
280 bar_texture_->set_tex_rect(uvs);
281}
282
283const std::vector<std::string>& status_bar::get_type_list_() const {
284 return get_type_list_impl_<status_bar>();
285}
286
287} // namespace lxgui::gui
Utility class for safe checking of region validity.
bool is_alive() const
Check if the wrapped region is still alive.
Holds a single color (float RGBA, 128 bits)
Definition gui_color.hpp:13
A region that can contain other regions and react to events.
std::string serialize(const std::string &tab) const override
Prints all relevant information about this region in a string.
Definition gui_frame.cpp:84
void copy_from(const region &obj) override
Copies a region's parameters into this frame (inheritance).
virtual void fire_script(const std::string &script_name, const event_data &data=event_data{})
Calls a script.
virtual bool can_use_script(const std::string &script_name) const
Returns 'true' if this frame can use a script.
Manages the user interface.
The base class of all elements in the GUI.
void initialize_(T &self, const region_core_attributes &attr)
Set up function to call in all derived class constructors.
A frame representing a variable-length bar.
void set_value(float value)
Sets this status_bar's value.
void set_orientation(orientation orient)
Sets this status_bar's orientation.
std::array< float, 4 > initial_text_coords_
float get_max_value() const
Returns this status_bar's maximum value.
utils::observer_ptr< texture > bar_texture_
float get_min_value() const
Returns this status_bar's minimum value.
float get_value() const
Returns this status_bar's value.
bool is_reversed() const
Checks if this status_bar is reversed.
const std::vector< std::string > & get_type_list_() const override
orientation get_orientation() const
Returns this status_bar's orientation.
void set_min_value(float min_value)
Sets this status_bar's minimum value.
const color & get_bar_color() const
Returns this status_bar's bar color.
void copy_from(const region &obj) override
Copies a region's parameters into this status_bar (inheritance).
status_bar(utils::control_block &block, manager &mgr, const frame_core_attributes &attr)
Constructor.
void set_min_max_values(float min_value, float max_value)
Sets this status_bar's value range.
void set_bar_draw_layer(layer bar_layer)
Sets the draw layer of this status_bar's bar texture.
void set_reversed(bool reversed)
Reverses this status_bar.
const utils::observer_ptr< texture > & get_bar_texture()
Returns this status_bar's bar texture.
layer get_bar_draw_layer() const
Returns the draw layer of status_bar's bar texture.
bool can_use_script(const std::string &script_name) const override
Returns 'true' if this status_bar can use a script.
void set_max_value(float max_value)
Sets this status_bar's maximum value.
void set_bar_texture(utils::observer_ptr< texture > bar_texture)
Sets this status_bar's bar texture.
std::string serialize(const std::string &tab) const override
Prints all relevant information about this region in a string.
void set_bar_color(const color &bar_color)
Sets this status_bar's bar color.
A layered_region that can draw images and colored rectangles.
layer
ID of a layer for rendering inside a frame.
vector2< float > vector2f
Holds 2D coordinates (as floats)
std::array< float, 4 > select_uvs(const std::array< float, 8 > &uvs)
Struct holding all the core information about a frame necessary for its creation.
Struct holding all the core information about a region necessary for its creation.
std::vector< utils::observer_ptr< const region > > inheritance