slider.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*************************************************************************/
  2. /* slider.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 "slider.h"
  30. #include "os/keyboard.h"
  31. Size2 Slider::get_minimum_size() const {
  32. Ref<StyleBox> style = get_stylebox("slider");
  33. Size2i ms = style->get_minimum_size()+style->get_center_size();
  34. return ms;
  35. }
  36. void Slider::_input_event(InputEvent p_event) {
  37. if (p_event.type==InputEvent::MOUSE_BUTTON) {
  38. InputEventMouseButton &mb = p_event.mouse_button;
  39. if (mb.button_index==BUTTON_LEFT) {
  40. if (mb.pressed) {
  41. grab.pos=orientation==VERTICAL?mb.y:mb.x;
  42. double max = orientation==VERTICAL ? get_size().height : get_size().width ;
  43. set_val( ( ( (double)grab.pos / max) * ( get_max() - get_min() ) ) + get_min() );
  44. grab.active=true;
  45. grab.uvalue=get_unit_value();
  46. } else {
  47. grab.active=false;
  48. }
  49. } else if (mb.pressed && mb.button_index==BUTTON_WHEEL_UP) {
  50. set_val( get_val() + get_step());
  51. } else if (mb.pressed && mb.button_index==BUTTON_WHEEL_DOWN) {
  52. set_val( get_val() - get_step());
  53. }
  54. } else if (p_event.type==InputEvent::MOUSE_MOTION) {
  55. if (grab.active) {
  56. Size2i size = get_size();
  57. Ref<Texture> grabber = get_icon("grabber");
  58. float motion = (orientation==VERTICAL?p_event.mouse_motion.y:p_event.mouse_motion.x) - grab.pos;
  59. if (orientation==VERTICAL)
  60. motion=-motion;
  61. float areasize = orientation==VERTICAL?size.height - grabber->get_size().height:size.width - grabber->get_size().width;
  62. if (areasize<=0)
  63. return;
  64. float umotion = motion / float(areasize);
  65. set_unit_value( grab.uvalue + umotion );
  66. }
  67. } else {
  68. if (p_event.is_action("ui_left") && p_event.is_pressed()) {
  69. if (orientation!=HORIZONTAL)
  70. return;
  71. set_val( get_val() - (custom_step>=0?custom_step:get_step()) );
  72. accept_event();
  73. } else if (p_event.is_action("ui_right") && p_event.is_pressed()) {
  74. if (orientation!=HORIZONTAL)
  75. return;
  76. set_val( get_val() + (custom_step>=0?custom_step:get_step()) );
  77. accept_event();
  78. } else if (p_event.is_action("ui_up") && p_event.is_pressed()) {
  79. if (orientation!=VERTICAL)
  80. return;
  81. set_val( get_val() + (custom_step>=0?custom_step:get_step()) );
  82. accept_event();
  83. } else if (p_event.is_action("ui_down") && p_event.is_pressed()) {
  84. if (orientation!=VERTICAL)
  85. return;
  86. set_val( get_val() - (custom_step>=0?custom_step:get_step()) );
  87. accept_event();
  88. } else if (p_event.type==InputEvent::KEY) {
  89. const InputEventKey &k=p_event.key;
  90. if (!k.pressed)
  91. return;
  92. switch (k.scancode) {
  93. case KEY_HOME: {
  94. set_val( get_min() );
  95. accept_event();
  96. } break;
  97. case KEY_END: {
  98. set_val( get_max() );
  99. accept_event();
  100. } break;
  101. } ;
  102. }
  103. }
  104. }
  105. void Slider::_notification(int p_what) {
  106. switch(p_what) {
  107. case NOTIFICATION_MOUSE_ENTER: {
  108. mouse_inside=true;
  109. update();
  110. } break;
  111. case NOTIFICATION_MOUSE_EXIT: {
  112. mouse_inside=false;
  113. update();
  114. } break;
  115. case NOTIFICATION_DRAW: {
  116. RID ci = get_canvas_item();
  117. Size2i size = get_size();
  118. Ref<StyleBox> style = get_stylebox("slider");
  119. Ref<StyleBox> focus = get_stylebox("focus");
  120. Ref<Texture> grabber = get_icon(mouse_inside||has_focus()?"grabber_hilite":"grabber");
  121. Ref<Texture> tick = get_icon("tick");
  122. if (orientation==VERTICAL) {
  123. style->draw(ci,Rect2i(Point2i(),Size2i(style->get_minimum_size().width+style->get_center_size().width,size.height)));
  124. //if (mouse_inside||has_focus())
  125. // focus->draw(ci,Rect2i(Point2i(),Size2i(style->get_minimum_size().width+style->get_center_size().width,size.height)));
  126. float areasize = size.height - grabber->get_size().height;
  127. if (ticks>1) {
  128. int tickarea = size.height - tick->get_height();
  129. for(int i=0;i<ticks;i++) {
  130. if( ! ticks_on_borders && (i == 0 || i + 1 == ticks) ) continue;
  131. int ofs = i*tickarea/(ticks-1);
  132. tick->draw(ci,Point2(0,ofs));
  133. }
  134. }
  135. grabber->draw(ci,Point2i(size.width/2-grabber->get_size().width/2,size.height - get_unit_value()*areasize - grabber->get_size().height));
  136. } else {
  137. style->draw(ci,Rect2i(Point2i(),Size2i(size.width,style->get_minimum_size().height+style->get_center_size().height)));
  138. //if (mouse_inside||has_focus())
  139. // focus->draw(ci,Rect2i(Point2i(),Size2i(size.width,style->get_minimum_size().height+style->get_center_size().height)));
  140. float areasize = size.width - grabber->get_size().width;
  141. if (ticks>1) {
  142. int tickarea = size.width - tick->get_width();
  143. for(int i=0;i<ticks;i++) {
  144. if( (! ticks_on_borders) && ( (i == 0) || ((i + 1) == ticks)) ) continue;
  145. int ofs = i*tickarea/(ticks-1);
  146. tick->draw(ci,Point2(ofs,0));
  147. }
  148. }
  149. grabber->draw(ci,Point2i(get_unit_value()*areasize,size.height/2-grabber->get_size().height/2));
  150. }
  151. } break;
  152. }
  153. }
  154. void Slider::set_custom_step(float p_custom_step) {
  155. custom_step=p_custom_step;
  156. }
  157. float Slider::get_custom_step() const {
  158. return custom_step;
  159. }
  160. void Slider::set_ticks(int p_count) {
  161. ticks=p_count;
  162. update();
  163. }
  164. int Slider::get_ticks() const {
  165. return ticks;
  166. }
  167. bool Slider::get_ticks_on_borders() const{
  168. return ticks_on_borders;
  169. }
  170. void Slider::set_ticks_on_borders(bool _tob){
  171. ticks_on_borders = _tob;
  172. update();
  173. }
  174. void Slider::_bind_methods() {
  175. ObjectTypeDB::bind_method(_MD("_input_event"),&Slider::_input_event);
  176. ObjectTypeDB::bind_method(_MD("set_ticks","count"),&Slider::set_ticks);
  177. ObjectTypeDB::bind_method(_MD("get_ticks"),&Slider::get_ticks);
  178. ObjectTypeDB::bind_method(_MD("get_ticks_on_borders"),&Slider::get_ticks_on_borders);
  179. ObjectTypeDB::bind_method(_MD("set_ticks_on_borders","ticks_on_border"),&Slider::set_ticks_on_borders);
  180. ADD_PROPERTY( PropertyInfo( Variant::INT, "tick_count", PROPERTY_HINT_RANGE,"0,4096,1"), _SCS("set_ticks"), _SCS("get_ticks") );
  181. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "ticks_on_borders" ), _SCS("set_ticks_on_borders"), _SCS("get_ticks_on_borders") );
  182. }
  183. Slider::Slider(Orientation p_orientation) {
  184. orientation=p_orientation;
  185. mouse_inside=false;
  186. grab.active=false;
  187. ticks=0;
  188. custom_step=-1;
  189. set_focus_mode(FOCUS_ALL);
  190. }