2
0

range.cpp 10 KB

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