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 | PackedStringArray Range::get_configuration_warnings() const { |
34 | PackedStringArray warnings = Node::get_configuration_warnings(); |
35 | |
36 | if (shared->exp_ratio && shared->min <= 0) { |
37 | warnings.push_back(RTR("If \"Exp Edit\" is enabled, \"Min Value\" must be greater than 0." )); |
38 | } |
39 | |
40 | return warnings; |
41 | } |
42 | |
43 | void Range::_value_changed(double p_value) { |
44 | GDVIRTUAL_CALL(_value_changed, p_value); |
45 | } |
46 | void Range::_value_changed_notify() { |
47 | _value_changed(shared->val); |
48 | emit_signal(SNAME("value_changed" ), shared->val); |
49 | queue_redraw(); |
50 | } |
51 | |
52 | void Range::Shared::emit_value_changed() { |
53 | for (Range *E : owners) { |
54 | Range *r = E; |
55 | if (!r->is_inside_tree()) { |
56 | continue; |
57 | } |
58 | r->_value_changed_notify(); |
59 | } |
60 | } |
61 | |
62 | void Range::_changed_notify(const char *p_what) { |
63 | emit_signal(SNAME("changed" )); |
64 | queue_redraw(); |
65 | } |
66 | |
67 | void Range::Shared::emit_changed(const char *p_what) { |
68 | for (Range *E : owners) { |
69 | Range *r = E; |
70 | if (!r->is_inside_tree()) { |
71 | continue; |
72 | } |
73 | r->_changed_notify(p_what); |
74 | } |
75 | } |
76 | |
77 | void Range::Shared::redraw_owners() { |
78 | for (Range *E : owners) { |
79 | Range *r = E; |
80 | if (!r->is_inside_tree()) { |
81 | continue; |
82 | } |
83 | r->queue_redraw(); |
84 | } |
85 | } |
86 | |
87 | void Range::set_value(double p_val) { |
88 | double prev_val = shared->val; |
89 | _set_value_no_signal(p_val); |
90 | |
91 | if (shared->val != prev_val) { |
92 | shared->emit_value_changed(); |
93 | } |
94 | } |
95 | |
96 | void Range::_set_value_no_signal(double p_val) { |
97 | if (!Math::is_finite(p_val)) { |
98 | return; |
99 | } |
100 | |
101 | if (shared->step > 0) { |
102 | p_val = Math::round((p_val - shared->min) / shared->step) * shared->step + shared->min; |
103 | } |
104 | |
105 | if (_rounded_values) { |
106 | p_val = Math::round(p_val); |
107 | } |
108 | |
109 | if (!shared->allow_greater && p_val > shared->max - shared->page) { |
110 | p_val = shared->max - shared->page; |
111 | } |
112 | |
113 | if (!shared->allow_lesser && p_val < shared->min) { |
114 | p_val = shared->min; |
115 | } |
116 | |
117 | if (shared->val == p_val) { |
118 | return; |
119 | } |
120 | |
121 | shared->val = p_val; |
122 | } |
123 | |
124 | void Range::set_value_no_signal(double p_val) { |
125 | double prev_val = shared->val; |
126 | _set_value_no_signal(p_val); |
127 | |
128 | if (shared->val != prev_val) { |
129 | shared->redraw_owners(); |
130 | } |
131 | } |
132 | |
133 | void Range::set_min(double p_min) { |
134 | if (shared->min == p_min) { |
135 | return; |
136 | } |
137 | |
138 | shared->min = p_min; |
139 | shared->max = MAX(shared->max, shared->min); |
140 | shared->page = CLAMP(shared->page, 0, shared->max - shared->min); |
141 | set_value(shared->val); |
142 | |
143 | shared->emit_changed("min" ); |
144 | |
145 | update_configuration_warnings(); |
146 | } |
147 | |
148 | void Range::set_max(double p_max) { |
149 | double max_validated = MAX(p_max, shared->min); |
150 | if (shared->max == max_validated) { |
151 | return; |
152 | } |
153 | |
154 | shared->max = max_validated; |
155 | shared->page = CLAMP(shared->page, 0, shared->max - shared->min); |
156 | set_value(shared->val); |
157 | |
158 | shared->emit_changed("max" ); |
159 | } |
160 | |
161 | void Range::set_step(double p_step) { |
162 | if (shared->step == p_step) { |
163 | return; |
164 | } |
165 | |
166 | shared->step = p_step; |
167 | shared->emit_changed("step" ); |
168 | } |
169 | |
170 | void Range::set_page(double p_page) { |
171 | double page_validated = CLAMP(p_page, 0, shared->max - shared->min); |
172 | if (shared->page == page_validated) { |
173 | return; |
174 | } |
175 | |
176 | shared->page = page_validated; |
177 | set_value(shared->val); |
178 | |
179 | shared->emit_changed("page" ); |
180 | } |
181 | |
182 | double Range::get_value() const { |
183 | return shared->val; |
184 | } |
185 | |
186 | double Range::get_min() const { |
187 | return shared->min; |
188 | } |
189 | |
190 | double Range::get_max() const { |
191 | return shared->max; |
192 | } |
193 | |
194 | double Range::get_step() const { |
195 | return shared->step; |
196 | } |
197 | |
198 | double Range::get_page() const { |
199 | return shared->page; |
200 | } |
201 | |
202 | void Range::set_as_ratio(double p_value) { |
203 | double v; |
204 | |
205 | if (shared->exp_ratio && get_min() >= 0) { |
206 | double exp_min = get_min() == 0 ? 0.0 : Math::log(get_min()) / Math::log((double)2); |
207 | double exp_max = Math::log(get_max()) / Math::log((double)2); |
208 | v = Math::pow(2, exp_min + (exp_max - exp_min) * p_value); |
209 | } else { |
210 | double percent = (get_max() - get_min()) * p_value; |
211 | if (get_step() > 0) { |
212 | double steps = round(percent / get_step()); |
213 | v = steps * get_step() + get_min(); |
214 | } else { |
215 | v = percent + get_min(); |
216 | } |
217 | } |
218 | v = CLAMP(v, get_min(), get_max()); |
219 | set_value(v); |
220 | } |
221 | |
222 | double Range::get_as_ratio() const { |
223 | if (Math::is_equal_approx(get_max(), get_min())) { |
224 | // Avoid division by zero. |
225 | return 1.0; |
226 | } |
227 | |
228 | if (shared->exp_ratio && get_min() >= 0) { |
229 | double exp_min = get_min() == 0 ? 0.0 : Math::log(get_min()) / Math::log((double)2); |
230 | double exp_max = Math::log(get_max()) / Math::log((double)2); |
231 | float value = CLAMP(get_value(), shared->min, shared->max); |
232 | double v = Math::log(value) / Math::log((double)2); |
233 | |
234 | return CLAMP((v - exp_min) / (exp_max - exp_min), 0, 1); |
235 | } else { |
236 | float value = CLAMP(get_value(), shared->min, shared->max); |
237 | return CLAMP((value - get_min()) / (get_max() - get_min()), 0, 1); |
238 | } |
239 | } |
240 | |
241 | void Range::_share(Node *p_range) { |
242 | Range *r = Object::cast_to<Range>(p_range); |
243 | ERR_FAIL_NULL(r); |
244 | share(r); |
245 | } |
246 | |
247 | void Range::share(Range *p_range) { |
248 | ERR_FAIL_NULL(p_range); |
249 | |
250 | p_range->_ref_shared(shared); |
251 | p_range->_changed_notify(); |
252 | p_range->_value_changed_notify(); |
253 | } |
254 | |
255 | void Range::unshare() { |
256 | Shared *nshared = memnew(Shared); |
257 | nshared->min = shared->min; |
258 | nshared->max = shared->max; |
259 | nshared->val = shared->val; |
260 | nshared->step = shared->step; |
261 | nshared->page = shared->page; |
262 | nshared->exp_ratio = shared->exp_ratio; |
263 | nshared->allow_greater = shared->allow_greater; |
264 | nshared->allow_lesser = shared->allow_lesser; |
265 | _unref_shared(); |
266 | _ref_shared(nshared); |
267 | } |
268 | |
269 | void Range::_ref_shared(Shared *p_shared) { |
270 | if (shared && p_shared == shared) { |
271 | return; |
272 | } |
273 | |
274 | _unref_shared(); |
275 | shared = p_shared; |
276 | shared->owners.insert(this); |
277 | } |
278 | |
279 | void Range::_unref_shared() { |
280 | if (shared) { |
281 | shared->owners.erase(this); |
282 | if (shared->owners.size() == 0) { |
283 | memdelete(shared); |
284 | shared = nullptr; |
285 | } |
286 | } |
287 | } |
288 | |
289 | void Range::_bind_methods() { |
290 | ClassDB::bind_method(D_METHOD("get_value" ), &Range::get_value); |
291 | ClassDB::bind_method(D_METHOD("get_min" ), &Range::get_min); |
292 | ClassDB::bind_method(D_METHOD("get_max" ), &Range::get_max); |
293 | ClassDB::bind_method(D_METHOD("get_step" ), &Range::get_step); |
294 | ClassDB::bind_method(D_METHOD("get_page" ), &Range::get_page); |
295 | ClassDB::bind_method(D_METHOD("get_as_ratio" ), &Range::get_as_ratio); |
296 | ClassDB::bind_method(D_METHOD("set_value" , "value" ), &Range::set_value); |
297 | ClassDB::bind_method(D_METHOD("set_value_no_signal" , "value" ), &Range::set_value_no_signal); |
298 | ClassDB::bind_method(D_METHOD("set_min" , "minimum" ), &Range::set_min); |
299 | ClassDB::bind_method(D_METHOD("set_max" , "maximum" ), &Range::set_max); |
300 | ClassDB::bind_method(D_METHOD("set_step" , "step" ), &Range::set_step); |
301 | ClassDB::bind_method(D_METHOD("set_page" , "pagesize" ), &Range::set_page); |
302 | ClassDB::bind_method(D_METHOD("set_as_ratio" , "value" ), &Range::set_as_ratio); |
303 | ClassDB::bind_method(D_METHOD("set_use_rounded_values" , "enabled" ), &Range::set_use_rounded_values); |
304 | ClassDB::bind_method(D_METHOD("is_using_rounded_values" ), &Range::is_using_rounded_values); |
305 | ClassDB::bind_method(D_METHOD("set_exp_ratio" , "enabled" ), &Range::set_exp_ratio); |
306 | ClassDB::bind_method(D_METHOD("is_ratio_exp" ), &Range::is_ratio_exp); |
307 | ClassDB::bind_method(D_METHOD("set_allow_greater" , "allow" ), &Range::set_allow_greater); |
308 | ClassDB::bind_method(D_METHOD("is_greater_allowed" ), &Range::is_greater_allowed); |
309 | ClassDB::bind_method(D_METHOD("set_allow_lesser" , "allow" ), &Range::set_allow_lesser); |
310 | ClassDB::bind_method(D_METHOD("is_lesser_allowed" ), &Range::is_lesser_allowed); |
311 | |
312 | ClassDB::bind_method(D_METHOD("share" , "with" ), &Range::_share); |
313 | ClassDB::bind_method(D_METHOD("unshare" ), &Range::unshare); |
314 | |
315 | ADD_SIGNAL(MethodInfo("value_changed" , PropertyInfo(Variant::FLOAT, "value" ))); |
316 | ADD_SIGNAL(MethodInfo("changed" )); |
317 | |
318 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min_value" ), "set_min" , "get_min" ); |
319 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_value" ), "set_max" , "get_max" ); |
320 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "step" ), "set_step" , "get_step" ); |
321 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "page" ), "set_page" , "get_page" ); |
322 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "value" ), "set_value" , "get_value" ); |
323 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "ratio" , PROPERTY_HINT_RANGE, "0,1,0.01" , PROPERTY_USAGE_NONE), "set_as_ratio" , "get_as_ratio" ); |
324 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exp_edit" ), "set_exp_ratio" , "is_ratio_exp" ); |
325 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rounded" ), "set_use_rounded_values" , "is_using_rounded_values" ); |
326 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_greater" ), "set_allow_greater" , "is_greater_allowed" ); |
327 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_lesser" ), "set_allow_lesser" , "is_lesser_allowed" ); |
328 | |
329 | GDVIRTUAL_BIND(_value_changed, "new_value" ); |
330 | |
331 | ADD_LINKED_PROPERTY("min_value" , "value" ); |
332 | ADD_LINKED_PROPERTY("min_value" , "max_value" ); |
333 | ADD_LINKED_PROPERTY("min_value" , "page" ); |
334 | ADD_LINKED_PROPERTY("max_value" , "value" ); |
335 | ADD_LINKED_PROPERTY("max_value" , "page" ); |
336 | } |
337 | |
338 | void Range::set_use_rounded_values(bool p_enable) { |
339 | _rounded_values = p_enable; |
340 | } |
341 | |
342 | bool Range::is_using_rounded_values() const { |
343 | return _rounded_values; |
344 | } |
345 | |
346 | void Range::set_exp_ratio(bool p_enable) { |
347 | if (shared->exp_ratio == p_enable) { |
348 | return; |
349 | } |
350 | |
351 | shared->exp_ratio = p_enable; |
352 | |
353 | update_configuration_warnings(); |
354 | } |
355 | |
356 | bool Range::is_ratio_exp() const { |
357 | return shared->exp_ratio; |
358 | } |
359 | |
360 | void Range::set_allow_greater(bool p_allow) { |
361 | shared->allow_greater = p_allow; |
362 | } |
363 | |
364 | bool Range::is_greater_allowed() const { |
365 | return shared->allow_greater; |
366 | } |
367 | |
368 | void Range::set_allow_lesser(bool p_allow) { |
369 | shared->allow_lesser = p_allow; |
370 | } |
371 | |
372 | bool Range::is_lesser_allowed() const { |
373 | return shared->allow_lesser; |
374 | } |
375 | |
376 | Range::Range() { |
377 | shared = memnew(Shared); |
378 | shared->owners.insert(this); |
379 | } |
380 | |
381 | Range::~Range() { |
382 | _unref_shared(); |
383 | } |
384 | |