slider.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*************************************************************************/
  2. /* slider.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "core/os/keyboard.h"
  32. Size2 Slider::get_minimum_size() const {
  33. Ref<StyleBox> style = get_stylebox("slider");
  34. Size2i ss = style->get_minimum_size() + style->get_center_size();
  35. Ref<Texture> grabber = get_icon("grabber");
  36. Size2i rs = grabber->get_size();
  37. if (orientation == HORIZONTAL)
  38. return Size2i(ss.width, MAX(ss.height, rs.height));
  39. else
  40. return Size2i(MAX(ss.width, rs.width), ss.height);
  41. }
  42. void Slider::_gui_input(Ref<InputEvent> p_event) {
  43. if (!editable) {
  44. return;
  45. }
  46. Ref<InputEventMouseButton> mb = p_event;
  47. if (mb.is_valid()) {
  48. if (mb->get_button_index() == BUTTON_LEFT) {
  49. if (mb->is_pressed()) {
  50. Ref<Texture> grabber = get_icon(mouse_inside || has_focus() ? "grabber_highlight" : "grabber");
  51. grab.pos = orientation == VERTICAL ? mb->get_position().y : mb->get_position().x;
  52. double grab_width = (double)grabber->get_size().width;
  53. double grab_height = (double)grabber->get_size().height;
  54. double max = orientation == VERTICAL ? get_size().height - grab_height : get_size().width - grab_width;
  55. if (orientation == VERTICAL)
  56. set_as_ratio(1 - (((double)grab.pos - (grab_height / 2.0)) / max));
  57. else
  58. set_as_ratio(((double)grab.pos - (grab_width / 2.0)) / max);
  59. grab.active = true;
  60. grab.uvalue = get_as_ratio();
  61. } else {
  62. grab.active = false;
  63. }
  64. } else if (scrollable) {
  65. if (mb->is_pressed() && mb->get_button_index() == BUTTON_WHEEL_UP) {
  66. grab_focus();
  67. set_value(get_value() + get_step());
  68. } else if (mb->is_pressed() && mb->get_button_index() == BUTTON_WHEEL_DOWN) {
  69. grab_focus();
  70. set_value(get_value() - get_step());
  71. }
  72. }
  73. }
  74. Ref<InputEventMouseMotion> mm = p_event;
  75. if (mm.is_valid()) {
  76. if (grab.active) {
  77. Size2i size = get_size();
  78. Ref<Texture> grabber = get_icon("grabber");
  79. float motion = (orientation == VERTICAL ? mm->get_position().y : mm->get_position().x) - grab.pos;
  80. if (orientation == VERTICAL)
  81. motion = -motion;
  82. float areasize = orientation == VERTICAL ? size.height - grabber->get_size().height : size.width - grabber->get_size().width;
  83. if (areasize <= 0)
  84. return;
  85. float umotion = motion / float(areasize);
  86. set_as_ratio(grab.uvalue + umotion);
  87. }
  88. }
  89. if (!mm.is_valid() && !mb.is_valid()) {
  90. if (p_event->is_action_pressed("ui_left", true)) {
  91. if (orientation != HORIZONTAL)
  92. return;
  93. set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
  94. accept_event();
  95. } else if (p_event->is_action_pressed("ui_right", true)) {
  96. if (orientation != HORIZONTAL)
  97. return;
  98. set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
  99. accept_event();
  100. } else if (p_event->is_action_pressed("ui_up", true)) {
  101. if (orientation != VERTICAL)
  102. return;
  103. set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
  104. accept_event();
  105. } else if (p_event->is_action_pressed("ui_down", true)) {
  106. if (orientation != VERTICAL)
  107. return;
  108. set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
  109. accept_event();
  110. } else if (p_event->is_action("ui_home") && p_event->is_pressed()) {
  111. set_value(get_min());
  112. accept_event();
  113. } else if (p_event->is_action("ui_end") && p_event->is_pressed()) {
  114. set_value(get_max());
  115. accept_event();
  116. }
  117. }
  118. }
  119. void Slider::_notification(int p_what) {
  120. switch (p_what) {
  121. case NOTIFICATION_THEME_CHANGED: {
  122. minimum_size_changed();
  123. update();
  124. } break;
  125. case NOTIFICATION_MOUSE_ENTER: {
  126. mouse_inside = true;
  127. update();
  128. } break;
  129. case NOTIFICATION_MOUSE_EXIT: {
  130. mouse_inside = false;
  131. update();
  132. } break;
  133. case NOTIFICATION_VISIBILITY_CHANGED: // fallthrough
  134. case NOTIFICATION_EXIT_TREE: {
  135. mouse_inside = false;
  136. grab.active = false;
  137. } break;
  138. case NOTIFICATION_DRAW: {
  139. RID ci = get_canvas_item();
  140. Size2i size = get_size();
  141. Ref<StyleBox> style = get_stylebox("slider");
  142. bool highlighted = mouse_inside || has_focus();
  143. Ref<StyleBox> grabber_area = get_stylebox(highlighted ? "grabber_area_highlight" : "grabber_area");
  144. Ref<Texture> grabber = get_icon(editable ? (highlighted ? "grabber_highlight" : "grabber") : "grabber_disabled");
  145. Ref<Texture> tick = get_icon("tick");
  146. double ratio = Math::is_nan(get_as_ratio()) ? 0 : get_as_ratio();
  147. if (orientation == VERTICAL) {
  148. int widget_width = style->get_minimum_size().width + style->get_center_size().width;
  149. float areasize = size.height - grabber->get_size().height;
  150. style->draw(ci, Rect2i(Point2i(size.width / 2 - widget_width / 2, 0), Size2i(widget_width, size.height)));
  151. grabber_area->draw(ci, Rect2i(Point2i((size.width - widget_width) / 2, size.height - areasize * ratio - grabber->get_size().height / 2), Size2i(widget_width, areasize * ratio + grabber->get_size().width / 2)));
  152. if (ticks > 1) {
  153. int grabber_offset = (grabber->get_size().height / 2 - tick->get_height() / 2);
  154. for (int i = 0; i < ticks; i++) {
  155. if (!ticks_on_borders && (i == 0 || i + 1 == ticks)) continue;
  156. int ofs = (i * areasize / (ticks - 1)) + grabber_offset;
  157. tick->draw(ci, Point2i((size.width - widget_width) / 2, ofs));
  158. }
  159. }
  160. grabber->draw(ci, Point2i(size.width / 2 - grabber->get_size().width / 2, size.height - ratio * areasize - grabber->get_size().height));
  161. } else {
  162. int widget_height = style->get_minimum_size().height + style->get_center_size().height;
  163. float areasize = size.width - grabber->get_size().width;
  164. style->draw(ci, Rect2i(Point2i(0, (size.height - widget_height) / 2), Size2i(size.width, widget_height)));
  165. grabber_area->draw(ci, Rect2i(Point2i(0, (size.height - widget_height) / 2), Size2i(areasize * ratio + grabber->get_size().width / 2, widget_height)));
  166. if (ticks > 1) {
  167. int grabber_offset = (grabber->get_size().width / 2 - tick->get_width() / 2);
  168. for (int i = 0; i < ticks; i++) {
  169. if ((!ticks_on_borders) && ((i == 0) || ((i + 1) == ticks))) continue;
  170. int ofs = (i * areasize / (ticks - 1)) + grabber_offset;
  171. tick->draw(ci, Point2i(ofs, (size.height - widget_height) / 2));
  172. }
  173. }
  174. grabber->draw(ci, Point2i(ratio * areasize, size.height / 2 - grabber->get_size().height / 2));
  175. }
  176. } break;
  177. }
  178. }
  179. void Slider::set_custom_step(float p_custom_step) {
  180. custom_step = p_custom_step;
  181. }
  182. float Slider::get_custom_step() const {
  183. return custom_step;
  184. }
  185. void Slider::set_ticks(int p_count) {
  186. ticks = p_count;
  187. update();
  188. }
  189. int Slider::get_ticks() const {
  190. return ticks;
  191. }
  192. bool Slider::get_ticks_on_borders() const {
  193. return ticks_on_borders;
  194. }
  195. void Slider::set_ticks_on_borders(bool _tob) {
  196. ticks_on_borders = _tob;
  197. update();
  198. }
  199. void Slider::set_editable(bool p_editable) {
  200. editable = p_editable;
  201. update();
  202. }
  203. bool Slider::is_editable() const {
  204. return editable;
  205. }
  206. void Slider::set_scrollable(bool p_scrollable) {
  207. scrollable = p_scrollable;
  208. }
  209. bool Slider::is_scrollable() const {
  210. return scrollable;
  211. }
  212. void Slider::_bind_methods() {
  213. ClassDB::bind_method(D_METHOD("_gui_input"), &Slider::_gui_input);
  214. ClassDB::bind_method(D_METHOD("set_ticks", "count"), &Slider::set_ticks);
  215. ClassDB::bind_method(D_METHOD("get_ticks"), &Slider::get_ticks);
  216. ClassDB::bind_method(D_METHOD("get_ticks_on_borders"), &Slider::get_ticks_on_borders);
  217. ClassDB::bind_method(D_METHOD("set_ticks_on_borders", "ticks_on_border"), &Slider::set_ticks_on_borders);
  218. ClassDB::bind_method(D_METHOD("set_editable", "editable"), &Slider::set_editable);
  219. ClassDB::bind_method(D_METHOD("is_editable"), &Slider::is_editable);
  220. ClassDB::bind_method(D_METHOD("set_scrollable", "scrollable"), &Slider::set_scrollable);
  221. ClassDB::bind_method(D_METHOD("is_scrollable"), &Slider::is_scrollable);
  222. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
  223. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scrollable"), "set_scrollable", "is_scrollable");
  224. ADD_PROPERTY(PropertyInfo(Variant::INT, "tick_count", PROPERTY_HINT_RANGE, "0,4096,1"), "set_ticks", "get_ticks");
  225. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ticks_on_borders"), "set_ticks_on_borders", "get_ticks_on_borders");
  226. }
  227. Slider::Slider(Orientation p_orientation) {
  228. orientation = p_orientation;
  229. mouse_inside = false;
  230. grab.active = false;
  231. ticks = 0;
  232. ticks_on_borders = false;
  233. custom_step = -1;
  234. editable = true;
  235. scrollable = true;
  236. set_focus_mode(FOCUS_ALL);
  237. }