slider.cpp 8.0 KB

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