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