summaryrefslogtreecommitdiff
path: root/editor
diff options
context:
space:
mode:
authorJuan Linietsky <reduzio@gmail.com>2021-07-17 12:28:41 -0300
committerGitHub <noreply@github.com>2021-07-17 12:28:41 -0300
commitdeeec8894ad39d7254cf31f46321fb3cd869461f (patch)
tree3ea5917ef09ef5e431783284e3295eb90099a48e /editor
parent7537875d862837d43e50be2cc14de9d8db8f5fe3 (diff)
parenta3fb76cd450661816f2e4f65dc172917b47c11e6 (diff)
Merge pull request #50530 from reduz/create-popups-on-demand
Create many types of popups on demand
Diffstat (limited to 'editor')
-rw-r--r--editor/editor_resource_picker.cpp15
-rw-r--r--editor/editor_resource_picker.h4
-rw-r--r--editor/editor_spin_slider.cpp79
-rw-r--r--editor/editor_spin_slider.h9
4 files changed, 69 insertions, 38 deletions
diff --git a/editor/editor_resource_picker.cpp b/editor/editor_resource_picker.cpp
index 4d9865622c..495db29789 100644
--- a/editor/editor_resource_picker.cpp
+++ b/editor/editor_resource_picker.cpp
@@ -141,6 +141,7 @@ void EditorResourcePicker::_update_menu() {
}
void EditorResourcePicker::_update_menu_items() {
+ _ensure_resource_menu();
edit_menu->clear();
// Add options for creating specific subtypes of the base resource type.
@@ -360,6 +361,7 @@ void EditorResourcePicker::_edit_menu_cbk(int p_which) {
}
void EditorResourcePicker::set_create_options(Object *p_menu_node) {
+ _ensure_resource_menu();
// If a subclass implements this method, use it to replace all create items.
if (get_script_instance() && get_script_instance()->has_method("_set_create_options")) {
get_script_instance()->call("_set_create_options", p_menu_node);
@@ -783,6 +785,15 @@ bool EditorResourcePicker::is_editable() const {
return editable;
}
+void EditorResourcePicker::_ensure_resource_menu() {
+ if (edit_menu) {
+ return;
+ }
+ edit_menu = memnew(PopupMenu);
+ add_child(edit_menu);
+ edit_menu->connect("id_pressed", callable_mp(this, &EditorResourcePicker::_edit_menu_cbk));
+ edit_menu->connect("popup_hide", callable_mp((BaseButton *)edit_button, &BaseButton::set_pressed), varray(false));
+}
EditorResourcePicker::EditorResourcePicker() {
assign_button = memnew(Button);
assign_button->set_flat(true);
@@ -808,10 +819,6 @@ EditorResourcePicker::EditorResourcePicker() {
edit_button->connect("pressed", callable_mp(this, &EditorResourcePicker::_update_menu));
add_child(edit_button);
edit_button->connect("gui_input", callable_mp(this, &EditorResourcePicker::_button_input));
- edit_menu = memnew(PopupMenu);
- add_child(edit_menu);
- edit_menu->connect("id_pressed", callable_mp(this, &EditorResourcePicker::_edit_menu_cbk));
- edit_menu->connect("popup_hide", callable_mp((BaseButton *)edit_button, &BaseButton::set_pressed), varray(false));
}
void EditorScriptPicker::set_create_options(Object *p_menu_node) {
diff --git a/editor/editor_resource_picker.h b/editor/editor_resource_picker.h
index 9a4b945bc7..82a4817dfc 100644
--- a/editor/editor_resource_picker.h
+++ b/editor/editor_resource_picker.h
@@ -67,7 +67,7 @@ class EditorResourcePicker : public HBoxContainer {
CONVERT_BASE_ID = 1000,
};
- PopupMenu *edit_menu;
+ PopupMenu *edit_menu = nullptr;
void _update_resource();
void _update_resource_preview(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, ObjectID p_obj);
@@ -90,6 +90,8 @@ class EditorResourcePicker : public HBoxContainer {
bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
+ void _ensure_resource_menu();
+
protected:
static void _bind_methods();
void _notification(int p_what);
diff --git a/editor/editor_spin_slider.cpp b/editor/editor_spin_slider.cpp
index aa4a394d30..247eaab9f8 100644
--- a/editor/editor_spin_slider.cpp
+++ b/editor/editor_spin_slider.cpp
@@ -185,6 +185,27 @@ void EditorSpinSlider::_grabber_gui_input(const Ref<InputEvent> &p_event) {
}
}
+void EditorSpinSlider::_update_value_input_stylebox() {
+ if (!value_input) {
+ return;
+ }
+ // 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_theme_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.
+ if (is_layout_rtl()) {
+ stylebox->set_default_margin(SIDE_LEFT, 0);
+ stylebox->set_default_margin(SIDE_RIGHT, (get_label() != String() ? 23 : 16) * EDSCALE);
+ } else {
+ stylebox->set_default_margin(SIDE_LEFT, (get_label() != String() ? 23 : 16) * EDSCALE);
+ stylebox->set_default_margin(SIDE_RIGHT, 0);
+ }
+ 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 ||
@@ -197,23 +218,8 @@ void EditorSpinSlider::_notification(int p_what) {
}
}
- 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_theme_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.
- if (is_layout_rtl()) {
- stylebox->set_default_margin(SIDE_LEFT, 0);
- stylebox->set_default_margin(SIDE_RIGHT, (get_label() != String() ? 23 : 16) * EDSCALE);
- } else {
- stylebox->set_default_margin(SIDE_LEFT, (get_label() != String() ? 23 : 16) * EDSCALE);
- stylebox->set_default_margin(SIDE_RIGHT, 0);
- }
- value_input->add_theme_style_override("normal", stylebox);
+ if (p_what == NOTIFICATION_READY || p_what == NOTIFICATION_THEME_CHANGED) {
+ _update_value_input_stylebox();
}
if (p_what == NOTIFICATION_DRAW) {
@@ -332,7 +338,7 @@ void EditorSpinSlider::_notification(int p_what) {
grabbing_spinner_mouse_pos = get_global_position() + grabber_rect.position + grabber_rect.size * 0.5;
- bool display_grabber = (mouse_over_spin || mouse_over_grabber) && !grabbing_spinner && !value_input_popup->is_visible();
+ bool display_grabber = (mouse_over_spin || mouse_over_grabber) && !grabbing_spinner && !(value_input_popup && value_input_popup->is_visible());
if (grabber->is_visible() != display_grabber) {
if (display_grabber) {
grabber->show();
@@ -445,7 +451,9 @@ void EditorSpinSlider::_evaluate_input_text() {
//text_submitted signal
void EditorSpinSlider::_value_input_submitted(const String &p_text) {
value_input_just_closed = true;
- value_input_popup->hide();
+ if (value_input_popup) {
+ value_input_popup->hide();
+ }
}
//modal_closed signal
@@ -457,7 +465,7 @@ void EditorSpinSlider::_value_input_closed() {
//focus_exited signal
void EditorSpinSlider::_value_focus_exited() {
// discontinue because the focus_exit was caused by right-click context menu
- if (value_input->get_menu()->is_visible()) {
+ if (value_input->is_menu_visible()) {
return;
}
@@ -468,7 +476,9 @@ void EditorSpinSlider::_value_focus_exited() {
// -> 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_popup->hide();
+ if (value_input_popup) {
+ value_input_popup->hide();
+ }
//tab was pressed
} else {
//enter, click, esc
@@ -509,6 +519,7 @@ void EditorSpinSlider::set_custom_label_color(bool p_use_custom_label_color, Col
}
void EditorSpinSlider::_focus_entered() {
+ _ensure_input_popup();
Rect2 gr = get_screen_rect();
value_input->set_text(get_text_value());
value_input_popup->set_position(gr.position);
@@ -541,6 +552,23 @@ void EditorSpinSlider::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
}
+void EditorSpinSlider::_ensure_input_popup() {
+ if (value_input_popup) {
+ return;
+ }
+ value_input_popup = memnew(Popup);
+ add_child(value_input_popup);
+ value_input = memnew(LineEdit);
+ value_input_popup->add_child(value_input);
+ value_input_popup->set_wrap_controls(true);
+ value_input->set_anchors_and_offsets_preset(PRESET_WIDE);
+ 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("focus_exited", callable_mp(this, &EditorSpinSlider::_value_focus_exited));
+ if (is_inside_tree()) {
+ _update_value_input_stylebox();
+ }
+}
EditorSpinSlider::EditorSpinSlider() {
flat = false;
grabbing_spinner_attempt = false;
@@ -563,15 +591,6 @@ EditorSpinSlider::EditorSpinSlider() {
mousewheel_over_grabber = false;
grabbing_grabber = false;
grabber_range = 1;
- value_input_popup = memnew(Popup);
- add_child(value_input_popup);
- value_input = memnew(LineEdit);
- value_input_popup->add_child(value_input);
- value_input_popup->set_wrap_controls(true);
- value_input->set_anchors_and_offsets_preset(PRESET_WIDE);
- 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("focus_exited", callable_mp(this, &EditorSpinSlider::_value_focus_exited));
value_input_just_closed = false;
hide_slider = false;
read_only = false;
diff --git a/editor/editor_spin_slider.h b/editor/editor_spin_slider.h
index c30ff30390..af1f385fad 100644
--- a/editor/editor_spin_slider.h
+++ b/editor/editor_spin_slider.h
@@ -63,9 +63,9 @@ class EditorSpinSlider : public Range {
Vector2 grabbing_spinner_mouse_pos;
double pre_grab_value;
- Popup *value_input_popup;
- LineEdit *value_input;
- bool value_input_just_closed;
+ Popup *value_input_popup = nullptr;
+ LineEdit *value_input = nullptr;
+ bool value_input_just_closed = false;
void _grabber_gui_input(const Ref<InputEvent> &p_event);
void _value_input_closed();
@@ -79,6 +79,9 @@ class EditorSpinSlider : public Range {
void _evaluate_input_text();
+ void _update_value_input_stylebox();
+ void _ensure_input_popup();
+
protected:
void _notification(int p_what);
void _gui_input(const Ref<InputEvent> &p_event);