2
0

range.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*************************************************************************/
  2. /* range.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "range.h"
  30. void Range::_value_changed_notify() {
  31. _value_changed(shared->val);
  32. emit_signal("value_changed",shared->val);
  33. update();
  34. _change_notify("range/value");
  35. }
  36. void Range::Shared::emit_value_changed() {
  37. for (Set<Range*>::Element *E=owners.front();E;E=E->next()) {
  38. Range *r=E->get();
  39. if (!r->is_inside_tree())
  40. continue;
  41. r->_value_changed_notify();
  42. }
  43. }
  44. void Range::_changed_notify(const char *p_what) {
  45. emit_signal("changed",shared->val);
  46. update();
  47. _change_notify(p_what);
  48. }
  49. void Range::Shared::emit_changed(const char *p_what) {
  50. for (Set<Range*>::Element *E=owners.front();E;E=E->next()) {
  51. Range *r=E->get();
  52. if (!r->is_inside_tree())
  53. continue;
  54. r->_changed_notify(p_what);
  55. }
  56. }
  57. void Range::set_val(double p_val) {
  58. if(_rounded_values){
  59. p_val = Math::round(p_val);
  60. }
  61. if (p_val>shared->max-shared->page)
  62. p_val=shared->max-shared->page;
  63. if (p_val<shared->min)
  64. p_val=shared->min;
  65. //avoid to set -0
  66. if (p_val == 0)
  67. p_val = 0;
  68. if (shared->val==p_val)
  69. return;
  70. shared->val=p_val;
  71. shared->emit_value_changed();
  72. }
  73. void Range::set_min(double p_min) {
  74. shared->min=p_min;
  75. set_val(shared->val);
  76. shared->emit_changed("range/min");
  77. }
  78. void Range::set_max(double p_max) {
  79. shared->max=p_max;
  80. set_val(shared->val);
  81. shared->emit_changed("range/max");
  82. }
  83. void Range::set_step(double p_step) {
  84. shared->step=p_step;
  85. shared->emit_changed("range/step");
  86. }
  87. void Range::set_page(double p_page) {
  88. shared->page=p_page;
  89. set_val(shared->val);
  90. shared->emit_changed("range/page");
  91. }
  92. double Range::get_val() const {
  93. return shared->val;
  94. }
  95. double Range::get_min() const {
  96. return shared->min;
  97. }
  98. double Range::get_max() const {
  99. return shared->max;
  100. }
  101. double Range::get_step() const {
  102. return shared->step;
  103. }
  104. double Range::get_page() const {
  105. return shared->page;
  106. }
  107. void Range::set_unit_value(double p_value) {
  108. if (shared->exp_unit_value && get_min()>0) {
  109. double exp_min = Math::log(get_min())/Math::log(2);
  110. double exp_max = Math::log(get_max())/Math::log(2);
  111. double v = Math::pow(2,exp_min+(exp_max-exp_min)*p_value);
  112. set_val( v );
  113. } else {
  114. set_val( (get_max() - get_min()) * p_value + get_min() );
  115. }
  116. }
  117. double Range::get_unit_value() const {
  118. if (shared->exp_unit_value && get_min()>0) {
  119. double exp_min = Math::log(get_min())/Math::log(2);
  120. double exp_max = Math::log(get_max())/Math::log(2);
  121. double v = Math::log(get_val())/Math::log(2);
  122. return (v - exp_min) / (exp_max - exp_min);
  123. } else {
  124. return (get_val() - get_min()) / (get_max() - get_min());
  125. }
  126. }
  127. void Range::_share(Node *p_range) {
  128. Range * r = p_range->cast_to<Range>();
  129. ERR_FAIL_COND(!r);
  130. share(r);
  131. }
  132. void Range::share(Range *p_range) {
  133. ERR_FAIL_NULL(p_range);
  134. p_range->_ref_shared(shared);
  135. p_range->_changed_notify();
  136. p_range->_value_changed_notify();
  137. }
  138. void Range::unshare() {
  139. Shared * nshared = memnew(Shared);
  140. nshared->min=shared->min;
  141. nshared->max=shared->max;
  142. nshared->val=shared->val;
  143. nshared->step=shared->step;
  144. nshared->page=shared->page;
  145. _unref_shared();
  146. _ref_shared(nshared);
  147. }
  148. void Range::_ref_shared(Shared *p_shared) {
  149. if (shared && p_shared==shared)
  150. return;
  151. _unref_shared();
  152. shared=p_shared;
  153. shared->owners.insert(this);
  154. }
  155. void Range::_unref_shared() {
  156. shared->owners.erase(this);
  157. if (shared->owners.size()==0) {
  158. memdelete(shared);
  159. shared=NULL;
  160. }
  161. }
  162. void Range::_bind_methods() {
  163. ObjectTypeDB::bind_method(_MD("get_val"),&Range::get_val);
  164. ObjectTypeDB::bind_method(_MD("get_value"),&Range::get_val);
  165. ObjectTypeDB::bind_method(_MD("get_min"),&Range::get_min);
  166. ObjectTypeDB::bind_method(_MD("get_max"),&Range::get_max);
  167. ObjectTypeDB::bind_method(_MD("get_step"),&Range::get_step);
  168. ObjectTypeDB::bind_method(_MD("get_page"),&Range::get_page);
  169. ObjectTypeDB::bind_method(_MD("get_unit_value"),&Range::get_unit_value);
  170. ObjectTypeDB::bind_method(_MD("set_val","value"),&Range::set_val);
  171. ObjectTypeDB::bind_method(_MD("set_value","value"),&Range::set_val);
  172. ObjectTypeDB::bind_method(_MD("set_min","minimum"),&Range::set_min);
  173. ObjectTypeDB::bind_method(_MD("set_max","maximum"),&Range::set_max);
  174. ObjectTypeDB::bind_method(_MD("set_step","step"),&Range::set_step);
  175. ObjectTypeDB::bind_method(_MD("set_page","pagesize"),&Range::set_page);
  176. ObjectTypeDB::bind_method(_MD("set_unit_value","value"),&Range::set_unit_value);
  177. ObjectTypeDB::bind_method(_MD("set_rounded_values","enabled"),&Range::set_rounded_values);
  178. ObjectTypeDB::bind_method(_MD("is_rounded_values"),&Range::is_rounded_values);
  179. ObjectTypeDB::bind_method(_MD("set_exp_unit_value","enabled"),&Range::set_exp_unit_value);
  180. ObjectTypeDB::bind_method(_MD("is_unit_value_exp"),&Range::is_unit_value_exp);
  181. ObjectTypeDB::bind_method(_MD("share","with"),&Range::_share);
  182. ObjectTypeDB::bind_method(_MD("unshare"),&Range::unshare);
  183. ADD_SIGNAL( MethodInfo("value_changed", PropertyInfo(Variant::REAL,"value")));
  184. ADD_SIGNAL( MethodInfo("changed"));
  185. ADD_PROPERTY( PropertyInfo( Variant::REAL, "range/min" ), _SCS("set_min"), _SCS("get_min") );
  186. ADD_PROPERTY( PropertyInfo( Variant::REAL, "range/max" ), _SCS("set_max"), _SCS("get_max") );
  187. ADD_PROPERTY( PropertyInfo( Variant::REAL, "range/step" ), _SCS("set_step"), _SCS("get_step") );
  188. ADD_PROPERTY( PropertyInfo( Variant::REAL, "range/page" ), _SCS("set_page"), _SCS("get_page") );
  189. ADD_PROPERTY( PropertyInfo( Variant::REAL, "range/value" ), _SCS("set_val"), _SCS("get_val") );
  190. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "range/exp_edit" ), _SCS("set_exp_unit_value"), _SCS("is_unit_value_exp") );
  191. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "range/rounded" ), _SCS("set_rounded_values"), _SCS("is_rounded_values") );
  192. }
  193. void Range::set_rounded_values(bool p_enable) {
  194. _rounded_values = p_enable;
  195. }
  196. bool Range::is_rounded_values() const {
  197. return _rounded_values;
  198. }
  199. void Range::set_exp_unit_value(bool p_enable) {
  200. shared->exp_unit_value=p_enable;
  201. }
  202. bool Range::is_unit_value_exp() const {
  203. return shared->exp_unit_value;
  204. }
  205. Range::Range()
  206. {
  207. shared = memnew(Shared);
  208. shared->min=0;
  209. shared->max=100;
  210. shared->val=
  211. shared->step=1;
  212. shared->page=0;
  213. shared->owners.insert(this);
  214. shared->exp_unit_value=false;
  215. _rounded_values = false;
  216. }
  217. Range::~Range() {
  218. _unref_shared();
  219. }