2
0

range.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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::Shared::emit_changed(const char *p_what) {
  57. for (Set<Range *>::Element *E = owners.front(); E; E = E->next()) {
  58. Range *r = E->get();
  59. if (!r->is_inside_tree()) {
  60. continue;
  61. }
  62. r->_changed_notify(p_what);
  63. }
  64. }
  65. void Range::set_value(double p_val) {
  66. if (shared->step > 0) {
  67. p_val = Math::round(p_val / shared->step) * shared->step;
  68. }
  69. if (_rounded_values) {
  70. p_val = Math::round(p_val);
  71. }
  72. if (!shared->allow_greater && p_val > shared->max - shared->page) {
  73. p_val = shared->max - shared->page;
  74. }
  75. if (!shared->allow_lesser && p_val < shared->min) {
  76. p_val = shared->min;
  77. }
  78. if (shared->val == p_val) {
  79. return;
  80. }
  81. shared->val = p_val;
  82. shared->emit_value_changed();
  83. }
  84. void Range::set_min(double p_min) {
  85. shared->min = p_min;
  86. set_value(shared->val);
  87. shared->emit_changed("min");
  88. update_configuration_warnings();
  89. }
  90. void Range::set_max(double p_max) {
  91. shared->max = p_max;
  92. set_value(shared->val);
  93. shared->emit_changed("max");
  94. }
  95. void Range::set_step(double p_step) {
  96. shared->step = p_step;
  97. shared->emit_changed("step");
  98. }
  99. void Range::set_page(double p_page) {
  100. shared->page = p_page;
  101. set_value(shared->val);
  102. shared->emit_changed("page");
  103. }
  104. double Range::get_value() const {
  105. return shared->val;
  106. }
  107. double Range::get_min() const {
  108. return shared->min;
  109. }
  110. double Range::get_max() const {
  111. return shared->max;
  112. }
  113. double Range::get_step() const {
  114. return shared->step;
  115. }
  116. double Range::get_page() const {
  117. return shared->page;
  118. }
  119. void Range::set_as_ratio(double p_value) {
  120. double v;
  121. if (shared->exp_ratio && get_min() >= 0) {
  122. double exp_min = get_min() == 0 ? 0.0 : Math::log(get_min()) / Math::log((double)2);
  123. double exp_max = Math::log(get_max()) / Math::log((double)2);
  124. v = Math::pow(2, exp_min + (exp_max - exp_min) * p_value);
  125. } else {
  126. double percent = (get_max() - get_min()) * p_value;
  127. if (get_step() > 0) {
  128. double steps = round(percent / get_step());
  129. v = steps * get_step() + get_min();
  130. } else {
  131. v = percent + get_min();
  132. }
  133. }
  134. v = CLAMP(v, get_min(), get_max());
  135. set_value(v);
  136. }
  137. double Range::get_as_ratio() const {
  138. if (Math::is_equal_approx(get_max(), get_min())) {
  139. // Avoid division by zero.
  140. return 1.0;
  141. }
  142. if (shared->exp_ratio && get_min() >= 0) {
  143. double exp_min = get_min() == 0 ? 0.0 : Math::log(get_min()) / Math::log((double)2);
  144. double exp_max = Math::log(get_max()) / Math::log((double)2);
  145. float value = CLAMP(get_value(), shared->min, shared->max);
  146. double v = Math::log(value) / Math::log((double)2);
  147. return CLAMP((v - exp_min) / (exp_max - exp_min), 0, 1);
  148. } else {
  149. float value = CLAMP(get_value(), shared->min, shared->max);
  150. return CLAMP((value - get_min()) / (get_max() - get_min()), 0, 1);
  151. }
  152. }
  153. void Range::_share(Node *p_range) {
  154. Range *r = Object::cast_to<Range>(p_range);
  155. ERR_FAIL_COND(!r);
  156. share(r);
  157. }
  158. void Range::share(Range *p_range) {
  159. ERR_FAIL_NULL(p_range);
  160. p_range->_ref_shared(shared);
  161. p_range->_changed_notify();
  162. p_range->_value_changed_notify();
  163. }
  164. void Range::unshare() {
  165. Shared *nshared = memnew(Shared);
  166. nshared->min = shared->min;
  167. nshared->max = shared->max;
  168. nshared->val = shared->val;
  169. nshared->step = shared->step;
  170. nshared->page = shared->page;
  171. nshared->exp_ratio = shared->exp_ratio;
  172. nshared->allow_greater = shared->allow_greater;
  173. nshared->allow_lesser = shared->allow_lesser;
  174. _unref_shared();
  175. _ref_shared(nshared);
  176. }
  177. void Range::_ref_shared(Shared *p_shared) {
  178. if (shared && p_shared == shared) {
  179. return;
  180. }
  181. _unref_shared();
  182. shared = p_shared;
  183. shared->owners.insert(this);
  184. }
  185. void Range::_unref_shared() {
  186. if (shared) {
  187. shared->owners.erase(this);
  188. if (shared->owners.size() == 0) {
  189. memdelete(shared);
  190. shared = nullptr;
  191. }
  192. }
  193. }
  194. void Range::_bind_methods() {
  195. ClassDB::bind_method(D_METHOD("get_value"), &Range::get_value);
  196. ClassDB::bind_method(D_METHOD("get_min"), &Range::get_min);
  197. ClassDB::bind_method(D_METHOD("get_max"), &Range::get_max);
  198. ClassDB::bind_method(D_METHOD("get_step"), &Range::get_step);
  199. ClassDB::bind_method(D_METHOD("get_page"), &Range::get_page);
  200. ClassDB::bind_method(D_METHOD("get_as_ratio"), &Range::get_as_ratio);
  201. ClassDB::bind_method(D_METHOD("set_value", "value"), &Range::set_value);
  202. ClassDB::bind_method(D_METHOD("set_min", "minimum"), &Range::set_min);
  203. ClassDB::bind_method(D_METHOD("set_max", "maximum"), &Range::set_max);
  204. ClassDB::bind_method(D_METHOD("set_step", "step"), &Range::set_step);
  205. ClassDB::bind_method(D_METHOD("set_page", "pagesize"), &Range::set_page);
  206. ClassDB::bind_method(D_METHOD("set_as_ratio", "value"), &Range::set_as_ratio);
  207. ClassDB::bind_method(D_METHOD("set_use_rounded_values", "enabled"), &Range::set_use_rounded_values);
  208. ClassDB::bind_method(D_METHOD("is_using_rounded_values"), &Range::is_using_rounded_values);
  209. ClassDB::bind_method(D_METHOD("set_exp_ratio", "enabled"), &Range::set_exp_ratio);
  210. ClassDB::bind_method(D_METHOD("is_ratio_exp"), &Range::is_ratio_exp);
  211. ClassDB::bind_method(D_METHOD("set_allow_greater", "allow"), &Range::set_allow_greater);
  212. ClassDB::bind_method(D_METHOD("is_greater_allowed"), &Range::is_greater_allowed);
  213. ClassDB::bind_method(D_METHOD("set_allow_lesser", "allow"), &Range::set_allow_lesser);
  214. ClassDB::bind_method(D_METHOD("is_lesser_allowed"), &Range::is_lesser_allowed);
  215. ClassDB::bind_method(D_METHOD("share", "with"), &Range::_share);
  216. ClassDB::bind_method(D_METHOD("unshare"), &Range::unshare);
  217. ADD_SIGNAL(MethodInfo("value_changed", PropertyInfo(Variant::FLOAT, "value")));
  218. ADD_SIGNAL(MethodInfo("changed"));
  219. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min_value"), "set_min", "get_min");
  220. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_value"), "set_max", "get_max");
  221. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "step"), "set_step", "get_step");
  222. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "page"), "set_page", "get_page");
  223. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "value"), "set_value", "get_value");
  224. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "ratio", PROPERTY_HINT_RANGE, "0,1,0.01", PROPERTY_USAGE_NONE), "set_as_ratio", "get_as_ratio");
  225. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exp_edit"), "set_exp_ratio", "is_ratio_exp");
  226. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rounded"), "set_use_rounded_values", "is_using_rounded_values");
  227. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_greater"), "set_allow_greater", "is_greater_allowed");
  228. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_lesser"), "set_allow_lesser", "is_lesser_allowed");
  229. }
  230. void Range::set_use_rounded_values(bool p_enable) {
  231. _rounded_values = p_enable;
  232. }
  233. bool Range::is_using_rounded_values() const {
  234. return _rounded_values;
  235. }
  236. void Range::set_exp_ratio(bool p_enable) {
  237. shared->exp_ratio = p_enable;
  238. update_configuration_warnings();
  239. }
  240. bool Range::is_ratio_exp() const {
  241. return shared->exp_ratio;
  242. }
  243. void Range::set_allow_greater(bool p_allow) {
  244. shared->allow_greater = p_allow;
  245. }
  246. bool Range::is_greater_allowed() const {
  247. return shared->allow_greater;
  248. }
  249. void Range::set_allow_lesser(bool p_allow) {
  250. shared->allow_lesser = p_allow;
  251. }
  252. bool Range::is_lesser_allowed() const {
  253. return shared->allow_lesser;
  254. }
  255. Range::Range() {
  256. shared = memnew(Shared);
  257. shared->owners.insert(this);
  258. }
  259. Range::~Range() {
  260. _unref_shared();
  261. }