range.cpp 8.2 KB

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