range.cpp 14 KB

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