range.cpp 11 KB

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