2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* spin_box.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 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. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "spin_box.h"
|
2019-08-29 21:48:50 +00:00
|
|
|
#include "core/math/expression.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/os/input.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Size2 SpinBox::get_minimum_size() const {
|
|
|
|
|
|
|
|
Size2 ms = line_edit->get_combined_minimum_size();
|
2017-03-05 15:44:50 +00:00
|
|
|
ms.width += last_w;
|
2014-02-10 01:10:30 +00:00
|
|
|
return ms;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpinBox::_value_changed(double) {
|
|
|
|
|
2019-07-23 15:27:55 +00:00
|
|
|
String value = String::num(get_value(), Math::range_step_decimals(get_step()));
|
2017-03-05 15:44:50 +00:00
|
|
|
if (prefix != "")
|
|
|
|
value = prefix + " " + value;
|
|
|
|
if (suffix != "")
|
|
|
|
value += " " + suffix;
|
2014-02-10 01:10:30 +00:00
|
|
|
line_edit->set_text(value);
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void SpinBox::_text_entered(const String &p_string) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-08-29 21:48:50 +00:00
|
|
|
Ref<Expression> expr;
|
|
|
|
expr.instance();
|
2019-08-30 12:28:32 +00:00
|
|
|
// Ignore the prefix and suffix in the expression
|
|
|
|
Error err = expr->parse(p_string.trim_prefix(prefix + " ").trim_suffix(" " + suffix));
|
2019-08-29 21:48:50 +00:00
|
|
|
if (err != OK) {
|
2017-01-14 11:26:56 +00:00
|
|
|
return;
|
2019-08-29 21:48:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Variant value = expr->execute(Array(), NULL, false);
|
|
|
|
if (value.get_type() != Variant::NIL) {
|
|
|
|
set_value(value);
|
|
|
|
}
|
2020-04-26 12:10:11 +00:00
|
|
|
_value_changed(0);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LineEdit *SpinBox::get_line_edit() {
|
|
|
|
|
|
|
|
return line_edit;
|
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
void SpinBox::_line_edit_input(const Ref<InputEvent> &p_event) {
|
2015-08-29 04:43:21 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 18:04:56 +00:00
|
|
|
void SpinBox::_range_click_timeout() {
|
|
|
|
|
|
|
|
if (!drag.enabled && Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT)) {
|
|
|
|
|
2017-09-10 13:37:49 +00:00
|
|
|
bool up = get_local_mouse_position().y < (get_size().height / 2);
|
2017-03-05 15:44:50 +00:00
|
|
|
set_value(get_value() + (up ? get_step() : -get_step()));
|
2015-12-08 18:04:56 +00:00
|
|
|
|
|
|
|
if (range_click_timer->is_one_shot()) {
|
|
|
|
range_click_timer->set_wait_time(0.075);
|
|
|
|
range_click_timer->set_one_shot(false);
|
|
|
|
range_click_timer->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
range_click_timer->stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-06 21:22:24 +00:00
|
|
|
void SpinBox::_release_mouse() {
|
|
|
|
if (drag.enabled) {
|
|
|
|
drag.enabled = false;
|
|
|
|
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
|
|
|
|
warp_mouse(drag.capture_pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
void SpinBox::_gui_input(const Ref<InputEvent> &p_event) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-11-24 18:15:42 +00:00
|
|
|
if (!is_editable()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEventMouseButton> mb = p_event;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
if (mb.is_valid() && mb->is_pressed()) {
|
|
|
|
|
2017-06-03 08:54:24 +00:00
|
|
|
bool up = mb->get_position().y < (get_size().height / 2);
|
2017-05-20 15:38:03 +00:00
|
|
|
|
|
|
|
switch (mb->get_button_index()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
case BUTTON_LEFT: {
|
|
|
|
|
2019-10-21 23:42:17 +00:00
|
|
|
line_edit->grab_focus();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
set_value(get_value() + (up ? get_step() : -get_step()));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-12-08 18:04:56 +00:00
|
|
|
range_click_timer->set_wait_time(0.6);
|
|
|
|
range_click_timer->set_one_shot(true);
|
|
|
|
range_click_timer->start();
|
|
|
|
|
2018-08-27 05:57:30 +00:00
|
|
|
drag.allowed = true;
|
|
|
|
drag.capture_pos = mb->get_position();
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
case BUTTON_RIGHT: {
|
|
|
|
|
2016-11-24 18:15:42 +00:00
|
|
|
line_edit->grab_focus();
|
2019-10-21 23:42:17 +00:00
|
|
|
set_value((up ? get_max() : get_min()));
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
case BUTTON_WHEEL_UP: {
|
2016-11-24 18:15:42 +00:00
|
|
|
if (line_edit->has_focus()) {
|
2017-02-21 22:45:31 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
set_value(get_value() + get_step() * mb->get_factor());
|
2016-11-24 18:15:42 +00:00
|
|
|
accept_event();
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
case BUTTON_WHEEL_DOWN: {
|
2016-11-24 18:15:42 +00:00
|
|
|
if (line_edit->has_focus()) {
|
2017-02-21 22:45:31 +00:00
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
set_value(get_value() - get_step() * mb->get_factor());
|
2016-11-24 18:15:42 +00:00
|
|
|
accept_event();
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
2015-08-29 04:43:21 +00:00
|
|
|
|
2018-08-27 05:57:30 +00:00
|
|
|
if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
|
2015-08-29 04:43:21 +00:00
|
|
|
|
|
|
|
//set_default_cursor_shape(CURSOR_ARROW);
|
2015-12-08 18:04:56 +00:00
|
|
|
range_click_timer->stop();
|
2021-03-06 21:22:24 +00:00
|
|
|
_release_mouse();
|
2018-08-27 05:57:30 +00:00
|
|
|
drag.allowed = false;
|
2015-08-29 04:43:21 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEventMouseMotion> mm = p_event;
|
|
|
|
|
2018-08-27 05:57:30 +00:00
|
|
|
if (mm.is_valid() && mm->get_button_mask() & BUTTON_MASK_LEFT) {
|
2015-08-29 04:43:21 +00:00
|
|
|
|
|
|
|
if (drag.enabled) {
|
|
|
|
|
2018-08-27 05:57:30 +00:00
|
|
|
drag.diff_y += mm->get_relative().y;
|
|
|
|
float diff_y = -0.01 * Math::pow(ABS(drag.diff_y), 1.8f) * SGN(drag.diff_y);
|
|
|
|
set_value(CLAMP(drag.base_val + get_step() * diff_y, get_min(), get_max()));
|
|
|
|
} else if (drag.allowed && drag.capture_pos.distance_to(mm->get_position()) > 2) {
|
2015-08-29 04:43:21 +00:00
|
|
|
|
|
|
|
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
|
2017-03-05 15:44:50 +00:00
|
|
|
drag.enabled = true;
|
|
|
|
drag.base_val = get_value();
|
2018-08-27 05:57:30 +00:00
|
|
|
drag.diff_y = 0;
|
2015-08-29 04:43:21 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpinBox::_line_edit_focus_exit() {
|
|
|
|
|
2019-08-12 10:39:27 +00:00
|
|
|
// discontinue because the focus_exit was caused by right-click context menu
|
|
|
|
if (line_edit->get_menu()->is_visible())
|
|
|
|
return;
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
_text_entered(line_edit->get_text());
|
|
|
|
}
|
|
|
|
|
2019-07-10 09:54:12 +00:00
|
|
|
inline void SpinBox::_adjust_width_for_icon(const Ref<Texture> &icon) {
|
2018-02-05 21:42:30 +00:00
|
|
|
|
|
|
|
int w = icon->get_width();
|
|
|
|
if (w != last_w) {
|
|
|
|
line_edit->set_margin(MARGIN_RIGHT, -w);
|
|
|
|
last_w = w;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void SpinBox::_notification(int p_what) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_what == NOTIFICATION_DRAW) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Ref<Texture> updown = get_icon("updown");
|
|
|
|
|
2018-02-05 21:42:30 +00:00
|
|
|
_adjust_width_for_icon(updown);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
RID ci = get_canvas_item();
|
|
|
|
Size2i size = get_size();
|
|
|
|
|
2017-05-02 21:02:06 +00:00
|
|
|
updown->draw(ci, Point2i(size.width - updown->get_width(), (size.height - updown->get_height()) / 2));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (p_what == NOTIFICATION_FOCUS_EXIT) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//_value_changed(0);
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (p_what == NOTIFICATION_ENTER_TREE) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-02-05 21:42:30 +00:00
|
|
|
_adjust_width_for_icon(get_icon("updown"));
|
2014-02-10 01:10:30 +00:00
|
|
|
_value_changed(0);
|
2021-03-06 21:22:24 +00:00
|
|
|
} else if (p_what == NOTIFICATION_EXIT_TREE) {
|
|
|
|
_release_mouse();
|
2019-12-10 01:11:22 +00:00
|
|
|
} else if (p_what == NOTIFICATION_THEME_CHANGED) {
|
|
|
|
|
|
|
|
call_deferred("minimum_size_changed");
|
|
|
|
get_line_edit()->call_deferred("minimum_size_changed");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-25 03:17:07 +00:00
|
|
|
void SpinBox::set_align(LineEdit::Align p_align) {
|
|
|
|
|
|
|
|
line_edit->set_align(p_align);
|
|
|
|
}
|
|
|
|
|
|
|
|
LineEdit::Align SpinBox::get_align() const {
|
|
|
|
|
|
|
|
return line_edit->get_align();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void SpinBox::set_suffix(const String &p_suffix) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
suffix = p_suffix;
|
2014-02-10 01:10:30 +00:00
|
|
|
_value_changed(0);
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String SpinBox::get_suffix() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return suffix;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void SpinBox::set_prefix(const String &p_prefix) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
prefix = p_prefix;
|
2014-02-10 01:10:30 +00:00
|
|
|
_value_changed(0);
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String SpinBox::get_prefix() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpinBox::set_editable(bool p_editable) {
|
|
|
|
line_edit->set_editable(p_editable);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpinBox::is_editable() const {
|
|
|
|
|
|
|
|
return line_edit->is_editable();
|
|
|
|
}
|
|
|
|
|
2020-01-09 13:44:23 +00:00
|
|
|
void SpinBox::apply() {
|
|
|
|
_text_entered(line_edit->get_text());
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void SpinBox::_bind_methods() {
|
|
|
|
|
2017-02-13 11:47:24 +00:00
|
|
|
//ClassDB::bind_method(D_METHOD("_value_changed"),&SpinBox::_value_changed);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_gui_input"), &SpinBox::_gui_input);
|
|
|
|
ClassDB::bind_method(D_METHOD("_text_entered"), &SpinBox::_text_entered);
|
2018-08-25 03:17:07 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_align", "align"), &SpinBox::set_align);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_align"), &SpinBox::get_align);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_suffix", "suffix"), &SpinBox::set_suffix);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_suffix"), &SpinBox::get_suffix);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_prefix", "prefix"), &SpinBox::set_prefix);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_prefix"), &SpinBox::get_prefix);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_editable", "editable"), &SpinBox::set_editable);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_editable"), &SpinBox::is_editable);
|
2020-01-09 13:44:23 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("apply"), &SpinBox::apply);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_line_edit_focus_exit"), &SpinBox::_line_edit_focus_exit);
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_line_edit"), &SpinBox::get_line_edit);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_line_edit_input"), &SpinBox::_line_edit_input);
|
|
|
|
ClassDB::bind_method(D_METHOD("_range_click_timeout"), &SpinBox::_range_click_timeout);
|
|
|
|
|
2018-11-08 14:30:02 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "align", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_align", "get_align");
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "prefix"), "set_prefix", "get_prefix");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "suffix"), "set_suffix", "get_suffix");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SpinBox::SpinBox() {
|
|
|
|
|
|
|
|
last_w = 0;
|
2017-03-05 15:44:50 +00:00
|
|
|
line_edit = memnew(LineEdit);
|
2014-02-10 01:10:30 +00:00
|
|
|
add_child(line_edit);
|
|
|
|
|
2017-09-21 22:12:33 +00:00
|
|
|
line_edit->set_anchors_and_margins_preset(Control::PRESET_WIDE);
|
2019-05-21 12:49:53 +00:00
|
|
|
line_edit->set_mouse_filter(MOUSE_FILTER_PASS);
|
2014-02-10 01:10:30 +00:00
|
|
|
//connect("value_changed",this,"_value_changed");
|
2017-03-05 15:44:50 +00:00
|
|
|
line_edit->connect("text_entered", this, "_text_entered", Vector<Variant>(), CONNECT_DEFERRED);
|
|
|
|
line_edit->connect("focus_exited", this, "_line_edit_focus_exit", Vector<Variant>(), CONNECT_DEFERRED);
|
|
|
|
line_edit->connect("gui_input", this, "_line_edit_input");
|
2015-12-08 18:04:56 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
range_click_timer = memnew(Timer);
|
|
|
|
range_click_timer->connect("timeout", this, "_range_click_timeout");
|
2015-12-08 18:04:56 +00:00
|
|
|
add_child(range_click_timer);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|