range.cpp 10.0 KB

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