range.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. #include "range.h"
  31. #include "thirdparty/misc/r128.h"
  32. double Range::_snapped_r128(double p_value, double p_step) {
  33. if (p_step == 0.0) {
  34. return p_value;
  35. }
  36. if (p_value > (1e18 * p_step) || p_value < -(1e18 * p_step)) {
  37. // If the value goes outside of the range R128 supports, fallback to normal snapping.
  38. return Math::snapped(p_value, p_step);
  39. }
  40. // Rescale values to better utilize R128's range before snapping.
  41. // R128 is fixed-precision with 64 bits after the decimal point, but double already uses 53 of those,
  42. // so a step size finer than 2^-11 will lose precision, and in practice even 1e-3 can be problematic.
  43. // By rescaling the value and step, we can shift precision into the higher bits (effectively turning R128 into a makeshift float).
  44. const int decimals = 14 - Math::floor(std::log10(MAX(Math::abs(p_value), p_step)));
  45. const double scale = Math::pow(10.0, decimals);
  46. p_value *= scale;
  47. p_step *= scale;
  48. // All these lines are the equivalent of: p_value = Math::floor(p_value / p_step + 0.5) * p_step;
  49. // Convert to String to force rounding to a decimal value (not a binary one).
  50. String step_str = String::num(p_step);
  51. String value_str = String::num(p_value);
  52. R128 step_r128;
  53. R128 value_r128;
  54. const R128 half_r128 = R128(0.5);
  55. r128FromString(&step_r128, step_str.ascii().get_data(), nullptr);
  56. r128FromString(&value_r128, value_str.ascii().get_data(), nullptr);
  57. r128Div(&value_r128, &value_r128, &step_r128);
  58. r128Add(&value_r128, &value_r128, &half_r128);
  59. r128Floor(&value_r128, &value_r128);
  60. r128Mul(&value_r128, &value_r128, &step_r128);
  61. if (scale != 1.0) {
  62. const R128 scale_r128 = R128(scale);
  63. r128Div(&value_r128, &value_r128, &scale_r128);
  64. }
  65. p_value = (double)value_r128;
  66. return p_value;
  67. }
  68. PackedStringArray Range::get_configuration_warnings() const {
  69. PackedStringArray warnings = Control::get_configuration_warnings();
  70. if (shared->exp_ratio && shared->min < 0) {
  71. warnings.push_back(RTR("If \"Exp Edit\" is enabled, \"Min Value\" must be greater or equal to 0."));
  72. }
  73. return warnings;
  74. }
  75. void Range::_value_changed(double p_value) {
  76. GDVIRTUAL_CALL(_value_changed, p_value);
  77. }
  78. void Range::_value_changed_notify() {
  79. _value_changed(shared->val);
  80. emit_signal(SceneStringName(value_changed), shared->val);
  81. queue_accessibility_update();
  82. queue_redraw();
  83. }
  84. void Range::_accessibility_action_inc(const Variant &p_data) {
  85. double step = ((shared->step > 0) ? shared->step : 1);
  86. set_value(shared->val + step);
  87. }
  88. void Range::_accessibility_action_dec(const Variant &p_data) {
  89. double step = ((shared->step > 0) ? shared->step : 1);
  90. set_value(shared->val - step);
  91. }
  92. void Range::_accessibility_action_set_value(const Variant &p_data) {
  93. double new_val = p_data;
  94. set_value(new_val);
  95. }
  96. void Range::_notification(int p_what) {
  97. ERR_MAIN_THREAD_GUARD;
  98. switch (p_what) {
  99. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  100. RID ae = get_accessibility_element();
  101. ERR_FAIL_COND(ae.is_null());
  102. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_SPIN_BUTTON);
  103. DisplayServer::get_singleton()->accessibility_update_set_num_value(ae, shared->val);
  104. DisplayServer::get_singleton()->accessibility_update_set_num_range(ae, shared->min, shared->max);
  105. if (shared->step > 0) {
  106. DisplayServer::get_singleton()->accessibility_update_set_num_step(ae, shared->step);
  107. } else {
  108. DisplayServer::get_singleton()->accessibility_update_set_num_step(ae, 1);
  109. }
  110. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_DECREMENT, callable_mp(this, &Range::_accessibility_action_dec));
  111. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_INCREMENT, callable_mp(this, &Range::_accessibility_action_inc));
  112. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_VALUE, callable_mp(this, &Range::_accessibility_action_set_value));
  113. } break;
  114. }
  115. }
  116. void Range::Shared::emit_value_changed() {
  117. for (Range *E : owners) {
  118. Range *r = E;
  119. if (!r->is_inside_tree()) {
  120. continue;
  121. }
  122. r->_value_changed_notify();
  123. }
  124. }
  125. void Range::_changed_notify() {
  126. emit_signal(CoreStringName(changed));
  127. queue_redraw();
  128. }
  129. void Range::Shared::emit_changed() {
  130. for (Range *E : owners) {
  131. Range *r = E;
  132. if (!r->is_inside_tree()) {
  133. continue;
  134. }
  135. r->_changed_notify();
  136. }
  137. }
  138. void Range::Shared::redraw_owners() {
  139. for (Range *E : owners) {
  140. Range *r = E;
  141. if (!r->is_inside_tree()) {
  142. continue;
  143. }
  144. r->queue_accessibility_update();
  145. r->queue_redraw();
  146. }
  147. }
  148. void Range::set_value(double p_val) {
  149. double prev_val = shared->val;
  150. _set_value_no_signal(p_val);
  151. if (shared->val != prev_val) {
  152. shared->emit_value_changed();
  153. }
  154. queue_accessibility_update();
  155. }
  156. void Range::_set_value_no_signal(double p_val) {
  157. shared->val = _calc_value(p_val, shared->step);
  158. }
  159. double Range::_calc_value(double p_val, double p_step) const {
  160. if (p_step > 0) {
  161. // Subtract min to support cases like min = 0.1, step = 0.2, snaps to 0.1, 0.3, 0.5, etc.
  162. p_val = _snapped_r128(p_val - shared->min, p_step) + shared->min;
  163. }
  164. if (_rounded_values) {
  165. p_val = Math::round(p_val);
  166. }
  167. if (!shared->allow_greater && p_val > shared->max - shared->page) {
  168. p_val = shared->max - shared->page;
  169. }
  170. if (!shared->allow_lesser && p_val < shared->min) {
  171. p_val = shared->min;
  172. }
  173. return p_val;
  174. }
  175. void Range::set_value_no_signal(double p_val) {
  176. double prev_val = shared->val;
  177. _set_value_no_signal(p_val);
  178. if (shared->val != prev_val) {
  179. shared->redraw_owners();
  180. }
  181. }
  182. void Range::set_min(double p_min) {
  183. if (shared->min == p_min) {
  184. return;
  185. }
  186. shared->min = p_min;
  187. shared->max = MAX(shared->max, shared->min);
  188. shared->page = CLAMP(shared->page, 0, shared->max - shared->min);
  189. set_value(shared->val);
  190. shared->emit_changed();
  191. update_configuration_warnings();
  192. queue_accessibility_update();
  193. }
  194. void Range::set_max(double p_max) {
  195. double max_validated = MAX(p_max, shared->min);
  196. if (shared->max == max_validated) {
  197. return;
  198. }
  199. shared->max = max_validated;
  200. shared->page = CLAMP(shared->page, 0, shared->max - shared->min);
  201. set_value(shared->val);
  202. shared->emit_changed();
  203. queue_accessibility_update();
  204. }
  205. void Range::set_step(double p_step) {
  206. if (shared->step == p_step) {
  207. return;
  208. }
  209. shared->step = p_step;
  210. shared->emit_changed();
  211. queue_accessibility_update();
  212. }
  213. void Range::set_page(double p_page) {
  214. double page_validated = CLAMP(p_page, 0, shared->max - shared->min);
  215. if (shared->page == page_validated) {
  216. return;
  217. }
  218. shared->page = page_validated;
  219. set_value(shared->val);
  220. shared->emit_changed();
  221. queue_accessibility_update();
  222. }
  223. double Range::get_value() const {
  224. return shared->val;
  225. }
  226. double Range::get_min() const {
  227. return shared->min;
  228. }
  229. double Range::get_max() const {
  230. return shared->max;
  231. }
  232. double Range::get_step() const {
  233. return shared->step;
  234. }
  235. double Range::get_page() const {
  236. return shared->page;
  237. }
  238. void Range::set_as_ratio(double p_value) {
  239. double v;
  240. if (shared->exp_ratio && get_min() >= 0) {
  241. double exp_min = get_min() == 0 ? 0.0 : Math::log(get_min()) / Math::log((double)2);
  242. double exp_max = Math::log(get_max()) / Math::log((double)2);
  243. v = Math::pow(2, exp_min + (exp_max - exp_min) * p_value);
  244. } else {
  245. double percent = (get_max() - get_min()) * p_value;
  246. if (get_step() > 0) {
  247. double steps = std::round(percent / get_step());
  248. v = steps * get_step() + get_min();
  249. } else {
  250. v = percent + get_min();
  251. }
  252. }
  253. v = CLAMP(v, get_min(), get_max());
  254. set_value(v);
  255. }
  256. double Range::get_as_ratio() const {
  257. if (Math::is_equal_approx(get_max(), get_min())) {
  258. // Avoid division by zero.
  259. return 1.0;
  260. }
  261. if (shared->exp_ratio && get_min() >= 0) {
  262. double exp_min = get_min() == 0 ? 0.0 : Math::log(get_min()) / Math::log((double)2);
  263. double exp_max = Math::log(get_max()) / Math::log((double)2);
  264. double value = CLAMP(get_value(), shared->min, shared->max);
  265. double v = Math::log(value) / Math::log((double)2);
  266. return CLAMP((v - exp_min) / (exp_max - exp_min), 0, 1);
  267. } else {
  268. double value = CLAMP(get_value(), shared->min, shared->max);
  269. return CLAMP((value - get_min()) / (get_max() - get_min()), 0, 1);
  270. }
  271. }
  272. void Range::_share(Node *p_range) {
  273. Range *r = Object::cast_to<Range>(p_range);
  274. ERR_FAIL_NULL(r);
  275. share(r);
  276. }
  277. void Range::share(Range *p_range) {
  278. ERR_FAIL_NULL(p_range);
  279. p_range->_ref_shared(shared);
  280. p_range->_changed_notify();
  281. p_range->_value_changed_notify();
  282. }
  283. void Range::unshare() {
  284. Shared *nshared = memnew(Shared);
  285. nshared->min = shared->min;
  286. nshared->max = shared->max;
  287. nshared->val = shared->val;
  288. nshared->step = shared->step;
  289. nshared->page = shared->page;
  290. nshared->exp_ratio = shared->exp_ratio;
  291. nshared->allow_greater = shared->allow_greater;
  292. nshared->allow_lesser = shared->allow_lesser;
  293. _unref_shared();
  294. _ref_shared(nshared);
  295. queue_accessibility_update();
  296. }
  297. void Range::_ref_shared(Shared *p_shared) {
  298. if (shared && p_shared == shared) {
  299. return;
  300. }
  301. _unref_shared();
  302. shared = p_shared;
  303. shared->owners.insert(this);
  304. }
  305. void Range::_unref_shared() {
  306. if (shared) {
  307. shared->owners.erase(this);
  308. if (shared->owners.is_empty()) {
  309. memdelete(shared);
  310. shared = nullptr;
  311. }
  312. }
  313. }
  314. void Range::_bind_methods() {
  315. ClassDB::bind_method(D_METHOD("get_value"), &Range::get_value);
  316. ClassDB::bind_method(D_METHOD("get_min"), &Range::get_min);
  317. ClassDB::bind_method(D_METHOD("get_max"), &Range::get_max);
  318. ClassDB::bind_method(D_METHOD("get_step"), &Range::get_step);
  319. ClassDB::bind_method(D_METHOD("get_page"), &Range::get_page);
  320. ClassDB::bind_method(D_METHOD("get_as_ratio"), &Range::get_as_ratio);
  321. ClassDB::bind_method(D_METHOD("set_value", "value"), &Range::set_value);
  322. ClassDB::bind_method(D_METHOD("set_value_no_signal", "value"), &Range::set_value_no_signal);
  323. ClassDB::bind_method(D_METHOD("set_min", "minimum"), &Range::set_min);
  324. ClassDB::bind_method(D_METHOD("set_max", "maximum"), &Range::set_max);
  325. ClassDB::bind_method(D_METHOD("set_step", "step"), &Range::set_step);
  326. ClassDB::bind_method(D_METHOD("set_page", "pagesize"), &Range::set_page);
  327. ClassDB::bind_method(D_METHOD("set_as_ratio", "value"), &Range::set_as_ratio);
  328. ClassDB::bind_method(D_METHOD("set_use_rounded_values", "enabled"), &Range::set_use_rounded_values);
  329. ClassDB::bind_method(D_METHOD("is_using_rounded_values"), &Range::is_using_rounded_values);
  330. ClassDB::bind_method(D_METHOD("set_exp_ratio", "enabled"), &Range::set_exp_ratio);
  331. ClassDB::bind_method(D_METHOD("is_ratio_exp"), &Range::is_ratio_exp);
  332. ClassDB::bind_method(D_METHOD("set_allow_greater", "allow"), &Range::set_allow_greater);
  333. ClassDB::bind_method(D_METHOD("is_greater_allowed"), &Range::is_greater_allowed);
  334. ClassDB::bind_method(D_METHOD("set_allow_lesser", "allow"), &Range::set_allow_lesser);
  335. ClassDB::bind_method(D_METHOD("is_lesser_allowed"), &Range::is_lesser_allowed);
  336. ClassDB::bind_method(D_METHOD("share", "with"), &Range::_share);
  337. ClassDB::bind_method(D_METHOD("unshare"), &Range::unshare);
  338. ADD_SIGNAL(MethodInfo("value_changed", PropertyInfo(Variant::FLOAT, "value")));
  339. ADD_SIGNAL(MethodInfo("changed"));
  340. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min_value"), "set_min", "get_min");
  341. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_value"), "set_max", "get_max");
  342. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "step"), "set_step", "get_step");
  343. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "page"), "set_page", "get_page");
  344. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "value"), "set_value", "get_value");
  345. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "ratio", PROPERTY_HINT_RANGE, "0,1,0.01", PROPERTY_USAGE_NONE), "set_as_ratio", "get_as_ratio");
  346. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exp_edit"), "set_exp_ratio", "is_ratio_exp");
  347. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rounded"), "set_use_rounded_values", "is_using_rounded_values");
  348. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_greater"), "set_allow_greater", "is_greater_allowed");
  349. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_lesser"), "set_allow_lesser", "is_lesser_allowed");
  350. GDVIRTUAL_BIND(_value_changed, "new_value");
  351. ADD_LINKED_PROPERTY("min_value", "value");
  352. ADD_LINKED_PROPERTY("min_value", "max_value");
  353. ADD_LINKED_PROPERTY("min_value", "page");
  354. ADD_LINKED_PROPERTY("max_value", "value");
  355. ADD_LINKED_PROPERTY("max_value", "page");
  356. }
  357. void Range::set_use_rounded_values(bool p_enable) {
  358. _rounded_values = p_enable;
  359. }
  360. bool Range::is_using_rounded_values() const {
  361. return _rounded_values;
  362. }
  363. void Range::set_exp_ratio(bool p_enable) {
  364. if (shared->exp_ratio == p_enable) {
  365. return;
  366. }
  367. shared->exp_ratio = p_enable;
  368. update_configuration_warnings();
  369. }
  370. bool Range::is_ratio_exp() const {
  371. return shared->exp_ratio;
  372. }
  373. void Range::set_allow_greater(bool p_allow) {
  374. shared->allow_greater = p_allow;
  375. }
  376. bool Range::is_greater_allowed() const {
  377. return shared->allow_greater;
  378. }
  379. void Range::set_allow_lesser(bool p_allow) {
  380. shared->allow_lesser = p_allow;
  381. }
  382. bool Range::is_lesser_allowed() const {
  383. return shared->allow_lesser;
  384. }
  385. Range::Range() {
  386. shared = memnew(Shared);
  387. shared->owners.insert(this);
  388. }
  389. Range::~Range() {
  390. _unref_shared();
  391. }