2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* slider.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2021-01-01 20:13:46 +01:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "slider.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/os/keyboard.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Size2 Slider::get_minimum_size() const {
|
|
|
|
|
|
|
|
Ref<StyleBox> style = get_stylebox("slider");
|
2019-04-09 21:58:19 +02:00
|
|
|
Size2i ss = style->get_minimum_size() + style->get_center_size();
|
|
|
|
|
|
|
|
Ref<Texture> grabber = get_icon("grabber");
|
|
|
|
Size2i rs = grabber->get_size();
|
|
|
|
|
|
|
|
if (orientation == HORIZONTAL)
|
|
|
|
return Size2i(ss.width, MAX(ss.height, rs.height));
|
|
|
|
else
|
|
|
|
return Size2i(MAX(ss.width, rs.width), ss.height);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
void Slider::_gui_input(Ref<InputEvent> p_event) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-07-10 12:58:53 +02:00
|
|
|
if (!editable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
Ref<InputEventMouseButton> mb = p_event;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
if (mb.is_valid()) {
|
|
|
|
if (mb->get_button_index() == BUTTON_LEFT) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
if (mb->is_pressed()) {
|
2017-05-02 22:13:12 +02:00
|
|
|
Ref<Texture> grabber = get_icon(mouse_inside || has_focus() ? "grabber_highlight" : "grabber");
|
2017-06-03 10:54:24 +02:00
|
|
|
grab.pos = orientation == VERTICAL ? mb->get_position().y : mb->get_position().x;
|
2017-05-20 17:38:03 +02:00
|
|
|
|
2016-08-24 09:17:54 +02:00
|
|
|
double grab_width = (double)grabber->get_size().width;
|
|
|
|
double grab_height = (double)grabber->get_size().height;
|
2017-03-05 16:44:50 +01:00
|
|
|
double max = orientation == VERTICAL ? get_size().height - grab_height : get_size().width - grab_width;
|
|
|
|
if (orientation == VERTICAL)
|
|
|
|
set_as_ratio(1 - (((double)grab.pos - (grab_height / 2.0)) / max));
|
2014-09-19 00:52:05 +02:00
|
|
|
else
|
2017-03-05 16:44:50 +01:00
|
|
|
set_as_ratio(((double)grab.pos - (grab_width / 2.0)) / max);
|
|
|
|
grab.active = true;
|
|
|
|
grab.uvalue = get_as_ratio();
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
grab.active = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2018-07-07 11:35:59 +02:00
|
|
|
} else if (scrollable) {
|
|
|
|
if (mb->is_pressed() && mb->get_button_index() == BUTTON_WHEEL_UP) {
|
2020-05-21 13:26:46 +02:00
|
|
|
grab_focus();
|
2018-07-07 11:35:59 +02:00
|
|
|
set_value(get_value() + get_step());
|
|
|
|
} else if (mb->is_pressed() && mb->get_button_index() == BUTTON_WHEEL_DOWN) {
|
2020-05-21 13:26:46 +02:00
|
|
|
grab_focus();
|
2018-07-07 11:35:59 +02:00
|
|
|
set_value(get_value() - get_step());
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-05-20 17:38:03 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
Ref<InputEventMouseMotion> mm = p_event;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
if (mm.is_valid()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
if (grab.active) {
|
|
|
|
|
|
|
|
Size2i size = get_size();
|
|
|
|
Ref<Texture> grabber = get_icon("grabber");
|
2017-06-03 10:54:24 +02:00
|
|
|
float motion = (orientation == VERTICAL ? mm->get_position().y : mm->get_position().x) - grab.pos;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (orientation == VERTICAL)
|
|
|
|
motion = -motion;
|
|
|
|
float areasize = orientation == VERTICAL ? size.height - grabber->get_size().height : size.width - grabber->get_size().width;
|
|
|
|
if (areasize <= 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
|
|
|
float umotion = motion / float(areasize);
|
2017-03-05 16:44:50 +01:00
|
|
|
set_as_ratio(grab.uvalue + umotion);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-05-20 17:38:03 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-05-20 17:38:03 +02:00
|
|
|
if (!mm.is_valid() && !mb.is_valid()) {
|
|
|
|
|
2019-10-24 23:51:33 +02:00
|
|
|
if (p_event->is_action_pressed("ui_left", true)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (orientation != HORIZONTAL)
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2017-03-05 16:44:50 +01:00
|
|
|
set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
|
2014-02-10 02:10:30 +01:00
|
|
|
accept_event();
|
2019-10-24 23:51:33 +02:00
|
|
|
} else if (p_event->is_action_pressed("ui_right", true)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (orientation != HORIZONTAL)
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2017-03-05 16:44:50 +01:00
|
|
|
set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
|
2014-02-10 02:10:30 +01:00
|
|
|
accept_event();
|
2019-10-24 23:51:33 +02:00
|
|
|
} else if (p_event->is_action_pressed("ui_up", true)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (orientation != VERTICAL)
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
|
2014-02-10 02:10:30 +01:00
|
|
|
accept_event();
|
2019-10-24 23:51:33 +02:00
|
|
|
} else if (p_event->is_action_pressed("ui_down", true)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (orientation != VERTICAL)
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2017-03-05 16:44:50 +01:00
|
|
|
set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
|
2014-02-10 02:10:30 +01:00
|
|
|
accept_event();
|
2018-02-23 12:17:15 +01:00
|
|
|
} else if (p_event->is_action("ui_home") && p_event->is_pressed()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-02-23 12:17:15 +01:00
|
|
|
set_value(get_min());
|
|
|
|
accept_event();
|
|
|
|
} else if (p_event->is_action("ui_end") && p_event->is_pressed()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-02-23 12:17:15 +01:00
|
|
|
set_value(get_max());
|
|
|
|
accept_event();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::_notification(int p_what) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_what) {
|
2019-04-09 21:58:19 +02:00
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-04-09 21:58:19 +02:00
|
|
|
minimum_size_changed();
|
|
|
|
update();
|
|
|
|
} break;
|
2014-02-10 02:10:30 +01:00
|
|
|
case NOTIFICATION_MOUSE_ENTER: {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
mouse_inside = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
update();
|
|
|
|
} break;
|
|
|
|
case NOTIFICATION_MOUSE_EXIT: {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
mouse_inside = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
update();
|
|
|
|
} break;
|
2017-10-16 20:53:02 +02:00
|
|
|
case NOTIFICATION_VISIBILITY_CHANGED: // fallthrough
|
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
|
|
|
|
|
|
|
mouse_inside = false;
|
|
|
|
grab.active = false;
|
|
|
|
} break;
|
2014-02-10 02:10:30 +01:00
|
|
|
case NOTIFICATION_DRAW: {
|
|
|
|
RID ci = get_canvas_item();
|
|
|
|
Size2i size = get_size();
|
|
|
|
Ref<StyleBox> style = get_stylebox("slider");
|
2020-04-02 13:15:40 +02:00
|
|
|
bool highlighted = mouse_inside || has_focus();
|
|
|
|
Ref<StyleBox> grabber_area = get_stylebox(highlighted ? "grabber_area_highlight" : "grabber_area");
|
|
|
|
Ref<Texture> grabber = get_icon(editable ? (highlighted ? "grabber_highlight" : "grabber") : "grabber_disabled");
|
2014-02-10 02:10:30 +01:00
|
|
|
Ref<Texture> tick = get_icon("tick");
|
2017-12-09 22:03:57 +01:00
|
|
|
double ratio = Math::is_nan(get_as_ratio()) ? 0 : get_as_ratio();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (orientation == VERTICAL) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-07-18 08:32:25 +02:00
|
|
|
int widget_width = style->get_minimum_size().width + style->get_center_size().width;
|
2017-09-12 17:12:47 +02:00
|
|
|
float areasize = size.height - grabber->get_size().height;
|
2017-07-18 08:32:25 +02:00
|
|
|
style->draw(ci, Rect2i(Point2i(size.width / 2 - widget_width / 2, 0), Size2i(widget_width, size.height)));
|
2017-12-09 22:03:57 +01:00
|
|
|
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().width / 2)));
|
2019-12-14 01:17:51 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (ticks > 1) {
|
2017-12-15 12:52:28 +01:00
|
|
|
int grabber_offset = (grabber->get_size().height / 2 - tick->get_height() / 2);
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < ticks; i++) {
|
2021-05-04 14:28:27 +02:00
|
|
|
if (!ticks_on_borders && (i == 0 || i + 1 == ticks))
|
|
|
|
continue;
|
2017-12-15 12:52:28 +01:00
|
|
|
int ofs = (i * areasize / (ticks - 1)) + grabber_offset;
|
2017-09-27 21:44:48 +02:00
|
|
|
tick->draw(ci, Point2i((size.width - widget_width) / 2, ofs));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
2017-12-09 22:03:57 +01:00
|
|
|
grabber->draw(ci, Point2i(size.width / 2 - grabber->get_size().width / 2, size.height - ratio * areasize - grabber->get_size().height));
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-07-18 08:32:25 +02:00
|
|
|
|
|
|
|
int widget_height = style->get_minimum_size().height + style->get_center_size().height;
|
2017-09-12 17:12:47 +02:00
|
|
|
float areasize = size.width - grabber->get_size().width;
|
|
|
|
|
|
|
|
style->draw(ci, Rect2i(Point2i(0, (size.height - widget_height) / 2), Size2i(size.width, widget_height)));
|
2017-12-09 22:03:57 +01:00
|
|
|
grabber_area->draw(ci, Rect2i(Point2i(0, (size.height - widget_height) / 2), Size2i(areasize * ratio + grabber->get_size().width / 2, widget_height)));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (ticks > 1) {
|
2017-12-15 12:52:28 +01:00
|
|
|
int grabber_offset = (grabber->get_size().width / 2 - tick->get_width() / 2);
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < ticks; i++) {
|
2021-05-04 14:28:27 +02:00
|
|
|
if ((!ticks_on_borders) && ((i == 0) || ((i + 1) == ticks)))
|
|
|
|
continue;
|
2017-12-15 12:52:28 +01:00
|
|
|
int ofs = (i * areasize / (ticks - 1)) + grabber_offset;
|
2017-09-27 21:44:48 +02:00
|
|
|
tick->draw(ci, Point2i(ofs, (size.height - widget_height) / 2));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
2017-12-09 22:03:57 +01:00
|
|
|
grabber->draw(ci, Point2i(ratio * areasize, size.height / 2 - grabber->get_size().height / 2));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::set_custom_step(float p_custom_step) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
custom_step = p_custom_step;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
float Slider::get_custom_step() const {
|
|
|
|
|
|
|
|
return custom_step;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::set_ticks(int p_count) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ticks = p_count;
|
2014-02-10 02:10:30 +01:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Slider::get_ticks() const {
|
|
|
|
|
|
|
|
return ticks;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool Slider::get_ticks_on_borders() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return ticks_on_borders;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void Slider::set_ticks_on_borders(bool _tob) {
|
2014-02-10 02:10:30 +01:00
|
|
|
ticks_on_borders = _tob;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2017-07-10 12:58:53 +02:00
|
|
|
void Slider::set_editable(bool p_editable) {
|
|
|
|
|
|
|
|
editable = p_editable;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Slider::is_editable() const {
|
|
|
|
|
|
|
|
return editable;
|
|
|
|
}
|
|
|
|
|
2018-07-07 11:35:59 +02:00
|
|
|
void Slider::set_scrollable(bool p_scrollable) {
|
|
|
|
|
|
|
|
scrollable = p_scrollable;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Slider::is_scrollable() const {
|
|
|
|
|
|
|
|
return scrollable;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void Slider::_bind_methods() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_gui_input"), &Slider::_gui_input);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_ticks", "count"), &Slider::set_ticks);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_ticks"), &Slider::get_ticks);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_ticks_on_borders"), &Slider::get_ticks_on_borders);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_ticks_on_borders", "ticks_on_border"), &Slider::set_ticks_on_borders);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-07-10 12:58:53 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_editable", "editable"), &Slider::set_editable);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_editable"), &Slider::is_editable);
|
2018-07-07 11:35:59 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_scrollable", "scrollable"), &Slider::set_scrollable);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_scrollable"), &Slider::is_scrollable);
|
2017-07-10 12:58:53 +02:00
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
|
2018-07-07 11:35:59 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scrollable"), "set_scrollable", "is_scrollable");
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "tick_count", PROPERTY_HINT_RANGE, "0,4096,1"), "set_ticks", "get_ticks");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ticks_on_borders"), "set_ticks_on_borders", "get_ticks_on_borders");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Slider::Slider(Orientation p_orientation) {
|
2017-03-05 16:44:50 +01:00
|
|
|
orientation = p_orientation;
|
|
|
|
mouse_inside = false;
|
|
|
|
grab.active = false;
|
|
|
|
ticks = 0;
|
2019-05-28 19:12:19 +02:00
|
|
|
ticks_on_borders = false;
|
2017-03-05 16:44:50 +01:00
|
|
|
custom_step = -1;
|
2017-07-10 12:58:53 +02:00
|
|
|
editable = true;
|
2018-07-07 11:35:59 +02:00
|
|
|
scrollable = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
set_focus_mode(FOCUS_ALL);
|
|
|
|
}
|