range.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. if (shared->val==p_val)
  66. return;
  67. shared->val=p_val;
  68. shared->emit_value_changed();
  69. }
  70. void Range::set_min(double p_min) {
  71. shared->min=p_min;
  72. set_val(shared->val);
  73. shared->emit_changed("range/min");
  74. }
  75. void Range::set_max(double p_max) {
  76. shared->max=p_max;
  77. set_val(shared->val);
  78. shared->emit_changed("range/max");
  79. }
  80. void Range::set_step(double p_step) {
  81. shared->step=p_step;
  82. shared->emit_changed("range/step");
  83. }
  84. void Range::set_page(double p_page) {
  85. shared->page=p_page;
  86. set_val(shared->val);
  87. shared->emit_changed("range/page");
  88. }
  89. double Range::get_val() const {
  90. return shared->val;
  91. }
  92. double Range::get_min() const {
  93. return shared->min;
  94. }
  95. double Range::get_max() const {
  96. return shared->max;
  97. }
  98. double Range::get_step() const {
  99. return shared->step;
  100. }
  101. double Range::get_page() const {
  102. return shared->page;
  103. }
  104. void Range::set_unit_value(double p_value) {
  105. if (shared->exp_unit_value && get_min()>0) {
  106. double exp_min = Math::log(get_min())/Math::log(2);
  107. double exp_max = Math::log(get_max())/Math::log(2);
  108. double v = Math::pow(2,exp_min+(exp_max-exp_min)*p_value);
  109. set_val( v );
  110. } else {
  111. set_val( (get_max() - get_min()) * p_value + get_min() );
  112. }
  113. }
  114. double Range::get_unit_value() const {
  115. if (shared->exp_unit_value && get_min()>0) {
  116. double exp_min = Math::log(get_min())/Math::log(2);
  117. double exp_max = Math::log(get_max())/Math::log(2);
  118. double v = Math::log(get_val())/Math::log(2);
  119. return (v - exp_min) / (exp_max - exp_min);
  120. } else {
  121. return (get_val() - get_min()) / (get_max() - get_min());
  122. }
  123. }
  124. void Range::_share(Node *p_range) {
  125. Range * r = p_range->cast_to<Range>();
  126. ERR_FAIL_COND(!r);
  127. share(r);
  128. }
  129. void Range::share(Range *p_range) {
  130. ERR_FAIL_NULL(p_range);
  131. p_range->_ref_shared(shared);
  132. p_range->_changed_notify();
  133. p_range->_value_changed_notify();
  134. }
  135. void Range::unshare() {
  136. Shared * nshared = memnew(Shared);
  137. nshared->min=shared->min;
  138. nshared->max=shared->max;
  139. nshared->val=shared->val;
  140. nshared->step=shared->step;
  141. nshared->page=shared->page;
  142. _unref_shared();
  143. _ref_shared(nshared);
  144. }
  145. void Range::_ref_shared(Shared *p_shared) {
  146. if (shared && p_shared==shared)
  147. return;
  148. _unref_shared();
  149. shared=p_shared;
  150. shared->owners.insert(this);
  151. }
  152. void Range::_unref_shared() {
  153. shared->owners.erase(this);
  154. if (shared->owners.size()==0) {
  155. memdelete(shared);
  156. shared=NULL;
  157. }
  158. }
  159. void Range::_bind_methods() {
  160. ObjectTypeDB::bind_method(_MD("get_val"),&Range::get_val);
  161. ObjectTypeDB::bind_method(_MD("get_value"),&Range::get_val);
  162. ObjectTypeDB::bind_method(_MD("get_min"),&Range::get_min);
  163. ObjectTypeDB::bind_method(_MD("get_max"),&Range::get_max);
  164. ObjectTypeDB::bind_method(_MD("get_step"),&Range::get_step);
  165. ObjectTypeDB::bind_method(_MD("get_page"),&Range::get_page);
  166. ObjectTypeDB::bind_method(_MD("get_unit_value"),&Range::get_unit_value);
  167. ObjectTypeDB::bind_method(_MD("set_val","value"),&Range::set_val);
  168. ObjectTypeDB::bind_method(_MD("set_value","value"),&Range::set_val);
  169. ObjectTypeDB::bind_method(_MD("set_min","minimum"),&Range::set_min);
  170. ObjectTypeDB::bind_method(_MD("set_max","maximum"),&Range::set_max);
  171. ObjectTypeDB::bind_method(_MD("set_step","step"),&Range::set_step);
  172. ObjectTypeDB::bind_method(_MD("set_page","pagesize"),&Range::set_page);
  173. ObjectTypeDB::bind_method(_MD("set_unit_value","value"),&Range::set_unit_value);
  174. ObjectTypeDB::bind_method(_MD("set_rounded_values","enabled"),&Range::set_rounded_values);
  175. ObjectTypeDB::bind_method(_MD("is_rounded_values"),&Range::is_rounded_values);
  176. ObjectTypeDB::bind_method(_MD("set_exp_unit_value","enabled"),&Range::set_exp_unit_value);
  177. ObjectTypeDB::bind_method(_MD("is_unit_value_exp"),&Range::is_unit_value_exp);
  178. ObjectTypeDB::bind_method(_MD("share","with"),&Range::_share);
  179. ObjectTypeDB::bind_method(_MD("unshare"),&Range::unshare);
  180. ADD_SIGNAL( MethodInfo("value_changed", PropertyInfo(Variant::REAL,"value")));
  181. ADD_SIGNAL( MethodInfo("changed"));
  182. ADD_PROPERTY( PropertyInfo( Variant::REAL, "range/min" ), _SCS("set_min"), _SCS("get_min") );
  183. ADD_PROPERTY( PropertyInfo( Variant::REAL, "range/max" ), _SCS("set_max"), _SCS("get_max") );
  184. ADD_PROPERTY( PropertyInfo( Variant::REAL, "range/step" ), _SCS("set_step"), _SCS("get_step") );
  185. ADD_PROPERTY( PropertyInfo( Variant::REAL, "range/page" ), _SCS("set_page"), _SCS("get_page") );
  186. ADD_PROPERTY( PropertyInfo( Variant::REAL, "range/value" ), _SCS("set_val"), _SCS("get_val") );
  187. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "range/exp_edit" ), _SCS("set_exp_unit_value"), _SCS("is_unit_value_exp") );
  188. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "range/rounded" ), _SCS("set_rounded_values"), _SCS("is_rounded_values") );
  189. }
  190. void Range::set_rounded_values(bool p_enable) {
  191. _rounded_values = p_enable;
  192. }
  193. bool Range::is_rounded_values() const {
  194. return _rounded_values;
  195. }
  196. void Range::set_exp_unit_value(bool p_enable) {
  197. shared->exp_unit_value=p_enable;
  198. }
  199. bool Range::is_unit_value_exp() const {
  200. return shared->exp_unit_value;
  201. }
  202. Range::Range()
  203. {
  204. shared = memnew(Shared);
  205. shared->min=0;
  206. shared->max=100;
  207. shared->val=
  208. shared->step=1;
  209. shared->page=0;
  210. shared->owners.insert(this);
  211. shared->exp_unit_value=false;
  212. _rounded_values = false;
  213. }
  214. Range::~Range() {
  215. _unref_shared();
  216. }