slider.cpp 12 KB

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