Debian Codegraph @

scene/gui/range.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* range.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "range.h"
32
33 #include "thirdparty/misc/r128.h"
34
35 double Range::_snapped_r128(double p_value, double p_step) {
36 if (p_step == 0.0) {
37 return p_value;
38 }
39 if (p_value > (1e18 * p_step) || p_value < -(1e18 * p_step)) {
40 // If the value goes outside of the range R128 supports, fallback to normal snapping.
41 return Math::snapped(p_value, p_step);
42 }
43 // Rescale values to better utilize R128's range before snapping.
44 // R128 is fixed-precision with 64 bits after the decimal point, but double already uses 53 of those,
45 // so a step size finer than 2^-11 will lose precision, and in practice even 1e-3 can be problematic.
46 // By rescaling the value and step, we can shift precision into the higher bits (effectively turning R128 into a makeshift float).
47 const int decimals = MIN(18, 14 - Math::floor(std::log10(MAX(Math::abs(p_value), p_step))));
48 const double scale = Math::pow(10.0, decimals);
49 p_value *= scale;
50 p_step *= scale;
51 // All these lines are the equivalent of: p_value = Math::floor(p_value / p_step + 0.5) * p_step;
52 // Convert to String to force rounding to a decimal value (not a binary one).
53 String step_str = String::num(p_step);
54 String value_str = String::num(p_value);
55 R128 step_r128;
56 R128 value_r128;
57 const R128 half_r128 = R128(0.5);
58 r128FromString(&step_r128, step_str.ascii().get_data(), nullptr);
59 r128FromString(&value_r128, value_str.ascii().get_data(), nullptr);
60 r128Div(&value_r128, &value_r128, &step_r128);
61 r128Add(&value_r128, &value_r128, &half_r128);
62 r128Floor(&value_r128, &value_r128);
63 r128Mul(&value_r128, &value_r128, &step_r128);
64 if (scale != 1.0) {
65 const R128 scale_r128 = R128(scale);
66 r128Div(&value_r128, &value_r128, &scale_r128);
67 }
68 p_value = (double)value_r128;
69 return p_value;
70 }
71
72 PackedStringArray Range::get_configuration_warnings() const {
73 PackedStringArray warnings = Control::get_configuration_warnings();
74
75 if (shared->exp_ratio && shared->min < 0) {
76 warnings.push_back(RTR("If \"Exp Edit\" is enabled, \"Min Value\" must be greater or equal to 0."));
77 }
78
79 return warnings;
80 }
81
82 void Range::_value_changed(double p_value) {
83 GDVIRTUAL_CALL(_value_changed, p_value);
84 }
85
86 void Range::_value_changed_notify() {
87 _value_changed(shared->val);
88 emit_signal(SceneStringName(value_changed), shared->val);
89 queue_accessibility_update();
90 queue_redraw();
91 }
92
93 void Range::_accessibility_action_inc(const Variant &p_data) {
94 double step = ((shared->step > 0) ? shared->step : 1);
95 set_value(shared->val + step);
96 }
97
98 void Range::_accessibility_action_dec(const Variant &p_data) {
99 double step = ((shared->step > 0) ? shared->step : 1);
100 set_value(shared->val - step);
101 }
102
103 void Range::_accessibility_action_set_value(const Variant &p_data) {
104 double new_val = p_data;
105 set_value(new_val);
106 }
107
108 void Range::_notification(int p_what) {
109 ERR_MAIN_THREAD_GUARD;
110 switch (p_what) {
111 case NOTIFICATION_ACCESSIBILITY_UPDATE: {
112 RID ae = get_accessibility_element();
113 ERR_FAIL_COND(ae.is_null());
114
115 DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_SPIN_BUTTON);
116 DisplayServer::get_singleton()->accessibility_update_set_num_value(ae, shared->val);
117 DisplayServer::get_singleton()->accessibility_update_set_num_range(ae, shared->min, shared->max);
118 if (shared->step > 0) {
119 DisplayServer::get_singleton()->accessibility_update_set_num_step(ae, shared->step);
120 } else {
121 DisplayServer::get_singleton()->accessibility_update_set_num_step(ae, 1);
122 }
123 DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_DECREMENT, callable_mp(this, &Range::_accessibility_action_dec));
124 DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_INCREMENT, callable_mp(this, &Range::_accessibility_action_inc));
125 DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_VALUE, callable_mp(this, &Range::_accessibility_action_set_value));
126 } break;
127 }
128 }
129
130 void Range::Shared::emit_value_changed() {
131 for (Range *E : owners) {
132 Range *r = E;
133 if (!r->is_inside_tree()) {
134 continue;
135 }
136 r->_value_changed_notify();
137 }
138 }
139
140 void Range::_changed_notify() {
141 emit_signal(CoreStringName(changed));
142 queue_redraw();
143 }
144
145 void Range::Shared::emit_changed() {
146 for (Range *E : owners) {
147 Range *r = E;
148 if (!r->is_inside_tree()) {
149 continue;
150 }
151 r->_changed_notify();
152 }
153 }
154
155 void Range::Shared::redraw_owners() {
156 for (Range *E : owners) {
157 Range *r = E;
158 if (!r->is_inside_tree()) {
159 continue;
160 }
161
162 r->_value_changed(val);
163 r->queue_accessibility_update();
164 r->queue_redraw();
165 }
166 }
167
168 void Range::set_value(double p_val) {
169 double prev_val = shared->val;
170 _set_value_no_signal(p_val);
171
172 if (shared->val != prev_val) {
173 shared->emit_value_changed();
174 }
175 queue_accessibility_update();
176 }
177
178 void Range::_set_value_no_signal(double p_val) {
179 shared->val = _calc_value(p_val, shared->step);
180 }
181
182 double Range::_calc_value(double p_val, double p_step) const {
183 if (p_step > 0) {
184 // Subtract min to support cases like min = 0.1, step = 0.2, snaps to 0.1, 0.3, 0.5, etc.
185 p_val = _snapped_r128(p_val - shared->min, p_step) + shared->min;
186 }
187
188 if (_rounded_values) {
189 p_val = Math::round(p_val);
190 }
191
192 if (!shared->allow_greater && p_val > shared->max - shared->page) {
193 p_val = shared->max - shared->page;
194 }
195
196 if (!shared->allow_lesser && p_val < shared->min) {
197 p_val = shared->min;
198 }
199 return p_val;
200 }
201
202 void Range::set_value_no_signal(double p_val) {
203 double prev_val = shared->val;
204 _set_value_no_signal(p_val);
205
206 if (shared->val != prev_val) {
207 shared->redraw_owners();
208 }
209 }
210
211 void Range::set_min(double p_min) {
212 if (shared->min == p_min) {
213 return;
214 }
215
216 shared->min = p_min;
217 shared->max = MAX(shared->max, shared->min);
218 shared->page = CLAMP(shared->page, 0, shared->max - shared->min);
219 set_value(shared->val);
220
221 shared->emit_changed();
222
223 update_configuration_warnings();
224
225 queue_accessibility_update();
226 }
227
228 void Range::set_max(double p_max) {
229 double max_validated = MAX(p_max, shared->min);
230 if (shared->max == max_validated) {
231 return;
232 }
233
234 shared->max = max_validated;
235 shared->page = CLAMP(shared->page, 0, shared->max - shared->min);
236 set_value(shared->val);
237
238 shared->emit_changed();
239
240 queue_accessibility_update();
241 }
242
243 void Range::set_step(double p_step) {
244 if (shared->step == p_step) {
245 return;
246 }
247
248 shared->step = p_step;
249 shared->emit_changed();
250
251 queue_accessibility_update();
252 }
253
254 void Range::set_page(double p_page) {
255 double page_validated = CLAMP(p_page, 0, shared->max - shared->min);
256 if (shared->page == page_validated) {
257 return;
258 }
259
260 shared->page = page_validated;
261 set_value(shared->val);
262
263 shared->emit_changed();
264
265 queue_accessibility_update();
266 }
267
268 double Range::get_value() const {
269 return shared->val;
270 }
271
272 double Range::get_min() const {
273 return shared->min;
274 }
275
276 double Range::get_max() const {
277 return shared->max;
278 }
279
280 double Range::get_step() const {
281 return shared->step;
282 }
283
284 double Range::get_page() const {
285 return shared->page;
286 }
287
288 void Range::set_as_ratio(double p_value) {
289 double v;
290
291 if (shared->exp_ratio && get_min() >= 0) {
292 double exp_min = get_min() == 0 ? 0.0 : Math::log(get_min()) / Math::log((double)2);
293 double exp_max = Math::log(get_max()) / Math::log((double)2);
294 v = Math::pow(2, exp_min + (exp_max - exp_min) * p_value);
295 } else {
296 double percent = (get_max() - get_min()) * p_value;
297 if (get_step() > 0) {
298 double steps = std::round(percent / get_step());
299 v = steps * get_step() + get_min();
300 } else {
301 v = percent + get_min();
302 }
303 }
304 v = CLAMP(v, get_min(), get_max());
305 set_value(v);
306 }
307
308 double Range::get_as_ratio() const {
309 if (Math::is_equal_approx(get_max(), get_min())) {
310 // Avoid division by zero.
311 return 1.0;
312 }
313
314 if (shared->exp_ratio && get_min() >= 0) {
315 double exp_min = get_min() == 0 ? 0.0 : Math::log(get_min()) / Math::log((double)2);
316 double exp_max = Math::log(get_max()) / Math::log((double)2);
317 double value = CLAMP(get_value(), shared->min, shared->max);
318 double v = Math::log(value) / Math::log((double)2);
319
320 return CLAMP((v - exp_min) / (exp_max - exp_min), 0, 1);
321 } else {
322 double value = CLAMP(get_value(), shared->min, shared->max);
323 return CLAMP((value - get_min()) / (get_max() - get_min()), 0, 1);
324 }
325 }
326
327 void Range::_share(Node *p_range) {
328 Range *r = Object::cast_to<Range>(p_range);
329 ERR_FAIL_NULL(r);
330 share(r);
331 }
332
333 void Range::share(Range *p_range) {
334 ERR_FAIL_NULL(p_range);
335
336 p_range->_ref_shared(shared);
337 p_range->_changed_notify();
338 p_range->_value_changed_notify();
339 }
340
341 void Range::unshare() {
342 Shared *nshared = memnew(Shared);
343 nshared->min = shared->min;
344 nshared->max = shared->max;
345 nshared->val = shared->val;
346 nshared->step = shared->step;
347 nshared->page = shared->page;
348 nshared->exp_ratio = shared->exp_ratio;
349 nshared->allow_greater = shared->allow_greater;
350 nshared->allow_lesser = shared->allow_lesser;
351 _unref_shared();
352 _ref_shared(nshared);
353 queue_accessibility_update();
354 }
355
356 void Range::_ref_shared(Shared *p_shared) {
357 if (shared && p_shared == shared) {
358 return;
359 }
360
361 _unref_shared();
362 shared = p_shared;
363 shared->owners.insert(this);
364 }
365
366 void Range::_unref_shared() {
367 if (shared) {
368 shared->owners.erase(this);
369 if (shared->owners.is_empty()) {
370 memdelete(shared);
371 shared = nullptr;
372 }
373 }
374 }
375
376 void Range::_bind_methods() {
377 ClassDB::bind_method(D_METHOD("get_value"), &Range::get_value);
378 ClassDB::bind_method(D_METHOD("get_min"), &Range::get_min);
379 ClassDB::bind_method(D_METHOD("get_max"), &Range::get_max);
380 ClassDB::bind_method(D_METHOD("get_step"), &Range::get_step);
381 ClassDB::bind_method(D_METHOD("get_page"), &Range::get_page);
382 ClassDB::bind_method(D_METHOD("get_as_ratio"), &Range::get_as_ratio);
383 ClassDB::bind_method(D_METHOD("set_value", "value"), &Range::set_value);
384 ClassDB::bind_method(D_METHOD("set_value_no_signal", "value"), &Range::set_value_no_signal);
385 ClassDB::bind_method(D_METHOD("set_min", "minimum"), &Range::set_min);
386 ClassDB::bind_method(D_METHOD("set_max", "maximum"), &Range::set_max);
387 ClassDB::bind_method(D_METHOD("set_step", "step"), &Range::set_step);
388 ClassDB::bind_method(D_METHOD("set_page", "pagesize"), &Range::set_page);
389 ClassDB::bind_method(D_METHOD("set_as_ratio", "value"), &Range::set_as_ratio);
390 ClassDB::bind_method(D_METHOD("set_use_rounded_values", "enabled"), &Range::set_use_rounded_values);
391 ClassDB::bind_method(D_METHOD("is_using_rounded_values"), &Range::is_using_rounded_values);
392 ClassDB::bind_method(D_METHOD("set_exp_ratio", "enabled"), &Range::set_exp_ratio);
393 ClassDB::bind_method(D_METHOD("is_ratio_exp"), &Range::is_ratio_exp);
394 ClassDB::bind_method(D_METHOD("set_allow_greater", "allow"), &Range::set_allow_greater);
395 ClassDB::bind_method(D_METHOD("is_greater_allowed"), &Range::is_greater_allowed);
396 ClassDB::bind_method(D_METHOD("set_allow_lesser", "allow"), &Range::set_allow_lesser);
397 ClassDB::bind_method(D_METHOD("is_lesser_allowed"), &Range::is_lesser_allowed);
398
399 ClassDB::bind_method(D_METHOD("share", "with"), &Range::_share);
400 ClassDB::bind_method(D_METHOD("unshare"), &Range::unshare);
401
402 ADD_SIGNAL(MethodInfo("value_changed", PropertyInfo(Variant::FLOAT, "value")));
403 ADD_SIGNAL(MethodInfo("changed"));
404
405 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min_value"), "set_min", "get_min");
406 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_value"), "set_max", "get_max");
407 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "step"), "set_step", "get_step");
408 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "page"), "set_page", "get_page");
409 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "value"), "set_value", "get_value");
410 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "ratio", PROPERTY_HINT_RANGE, "0,1,0.01", PROPERTY_USAGE_NONE), "set_as_ratio", "get_as_ratio");
411 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exp_edit"), "set_exp_ratio", "is_ratio_exp");
412 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rounded"), "set_use_rounded_values", "is_using_rounded_values");
413 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_greater"), "set_allow_greater", "is_greater_allowed");
414 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_lesser"), "set_allow_lesser", "is_lesser_allowed");
415
416 GDVIRTUAL_BIND(_value_changed, "new_value");
417
418 ADD_LINKED_PROPERTY("min_value", "value");
419 ADD_LINKED_PROPERTY("min_value", "max_value");
420 ADD_LINKED_PROPERTY("min_value", "page");
421 ADD_LINKED_PROPERTY("max_value", "value");
422 ADD_LINKED_PROPERTY("max_value", "page");
423 }
424
425 void Range::set_use_rounded_values(bool p_enable) {
426 _rounded_values = p_enable;
427 }
428
429 bool Range::is_using_rounded_values() const {
430 return _rounded_values;
431 }
432
433 void Range::set_exp_ratio(bool p_enable) {
434 if (shared->exp_ratio == p_enable) {
435 return;
436 }
437
438 shared->exp_ratio = p_enable;
439
440 update_configuration_warnings();
441 }
442
443 bool Range::is_ratio_exp() const {
444 return shared->exp_ratio;
445 }
446
447 void Range::set_allow_greater(bool p_allow) {
448 shared->allow_greater = p_allow;
449 }
450
451 bool Range::is_greater_allowed() const {
452 return shared->allow_greater;
453 }
454
455 void Range::set_allow_lesser(bool p_allow) {
456 shared->allow_lesser = p_allow;
457 }
458
459 bool Range::is_lesser_allowed() const {
460 return shared->allow_lesser;
461 }
462
463 Range::Range() {
464 shared = memnew(Shared);
465 shared->owners.insert(this);
466 }
467
468 Range::~Range() {
469 _unref_shared();
470 }