spin_box.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*************************************************************************/
  2. /* spin_box.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 "spin_box.h"
  30. #include "os/input.h"
  31. Size2 SpinBox::get_minimum_size() const {
  32. Size2 ms = line_edit->get_combined_minimum_size();
  33. ms.width+=last_w;
  34. return ms;
  35. }
  36. void SpinBox::_value_changed(double) {
  37. String value = String::num(get_val(),Math::decimals(get_step()));
  38. if (prefix!="")
  39. value=prefix+" "+value;
  40. if (suffix!="")
  41. value+=" "+suffix;
  42. line_edit->set_text(value);
  43. }
  44. void SpinBox::_text_entered(const String& p_string) {
  45. //if (!p_string.is_numeric())
  46. // return;
  47. set_val( p_string.to_double() );
  48. _value_changed(0);
  49. }
  50. LineEdit *SpinBox::get_line_edit() {
  51. return line_edit;
  52. }
  53. void SpinBox::_line_edit_input(const InputEvent& p_event) {
  54. }
  55. void SpinBox::_range_click_timeout() {
  56. if (!drag.enabled && Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT)) {
  57. bool up = get_local_mouse_pos().y < (get_size().height/2);
  58. set_val( get_val() + (up?get_step():-get_step()));
  59. if (range_click_timer->is_one_shot()) {
  60. range_click_timer->set_wait_time(0.075);
  61. range_click_timer->set_one_shot(false);
  62. range_click_timer->start();
  63. }
  64. } else {
  65. range_click_timer->stop();
  66. }
  67. }
  68. void SpinBox::_input_event(const InputEvent& p_event) {
  69. if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.pressed) {
  70. const InputEventMouseButton &mb=p_event.mouse_button;
  71. if (mb.doubleclick)
  72. return; //ignore doubleclick
  73. bool up = mb.y < (get_size().height/2);
  74. switch(mb.button_index) {
  75. case BUTTON_LEFT: {
  76. set_val( get_val() + (up?get_step():-get_step()));
  77. range_click_timer->set_wait_time(0.6);
  78. range_click_timer->set_one_shot(true);
  79. range_click_timer->start();
  80. } break;
  81. case BUTTON_RIGHT: {
  82. set_val( (up?get_max():get_min()) );
  83. } break;
  84. case BUTTON_WHEEL_UP: {
  85. set_val( get_val() + get_step() );
  86. } break;
  87. case BUTTON_WHEEL_DOWN: {
  88. set_val( get_val() - get_step() );
  89. } break;
  90. }
  91. }
  92. if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.pressed && p_event.mouse_button.button_index==1) {
  93. //set_default_cursor_shape(CURSOR_VSIZE);
  94. Vector2 cpos = Vector2(p_event.mouse_button.x,p_event.mouse_button.y);
  95. drag.mouse_pos=cpos;
  96. }
  97. if (p_event.type==InputEvent::MOUSE_BUTTON && !p_event.mouse_button.pressed && p_event.mouse_button.button_index==1) {
  98. //set_default_cursor_shape(CURSOR_ARROW);
  99. range_click_timer->stop();
  100. if (drag.enabled) {
  101. drag.enabled=false;
  102. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  103. warp_mouse(drag.capture_pos);
  104. }
  105. }
  106. if (p_event.type==InputEvent::MOUSE_MOTION && p_event.mouse_button.button_mask&1) {
  107. Vector2 cpos = Vector2(p_event.mouse_motion.x,p_event.mouse_motion.y);
  108. if (drag.enabled) {
  109. float diff_y = drag.mouse_pos.y - cpos.y;
  110. diff_y=Math::pow(ABS(diff_y),1.8)*SGN(diff_y);
  111. diff_y*=0.1;
  112. drag.mouse_pos=cpos;
  113. drag.base_val=CLAMP(drag.base_val + get_step() * diff_y, get_min(), get_max());
  114. set_val( drag.base_val);
  115. } else if (drag.mouse_pos.distance_to(cpos)>2) {
  116. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  117. drag.enabled=true;
  118. drag.base_val=get_val();
  119. drag.mouse_pos=cpos;
  120. drag.capture_pos=cpos;
  121. }
  122. }
  123. }
  124. void SpinBox::_line_edit_focus_exit() {
  125. _text_entered(line_edit->get_text());
  126. }
  127. void SpinBox::_notification(int p_what) {
  128. if (p_what==NOTIFICATION_DRAW) {
  129. Ref<Texture> updown = get_icon("updown");
  130. int w = updown->get_width();
  131. if (w!=last_w) {
  132. line_edit->set_margin(MARGIN_RIGHT,w);
  133. last_w=w;
  134. }
  135. RID ci = get_canvas_item();
  136. Size2i size = get_size();
  137. updown->draw(ci,Point2i(size.width-updown->get_width(),(size.height-updown->get_height())/2));
  138. } else if (p_what==NOTIFICATION_FOCUS_EXIT) {
  139. //_value_changed(0);
  140. } else if (p_what==NOTIFICATION_ENTER_TREE) {
  141. _value_changed(0);
  142. }
  143. }
  144. void SpinBox::set_suffix(const String& p_suffix) {
  145. suffix=p_suffix;
  146. _value_changed(0);
  147. }
  148. String SpinBox::get_suffix() const{
  149. return suffix;
  150. }
  151. void SpinBox::set_prefix(const String& p_prefix) {
  152. prefix=p_prefix;
  153. _value_changed(0);
  154. }
  155. String SpinBox::get_prefix() const{
  156. return prefix;
  157. }
  158. void SpinBox::set_editable(bool p_editable) {
  159. line_edit->set_editable(p_editable);
  160. }
  161. bool SpinBox::is_editable() const {
  162. return line_edit->is_editable();
  163. }
  164. void SpinBox::_bind_methods() {
  165. //ObjectTypeDB::bind_method(_MD("_value_changed"),&SpinBox::_value_changed);
  166. ObjectTypeDB::bind_method(_MD("_input_event"),&SpinBox::_input_event);
  167. ObjectTypeDB::bind_method(_MD("_text_entered"),&SpinBox::_text_entered);
  168. ObjectTypeDB::bind_method(_MD("set_suffix","suffix"),&SpinBox::set_suffix);
  169. ObjectTypeDB::bind_method(_MD("get_suffix"),&SpinBox::get_suffix);
  170. ObjectTypeDB::bind_method(_MD("set_prefix","prefix"),&SpinBox::set_prefix);
  171. ObjectTypeDB::bind_method(_MD("get_prefix"),&SpinBox::get_prefix);
  172. ObjectTypeDB::bind_method(_MD("set_editable","editable"),&SpinBox::set_editable);
  173. ObjectTypeDB::bind_method(_MD("is_editable"),&SpinBox::is_editable);
  174. ObjectTypeDB::bind_method(_MD("_line_edit_focus_exit"),&SpinBox::_line_edit_focus_exit);
  175. ObjectTypeDB::bind_method(_MD("get_line_edit"),&SpinBox::get_line_edit);
  176. ObjectTypeDB::bind_method(_MD("_line_edit_input"),&SpinBox::_line_edit_input);
  177. ObjectTypeDB::bind_method(_MD("_range_click_timeout"),&SpinBox::_range_click_timeout);
  178. ADD_PROPERTY(PropertyInfo(Variant::BOOL,"editable"),_SCS("set_editable"),_SCS("is_editable"));
  179. ADD_PROPERTY(PropertyInfo(Variant::STRING,"prefix"),_SCS("set_prefix"),_SCS("get_prefix"));
  180. ADD_PROPERTY(PropertyInfo(Variant::STRING,"suffix"),_SCS("set_suffix"),_SCS("get_suffix"));
  181. }
  182. SpinBox::SpinBox() {
  183. last_w = 0;
  184. line_edit = memnew( LineEdit );
  185. add_child(line_edit);
  186. line_edit->set_area_as_parent_rect();
  187. //connect("value_changed",this,"_value_changed");
  188. line_edit->connect("text_entered",this,"_text_entered",Vector<Variant>(),CONNECT_DEFERRED);
  189. line_edit->connect("focus_exit",this,"_line_edit_focus_exit",Vector<Variant>(),CONNECT_DEFERRED);
  190. line_edit->connect("input_event",this,"_line_edit_input");
  191. drag.enabled=false;
  192. range_click_timer = memnew( Timer );
  193. range_click_timer->connect("timeout",this,"_range_click_timeout");
  194. add_child(range_click_timer);
  195. }