2018-05-16 17:19:33 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* editor_spin_slider.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
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). */
|
2018-05-16 17:19:33 +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-05-15 20:12:35 +00:00
|
|
|
#include "editor_spin_slider.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/math/expression.h"
|
|
|
|
#include "core/os/input.h"
|
2020-02-15 01:18:24 +00:00
|
|
|
#include "editor_node.h"
|
2018-05-15 20:12:35 +00:00
|
|
|
#include "editor_scale.h"
|
2018-08-07 15:19:19 +00:00
|
|
|
|
|
|
|
String EditorSpinSlider::get_tooltip(const Point2 &p_pos) const {
|
2020-05-06 00:38:15 +00:00
|
|
|
if (grabber->is_visible()) {
|
|
|
|
return rtos(get_value()) + "\n\n" + TTR("Hold Ctrl to round to integers. Hold Shift for more precise changes.");
|
|
|
|
}
|
2018-08-07 15:19:19 +00:00
|
|
|
return rtos(get_value());
|
|
|
|
}
|
|
|
|
|
2018-05-15 20:12:35 +00:00
|
|
|
String EditorSpinSlider::get_text_value() const {
|
2019-07-23 15:27:55 +00:00
|
|
|
return String::num(get_value(), Math::range_step_decimals(get_step()));
|
2018-05-15 20:12:35 +00:00
|
|
|
}
|
2019-07-23 15:27:55 +00:00
|
|
|
|
2018-05-15 20:12:35 +00:00
|
|
|
void EditorSpinSlider::_gui_input(const Ref<InputEvent> &p_event) {
|
|
|
|
|
2018-06-07 15:46:14 +00:00
|
|
|
if (read_only)
|
|
|
|
return;
|
|
|
|
|
2018-05-15 20:12:35 +00:00
|
|
|
Ref<InputEventMouseButton> mb = p_event;
|
2019-08-17 19:12:05 +00:00
|
|
|
if (mb.is_valid()) {
|
|
|
|
|
|
|
|
if (mb->get_button_index() == BUTTON_LEFT) {
|
|
|
|
if (mb->is_pressed()) {
|
|
|
|
|
|
|
|
if (updown_offset != -1 && mb->get_position().x > updown_offset) {
|
|
|
|
//there is an updown, so use it.
|
|
|
|
if (mb->get_position().y < get_size().height / 2) {
|
|
|
|
set_value(get_value() + get_step());
|
|
|
|
} else {
|
|
|
|
set_value(get_value() - get_step());
|
|
|
|
}
|
|
|
|
return;
|
2018-05-15 20:12:35 +00:00
|
|
|
} else {
|
2019-08-17 19:12:05 +00:00
|
|
|
|
|
|
|
grabbing_spinner_attempt = true;
|
|
|
|
grabbing_spinner_dist_cache = 0;
|
|
|
|
pre_grab_value = get_value();
|
|
|
|
grabbing_spinner = false;
|
|
|
|
grabbing_spinner_mouse_pos = Input::get_singleton()->get_mouse_position();
|
2018-05-15 20:12:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
2019-08-17 19:12:05 +00:00
|
|
|
if (grabbing_spinner_attempt) {
|
2018-05-15 20:12:35 +00:00
|
|
|
|
2019-08-17 19:12:05 +00:00
|
|
|
if (grabbing_spinner) {
|
2018-05-15 20:12:35 +00:00
|
|
|
|
2019-08-17 19:12:05 +00:00
|
|
|
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
|
|
|
|
Input::get_singleton()->warp_mouse_position(grabbing_spinner_mouse_pos);
|
|
|
|
update();
|
|
|
|
} else {
|
|
|
|
_focus_entered();
|
|
|
|
}
|
2018-05-15 20:12:35 +00:00
|
|
|
|
2019-08-17 19:12:05 +00:00
|
|
|
grabbing_spinner = false;
|
|
|
|
grabbing_spinner_attempt = false;
|
2018-05-15 20:12:35 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-17 19:12:05 +00:00
|
|
|
} else if (mb->get_button_index() == BUTTON_WHEEL_UP || mb->get_button_index() == BUTTON_WHEEL_DOWN) {
|
|
|
|
|
|
|
|
if (grabber->is_visible())
|
|
|
|
call_deferred("update");
|
2018-05-15 20:12:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<InputEventMouseMotion> mm = p_event;
|
|
|
|
if (mm.is_valid()) {
|
|
|
|
|
|
|
|
if (grabbing_spinner_attempt) {
|
|
|
|
|
2018-05-21 13:02:20 +00:00
|
|
|
double diff_x = mm->get_relative().x;
|
|
|
|
if (mm->get_shift() && grabbing_spinner) {
|
|
|
|
diff_x *= 0.1;
|
|
|
|
}
|
|
|
|
grabbing_spinner_dist_cache += diff_x;
|
|
|
|
|
2018-07-18 18:35:01 +00:00
|
|
|
if (!grabbing_spinner && ABS(grabbing_spinner_dist_cache) > 4 * EDSCALE) {
|
2018-05-15 20:12:35 +00:00
|
|
|
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
|
|
|
|
grabbing_spinner = true;
|
2018-07-18 18:35:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (grabbing_spinner) {
|
2020-05-06 00:38:15 +00:00
|
|
|
// Don't make the user scroll all the way back to 'in range' if they went off the end.
|
|
|
|
if (pre_grab_value < get_min() && !is_lesser_allowed()) {
|
|
|
|
pre_grab_value = get_min();
|
|
|
|
}
|
|
|
|
if (pre_grab_value > get_max() && !is_greater_allowed()) {
|
|
|
|
pre_grab_value = get_max();
|
|
|
|
}
|
|
|
|
|
2019-09-06 17:38:11 +00:00
|
|
|
if (mm->get_control()) {
|
2020-05-06 00:38:15 +00:00
|
|
|
// If control was just pressed, don't make the value do a huge jump in magnitude.
|
|
|
|
if (grabbing_spinner_dist_cache != 0) {
|
|
|
|
pre_grab_value += grabbing_spinner_dist_cache * get_step();
|
|
|
|
grabbing_spinner_dist_cache = 0;
|
|
|
|
}
|
|
|
|
|
2019-09-06 17:38:11 +00:00
|
|
|
set_value(Math::round(pre_grab_value + get_step() * grabbing_spinner_dist_cache * 10));
|
2018-05-21 13:02:20 +00:00
|
|
|
} else {
|
2019-11-05 14:53:19 +00:00
|
|
|
set_value(pre_grab_value + get_step() * grabbing_spinner_dist_cache);
|
2018-05-21 13:02:20 +00:00
|
|
|
}
|
2018-05-15 20:12:35 +00:00
|
|
|
}
|
|
|
|
} else if (updown_offset != -1) {
|
|
|
|
bool new_hover = (mm->get_position().x > updown_offset);
|
|
|
|
if (new_hover != hover_updown) {
|
|
|
|
hover_updown = new_hover;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<InputEventKey> k = p_event;
|
|
|
|
if (k.is_valid() && k->is_pressed() && k->is_action("ui_accept")) {
|
2018-05-21 19:36:43 +00:00
|
|
|
_focus_entered();
|
2018-05-15 20:12:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorSpinSlider::_grabber_gui_input(const Ref<InputEvent> &p_event) {
|
|
|
|
|
|
|
|
Ref<InputEventMouseButton> mb = p_event;
|
2019-08-13 10:11:24 +00:00
|
|
|
|
|
|
|
if (grabbing_grabber) {
|
|
|
|
if (mb.is_valid()) {
|
|
|
|
|
|
|
|
if (mb->get_button_index() == BUTTON_WHEEL_UP) {
|
|
|
|
set_value(get_value() + get_step());
|
|
|
|
mousewheel_over_grabber = true;
|
|
|
|
} else if (mb->get_button_index() == BUTTON_WHEEL_DOWN) {
|
|
|
|
set_value(get_value() - get_step());
|
|
|
|
mousewheel_over_grabber = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 20:12:35 +00:00
|
|
|
if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT) {
|
|
|
|
|
|
|
|
if (mb->is_pressed()) {
|
|
|
|
|
|
|
|
grabbing_grabber = true;
|
2019-08-13 10:11:24 +00:00
|
|
|
if (!mousewheel_over_grabber) {
|
|
|
|
grabbing_ratio = get_as_ratio();
|
|
|
|
grabbing_from = grabber->get_transform().xform(mb->get_position()).x;
|
|
|
|
}
|
2018-05-15 20:12:35 +00:00
|
|
|
} else {
|
|
|
|
grabbing_grabber = false;
|
2019-08-13 10:11:24 +00:00
|
|
|
mousewheel_over_grabber = false;
|
2018-05-15 20:12:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<InputEventMouseMotion> mm = p_event;
|
|
|
|
if (mm.is_valid() && grabbing_grabber) {
|
2019-08-13 10:11:24 +00:00
|
|
|
if (mousewheel_over_grabber)
|
|
|
|
return;
|
2018-05-15 20:12:35 +00:00
|
|
|
|
|
|
|
float grabbing_ofs = (grabber->get_transform().xform(mm->get_position()).x - grabbing_from) / float(grabber_range);
|
|
|
|
set_as_ratio(grabbing_ratio + grabbing_ofs);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorSpinSlider::_notification(int p_what) {
|
|
|
|
|
2018-12-16 22:20:03 +00:00
|
|
|
if (p_what == MainLoop::NOTIFICATION_WM_FOCUS_OUT ||
|
|
|
|
p_what == MainLoop::NOTIFICATION_WM_FOCUS_IN ||
|
|
|
|
p_what == NOTIFICATION_EXIT_TREE) {
|
2018-05-15 20:12:35 +00:00
|
|
|
if (grabbing_spinner) {
|
2020-05-15 13:37:13 +00:00
|
|
|
grabber->hide();
|
2018-05-15 20:12:35 +00:00
|
|
|
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
|
|
|
|
grabbing_spinner = false;
|
|
|
|
grabbing_spinner_attempt = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 01:18:24 +00:00
|
|
|
if (p_what == NOTIFICATION_READY) {
|
|
|
|
// Add a left margin to the stylebox to make the number align with the Label
|
|
|
|
// when it's edited. The LineEdit "focus" stylebox uses the "normal" stylebox's
|
|
|
|
// default margins.
|
|
|
|
Ref<StyleBoxFlat> stylebox =
|
|
|
|
EditorNode::get_singleton()->get_theme_base()->get_stylebox("normal", "LineEdit")->duplicate();
|
|
|
|
// EditorSpinSliders with a label have more space on the left, so add an
|
|
|
|
// higher margin to match the location where the text begins.
|
|
|
|
// The margin values below were determined by empirical testing.
|
|
|
|
stylebox->set_default_margin(MARGIN_LEFT, (get_label() != String() ? 23 : 16) * EDSCALE);
|
|
|
|
value_input->add_style_override("normal", stylebox);
|
|
|
|
}
|
|
|
|
|
2018-05-15 20:12:35 +00:00
|
|
|
if (p_what == NOTIFICATION_DRAW) {
|
|
|
|
|
|
|
|
updown_offset = -1;
|
|
|
|
|
|
|
|
Ref<StyleBox> sb = get_stylebox("normal", "LineEdit");
|
2018-07-18 22:37:17 +00:00
|
|
|
if (!flat) {
|
|
|
|
draw_style_box(sb, Rect2(Vector2(), get_size()));
|
|
|
|
}
|
2018-05-15 20:12:35 +00:00
|
|
|
Ref<Font> font = get_font("font", "LineEdit");
|
2018-07-19 01:27:39 +00:00
|
|
|
int sep_base = 4 * EDSCALE;
|
|
|
|
int sep = sep_base + sb->get_offset().x; //make it have the same margin on both sides, looks better
|
2018-07-18 22:37:17 +00:00
|
|
|
|
|
|
|
int string_width = font->get_string_size(label).width;
|
|
|
|
int number_width = get_size().width - sb->get_minimum_size().width - string_width - sep;
|
2018-05-15 20:12:35 +00:00
|
|
|
|
|
|
|
Ref<Texture> updown = get_icon("updown", "SpinBox");
|
|
|
|
|
|
|
|
if (get_step() == 1) {
|
2018-07-18 22:37:17 +00:00
|
|
|
number_width -= updown->get_width();
|
2018-05-15 20:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String numstr = get_text_value();
|
|
|
|
|
|
|
|
int vofs = (get_size().height - font->get_height()) / 2 + font->get_ascent();
|
|
|
|
|
|
|
|
Color fc = get_color("font_color", "LineEdit");
|
2018-07-19 01:27:39 +00:00
|
|
|
Color lc;
|
|
|
|
if (use_custom_label_color) {
|
|
|
|
lc = custom_label_color;
|
|
|
|
} else {
|
|
|
|
lc = fc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flat && label != String()) {
|
|
|
|
Color label_bg_color = get_color("dark_color_3", "Editor");
|
|
|
|
draw_rect(Rect2(Vector2(), Vector2(sb->get_offset().x * 2 + string_width, get_size().height)), label_bg_color);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_focus()) {
|
|
|
|
Ref<StyleBox> focus = get_stylebox("focus", "LineEdit");
|
|
|
|
draw_style_box(focus, Rect2(Vector2(), get_size()));
|
|
|
|
}
|
|
|
|
|
2019-10-24 13:29:41 +00:00
|
|
|
draw_string(font, Vector2(Math::round(sb->get_offset().x), vofs), label, lc * Color(1, 1, 1, 0.5));
|
2018-05-15 20:12:35 +00:00
|
|
|
|
2019-10-24 13:29:41 +00:00
|
|
|
draw_string(font, Vector2(Math::round(sb->get_offset().x + string_width + sep), vofs), numstr, fc, number_width);
|
2018-05-15 20:12:35 +00:00
|
|
|
|
|
|
|
if (get_step() == 1) {
|
2019-02-12 20:10:08 +00:00
|
|
|
Ref<Texture> updown2 = get_icon("updown", "SpinBox");
|
|
|
|
int updown_vofs = (get_size().height - updown2->get_height()) / 2;
|
|
|
|
updown_offset = get_size().width - sb->get_margin(MARGIN_RIGHT) - updown2->get_width();
|
2018-05-15 20:12:35 +00:00
|
|
|
Color c(1, 1, 1);
|
|
|
|
if (hover_updown) {
|
|
|
|
c *= Color(1.2, 1.2, 1.2);
|
|
|
|
}
|
2019-02-12 20:10:08 +00:00
|
|
|
draw_texture(updown2, Vector2(updown_offset, updown_vofs), c);
|
2018-05-15 20:12:35 +00:00
|
|
|
if (grabber->is_visible()) {
|
|
|
|
grabber->hide();
|
|
|
|
}
|
|
|
|
} else if (!hide_slider) {
|
|
|
|
int grabber_w = 4 * EDSCALE;
|
|
|
|
int width = get_size().width - sb->get_minimum_size().width - grabber_w;
|
|
|
|
int ofs = sb->get_offset().x;
|
|
|
|
int svofs = (get_size().height + vofs) / 2 - 1;
|
|
|
|
Color c = fc;
|
|
|
|
c.a = 0.2;
|
|
|
|
|
|
|
|
draw_rect(Rect2(ofs, svofs + 1, width, 2 * EDSCALE), c);
|
|
|
|
int gofs = get_as_ratio() * width;
|
|
|
|
c.a = 0.9;
|
|
|
|
Rect2 grabber_rect = Rect2(ofs + gofs, svofs + 1, grabber_w, 2 * EDSCALE);
|
|
|
|
draw_rect(grabber_rect, c);
|
|
|
|
|
2018-05-21 21:08:18 +00:00
|
|
|
bool display_grabber = (mouse_over_spin || mouse_over_grabber) && !grabbing_spinner && !value_input->is_visible();
|
2018-05-15 20:12:35 +00:00
|
|
|
if (grabber->is_visible() != display_grabber) {
|
|
|
|
if (display_grabber) {
|
|
|
|
grabber->show();
|
|
|
|
} else {
|
|
|
|
grabber->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (display_grabber) {
|
|
|
|
Ref<Texture> grabber_tex;
|
|
|
|
if (mouse_over_grabber) {
|
|
|
|
grabber_tex = get_icon("grabber_highlight", "HSlider");
|
|
|
|
} else {
|
|
|
|
grabber_tex = get_icon("grabber", "HSlider");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (grabber->get_texture() != grabber_tex) {
|
|
|
|
grabber->set_texture(grabber_tex);
|
|
|
|
}
|
|
|
|
|
|
|
|
grabber->set_size(Size2(0, 0));
|
|
|
|
grabber->set_position(get_global_position() + grabber_rect.position + grabber_rect.size * 0.5 - grabber->get_size() * 0.5);
|
2019-08-13 10:11:24 +00:00
|
|
|
|
|
|
|
if (mousewheel_over_grabber) {
|
|
|
|
Input::get_singleton()->warp_mouse_position(grabber->get_position() + grabber_rect.size);
|
|
|
|
}
|
|
|
|
|
2018-05-15 20:12:35 +00:00
|
|
|
grabber_range = width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_what == NOTIFICATION_MOUSE_ENTER) {
|
|
|
|
|
|
|
|
mouse_over_spin = true;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
if (p_what == NOTIFICATION_MOUSE_EXIT) {
|
|
|
|
|
|
|
|
mouse_over_spin = false;
|
|
|
|
update();
|
|
|
|
}
|
2018-05-21 19:36:43 +00:00
|
|
|
if (p_what == NOTIFICATION_FOCUS_ENTER) {
|
2018-09-13 01:38:39 +00:00
|
|
|
/* Sorry, I don't like this, it makes navigating the different fields with arrows more difficult.
|
2018-07-19 21:58:15 +00:00
|
|
|
* Just press enter to edit.
|
2018-08-08 20:34:24 +00:00
|
|
|
* if (Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT) && !value_input_just_closed) {
|
2018-05-21 19:36:43 +00:00
|
|
|
_focus_entered();
|
2018-07-19 01:27:39 +00:00
|
|
|
}*/
|
2018-08-08 20:34:24 +00:00
|
|
|
if ((Input::get_singleton()->is_action_pressed("ui_focus_next") || Input::get_singleton()->is_action_pressed("ui_focus_prev")) && !value_input_just_closed) {
|
|
|
|
_focus_entered();
|
|
|
|
}
|
2018-05-21 19:36:43 +00:00
|
|
|
value_input_just_closed = false;
|
|
|
|
}
|
2018-05-15 20:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Size2 EditorSpinSlider::get_minimum_size() const {
|
|
|
|
|
|
|
|
Ref<StyleBox> sb = get_stylebox("normal", "LineEdit");
|
|
|
|
Ref<Font> font = get_font("font", "LineEdit");
|
|
|
|
|
|
|
|
Size2 ms = sb->get_minimum_size();
|
|
|
|
ms.height += font->get_height();
|
|
|
|
|
|
|
|
return ms;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorSpinSlider::set_hide_slider(bool p_hide) {
|
|
|
|
hide_slider = p_hide;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditorSpinSlider::is_hiding_slider() const {
|
|
|
|
return hide_slider;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorSpinSlider::set_label(const String &p_label) {
|
|
|
|
label = p_label;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
String EditorSpinSlider::get_label() const {
|
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
2018-08-08 20:34:24 +00:00
|
|
|
void EditorSpinSlider::_evaluate_input_text() {
|
2020-09-27 20:29:40 +00:00
|
|
|
// Replace comma with dot to support it as decimal separator (GH-6028).
|
|
|
|
// This prevents using functions like `pow()`, but using functions
|
|
|
|
// in EditorSpinSlider is a barely known (and barely used) feature.
|
|
|
|
// Instead, we'd rather support German/French keyboard layouts out of the box.
|
|
|
|
const String text = value_input->get_text().replace(",", ".");
|
|
|
|
|
2018-08-08 20:34:24 +00:00
|
|
|
Ref<Expression> expr;
|
|
|
|
expr.instance();
|
|
|
|
Error err = expr->parse(text);
|
|
|
|
if (err != OK) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant v = expr->execute(Array(), NULL, false);
|
|
|
|
if (v.get_type() == Variant::NIL)
|
|
|
|
return;
|
|
|
|
set_value(v);
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:36:43 +00:00
|
|
|
//text_entered signal
|
|
|
|
void EditorSpinSlider::_value_input_entered(const String &p_text) {
|
|
|
|
value_input_just_closed = true;
|
|
|
|
value_input->hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
//modal_closed signal
|
|
|
|
void EditorSpinSlider::_value_input_closed() {
|
2018-08-08 20:34:24 +00:00
|
|
|
_evaluate_input_text();
|
2018-05-21 19:36:43 +00:00
|
|
|
value_input_just_closed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//focus_exited signal
|
|
|
|
void EditorSpinSlider::_value_focus_exited() {
|
2019-08-12 10:39:27 +00:00
|
|
|
|
|
|
|
// discontinue because the focus_exit was caused by right-click context menu
|
|
|
|
if (value_input->get_menu()->is_visible())
|
|
|
|
return;
|
|
|
|
|
2018-08-08 20:34:24 +00:00
|
|
|
_evaluate_input_text();
|
2018-05-21 19:36:43 +00:00
|
|
|
// focus is not on the same element after the vlalue_input was exited
|
|
|
|
// -> focus is on next element
|
|
|
|
// -> TAB was pressed
|
|
|
|
// -> modal_close was not called
|
|
|
|
// -> need to close/hide manually
|
|
|
|
if (!value_input_just_closed) { //value_input_just_closed should do the same
|
|
|
|
value_input->hide();
|
|
|
|
//tab was pressed
|
|
|
|
} else {
|
|
|
|
//enter, click, esc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 20:12:35 +00:00
|
|
|
void EditorSpinSlider::_grabber_mouse_entered() {
|
|
|
|
mouse_over_grabber = true;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorSpinSlider::_grabber_mouse_exited() {
|
|
|
|
mouse_over_grabber = false;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2018-06-07 15:46:14 +00:00
|
|
|
void EditorSpinSlider::set_read_only(bool p_enable) {
|
|
|
|
|
|
|
|
read_only = p_enable;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditorSpinSlider::is_read_only() const {
|
|
|
|
return read_only;
|
|
|
|
}
|
|
|
|
|
2018-07-18 22:37:17 +00:00
|
|
|
void EditorSpinSlider::set_flat(bool p_enable) {
|
|
|
|
|
|
|
|
flat = p_enable;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditorSpinSlider::is_flat() const {
|
|
|
|
return flat;
|
|
|
|
}
|
|
|
|
|
2018-07-19 01:27:39 +00:00
|
|
|
void EditorSpinSlider::set_custom_label_color(bool p_use_custom_label_color, Color p_custom_label_color) {
|
|
|
|
use_custom_label_color = p_use_custom_label_color;
|
|
|
|
custom_label_color = p_custom_label_color;
|
|
|
|
}
|
|
|
|
|
2018-05-21 19:36:43 +00:00
|
|
|
void EditorSpinSlider::_focus_entered() {
|
|
|
|
Rect2 gr = get_global_rect();
|
|
|
|
value_input->set_text(get_text_value());
|
|
|
|
value_input->set_position(gr.position);
|
|
|
|
value_input->set_size(gr.size);
|
|
|
|
value_input->call_deferred("show_modal");
|
|
|
|
value_input->call_deferred("grab_focus");
|
|
|
|
value_input->call_deferred("select_all");
|
|
|
|
value_input->set_focus_next(find_next_valid_focus()->get_path());
|
|
|
|
value_input->set_focus_previous(find_prev_valid_focus()->get_path());
|
|
|
|
}
|
|
|
|
|
2018-05-15 20:12:35 +00:00
|
|
|
void EditorSpinSlider::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_label", "label"), &EditorSpinSlider::set_label);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_label"), &EditorSpinSlider::get_label);
|
|
|
|
|
2018-06-07 15:46:14 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_read_only", "read_only"), &EditorSpinSlider::set_read_only);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_read_only"), &EditorSpinSlider::is_read_only);
|
|
|
|
|
2018-07-18 22:37:17 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_flat", "flat"), &EditorSpinSlider::set_flat);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_flat"), &EditorSpinSlider::is_flat);
|
|
|
|
|
2018-05-15 20:12:35 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_gui_input"), &EditorSpinSlider::_gui_input);
|
|
|
|
ClassDB::bind_method(D_METHOD("_grabber_mouse_entered"), &EditorSpinSlider::_grabber_mouse_entered);
|
|
|
|
ClassDB::bind_method(D_METHOD("_grabber_mouse_exited"), &EditorSpinSlider::_grabber_mouse_exited);
|
|
|
|
ClassDB::bind_method(D_METHOD("_grabber_gui_input"), &EditorSpinSlider::_grabber_gui_input);
|
|
|
|
ClassDB::bind_method(D_METHOD("_value_input_closed"), &EditorSpinSlider::_value_input_closed);
|
|
|
|
ClassDB::bind_method(D_METHOD("_value_input_entered"), &EditorSpinSlider::_value_input_entered);
|
2018-05-21 19:36:43 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_value_focus_exited"), &EditorSpinSlider::_value_focus_exited);
|
2018-05-15 20:12:35 +00:00
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "label"), "set_label", "get_label");
|
2018-06-07 15:46:14 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "read_only"), "set_read_only", "is_read_only");
|
2018-07-18 22:37:17 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
|
2018-05-15 20:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EditorSpinSlider::EditorSpinSlider() {
|
|
|
|
|
2018-07-18 22:37:17 +00:00
|
|
|
flat = false;
|
2018-05-15 20:12:35 +00:00
|
|
|
grabbing_spinner_attempt = false;
|
|
|
|
grabbing_spinner = false;
|
2018-05-21 13:02:20 +00:00
|
|
|
grabbing_spinner_dist_cache = 0;
|
2018-11-09 03:48:44 +00:00
|
|
|
pre_grab_value = 0;
|
2018-05-15 20:12:35 +00:00
|
|
|
set_focus_mode(FOCUS_ALL);
|
|
|
|
updown_offset = -1;
|
|
|
|
hover_updown = false;
|
|
|
|
grabber = memnew(TextureRect);
|
|
|
|
add_child(grabber);
|
|
|
|
grabber->hide();
|
|
|
|
grabber->set_as_toplevel(true);
|
|
|
|
grabber->set_mouse_filter(MOUSE_FILTER_STOP);
|
|
|
|
grabber->connect("mouse_entered", this, "_grabber_mouse_entered");
|
|
|
|
grabber->connect("mouse_exited", this, "_grabber_mouse_exited");
|
|
|
|
grabber->connect("gui_input", this, "_grabber_gui_input");
|
|
|
|
mouse_over_spin = false;
|
|
|
|
mouse_over_grabber = false;
|
2019-08-13 10:11:24 +00:00
|
|
|
mousewheel_over_grabber = false;
|
2018-05-15 20:12:35 +00:00
|
|
|
grabbing_grabber = false;
|
|
|
|
grabber_range = 1;
|
|
|
|
value_input = memnew(LineEdit);
|
|
|
|
add_child(value_input);
|
|
|
|
value_input->set_as_toplevel(true);
|
|
|
|
value_input->hide();
|
|
|
|
value_input->connect("modal_closed", this, "_value_input_closed");
|
|
|
|
value_input->connect("text_entered", this, "_value_input_entered");
|
2018-05-21 19:36:43 +00:00
|
|
|
value_input->connect("focus_exited", this, "_value_focus_exited");
|
|
|
|
value_input_just_closed = false;
|
2018-05-15 20:12:35 +00:00
|
|
|
hide_slider = false;
|
2018-06-07 15:46:14 +00:00
|
|
|
read_only = false;
|
2018-07-19 01:27:39 +00:00
|
|
|
use_custom_label_color = false;
|
2018-05-15 20:12:35 +00:00
|
|
|
}
|