range.cpp 13 KB

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