range.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*************************************************************************/
  2. /* range.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 = Node::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(SNAME("value_changed"), shared->val);
  44. queue_redraw();
  45. }
  46. void Range::Shared::emit_value_changed() {
  47. for (Range *E : owners) {
  48. Range *r = E;
  49. if (!r->is_inside_tree()) {
  50. continue;
  51. }
  52. r->_value_changed_notify();
  53. }
  54. }
  55. void Range::_changed_notify(const char *p_what) {
  56. emit_signal(SNAME("changed"));
  57. queue_redraw();
  58. }
  59. void Range::_validate_values() {
  60. shared->max = MAX(shared->max, shared->min);
  61. shared->page = CLAMP(shared->page, 0, shared->max - shared->min);
  62. }
  63. void Range::Shared::emit_changed(const char *p_what) {
  64. for (Range *E : owners) {
  65. Range *r = E;
  66. if (!r->is_inside_tree()) {
  67. continue;
  68. }
  69. r->_changed_notify(p_what);
  70. }
  71. }
  72. void Range::set_value(double p_val) {
  73. double prev_val = shared->val;
  74. set_value_no_signal(p_val);
  75. if (shared->val != prev_val) {
  76. shared->emit_value_changed();
  77. }
  78. }
  79. void Range::set_value_no_signal(double p_val) {
  80. if (shared->step > 0) {
  81. p_val = Math::round(p_val / shared->step) * shared->step;
  82. }
  83. if (_rounded_values) {
  84. p_val = Math::round(p_val);
  85. }
  86. if (!shared->allow_greater && p_val > shared->max - shared->page) {
  87. p_val = shared->max - shared->page;
  88. }
  89. if (!shared->allow_lesser && p_val < shared->min) {
  90. p_val = shared->min;
  91. }
  92. if (shared->val == p_val) {
  93. return;
  94. }
  95. shared->val = p_val;
  96. }
  97. void Range::set_min(double p_min) {
  98. if (shared->min == p_min) {
  99. return;
  100. }
  101. shared->min = p_min;
  102. set_value(shared->val);
  103. _validate_values();
  104. shared->emit_changed("min");
  105. update_configuration_warnings();
  106. }
  107. void Range::set_max(double p_max) {
  108. if (shared->max == p_max) {
  109. return;
  110. }
  111. shared->max = p_max;
  112. set_value(shared->val);
  113. _validate_values();
  114. shared->emit_changed("max");
  115. }
  116. void Range::set_step(double p_step) {
  117. if (shared->step == p_step) {
  118. return;
  119. }
  120. shared->step = p_step;
  121. shared->emit_changed("step");
  122. }
  123. void Range::set_page(double p_page) {
  124. if (shared->page == p_page) {
  125. return;
  126. }
  127. shared->page = p_page;
  128. set_value(shared->val);
  129. _validate_values();
  130. shared->emit_changed("page");
  131. }
  132. double Range::get_value() const {
  133. return shared->val;
  134. }
  135. double Range::get_min() const {
  136. return shared->min;
  137. }
  138. double Range::get_max() const {
  139. return shared->max;
  140. }
  141. double Range::get_step() const {
  142. return shared->step;
  143. }
  144. double Range::get_page() const {
  145. return shared->page;
  146. }
  147. void Range::set_as_ratio(double p_value) {
  148. double v;
  149. if (shared->exp_ratio && get_min() >= 0) {
  150. double exp_min = get_min() == 0 ? 0.0 : Math::log(get_min()) / Math::log((double)2);
  151. double exp_max = Math::log(get_max()) / Math::log((double)2);
  152. v = Math::pow(2, exp_min + (exp_max - exp_min) * p_value);
  153. } else {
  154. double percent = (get_max() - get_min()) * p_value;
  155. if (get_step() > 0) {
  156. double steps = round(percent / get_step());
  157. v = steps * get_step() + get_min();
  158. } else {
  159. v = percent + get_min();
  160. }
  161. }
  162. v = CLAMP(v, get_min(), get_max());
  163. set_value(v);
  164. }
  165. double Range::get_as_ratio() const {
  166. if (Math::is_equal_approx(get_max(), get_min())) {
  167. // Avoid division by zero.
  168. return 1.0;
  169. }
  170. if (shared->exp_ratio && get_min() >= 0) {
  171. double exp_min = get_min() == 0 ? 0.0 : Math::log(get_min()) / Math::log((double)2);
  172. double exp_max = Math::log(get_max()) / Math::log((double)2);
  173. float value = CLAMP(get_value(), shared->min, shared->max);
  174. double v = Math::log(value) / Math::log((double)2);
  175. return CLAMP((v - exp_min) / (exp_max - exp_min), 0, 1);
  176. } else {
  177. float value = CLAMP(get_value(), shared->min, shared->max);
  178. return CLAMP((value - get_min()) / (get_max() - get_min()), 0, 1);
  179. }
  180. }
  181. void Range::_share(Node *p_range) {
  182. Range *r = Object::cast_to<Range>(p_range);
  183. ERR_FAIL_COND(!r);
  184. share(r);
  185. }
  186. void Range::share(Range *p_range) {
  187. ERR_FAIL_NULL(p_range);
  188. p_range->_ref_shared(shared);
  189. p_range->_changed_notify();
  190. p_range->_value_changed_notify();
  191. }
  192. void Range::unshare() {
  193. Shared *nshared = memnew(Shared);
  194. nshared->min = shared->min;
  195. nshared->max = shared->max;
  196. nshared->val = shared->val;
  197. nshared->step = shared->step;
  198. nshared->page = shared->page;
  199. nshared->exp_ratio = shared->exp_ratio;
  200. nshared->allow_greater = shared->allow_greater;
  201. nshared->allow_lesser = shared->allow_lesser;
  202. _unref_shared();
  203. _ref_shared(nshared);
  204. }
  205. void Range::_ref_shared(Shared *p_shared) {
  206. if (shared && p_shared == shared) {
  207. return;
  208. }
  209. _unref_shared();
  210. shared = p_shared;
  211. shared->owners.insert(this);
  212. }
  213. void Range::_unref_shared() {
  214. if (shared) {
  215. shared->owners.erase(this);
  216. if (shared->owners.size() == 0) {
  217. memdelete(shared);
  218. shared = nullptr;
  219. }
  220. }
  221. }
  222. void Range::_bind_methods() {
  223. ClassDB::bind_method(D_METHOD("get_value"), &Range::get_value);
  224. ClassDB::bind_method(D_METHOD("get_min"), &Range::get_min);
  225. ClassDB::bind_method(D_METHOD("get_max"), &Range::get_max);
  226. ClassDB::bind_method(D_METHOD("get_step"), &Range::get_step);
  227. ClassDB::bind_method(D_METHOD("get_page"), &Range::get_page);
  228. ClassDB::bind_method(D_METHOD("get_as_ratio"), &Range::get_as_ratio);
  229. ClassDB::bind_method(D_METHOD("set_value", "value"), &Range::set_value);
  230. ClassDB::bind_method(D_METHOD("set_value_no_signal", "value"), &Range::set_value_no_signal);
  231. ClassDB::bind_method(D_METHOD("set_min", "minimum"), &Range::set_min);
  232. ClassDB::bind_method(D_METHOD("set_max", "maximum"), &Range::set_max);
  233. ClassDB::bind_method(D_METHOD("set_step", "step"), &Range::set_step);
  234. ClassDB::bind_method(D_METHOD("set_page", "pagesize"), &Range::set_page);
  235. ClassDB::bind_method(D_METHOD("set_as_ratio", "value"), &Range::set_as_ratio);
  236. ClassDB::bind_method(D_METHOD("set_use_rounded_values", "enabled"), &Range::set_use_rounded_values);
  237. ClassDB::bind_method(D_METHOD("is_using_rounded_values"), &Range::is_using_rounded_values);
  238. ClassDB::bind_method(D_METHOD("set_exp_ratio", "enabled"), &Range::set_exp_ratio);
  239. ClassDB::bind_method(D_METHOD("is_ratio_exp"), &Range::is_ratio_exp);
  240. ClassDB::bind_method(D_METHOD("set_allow_greater", "allow"), &Range::set_allow_greater);
  241. ClassDB::bind_method(D_METHOD("is_greater_allowed"), &Range::is_greater_allowed);
  242. ClassDB::bind_method(D_METHOD("set_allow_lesser", "allow"), &Range::set_allow_lesser);
  243. ClassDB::bind_method(D_METHOD("is_lesser_allowed"), &Range::is_lesser_allowed);
  244. ClassDB::bind_method(D_METHOD("share", "with"), &Range::_share);
  245. ClassDB::bind_method(D_METHOD("unshare"), &Range::unshare);
  246. ADD_SIGNAL(MethodInfo("value_changed", PropertyInfo(Variant::FLOAT, "value")));
  247. ADD_SIGNAL(MethodInfo("changed"));
  248. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min_value"), "set_min", "get_min");
  249. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_value"), "set_max", "get_max");
  250. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "step"), "set_step", "get_step");
  251. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "page"), "set_page", "get_page");
  252. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "value"), "set_value", "get_value");
  253. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "ratio", PROPERTY_HINT_RANGE, "0,1,0.01", PROPERTY_USAGE_NONE), "set_as_ratio", "get_as_ratio");
  254. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exp_edit"), "set_exp_ratio", "is_ratio_exp");
  255. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rounded"), "set_use_rounded_values", "is_using_rounded_values");
  256. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_greater"), "set_allow_greater", "is_greater_allowed");
  257. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_lesser"), "set_allow_lesser", "is_lesser_allowed");
  258. GDVIRTUAL_BIND(_value_changed, "new_value");
  259. ADD_LINKED_PROPERTY("min_value", "value");
  260. ADD_LINKED_PROPERTY("min_value", "max_value");
  261. ADD_LINKED_PROPERTY("min_value", "page");
  262. ADD_LINKED_PROPERTY("max_value", "value");
  263. ADD_LINKED_PROPERTY("max_value", "page");
  264. }
  265. void Range::set_use_rounded_values(bool p_enable) {
  266. _rounded_values = p_enable;
  267. }
  268. bool Range::is_using_rounded_values() const {
  269. return _rounded_values;
  270. }
  271. void Range::set_exp_ratio(bool p_enable) {
  272. if (shared->exp_ratio == p_enable) {
  273. return;
  274. }
  275. shared->exp_ratio = p_enable;
  276. update_configuration_warnings();
  277. }
  278. bool Range::is_ratio_exp() const {
  279. return shared->exp_ratio;
  280. }
  281. void Range::set_allow_greater(bool p_allow) {
  282. shared->allow_greater = p_allow;
  283. }
  284. bool Range::is_greater_allowed() const {
  285. return shared->allow_greater;
  286. }
  287. void Range::set_allow_lesser(bool p_allow) {
  288. shared->allow_lesser = p_allow;
  289. }
  290. bool Range::is_lesser_allowed() const {
  291. return shared->allow_lesser;
  292. }
  293. Range::Range() {
  294. shared = memnew(Shared);
  295. shared->owners.insert(this);
  296. }
  297. Range::~Range() {
  298. _unref_shared();
  299. }