slider.cpp 11 KB

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