Debian Codegraph @

scene/gui/slider.cpp

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