Skip to content

Commit cdbd503

Browse files
committed
Fixed status_bar not firing OnValueChanged, and removed update()
1 parent bba386b commit cdbd503

File tree

3 files changed

+67
-49
lines changed

3 files changed

+67
-49
lines changed

changelog.txt

+1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ Other changes:
211211
- gui: fixed frame not marked as "user placed" when moved or resized
212212
- gui: fixed missing 'const' for slider::are_clicks_outside_thumb_allowed()
213213
- gui: fixed missing 'const' for region::render()
214+
- gui: fixed status_bar not firing OnValueChanged
214215

215216
v1.2.0:
216217
- added support for MSVC 2010

include/lxgui/gui_status_bar.hpp

-11
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,6 @@ class status_bar : public frame {
174174
*/
175175
bool is_reversed() const;
176176

177-
/**
178-
* \brief Updates this region's logic.
179-
* \param delta Time spent since last update
180-
* \note Triggered callbacks could destroy the frame. If you need
181-
* to use the frame again after calling this function, use
182-
* the helper class alive_checker.
183-
*/
184-
void update(float delta) override;
185-
186177
/// Registers this region class to the provided Lua state
187178
static void register_on_lua(sol::state& lua);
188179

@@ -195,8 +186,6 @@ class status_bar : public frame {
195186
void parse_attributes_(const layout_node& node) override;
196187
void parse_all_nodes_before_children_(const layout_node& node) override;
197188

198-
bool update_bar_texture_flag_ = false;
199-
200189
orientation orientation_ = orientation::horizontal;
201190
bool is_reversed_ = false;
202191

src/gui_status_bar.cpp

+66-38
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,15 @@ void status_bar::set_min_value(float min_value) {
7777
if (min_value_ > max_value_)
7878
min_value_ = max_value_;
7979

80-
value_ = std::clamp(value_, min_value_, max_value_);
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+
}
8189

8290
notify_bar_texture_needs_update_();
8391
}
@@ -90,7 +98,15 @@ void status_bar::set_max_value(float max_value) {
9098
if (max_value_ < min_value_)
9199
max_value_ = min_value_;
92100

93-
value_ = std::clamp(value_, min_value_, max_value_);
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+
}
94110

95111
notify_bar_texture_needs_update_();
96112
}
@@ -102,7 +118,15 @@ void status_bar::set_min_max_values(float min_value, float max_value) {
102118
min_value_ = std::min(min_value, max_value);
103119
max_value_ = std::max(min_value, max_value);
104120

105-
value_ = std::clamp(value_, min_value_, max_value_);
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+
}
106130

107131
notify_bar_texture_needs_update_();
108132
}
@@ -114,13 +138,21 @@ void status_bar::set_value(float value) {
114138
return;
115139

116140
value_ = value;
141+
142+
alive_checker checker(*this);
143+
fire_script("OnValueChanged", {value_});
144+
if (!checker.is_alive())
145+
return;
146+
117147
notify_bar_texture_needs_update_();
118148
}
119149

120150
void status_bar::set_bar_draw_layer(layer bar_layer) {
121151
bar_layer_ = bar_layer;
122-
if (bar_texture_)
152+
153+
if (bar_texture_) {
123154
bar_texture_->set_draw_layer(bar_layer_);
155+
}
124156
}
125157

126158
void status_bar::set_bar_texture(utils::observer_ptr<texture> bar_texture) {
@@ -133,10 +165,11 @@ void status_bar::set_bar_texture(utils::observer_ptr<texture> bar_texture) {
133165

134166
std::string parent = bar_texture_->get_parent().get() == this ? "$parent" : name_;
135167

136-
if (is_reversed_)
168+
if (is_reversed_) {
137169
bar_texture_->set_point(point::top_right, parent);
138-
else
170+
} else {
139171
bar_texture_->set_point(point::bottom_left, parent);
172+
}
140173

141174
initial_text_coords_ = select_uvs(bar_texture_->get_tex_coord());
142175
notify_bar_texture_needs_update_();
@@ -164,13 +197,15 @@ void status_bar::set_reversed(bool reversed) {
164197
is_reversed_ = reversed;
165198

166199
if (bar_texture_) {
167-
if (is_reversed_)
200+
if (is_reversed_) {
168201
bar_texture_->set_point(point::top_right);
169-
else
202+
} else {
170203
bar_texture_->set_point(point::bottom_left);
204+
}
171205

172-
if (!is_virtual_)
206+
if (!is_virtual_) {
173207
bar_texture_->notify_borders_need_update();
208+
}
174209
}
175210
}
176211

@@ -215,41 +250,34 @@ void status_bar::create_bar_texture_() {
215250
set_bar_texture(bar_texture);
216251
}
217252

218-
void status_bar::update(float delta) {
219-
if (update_bar_texture_flag_ && bar_texture_) {
220-
float coef = (value_ - min_value_) / (max_value_ - min_value_);
253+
void status_bar::notify_bar_texture_needs_update_() {
254+
if (!bar_texture_)
255+
return;
256+
257+
float coef = (value_ - min_value_) / (max_value_ - min_value_);
221258

222-
if (orientation_ == orientation::horizontal)
223-
bar_texture_->set_relative_dimensions(vector2f(coef, 1.0f));
224-
else
225-
bar_texture_->set_relative_dimensions(vector2f(1.0f, coef));
259+
if (orientation_ == orientation::horizontal) {
260+
bar_texture_->set_relative_dimensions(vector2f(coef, 1.0f));
261+
} else {
262+
bar_texture_->set_relative_dimensions(vector2f(1.0f, coef));
263+
}
226264

227-
std::array<float, 4> uvs = initial_text_coords_;
228-
if (orientation_ == orientation::horizontal) {
229-
if (is_reversed_)
230-
uvs[0] = (uvs[0] - uvs[2]) * coef + uvs[2];
231-
else
232-
uvs[2] = (uvs[2] - uvs[0]) * coef + uvs[0];
265+
std::array<float, 4> uvs = initial_text_coords_;
266+
if (orientation_ == orientation::horizontal) {
267+
if (is_reversed_) {
268+
uvs[0] = (uvs[0] - uvs[2]) * coef + uvs[2];
233269
} else {
234-
if (is_reversed_)
235-
uvs[3] = (uvs[3] - uvs[1]) * coef + uvs[1];
236-
else
237-
uvs[1] = (uvs[1] - uvs[3]) * coef + uvs[3];
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];
238277
}
239-
240-
bar_texture_->set_tex_rect(uvs);
241-
242-
update_bar_texture_flag_ = false;
243278
}
244279

245-
alive_checker checker(*this);
246-
base::update(delta);
247-
if (!checker.is_alive())
248-
return;
249-
}
250-
251-
void status_bar::notify_bar_texture_needs_update_() {
252-
update_bar_texture_flag_ = true;
280+
bar_texture_->set_tex_rect(uvs);
253281
}
254282

255283
} // namespace lxgui::gui

0 commit comments

Comments
 (0)