summaryrefslogtreecommitdiff
path: root/editor/editor_spin_slider.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'editor/editor_spin_slider.cpp')
-rw-r--r--editor/editor_spin_slider.cpp173
1 files changed, 125 insertions, 48 deletions
diff --git a/editor/editor_spin_slider.cpp b/editor/editor_spin_slider.cpp
index 0852a42794..0e6d81d13b 100644
--- a/editor/editor_spin_slider.cpp
+++ b/editor/editor_spin_slider.cpp
@@ -56,6 +56,7 @@ void EditorSpinSlider::_gui_input(const Ref<InputEvent> &p_event) {
} else {
grabbing_spinner_attempt = true;
+ grabbing_spinner_dist_cache = 0;
grabbing_spinner = false;
grabbing_spinner_mouse_pos = Input::get_singleton()->get_mouse_position();
}
@@ -69,13 +70,7 @@ void EditorSpinSlider::_gui_input(const Ref<InputEvent> &p_event) {
Input::get_singleton()->warp_mouse_position(grabbing_spinner_mouse_pos);
update();
} else {
- 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");
+ _focus_entered();
}
grabbing_spinner = false;
@@ -89,21 +84,29 @@ void EditorSpinSlider::_gui_input(const Ref<InputEvent> &p_event) {
if (grabbing_spinner_attempt) {
- if (!grabbing_spinner) {
+ double diff_x = mm->get_relative().x;
+ if (mm->get_shift() && grabbing_spinner) {
+ diff_x *= 0.1;
+ }
+ grabbing_spinner_dist_cache += diff_x;
+
+ if (!grabbing_spinner && ABS(grabbing_spinner_dist_cache) > 4 * EDSCALE) {
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
grabbing_spinner = true;
}
- double v = get_value();
-
- double diff_x = mm->get_relative().x;
- diff_x = Math::pow(ABS(diff_x), 1.8) * SGN(diff_x);
- diff_x *= 0.1;
-
- v += diff_x * get_step();
-
- set_value(v);
-
+ if (grabbing_spinner) {
+ if (mm->get_control() || updown_offset != -1) {
+ set_value(Math::round(get_value()));
+ if (ABS(grabbing_spinner_dist_cache) > 6) {
+ set_value(get_value() + SGN(grabbing_spinner_dist_cache));
+ grabbing_spinner_dist_cache = 0;
+ }
+ } else {
+ set_value(get_value() + get_step() * grabbing_spinner_dist_cache * 10);
+ grabbing_spinner_dist_cache = 0;
+ }
+ }
} else if (updown_offset != -1) {
bool new_hover = (mm->get_position().x > updown_offset);
if (new_hover != hover_updown) {
@@ -115,25 +118,10 @@ void EditorSpinSlider::_gui_input(const Ref<InputEvent> &p_event) {
Ref<InputEventKey> k = p_event;
if (k.is_valid() && k->is_pressed() && k->is_action("ui_accept")) {
- 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");
+ _focus_entered();
}
}
-void EditorSpinSlider::_value_input_closed() {
- set_value(value_input->get_text().to_double());
-}
-
-void EditorSpinSlider::_value_input_entered(const String &p_text) {
- set_value(p_text.to_double());
- value_input->hide();
-}
-
void EditorSpinSlider::_grabber_gui_input(const Ref<InputEvent> &p_event) {
Ref<InputEventMouseButton> mb = p_event;
@@ -173,20 +161,20 @@ void EditorSpinSlider::_notification(int p_what) {
updown_offset = -1;
Ref<StyleBox> sb = get_stylebox("normal", "LineEdit");
- draw_style_box(sb, Rect2(Vector2(), get_size()));
+ if (!flat) {
+ draw_style_box(sb, Rect2(Vector2(), get_size()));
+ }
Ref<Font> font = get_font("font", "LineEdit");
+ int sep_base = 4 * EDSCALE;
+ int sep = sep_base + sb->get_offset().x; //make it have the same margin on both sides, looks better
+
+ int string_width = font->get_string_size(label).width;
+ int number_width = get_size().width - sb->get_minimum_size().width - string_width - sep;
- int avail_width = get_size().width - sb->get_minimum_size().width - sb->get_minimum_size().width;
- avail_width -= font->get_string_size(label).width;
Ref<Texture> updown = get_icon("updown", "SpinBox");
if (get_step() == 1) {
- avail_width -= updown->get_width();
- }
-
- if (has_focus()) {
- Ref<StyleBox> focus = get_stylebox("focus", "LineEdit");
- draw_style_box(focus, Rect2(Vector2(), get_size()));
+ number_width -= updown->get_width();
}
String numstr = get_text_value();
@@ -194,10 +182,26 @@ void EditorSpinSlider::_notification(int p_what) {
int vofs = (get_size().height - font->get_height()) / 2 + font->get_ascent();
Color fc = get_color("font_color", "LineEdit");
+ Color lc;
+ if (use_custom_label_color) {
+ lc = custom_label_color;
+ } else {
+ lc = fc;
+ }
- int label_ofs = sb->get_offset().x + avail_width;
- draw_string(font, Vector2(label_ofs, vofs), label, fc * Color(1, 1, 1, 0.5));
- draw_string(font, Vector2(sb->get_offset().x, vofs), numstr, fc, avail_width);
+ 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()));
+ }
+
+ draw_string(font, Vector2(sb->get_offset().x, vofs), label, lc * Color(1, 1, 1, 0.5));
+
+ draw_string(font, Vector2(sb->get_offset().x + string_width + sep, vofs), numstr, fc, number_width);
if (get_step() == 1) {
Ref<Texture> updown = get_icon("updown", "SpinBox");
@@ -225,7 +229,7 @@ void EditorSpinSlider::_notification(int p_what) {
Rect2 grabber_rect = Rect2(ofs + gofs, svofs + 1, grabber_w, 2 * EDSCALE);
draw_rect(grabber_rect, c);
- bool display_grabber = (mouse_over_spin || mouse_over_grabber) && !grabbing_spinner;
+ bool display_grabber = (mouse_over_spin || mouse_over_grabber) && !grabbing_spinner && !value_input->is_visible();
if (grabber->is_visible() != display_grabber) {
if (display_grabber) {
grabber->show();
@@ -263,6 +267,15 @@ void EditorSpinSlider::_notification(int p_what) {
mouse_over_spin = false;
update();
}
+ if (p_what == NOTIFICATION_FOCUS_ENTER) {
+ /* Sorry, I dont like this, it makes navigating the different fields with arrows more difficult.
+ * Just press enter to edit.
+ * if (!Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT) && !value_input_just_closed) {
+ _focus_entered();
+ }*/
+
+ value_input_just_closed = false;
+ }
}
Size2 EditorSpinSlider::get_minimum_size() const {
@@ -294,6 +307,34 @@ String EditorSpinSlider::get_label() const {
return label;
}
+//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() {
+ set_value(value_input->get_text().to_double());
+ value_input_just_closed = true;
+}
+
+//focus_exited signal
+void EditorSpinSlider::_value_focus_exited() {
+ set_value(value_input->get_text().to_double());
+ // 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
+ }
+}
+
void EditorSpinSlider::_grabber_mouse_entered() {
mouse_over_grabber = true;
update();
@@ -314,6 +355,33 @@ bool EditorSpinSlider::is_read_only() const {
return read_only;
}
+void EditorSpinSlider::set_flat(bool p_enable) {
+
+ flat = p_enable;
+ update();
+}
+
+bool EditorSpinSlider::is_flat() const {
+ return flat;
+}
+
+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;
+}
+
+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());
+}
+
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);
@@ -321,22 +389,28 @@ void EditorSpinSlider::_bind_methods() {
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);
+ ClassDB::bind_method(D_METHOD("set_flat", "flat"), &EditorSpinSlider::set_flat);
+ ClassDB::bind_method(D_METHOD("is_flat"), &EditorSpinSlider::is_flat);
+
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);
+ ClassDB::bind_method(D_METHOD("_value_focus_exited"), &EditorSpinSlider::_value_focus_exited);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "label"), "set_label", "get_label");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "read_only"), "set_read_only", "is_read_only");
+ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
}
EditorSpinSlider::EditorSpinSlider() {
+ flat = false;
grabbing_spinner_attempt = false;
grabbing_spinner = false;
-
+ grabbing_spinner_dist_cache = 0;
set_focus_mode(FOCUS_ALL);
updown_offset = -1;
hover_updown = false;
@@ -358,6 +432,9 @@ EditorSpinSlider::EditorSpinSlider() {
value_input->hide();
value_input->connect("modal_closed", this, "_value_input_closed");
value_input->connect("text_entered", this, "_value_input_entered");
+ value_input->connect("focus_exited", this, "_value_focus_exited");
+ value_input_just_closed = false;
hide_slider = false;
read_only = false;
+ use_custom_label_color = false;
}