slider.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*************************************************************************/
  2. /* slider.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "slider.h"
  31. #include "os/keyboard.h"
  32. Size2 Slider::get_minimum_size() const {
  33. Ref<StyleBox> style = get_stylebox("slider");
  34. Size2i ms = style->get_minimum_size() + style->get_center_size();
  35. return ms;
  36. }
  37. void Slider::_gui_input(Ref<InputEvent> p_event) {
  38. if (!editable) {
  39. return;
  40. }
  41. Ref<InputEventMouseButton> mb = p_event;
  42. if (mb.is_valid()) {
  43. if (mb->get_button_index() == BUTTON_LEFT) {
  44. if (mb->is_pressed()) {
  45. Ref<Texture> grabber = get_icon(mouse_inside || has_focus() ? "grabber_highlight" : "grabber");
  46. grab.pos = orientation == VERTICAL ? mb->get_position().y : mb->get_position().x;
  47. double grab_width = (double)grabber->get_size().width;
  48. double grab_height = (double)grabber->get_size().height;
  49. double max = orientation == VERTICAL ? get_size().height - grab_height : get_size().width - grab_width;
  50. if (orientation == VERTICAL)
  51. set_as_ratio(1 - (((double)grab.pos - (grab_height / 2.0)) / max));
  52. else
  53. set_as_ratio(((double)grab.pos - (grab_width / 2.0)) / max);
  54. grab.active = true;
  55. grab.uvalue = get_as_ratio();
  56. } else {
  57. grab.active = false;
  58. }
  59. } else if (mb->is_pressed() && mb->get_button_index() == BUTTON_WHEEL_UP) {
  60. set_value(get_value() + get_step());
  61. } else if (mb->is_pressed() && mb->get_button_index() == BUTTON_WHEEL_DOWN) {
  62. set_value(get_value() - get_step());
  63. }
  64. }
  65. Ref<InputEventMouseMotion> mm = p_event;
  66. if (mm.is_valid()) {
  67. if (grab.active) {
  68. Size2i size = get_size();
  69. Ref<Texture> grabber = get_icon("grabber");
  70. float motion = (orientation == VERTICAL ? mm->get_position().y : mm->get_position().x) - grab.pos;
  71. if (orientation == VERTICAL)
  72. motion = -motion;
  73. float areasize = orientation == VERTICAL ? size.height - grabber->get_size().height : size.width - grabber->get_size().width;
  74. if (areasize <= 0)
  75. return;
  76. float umotion = motion / float(areasize);
  77. set_as_ratio(grab.uvalue + umotion);
  78. }
  79. }
  80. if (!mm.is_valid() && !mb.is_valid()) {
  81. if (p_event->is_action("ui_left") && p_event->is_pressed()) {
  82. if (orientation != HORIZONTAL)
  83. return;
  84. set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
  85. accept_event();
  86. } else if (p_event->is_action("ui_right") && p_event->is_pressed()) {
  87. if (orientation != HORIZONTAL)
  88. return;
  89. set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
  90. accept_event();
  91. } else if (p_event->is_action("ui_up") && p_event->is_pressed()) {
  92. if (orientation != VERTICAL)
  93. return;
  94. set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
  95. accept_event();
  96. } else if (p_event->is_action("ui_down") && p_event->is_pressed()) {
  97. if (orientation != VERTICAL)
  98. return;
  99. set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
  100. accept_event();
  101. } else {
  102. Ref<InputEventKey> k = p_event;
  103. if (!k.is_valid() || !k->is_pressed())
  104. return;
  105. switch (k->get_scancode()) {
  106. case KEY_HOME: {
  107. set_value(get_min());
  108. accept_event();
  109. } break;
  110. case KEY_END: {
  111. set_value(get_max());
  112. accept_event();
  113. } break;
  114. }
  115. }
  116. }
  117. }
  118. void Slider::_notification(int p_what) {
  119. switch (p_what) {
  120. case NOTIFICATION_MOUSE_ENTER: {
  121. mouse_inside = true;
  122. update();
  123. } break;
  124. case NOTIFICATION_MOUSE_EXIT: {
  125. mouse_inside = false;
  126. update();
  127. } break;
  128. case NOTIFICATION_VISIBILITY_CHANGED: // fallthrough
  129. case NOTIFICATION_EXIT_TREE: {
  130. mouse_inside = false;
  131. grab.active = false;
  132. } break;
  133. case NOTIFICATION_DRAW: {
  134. RID ci = get_canvas_item();
  135. Size2i size = get_size();
  136. Ref<StyleBox> style = get_stylebox("slider");
  137. Ref<StyleBox> focus = get_stylebox("focus");
  138. Ref<StyleBox> grabber_area = get_stylebox("grabber_area");
  139. Ref<Texture> grabber = get_icon(editable ? ((mouse_inside || has_focus()) ? "grabber_highlight" : "grabber") : "grabber_disabled");
  140. Ref<Texture> tick = get_icon("tick");
  141. if (orientation == VERTICAL) {
  142. int widget_width = style->get_minimum_size().width + style->get_center_size().width;
  143. float areasize = size.height - grabber->get_size().height;
  144. style->draw(ci, Rect2i(Point2i(size.width / 2 - widget_width / 2, 0), Size2i(widget_width, size.height)));
  145. grabber_area->draw(ci, Rect2i(Point2i((size.width - widget_width) / 2, size.height - areasize * get_as_ratio() - grabber->get_size().height / 2), Size2i(widget_width, areasize * get_as_ratio() + grabber->get_size().width / 2)));
  146. /*
  147. if (mouse_inside||has_focus())
  148. focus->draw(ci,Rect2i(Point2i(),Size2i(style->get_minimum_size().width+style->get_center_size().width,size.height)));
  149. */
  150. if (ticks > 1) {
  151. int tickarea = size.height - tick->get_height();
  152. for (int i = 0; i < ticks; i++) {
  153. if (!ticks_on_borders && (i == 0 || i + 1 == ticks)) continue;
  154. int ofs = i * tickarea / (ticks - 1);
  155. tick->draw(ci, Point2i((size.width - widget_width) / 2, ofs));
  156. }
  157. }
  158. grabber->draw(ci, Point2i(size.width / 2 - grabber->get_size().width / 2, size.height - get_as_ratio() * areasize - grabber->get_size().height));
  159. } else {
  160. int widget_height = style->get_minimum_size().height + style->get_center_size().height;
  161. float areasize = size.width - grabber->get_size().width;
  162. style->draw(ci, Rect2i(Point2i(0, (size.height - widget_height) / 2), Size2i(size.width, widget_height)));
  163. grabber_area->draw(ci, Rect2i(Point2i(0, (size.height - widget_height) / 2), Size2i(areasize * get_as_ratio() + grabber->get_size().width / 2, widget_height)));
  164. /*
  165. if (mouse_inside||has_focus())
  166. focus->draw(ci,Rect2i(Point2i(),Size2i(size.width,style->get_minimum_size().height+style->get_center_size().height)));
  167. */
  168. if (ticks > 1) {
  169. int tickarea = size.width - tick->get_width();
  170. for (int i = 0; i < ticks; i++) {
  171. if ((!ticks_on_borders) && ((i == 0) || ((i + 1) == ticks))) continue;
  172. int ofs = i * tickarea / (ticks - 1);
  173. tick->draw(ci, Point2i(ofs, (size.height - widget_height) / 2));
  174. }
  175. }
  176. grabber->draw(ci, Point2i(get_as_ratio() * areasize, size.height / 2 - grabber->get_size().height / 2));
  177. }
  178. } break;
  179. }
  180. }
  181. void Slider::set_custom_step(float p_custom_step) {
  182. custom_step = p_custom_step;
  183. }
  184. float Slider::get_custom_step() const {
  185. return custom_step;
  186. }
  187. void Slider::set_ticks(int p_count) {
  188. ticks = p_count;
  189. update();
  190. }
  191. int Slider::get_ticks() const {
  192. return ticks;
  193. }
  194. bool Slider::get_ticks_on_borders() const {
  195. return ticks_on_borders;
  196. }
  197. void Slider::set_ticks_on_borders(bool _tob) {
  198. ticks_on_borders = _tob;
  199. update();
  200. }
  201. void Slider::set_editable(bool p_editable) {
  202. editable = p_editable;
  203. update();
  204. }
  205. bool Slider::is_editable() const {
  206. return editable;
  207. }
  208. void Slider::_bind_methods() {
  209. ClassDB::bind_method(D_METHOD("_gui_input"), &Slider::_gui_input);
  210. ClassDB::bind_method(D_METHOD("set_ticks", "count"), &Slider::set_ticks);
  211. ClassDB::bind_method(D_METHOD("get_ticks"), &Slider::get_ticks);
  212. ClassDB::bind_method(D_METHOD("get_ticks_on_borders"), &Slider::get_ticks_on_borders);
  213. ClassDB::bind_method(D_METHOD("set_ticks_on_borders", "ticks_on_border"), &Slider::set_ticks_on_borders);
  214. ClassDB::bind_method(D_METHOD("set_editable", "editable"), &Slider::set_editable);
  215. ClassDB::bind_method(D_METHOD("is_editable"), &Slider::is_editable);
  216. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
  217. ADD_PROPERTY(PropertyInfo(Variant::INT, "tick_count", PROPERTY_HINT_RANGE, "0,4096,1"), "set_ticks", "get_ticks");
  218. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ticks_on_borders"), "set_ticks_on_borders", "get_ticks_on_borders");
  219. ADD_PROPERTY(PropertyInfo(Variant::INT, "focus_mode", PROPERTY_HINT_ENUM, "None,Click,All"), "set_focus_mode", "get_focus_mode");
  220. }
  221. Slider::Slider(Orientation p_orientation) {
  222. orientation = p_orientation;
  223. mouse_inside = false;
  224. grab.active = false;
  225. ticks = 0;
  226. custom_step = -1;
  227. editable = true;
  228. set_focus_mode(FOCUS_ALL);
  229. }