slider.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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 "scene/theme/theme_db.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. grab.value_before_dragging = get_as_ratio();
  58. emit_signal(SNAME("drag_started"));
  59. double grab_width = theme_cache.center_grabber ? 0.0 : (double)grabber->get_width();
  60. double grab_height = theme_cache.center_grabber ? 0.0 : (double)grabber->get_height();
  61. double max = orientation == VERTICAL ? get_size().height - grab_height : get_size().width - grab_width;
  62. set_block_signals(true);
  63. if (orientation == VERTICAL) {
  64. set_as_ratio(1 - (((double)grab.pos - (grab_height / 2.0)) / max));
  65. } else {
  66. double v = ((double)grab.pos - (grab_width / 2.0)) / max;
  67. set_as_ratio(is_layout_rtl() ? 1 - v : v);
  68. }
  69. set_block_signals(false);
  70. grab.active = true;
  71. grab.uvalue = get_as_ratio();
  72. _notify_shared_value_changed();
  73. } else {
  74. grab.active = false;
  75. const bool value_changed = !Math::is_equal_approx((double)grab.value_before_dragging, get_as_ratio());
  76. emit_signal(SNAME("drag_ended"), value_changed);
  77. }
  78. } else if (scrollable) {
  79. if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_UP) {
  80. if (get_focus_mode_with_override() != FOCUS_NONE) {
  81. grab_focus();
  82. }
  83. set_value(get_value() + get_step());
  84. } else if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_DOWN) {
  85. if (get_focus_mode_with_override() != FOCUS_NONE) {
  86. grab_focus();
  87. }
  88. set_value(get_value() - get_step());
  89. }
  90. }
  91. }
  92. Ref<InputEventMouseMotion> mm = p_event;
  93. if (mm.is_valid()) {
  94. if (grab.active) {
  95. Size2i size = get_size();
  96. Ref<Texture2D> grabber = theme_cache.grabber_hl_icon;
  97. double grab_width = theme_cache.center_grabber ? 0.0 : (double)grabber->get_width();
  98. double grab_height = theme_cache.center_grabber ? 0.0 : (double)grabber->get_height();
  99. double motion = (orientation == VERTICAL ? mm->get_position().y : mm->get_position().x) - grab.pos;
  100. if (orientation == VERTICAL) {
  101. motion = -motion;
  102. } else if (is_layout_rtl()) {
  103. motion = -motion;
  104. }
  105. double areasize = orientation == VERTICAL ? size.height - grab_height : size.width - grab_width;
  106. if (areasize <= 0) {
  107. return;
  108. }
  109. double umotion = motion / double(areasize);
  110. set_as_ratio(grab.uvalue + umotion);
  111. }
  112. }
  113. Input *input = Input::get_singleton();
  114. Ref<InputEventJoypadMotion> joypadmotion_event = p_event;
  115. Ref<InputEventJoypadButton> joypadbutton_event = p_event;
  116. bool is_joypad_event = (joypadmotion_event.is_valid() || joypadbutton_event.is_valid());
  117. if (mm.is_null() && mb.is_null()) {
  118. if (p_event->is_action_pressed("ui_left", true)) {
  119. if (orientation != HORIZONTAL) {
  120. return;
  121. }
  122. if (is_joypad_event) {
  123. if (!input->is_action_just_pressed("ui_left", true)) {
  124. return;
  125. }
  126. set_process_internal(true);
  127. }
  128. if (is_layout_rtl()) {
  129. set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
  130. } else {
  131. set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
  132. }
  133. accept_event();
  134. } else if (p_event->is_action_pressed("ui_right", true)) {
  135. if (orientation != HORIZONTAL) {
  136. return;
  137. }
  138. if (is_joypad_event) {
  139. if (!input->is_action_just_pressed("ui_right", true)) {
  140. return;
  141. }
  142. set_process_internal(true);
  143. }
  144. if (is_layout_rtl()) {
  145. set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
  146. } else {
  147. set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
  148. }
  149. accept_event();
  150. } else if (p_event->is_action_pressed("ui_up", true)) {
  151. if (orientation != VERTICAL) {
  152. return;
  153. }
  154. if (is_joypad_event) {
  155. if (!input->is_action_just_pressed("ui_up", true)) {
  156. return;
  157. }
  158. set_process_internal(true);
  159. }
  160. set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
  161. accept_event();
  162. } else if (p_event->is_action_pressed("ui_down", true)) {
  163. if (orientation != VERTICAL) {
  164. return;
  165. }
  166. if (is_joypad_event) {
  167. if (!input->is_action_just_pressed("ui_down", true)) {
  168. return;
  169. }
  170. set_process_internal(true);
  171. }
  172. set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
  173. accept_event();
  174. } else if (p_event->is_action("ui_home", true) && p_event->is_pressed()) {
  175. set_value(get_min());
  176. accept_event();
  177. } else if (p_event->is_action("ui_end", true) && p_event->is_pressed()) {
  178. set_value(get_max());
  179. accept_event();
  180. }
  181. }
  182. }
  183. void Slider::_notification(int p_what) {
  184. switch (p_what) {
  185. case NOTIFICATION_INTERNAL_PROCESS: {
  186. Input *input = Input::get_singleton();
  187. 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")) {
  188. gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS;
  189. set_process_internal(false);
  190. return;
  191. }
  192. gamepad_event_delay_ms -= get_process_delta_time();
  193. if (gamepad_event_delay_ms <= 0) {
  194. gamepad_event_delay_ms = GAMEPAD_EVENT_REPEAT_RATE_MS + gamepad_event_delay_ms;
  195. if (orientation == HORIZONTAL) {
  196. if (input->is_action_pressed("ui_left")) {
  197. if (is_layout_rtl()) {
  198. set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
  199. } else {
  200. set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
  201. }
  202. }
  203. if (input->is_action_pressed("ui_right")) {
  204. if (is_layout_rtl()) {
  205. set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
  206. } else {
  207. set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
  208. }
  209. }
  210. } else if (orientation == VERTICAL) {
  211. if (input->is_action_pressed("ui_down")) {
  212. set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
  213. }
  214. if (input->is_action_pressed("ui_up")) {
  215. set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
  216. }
  217. }
  218. }
  219. } break;
  220. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  221. RID ae = get_accessibility_element();
  222. ERR_FAIL_COND(ae.is_null());
  223. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_SLIDER);
  224. } break;
  225. case NOTIFICATION_THEME_CHANGED: {
  226. update_minimum_size();
  227. queue_redraw();
  228. } break;
  229. case NOTIFICATION_MOUSE_ENTER: {
  230. mouse_inside = true;
  231. queue_redraw();
  232. } break;
  233. case NOTIFICATION_MOUSE_EXIT: {
  234. mouse_inside = false;
  235. queue_redraw();
  236. } break;
  237. case NOTIFICATION_VISIBILITY_CHANGED:
  238. case NOTIFICATION_EXIT_TREE: {
  239. mouse_inside = false;
  240. grab.active = false;
  241. } break;
  242. case NOTIFICATION_DRAW: {
  243. RID ci = get_canvas_item();
  244. Size2i size = get_size();
  245. double ratio = Math::is_nan(get_as_ratio()) ? 0 : get_as_ratio();
  246. Ref<StyleBox> style = theme_cache.slider_style;
  247. Ref<Texture2D> tick = theme_cache.tick_icon;
  248. bool highlighted = editable && (mouse_inside || has_focus());
  249. Ref<Texture2D> grabber;
  250. if (editable) {
  251. if (highlighted) {
  252. grabber = theme_cache.grabber_hl_icon;
  253. } else {
  254. grabber = theme_cache.grabber_icon;
  255. }
  256. } else {
  257. grabber = theme_cache.grabber_disabled_icon;
  258. }
  259. Ref<StyleBox> grabber_area;
  260. if (highlighted) {
  261. grabber_area = theme_cache.grabber_area_hl_style;
  262. } else {
  263. grabber_area = theme_cache.grabber_area_style;
  264. }
  265. if (orientation == VERTICAL) {
  266. int widget_width = style->get_minimum_size().width;
  267. double areasize = size.height - (theme_cache.center_grabber ? 0 : grabber->get_height());
  268. int grabber_shift = theme_cache.center_grabber ? grabber->get_height() / 2 : 0;
  269. style->draw(ci, Rect2i(Point2i(size.width / 2 - widget_width / 2, 0), Size2i(widget_width, size.height)));
  270. grabber_area->draw(ci, Rect2i(Point2i((size.width - widget_width) / 2, Math::round(size.height - areasize * ratio - grabber->get_height() / 2 + grabber_shift)), Size2i(widget_width, Math::round(areasize * ratio + grabber->get_height() / 2 - grabber_shift))));
  271. if (ticks > 1) {
  272. int grabber_offset = (grabber->get_height() / 2 - tick->get_height() / 2);
  273. for (int i = 0; i < ticks; i++) {
  274. if (!ticks_on_borders && (i == 0 || i + 1 == ticks)) {
  275. continue;
  276. }
  277. int ofs = (i * areasize / (ticks - 1)) + grabber_offset - grabber_shift;
  278. if (ticks_position == TICK_POSITION_BOTTOM_RIGHT || ticks_position == TICK_POSITION_BOTH) {
  279. tick->draw(ci, Point2i(widget_width + (size.width - widget_width) / 2 + theme_cache.tick_offset, ofs));
  280. }
  281. if (ticks_position == TICK_POSITION_TOP_LEFT || ticks_position == TICK_POSITION_BOTH) {
  282. Point2i pos = Point2i((size.width - widget_width) / 2 - tick->get_width() - theme_cache.tick_offset, ofs);
  283. tick->draw_rect(ci, Rect2i(pos, Size2i(-tick->get_width(), tick->get_height())));
  284. }
  285. if (ticks_position == TICK_POSITION_CENTER) {
  286. tick->draw(ci, Point2i((size.width - tick->get_width()) / 2 + theme_cache.tick_offset, ofs));
  287. }
  288. }
  289. }
  290. grabber->draw(ci, Point2i(size.width / 2 - grabber->get_width() / 2 + theme_cache.grabber_offset, size.height - ratio * areasize - grabber->get_height() + grabber_shift));
  291. } else {
  292. int widget_height = style->get_minimum_size().height;
  293. double areasize = size.width - (theme_cache.center_grabber ? 0 : grabber->get_size().width);
  294. int grabber_shift = theme_cache.center_grabber ? -grabber->get_width() / 2 : 0;
  295. bool rtl = is_layout_rtl();
  296. style->draw(ci, Rect2i(Point2i(0, (size.height - widget_height) / 2), Size2i(size.width, widget_height)));
  297. int p = areasize * (rtl ? 1 - ratio : ratio) + grabber->get_width() / 2 + grabber_shift;
  298. if (rtl) {
  299. grabber_area->draw(ci, Rect2i(Point2i(p, (size.height - widget_height) / 2), Size2i(size.width - p, widget_height)));
  300. } else {
  301. grabber_area->draw(ci, Rect2i(Point2i(0, (size.height - widget_height) / 2), Size2i(p, widget_height)));
  302. }
  303. if (ticks > 1) {
  304. int grabber_offset = (grabber->get_width() / 2 - tick->get_width() / 2);
  305. for (int i = 0; i < ticks; i++) {
  306. if ((!ticks_on_borders) && ((i == 0) || ((i + 1) == ticks))) {
  307. continue;
  308. }
  309. int ofs = (i * areasize / (ticks - 1)) + grabber_offset + grabber_shift;
  310. if (ticks_position == TICK_POSITION_BOTTOM_RIGHT || ticks_position == TICK_POSITION_BOTH) {
  311. tick->draw(ci, Point2i(ofs, widget_height + (size.height - widget_height) / 2 + theme_cache.tick_offset));
  312. }
  313. if (ticks_position == TICK_POSITION_TOP_LEFT || ticks_position == TICK_POSITION_BOTH) {
  314. Point2i pos = Point2i(ofs, (size.height - widget_height) / 2 - tick->get_height() - theme_cache.tick_offset);
  315. tick->draw_rect(ci, Rect2i(pos, Size2i(tick->get_width(), -tick->get_height())));
  316. }
  317. if (ticks_position == TICK_POSITION_CENTER) {
  318. tick->draw(ci, Point2i(ofs, (size.height - tick->get_height()) / 2 + theme_cache.tick_offset));
  319. }
  320. }
  321. }
  322. grabber->draw(ci, Point2i((rtl ? 1 - ratio : ratio) * areasize + grabber_shift, size.height / 2 - grabber->get_height() / 2 + theme_cache.grabber_offset));
  323. }
  324. } break;
  325. }
  326. }
  327. void Slider::_validate_property(PropertyInfo &p_property) const {
  328. if (!Engine::get_singleton()->is_editor_hint()) {
  329. return;
  330. }
  331. if (p_property.name == "ticks_position") {
  332. p_property.hint_string = orientation == VERTICAL ? "Right,Left,Both,Center" : "Bottom,Top,Both,Center";
  333. }
  334. }
  335. void Slider::set_custom_step(double p_custom_step) {
  336. custom_step = p_custom_step;
  337. }
  338. double Slider::get_custom_step() const {
  339. return custom_step;
  340. }
  341. void Slider::set_ticks(int p_count) {
  342. if (ticks == p_count) {
  343. return;
  344. }
  345. ticks = p_count;
  346. queue_redraw();
  347. }
  348. int Slider::get_ticks() const {
  349. return ticks;
  350. }
  351. bool Slider::get_ticks_on_borders() const {
  352. return ticks_on_borders;
  353. }
  354. Slider::TickPosition Slider::get_ticks_position() const {
  355. return ticks_position;
  356. }
  357. void Slider::set_ticks_on_borders(bool _tob) {
  358. if (ticks_on_borders == _tob) {
  359. return;
  360. }
  361. ticks_on_borders = _tob;
  362. queue_redraw();
  363. }
  364. void Slider::set_ticks_position(TickPosition p_ticks_position) {
  365. if (ticks_position == p_ticks_position) {
  366. return;
  367. }
  368. ticks_position = p_ticks_position;
  369. queue_redraw();
  370. }
  371. void Slider::set_editable(bool p_editable) {
  372. if (editable == p_editable) {
  373. return;
  374. }
  375. grab.active = false;
  376. editable = p_editable;
  377. queue_redraw();
  378. }
  379. bool Slider::is_editable() const {
  380. return editable;
  381. }
  382. void Slider::set_scrollable(bool p_scrollable) {
  383. scrollable = p_scrollable;
  384. }
  385. bool Slider::is_scrollable() const {
  386. return scrollable;
  387. }
  388. void Slider::_bind_methods() {
  389. ClassDB::bind_method(D_METHOD("set_ticks", "count"), &Slider::set_ticks);
  390. ClassDB::bind_method(D_METHOD("get_ticks"), &Slider::get_ticks);
  391. ClassDB::bind_method(D_METHOD("get_ticks_on_borders"), &Slider::get_ticks_on_borders);
  392. ClassDB::bind_method(D_METHOD("set_ticks_on_borders", "ticks_on_border"), &Slider::set_ticks_on_borders);
  393. ClassDB::bind_method(D_METHOD("get_ticks_position"), &Slider::get_ticks_position);
  394. ClassDB::bind_method(D_METHOD("set_ticks_position", "ticks_on_border"), &Slider::set_ticks_position);
  395. ClassDB::bind_method(D_METHOD("set_editable", "editable"), &Slider::set_editable);
  396. ClassDB::bind_method(D_METHOD("is_editable"), &Slider::is_editable);
  397. ClassDB::bind_method(D_METHOD("set_scrollable", "scrollable"), &Slider::set_scrollable);
  398. ClassDB::bind_method(D_METHOD("is_scrollable"), &Slider::is_scrollable);
  399. ADD_SIGNAL(MethodInfo("drag_started"));
  400. ADD_SIGNAL(MethodInfo("drag_ended", PropertyInfo(Variant::BOOL, "value_changed")));
  401. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
  402. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scrollable"), "set_scrollable", "is_scrollable");
  403. ADD_PROPERTY(PropertyInfo(Variant::INT, "tick_count", PROPERTY_HINT_RANGE, "0,4096,1"), "set_ticks", "get_ticks");
  404. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ticks_on_borders"), "set_ticks_on_borders", "get_ticks_on_borders");
  405. ADD_PROPERTY(PropertyInfo(Variant::INT, "ticks_position", PROPERTY_HINT_ENUM), "set_ticks_position", "get_ticks_position");
  406. BIND_ENUM_CONSTANT(TICK_POSITION_BOTTOM_RIGHT);
  407. BIND_ENUM_CONSTANT(TICK_POSITION_TOP_LEFT);
  408. BIND_ENUM_CONSTANT(TICK_POSITION_BOTH);
  409. BIND_ENUM_CONSTANT(TICK_POSITION_CENTER);
  410. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Slider, slider_style, "slider");
  411. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Slider, grabber_area_style, "grabber_area");
  412. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Slider, grabber_area_hl_style, "grabber_area_highlight");
  413. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, Slider, grabber_icon, "grabber");
  414. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, Slider, grabber_hl_icon, "grabber_highlight");
  415. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, Slider, grabber_disabled_icon, "grabber_disabled");
  416. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, Slider, tick_icon, "tick");
  417. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Slider, center_grabber);
  418. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Slider, grabber_offset);
  419. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Slider, tick_offset);
  420. }
  421. Slider::Slider(Orientation p_orientation) {
  422. orientation = p_orientation;
  423. set_focus_mode(FOCUS_ALL);
  424. }