spin_box.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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-2014 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. Size2 SpinBox::get_minimum_size() const {
  31. Size2 ms = line_edit->get_combined_minimum_size();
  32. ms.width+=last_w;
  33. return ms;
  34. }
  35. void SpinBox::_value_changed(double) {
  36. String value = String::num(get_val(),Math::decimals(get_step()));
  37. if (prefix!="")
  38. value=prefix+" "+value;
  39. if (suffix!="")
  40. value+=" "+suffix;
  41. line_edit->set_text(value);
  42. }
  43. void SpinBox::_text_entered(const String& p_string) {
  44. //if (!p_string.is_numeric())
  45. // return;
  46. set_val( p_string.to_double() );
  47. _value_changed(0);
  48. }
  49. LineEdit *SpinBox::get_line_edit() {
  50. return line_edit;
  51. }
  52. void SpinBox::_input_event(const InputEvent& p_event) {
  53. if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.pressed) {
  54. const InputEventMouseButton &mb=p_event.mouse_button;
  55. if (mb.doubleclick)
  56. return; //ignore doubleclick
  57. bool up = mb.y < (get_size().height/2);
  58. switch(mb.button_index) {
  59. case BUTTON_LEFT: {
  60. set_val( get_val() + (up?get_step():-get_step()));
  61. } break;
  62. case BUTTON_RIGHT: {
  63. set_val( (up?get_max():get_min()) );
  64. } break;
  65. case BUTTON_WHEEL_UP: {
  66. set_val( get_val() + get_step() );
  67. } break;
  68. case BUTTON_WHEEL_DOWN: {
  69. set_val( get_val() - get_step() );
  70. } break;
  71. }
  72. }
  73. }
  74. void SpinBox::_line_edit_focus_exit() {
  75. _text_entered(line_edit->get_text());
  76. }
  77. void SpinBox::_notification(int p_what) {
  78. if (p_what==NOTIFICATION_DRAW) {
  79. Ref<Texture> updown = get_icon("updown");
  80. int w = updown->get_width();
  81. if (w!=last_w) {
  82. line_edit->set_margin(MARGIN_RIGHT,w);
  83. last_w=w;
  84. }
  85. RID ci = get_canvas_item();
  86. Size2i size = get_size();
  87. updown->draw(ci,Point2i(size.width-updown->get_width(),(size.height-updown->get_height())/2));
  88. } else if (p_what==NOTIFICATION_FOCUS_EXIT) {
  89. //_value_changed(0);
  90. } else if (p_what==NOTIFICATION_ENTER_SCENE) {
  91. _value_changed(0);
  92. }
  93. }
  94. void SpinBox::set_suffix(const String& p_suffix) {
  95. suffix=p_suffix;
  96. _value_changed(0);
  97. }
  98. String SpinBox::get_suffix() const{
  99. return suffix;
  100. }
  101. void SpinBox::set_prefix(const String& p_prefix) {
  102. prefix=p_prefix;
  103. _value_changed(0);
  104. }
  105. String SpinBox::get_prefix() const{
  106. return prefix;
  107. }
  108. void SpinBox::set_editable(bool p_editable) {
  109. line_edit->set_editable(p_editable);
  110. }
  111. bool SpinBox::is_editable() const {
  112. return line_edit->is_editable();
  113. }
  114. void SpinBox::_bind_methods() {
  115. //ObjectTypeDB::bind_method(_MD("_value_changed"),&SpinBox::_value_changed);
  116. ObjectTypeDB::bind_method(_MD("_input_event"),&SpinBox::_input_event);
  117. ObjectTypeDB::bind_method(_MD("_text_entered"),&SpinBox::_text_entered);
  118. ObjectTypeDB::bind_method(_MD("set_suffix","suffix"),&SpinBox::set_suffix);
  119. ObjectTypeDB::bind_method(_MD("get_suffix"),&SpinBox::get_suffix);
  120. ObjectTypeDB::bind_method(_MD("set_prefix","prefix"),&SpinBox::set_prefix);
  121. ObjectTypeDB::bind_method(_MD("get_prefix"),&SpinBox::get_prefix);
  122. ObjectTypeDB::bind_method(_MD("set_editable","editable"),&SpinBox::set_editable);
  123. ObjectTypeDB::bind_method(_MD("is_editable"),&SpinBox::is_editable);
  124. ObjectTypeDB::bind_method(_MD("_line_edit_focus_exit"),&SpinBox::_line_edit_focus_exit);
  125. ObjectTypeDB::bind_method(_MD("get_line_edit"),&SpinBox::get_line_edit);
  126. ADD_PROPERTY(PropertyInfo(Variant::BOOL,"editable"),_SCS("set_editable"),_SCS("is_editable"));
  127. ADD_PROPERTY(PropertyInfo(Variant::STRING,"prefix"),_SCS("set_prefix"),_SCS("get_prefix"));
  128. ADD_PROPERTY(PropertyInfo(Variant::STRING,"suffix"),_SCS("set_suffix"),_SCS("get_suffix"));
  129. }
  130. SpinBox::SpinBox() {
  131. last_w = 0;
  132. line_edit = memnew( LineEdit );
  133. add_child(line_edit);
  134. line_edit->set_area_as_parent_rect();
  135. //connect("value_changed",this,"_value_changed");
  136. line_edit->connect("text_entered",this,"_text_entered",Vector<Variant>(),CONNECT_DEFERRED);
  137. line_edit->connect("focus_exit",this,"_line_edit_focus_exit",Vector<Variant>(),CONNECT_DEFERRED);
  138. }