2
0

slider.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. #include "scene/theme/theme_db.h"
  33. Size2 Slider::get_minimum_size() const {
  34. Size2i ss = theme_cache.slider_style->get_minimum_size();
  35. Size2i rs = theme_cache.grabber_icon->get_size();
  36. if (orientation == HORIZONTAL) {
  37. return Size2i(ss.width, MAX(ss.height, rs.height));
  38. } else {
  39. return Size2i(MAX(ss.width, rs.width), ss.height);
  40. }
  41. }
  42. void Slider::gui_input(const Ref<InputEvent> &p_event) {
  43. ERR_FAIL_COND(p_event.is_null());
  44. if (!editable) {
  45. return;
  46. }
  47. Ref<InputEventMouseButton> mb = p_event;
  48. if (mb.is_valid()) {
  49. if (mb->get_button_index() == MouseButton::LEFT) {
  50. if (mb->is_pressed()) {
  51. Ref<Texture2D> grabber;
  52. if (mouse_inside || has_focus()) {
  53. grabber = theme_cache.grabber_hl_icon;
  54. } else {
  55. grabber = theme_cache.grabber_icon;
  56. }
  57. grab.pos = orientation == VERTICAL ? mb->get_position().y : mb->get_position().x;
  58. double grab_width = (double)grabber->get_width();
  59. double grab_height = (double)grabber->get_height();
  60. double max = orientation == VERTICAL ? get_size().height - grab_height : get_size().width - grab_width;
  61. set_block_signals(true);
  62. if (orientation == VERTICAL) {
  63. set_as_ratio(1 - (((double)grab.pos - (grab_height / 2.0)) / max));
  64. } else {
  65. set_as_ratio(((double)grab.pos - (grab_width / 2.0)) / max);
  66. }
  67. set_block_signals(false);
  68. grab.active = true;
  69. grab.uvalue = get_as_ratio();
  70. emit_signal(SNAME("drag_started"));
  71. _notify_shared_value_changed();
  72. } else {
  73. grab.active = false;
  74. const bool value_changed = !Math::is_equal_approx((double)grab.uvalue, get_as_ratio());
  75. emit_signal(SNAME("drag_ended"), value_changed);
  76. }
  77. } else if (scrollable) {
  78. if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_UP) {
  79. if (get_focus_mode() != FOCUS_NONE) {
  80. grab_focus();
  81. }
  82. set_value(get_value() + get_step());
  83. } else if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_DOWN) {
  84. if (get_focus_mode() != FOCUS_NONE) {
  85. grab_focus();
  86. }
  87. set_value(get_value() - get_step());
  88. }
  89. }
  90. }
  91. Ref<InputEventMouseMotion> mm = p_event;
  92. if (mm.is_valid()) {
  93. if (grab.active) {
  94. Size2i size = get_size();
  95. Ref<Texture2D> grabber = theme_cache.grabber_icon;
  96. double motion = (orientation == VERTICAL ? mm->get_position().y : mm->get_position().x) - grab.pos;
  97. if (orientation == VERTICAL) {
  98. motion = -motion;
  99. }
  100. double areasize = orientation == VERTICAL ? size.height - grabber->get_height() : size.width - grabber->get_width();
  101. if (areasize <= 0) {
  102. return;
  103. }
  104. double umotion = motion / double(areasize);
  105. set_as_ratio(grab.uvalue + umotion);
  106. }
  107. }
  108. Input *input = Input::get_singleton();
  109. Ref<InputEventJoypadMotion> joypadmotion_event = p_event;
  110. Ref<InputEventJoypadButton> joypadbutton_event = p_event;
  111. bool is_joypad_event = (joypadmotion_event.is_valid() || joypadbutton_event.is_valid());
  112. if (!mm.is_valid() && !mb.is_valid()) {
  113. if (p_event->is_action_pressed("ui_left", true)) {
  114. if (orientation != HORIZONTAL) {
  115. return;
  116. }
  117. if (is_joypad_event) {
  118. if (!input->is_action_just_pressed("ui_left", true)) {
  119. return;
  120. }
  121. set_process_internal(true);
  122. }
  123. set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
  124. accept_event();
  125. } else if (p_event->is_action_pressed("ui_right", true)) {
  126. if (orientation != HORIZONTAL) {
  127. return;
  128. }
  129. if (is_joypad_event) {
  130. if (!input->is_action_just_pressed("ui_right", true)) {
  131. return;
  132. }
  133. set_process_internal(true);
  134. }
  135. set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
  136. accept_event();
  137. } else if (p_event->is_action_pressed("ui_up", true)) {
  138. if (orientation != VERTICAL) {
  139. return;
  140. }
  141. if (is_joypad_event) {
  142. if (!input->is_action_just_pressed("ui_up", true)) {
  143. return;
  144. }
  145. set_process_internal(true);
  146. }
  147. set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
  148. accept_event();
  149. } else if (p_event->is_action_pressed("ui_down", true)) {
  150. if (orientation != VERTICAL) {
  151. return;
  152. }
  153. if (is_joypad_event) {
  154. if (!input->is_action_just_pressed("ui_down", true)) {
  155. return;
  156. }
  157. set_process_internal(true);
  158. }
  159. set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
  160. accept_event();
  161. } else if (p_event->is_action("ui_home", true) && p_event->is_pressed()) {
  162. set_value(get_min());
  163. accept_event();
  164. } else if (p_event->is_action("ui_end", true) && p_event->is_pressed()) {
  165. set_value(get_max());
  166. accept_event();
  167. }
  168. }
  169. }
  170. void Slider::_notification(int p_what) {
  171. switch (p_what) {
  172. case NOTIFICATION_INTERNAL_PROCESS: {
  173. Input *input = Input::get_singleton();
  174. if (input->is_action_just_released("ui_left") || input->is_action_just_released("ui_right") || input->is_action_just_released("ui_up") || input->is_action_just_released("ui_down")) {
  175. gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS;
  176. set_process_internal(false);
  177. return;
  178. }
  179. gamepad_event_delay_ms -= get_process_delta_time();
  180. if (gamepad_event_delay_ms <= 0) {
  181. gamepad_event_delay_ms = GAMEPAD_EVENT_REPEAT_RATE_MS + gamepad_event_delay_ms;
  182. if (orientation == HORIZONTAL) {
  183. if (input->is_action_pressed("ui_left")) {
  184. set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
  185. }
  186. if (input->is_action_pressed("ui_right")) {
  187. set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
  188. }
  189. } else if (orientation == VERTICAL) {
  190. if (input->is_action_pressed("ui_down")) {
  191. set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
  192. }
  193. if (input->is_action_pressed("ui_up")) {
  194. set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
  195. }
  196. }
  197. }
  198. } break;
  199. case NOTIFICATION_THEME_CHANGED: {
  200. update_minimum_size();
  201. queue_redraw();
  202. } break;
  203. case NOTIFICATION_MOUSE_ENTER: {
  204. mouse_inside = true;
  205. queue_redraw();
  206. } break;
  207. case NOTIFICATION_MOUSE_EXIT: {
  208. mouse_inside = false;
  209. queue_redraw();
  210. } break;
  211. case NOTIFICATION_VISIBILITY_CHANGED:
  212. case NOTIFICATION_EXIT_TREE: {
  213. mouse_inside = false;
  214. grab.active = false;
  215. } break;
  216. case NOTIFICATION_DRAW: {
  217. RID ci = get_canvas_item();
  218. Size2i size = get_size();
  219. double ratio = Math::is_nan(get_as_ratio()) ? 0 : get_as_ratio();
  220. Ref<StyleBox> style = theme_cache.slider_style;
  221. Ref<Texture2D> tick = theme_cache.tick_icon;
  222. bool highlighted = editable && (mouse_inside || has_focus());
  223. Ref<Texture2D> grabber;
  224. if (editable) {
  225. if (highlighted) {
  226. grabber = theme_cache.grabber_hl_icon;
  227. } else {
  228. grabber = theme_cache.grabber_icon;
  229. }
  230. } else {
  231. grabber = theme_cache.grabber_disabled_icon;
  232. }
  233. Ref<StyleBox> grabber_area;
  234. if (highlighted) {
  235. grabber_area = theme_cache.grabber_area_hl_style;
  236. } else {
  237. grabber_area = theme_cache.grabber_area_style;
  238. }
  239. if (orientation == VERTICAL) {
  240. int widget_width = style->get_minimum_size().width;
  241. double areasize = size.height - (theme_cache.center_grabber ? 0 : grabber->get_height());
  242. int grabber_shift = theme_cache.center_grabber ? grabber->get_height() / 2 : 0;
  243. style->draw(ci, Rect2i(Point2i(size.width / 2 - widget_width / 2, 0), Size2i(widget_width, size.height)));
  244. grabber_area->draw(ci, Rect2i(Point2i((size.width - widget_width) / 2, size.height - areasize * ratio - grabber->get_height() / 2 + grabber_shift), Size2i(widget_width, areasize * ratio + grabber->get_height() / 2 - grabber_shift)));
  245. if (ticks > 1) {
  246. int grabber_offset = (grabber->get_height() / 2 - tick->get_height() / 2);
  247. for (int i = 0; i < ticks; i++) {
  248. if (!ticks_on_borders && (i == 0 || i + 1 == ticks)) {
  249. continue;
  250. }
  251. int ofs = (i * areasize / (ticks - 1)) + grabber_offset - grabber_shift;
  252. tick->draw(ci, Point2i((size.width - widget_width) / 2, ofs));
  253. }
  254. }
  255. grabber->draw(ci, Point2i(size.width / 2 - grabber->get_width() / 2 + theme_cache.grabber_offset, size.height - ratio * areasize - grabber->get_height() + grabber_shift));
  256. } else {
  257. int widget_height = style->get_minimum_size().height;
  258. double areasize = size.width - (theme_cache.center_grabber ? 0 : grabber->get_size().width);
  259. int grabber_shift = theme_cache.center_grabber ? -grabber->get_width() / 2 : 0;
  260. style->draw(ci, Rect2i(Point2i(0, (size.height - widget_height) / 2), Size2i(size.width, widget_height)));
  261. grabber_area->draw(ci, Rect2i(Point2i(0, (size.height - widget_height) / 2), Size2i(areasize * ratio + grabber->get_width() / 2 + grabber_shift, widget_height)));
  262. if (ticks > 1) {
  263. int grabber_offset = (grabber->get_width() / 2 - tick->get_width() / 2);
  264. for (int i = 0; i < ticks; i++) {
  265. if ((!ticks_on_borders) && ((i == 0) || ((i + 1) == ticks))) {
  266. continue;
  267. }
  268. int ofs = (i * areasize / (ticks - 1)) + grabber_offset + grabber_shift;
  269. tick->draw(ci, Point2i(ofs, (size.height - widget_height) / 2));
  270. }
  271. }
  272. grabber->draw(ci, Point2i(ratio * areasize + grabber_shift, size.height / 2 - grabber->get_height() / 2 + theme_cache.grabber_offset));
  273. }
  274. } break;
  275. }
  276. }
  277. void Slider::set_custom_step(double p_custom_step) {
  278. custom_step = p_custom_step;
  279. }
  280. double Slider::get_custom_step() const {
  281. return custom_step;
  282. }
  283. void Slider::set_ticks(int p_count) {
  284. if (ticks == p_count) {
  285. return;
  286. }
  287. ticks = p_count;
  288. queue_redraw();
  289. }
  290. int Slider::get_ticks() const {
  291. return ticks;
  292. }
  293. bool Slider::get_ticks_on_borders() const {
  294. return ticks_on_borders;
  295. }
  296. void Slider::set_ticks_on_borders(bool _tob) {
  297. if (ticks_on_borders == _tob) {
  298. return;
  299. }
  300. ticks_on_borders = _tob;
  301. queue_redraw();
  302. }
  303. void Slider::set_editable(bool p_editable) {
  304. if (editable == p_editable) {
  305. return;
  306. }
  307. grab.active = false;
  308. editable = p_editable;
  309. queue_redraw();
  310. }
  311. bool Slider::is_editable() const {
  312. return editable;
  313. }
  314. void Slider::set_scrollable(bool p_scrollable) {
  315. scrollable = p_scrollable;
  316. }
  317. bool Slider::is_scrollable() const {
  318. return scrollable;
  319. }
  320. void Slider::_bind_methods() {
  321. ClassDB::bind_method(D_METHOD("set_ticks", "count"), &Slider::set_ticks);
  322. ClassDB::bind_method(D_METHOD("get_ticks"), &Slider::get_ticks);
  323. ClassDB::bind_method(D_METHOD("get_ticks_on_borders"), &Slider::get_ticks_on_borders);
  324. ClassDB::bind_method(D_METHOD("set_ticks_on_borders", "ticks_on_border"), &Slider::set_ticks_on_borders);
  325. ClassDB::bind_method(D_METHOD("set_editable", "editable"), &Slider::set_editable);
  326. ClassDB::bind_method(D_METHOD("is_editable"), &Slider::is_editable);
  327. ClassDB::bind_method(D_METHOD("set_scrollable", "scrollable"), &Slider::set_scrollable);
  328. ClassDB::bind_method(D_METHOD("is_scrollable"), &Slider::is_scrollable);
  329. ADD_SIGNAL(MethodInfo("drag_started"));
  330. ADD_SIGNAL(MethodInfo("drag_ended", PropertyInfo(Variant::BOOL, "value_changed")));
  331. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
  332. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scrollable"), "set_scrollable", "is_scrollable");
  333. ADD_PROPERTY(PropertyInfo(Variant::INT, "tick_count", PROPERTY_HINT_RANGE, "0,4096,1"), "set_ticks", "get_ticks");
  334. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ticks_on_borders"), "set_ticks_on_borders", "get_ticks_on_borders");
  335. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Slider, slider_style, "slider");
  336. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Slider, grabber_area_style, "grabber_area");
  337. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Slider, grabber_area_hl_style, "grabber_area_highlight");
  338. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, Slider, grabber_icon, "grabber");
  339. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, Slider, grabber_hl_icon, "grabber_highlight");
  340. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, Slider, grabber_disabled_icon, "grabber_disabled");
  341. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, Slider, tick_icon, "tick");
  342. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Slider, center_grabber);
  343. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Slider, grabber_offset);
  344. }
  345. Slider::Slider(Orientation p_orientation) {
  346. orientation = p_orientation;
  347. set_focus_mode(FOCUS_ALL);
  348. }