2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* slider.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:11:45 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2018-01-02 10:27:24 +00:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00: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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "slider.h"
|
|
|
|
#include "os/keyboard.h"
|
|
|
|
|
|
|
|
Size2 Slider::get_minimum_size() const {
|
|
|
|
|
|
|
|
Ref<StyleBox> style = get_stylebox("slider");
|
2017-03-18 23:36:26 +00:00
|
|
|
Size2i ms = style->get_minimum_size() + style->get_center_size();
|
2014-02-10 01:10:30 +00:00
|
|
|
return ms;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::_input_event(InputEvent p_event) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_event.type == InputEvent::MOUSE_BUTTON) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
InputEventMouseButton &mb = p_event.mouse_button;
|
2017-03-18 23:36:26 +00:00
|
|
|
if (mb.button_index == BUTTON_LEFT) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (mb.pressed) {
|
|
|
|
Ref<Texture> grabber = get_icon(mouse_inside || has_focus() ? "grabber_hilite" : "grabber");
|
|
|
|
grab.pos = orientation == VERTICAL ? mb.y : mb.x;
|
2016-08-24 07:17:54 +00:00
|
|
|
double grab_width = (double)grabber->get_size().width;
|
|
|
|
double grab_height = (double)grabber->get_size().height;
|
2017-03-18 23:36:26 +00:00
|
|
|
double max = orientation == VERTICAL ? get_size().height - grab_height : get_size().width - grab_width;
|
|
|
|
if (orientation == VERTICAL)
|
|
|
|
set_unit_value(1 - (((double)grab.pos - (grab_height / 2.0)) / max));
|
2014-09-18 22:52:05 +00:00
|
|
|
else
|
2017-03-18 23:36:26 +00:00
|
|
|
set_unit_value(((double)grab.pos - (grab_width / 2.0)) / max);
|
|
|
|
grab.active = true;
|
|
|
|
grab.uvalue = get_unit_value();
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
grab.active = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (mb.pressed && mb.button_index == BUTTON_WHEEL_UP) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
set_val(get_val() + get_step());
|
|
|
|
} else if (mb.pressed && mb.button_index == BUTTON_WHEEL_DOWN) {
|
|
|
|
set_val(get_val() - get_step());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (p_event.type == InputEvent::MOUSE_MOTION) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (grab.active) {
|
|
|
|
|
|
|
|
Size2i size = get_size();
|
|
|
|
Ref<Texture> grabber = get_icon("grabber");
|
2017-03-18 23:36:26 +00:00
|
|
|
float motion = (orientation == VERTICAL ? p_event.mouse_motion.y : p_event.mouse_motion.x) - grab.pos;
|
|
|
|
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 01:10:30 +00:00
|
|
|
return;
|
|
|
|
float umotion = motion / float(areasize);
|
2017-03-18 23:36:26 +00:00
|
|
|
set_unit_value(grab.uvalue + umotion);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (p_event.is_action("ui_left") && p_event.is_pressed()) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (orientation != HORIZONTAL)
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2017-03-18 23:36:26 +00:00
|
|
|
set_val(get_val() - (custom_step >= 0 ? custom_step : get_step()));
|
2014-02-10 01:10:30 +00:00
|
|
|
accept_event();
|
|
|
|
} else if (p_event.is_action("ui_right") && p_event.is_pressed()) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (orientation != HORIZONTAL)
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2017-03-18 23:36:26 +00:00
|
|
|
set_val(get_val() + (custom_step >= 0 ? custom_step : get_step()));
|
2014-02-10 01:10:30 +00:00
|
|
|
accept_event();
|
|
|
|
} else if (p_event.is_action("ui_up") && p_event.is_pressed()) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (orientation != VERTICAL)
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
set_val(get_val() + (custom_step >= 0 ? custom_step : get_step()));
|
2014-02-10 01:10:30 +00:00
|
|
|
accept_event();
|
|
|
|
} else if (p_event.is_action("ui_down") && p_event.is_pressed()) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (orientation != VERTICAL)
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2017-03-18 23:36:26 +00:00
|
|
|
set_val(get_val() - (custom_step >= 0 ? custom_step : get_step()));
|
2014-02-10 01:10:30 +00:00
|
|
|
accept_event();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (p_event.type == InputEvent::KEY) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const InputEventKey &k = p_event.key;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (!k.pressed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (k.scancode) {
|
|
|
|
|
|
|
|
case KEY_HOME: {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
set_val(get_min());
|
2014-02-10 01:10:30 +00:00
|
|
|
accept_event();
|
|
|
|
} break;
|
|
|
|
case KEY_END: {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
set_val(get_max());
|
2014-02-10 01:10:30 +00:00
|
|
|
accept_event();
|
|
|
|
|
|
|
|
} break;
|
2017-03-18 23:36:26 +00:00
|
|
|
};
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::_notification(int p_what) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
switch (p_what) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
case NOTIFICATION_MOUSE_ENTER: {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
mouse_inside = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
|
|
|
} break;
|
|
|
|
case NOTIFICATION_MOUSE_EXIT: {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
mouse_inside = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
|
|
|
} break;
|
|
|
|
case NOTIFICATION_DRAW: {
|
|
|
|
RID ci = get_canvas_item();
|
|
|
|
Size2i size = get_size();
|
|
|
|
Ref<StyleBox> style = get_stylebox("slider");
|
|
|
|
Ref<StyleBox> focus = get_stylebox("focus");
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<Texture> grabber = get_icon(mouse_inside || has_focus() ? "grabber_hilite" : "grabber");
|
2014-02-10 01:10:30 +00:00
|
|
|
Ref<Texture> tick = get_icon("tick");
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (orientation == VERTICAL) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
style->draw(ci, Rect2i(Point2i(), Size2i(style->get_minimum_size().width + style->get_center_size().width, size.height)));
|
2014-02-10 01:10:30 +00:00
|
|
|
//if (mouse_inside||has_focus())
|
|
|
|
// focus->draw(ci,Rect2i(Point2i(),Size2i(style->get_minimum_size().width+style->get_center_size().width,size.height)));
|
|
|
|
float areasize = size.height - grabber->get_size().height;
|
2017-03-18 23:36:26 +00:00
|
|
|
if (ticks > 1) {
|
2014-02-10 01:10:30 +00:00
|
|
|
int tickarea = size.height - tick->get_height();
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < ticks; i++) {
|
|
|
|
if (!ticks_on_borders && (i == 0 || i + 1 == ticks)) continue;
|
|
|
|
int ofs = i * tickarea / (ticks - 1);
|
|
|
|
tick->draw(ci, Point2(0, ofs));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
grabber->draw(ci, Point2i(size.width / 2 - grabber->get_size().width / 2, size.height - get_unit_value() * areasize - grabber->get_size().height));
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
style->draw(ci, Rect2i(Point2i(), Size2i(size.width, style->get_minimum_size().height + style->get_center_size().height)));
|
2014-02-10 01:10:30 +00:00
|
|
|
//if (mouse_inside||has_focus())
|
|
|
|
// focus->draw(ci,Rect2i(Point2i(),Size2i(size.width,style->get_minimum_size().height+style->get_center_size().height)));
|
|
|
|
|
|
|
|
float areasize = size.width - grabber->get_size().width;
|
2017-03-18 23:36:26 +00:00
|
|
|
if (ticks > 1) {
|
2014-02-10 01:10:30 +00:00
|
|
|
int tickarea = size.width - tick->get_width();
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < ticks; i++) {
|
|
|
|
if ((!ticks_on_borders) && ((i == 0) || ((i + 1) == ticks))) continue;
|
|
|
|
int ofs = i * tickarea / (ticks - 1);
|
|
|
|
tick->draw(ci, Point2(ofs, 0));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
grabber->draw(ci, Point2i(get_unit_value() * areasize, size.height / 2 - grabber->get_size().height / 2));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::set_custom_step(float p_custom_step) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
custom_step = p_custom_step;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float Slider::get_custom_step() const {
|
|
|
|
|
|
|
|
return custom_step;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::set_ticks(int p_count) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ticks = p_count;
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Slider::get_ticks() const {
|
|
|
|
|
|
|
|
return ticks;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool Slider::get_ticks_on_borders() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return ticks_on_borders;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void Slider::set_ticks_on_borders(bool _tob) {
|
2014-02-10 01:10:30 +00:00
|
|
|
ticks_on_borders = _tob;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::_bind_methods() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("_input_event"), &Slider::_input_event);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_ticks", "count"), &Slider::set_ticks);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_ticks"), &Slider::get_ticks);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("get_ticks_on_borders"), &Slider::get_ticks_on_borders);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_ticks_on_borders", "ticks_on_border"), &Slider::set_ticks_on_borders);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "tick_count", PROPERTY_HINT_RANGE, "0,4096,1"), _SCS("set_ticks"), _SCS("get_ticks"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ticks_on_borders"), _SCS("set_ticks_on_borders"), _SCS("get_ticks_on_borders"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "focus_mode", PROPERTY_HINT_ENUM, "None,Click,All"), _SCS("set_focus_mode"), _SCS("get_focus_mode"));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Slider::Slider(Orientation p_orientation) {
|
2017-03-18 23:36:26 +00:00
|
|
|
orientation = p_orientation;
|
|
|
|
mouse_inside = false;
|
|
|
|
grab.active = false;
|
|
|
|
ticks = 0;
|
|
|
|
custom_step = -1;
|
2014-02-10 01:10:30 +00:00
|
|
|
set_focus_mode(FOCUS_ALL);
|
|
|
|
}
|