Make sure stylebox is valid in EditorSpinSlider before using it

This commit is contained in:
Yuri Sizov 2021-08-14 00:07:59 +03:00
parent f32c042f3e
commit 0d3e85c665
2 changed files with 173 additions and 160 deletions

View file

@ -195,11 +195,11 @@ void EditorSpinSlider::_update_value_input_stylebox() {
if (!value_input) { if (!value_input) {
return; return;
} }
// Add a left margin to the stylebox to make the number align with the Label // 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 // when it's edited. The LineEdit "focus" stylebox uses the "normal" stylebox's
// default margins. // default margins.
Ref<StyleBoxFlat> stylebox = Ref<StyleBox> stylebox = get_theme_stylebox(SNAME("normal"), SNAME("LineEdit"))->duplicate();
EditorNode::get_singleton()->get_theme_base()->get_theme_stylebox(SNAME("normal"), SNAME("LineEdit"))->duplicate();
// EditorSpinSliders with a label have more space on the left, so add an // EditorSpinSliders with a label have more space on the left, so add an
// higher margin to match the location where the text begins. // higher margin to match the location where the text begins.
// The margin values below were determined by empirical testing. // The margin values below were determined by empirical testing.
@ -210,25 +210,11 @@ void EditorSpinSlider::_update_value_input_stylebox() {
stylebox->set_default_margin(SIDE_LEFT, (get_label() != String() ? 23 : 16) * EDSCALE); stylebox->set_default_margin(SIDE_LEFT, (get_label() != String() ? 23 : 16) * EDSCALE);
stylebox->set_default_margin(SIDE_RIGHT, 0); stylebox->set_default_margin(SIDE_RIGHT, 0);
} }
value_input->add_theme_style_override("normal", stylebox); value_input->add_theme_style_override("normal", stylebox);
} }
void EditorSpinSlider::_notification(int p_what) {
if (p_what == NOTIFICATION_WM_WINDOW_FOCUS_OUT ||
p_what == NOTIFICATION_WM_WINDOW_FOCUS_IN ||
p_what == NOTIFICATION_EXIT_TREE) {
if (grabbing_spinner) {
grabber->hide();
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
grabbing_spinner = false;
grabbing_spinner_attempt = false;
}
}
if (p_what == NOTIFICATION_READY || p_what == NOTIFICATION_THEME_CHANGED) { void EditorSpinSlider::_draw_spin_slider() {
_update_value_input_stylebox();
}
if (p_what == NOTIFICATION_DRAW) {
updown_offset = -1; updown_offset = -1;
RID ci = get_canvas_item(); RID ci = get_canvas_item();
@ -377,21 +363,44 @@ void EditorSpinSlider::_notification(int p_what) {
grabber_range = width; grabber_range = width;
} }
} }
} }
if (p_what == NOTIFICATION_MOUSE_ENTER) { void EditorSpinSlider::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE:
case NOTIFICATION_THEME_CHANGED:
_update_value_input_stylebox();
break;
case NOTIFICATION_DRAW:
_draw_spin_slider();
break;
case NOTIFICATION_WM_WINDOW_FOCUS_IN:
case NOTIFICATION_WM_WINDOW_FOCUS_OUT:
case NOTIFICATION_EXIT_TREE:
if (grabbing_spinner) {
grabber->hide();
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
grabbing_spinner = false;
grabbing_spinner_attempt = false;
}
break;
case NOTIFICATION_MOUSE_ENTER:
mouse_over_spin = true; mouse_over_spin = true;
update(); update();
} break;
if (p_what == NOTIFICATION_MOUSE_EXIT) { case NOTIFICATION_MOUSE_EXIT:
mouse_over_spin = false; mouse_over_spin = false;
update(); update();
} break;
if (p_what == NOTIFICATION_FOCUS_ENTER) { case NOTIFICATION_FOCUS_ENTER:
if ((Input::get_singleton()->is_action_pressed("ui_focus_next") || Input::get_singleton()->is_action_pressed("ui_focus_prev")) && !value_input_just_closed) { 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(); _focus_entered();
} }
value_input_just_closed = false; value_input_just_closed = false;
break;
} }
} }
@ -567,8 +576,10 @@ void EditorSpinSlider::_ensure_input_popup() {
if (value_input_popup) { if (value_input_popup) {
return; return;
} }
value_input_popup = memnew(Popup); value_input_popup = memnew(Popup);
add_child(value_input_popup); add_child(value_input_popup);
value_input = memnew(LineEdit); value_input = memnew(LineEdit);
value_input_popup->add_child(value_input); value_input_popup->add_child(value_input);
value_input_popup->set_wrap_controls(true); value_input_popup->set_wrap_controls(true);
@ -576,6 +587,7 @@ void EditorSpinSlider::_ensure_input_popup() {
value_input_popup->connect("popup_hide", callable_mp(this, &EditorSpinSlider::_value_input_closed)); value_input_popup->connect("popup_hide", callable_mp(this, &EditorSpinSlider::_value_input_closed));
value_input->connect("text_submitted", callable_mp(this, &EditorSpinSlider::_value_input_submitted)); value_input->connect("text_submitted", callable_mp(this, &EditorSpinSlider::_value_input_submitted));
value_input->connect("focus_exited", callable_mp(this, &EditorSpinSlider::_value_focus_exited)); value_input->connect("focus_exited", callable_mp(this, &EditorSpinSlider::_value_focus_exited));
if (is_inside_tree()) { if (is_inside_tree()) {
_update_value_input_stylebox(); _update_value_input_stylebox();
} }

View file

@ -81,6 +81,7 @@ class EditorSpinSlider : public Range {
void _update_value_input_stylebox(); void _update_value_input_stylebox();
void _ensure_input_popup(); void _ensure_input_popup();
void _draw_spin_slider();
protected: protected:
void _notification(int p_what); void _notification(int p_what);