Renaming layers from the inspector via a popup menu.

This commit is contained in:
jmb462 2021-12-20 21:30:05 +01:00 committed by Jean-Michel Bernard
parent b9a2569be6
commit b619a47416
2 changed files with 324 additions and 237 deletions

View file

@ -791,36 +791,60 @@ EditorPropertyFlags::EditorPropertyFlags() {
///////////////////// LAYERS /////////////////////////
class EditorPropertyLayersGrid : public Control {
GDCLASS(EditorPropertyLayersGrid, Control);
void EditorPropertyLayersGrid::_rename_pressed(int p_menu) {
// Show rename popup for active layer.
if (renamed_layer_index == -1) {
return;
}
String name = names[renamed_layer_index];
rename_dialog->set_title(vformat(TTR("Renaming layer %d:"), renamed_layer_index + 1));
rename_dialog_text->set_text(name);
rename_dialog_text->select(0, name.length());
rename_dialog->popup_centered(Size2(300, 80) * EDSCALE);
rename_dialog_text->grab_focus();
}
private:
Vector<Rect2> flag_rects;
Rect2 expand_rect;
bool expand_hovered = false;
bool expanded = false;
int expansion_rows = 0;
int hovered_index = -1;
bool read_only = false;
void EditorPropertyLayersGrid::_rename_operation_confirm() {
String new_name = rename_dialog_text->get_text().strip_edges();
if (new_name.length() == 0) {
EditorNode::get_singleton()->show_warning(TTR("No name provided."));
return;
} else if (new_name.find("/") != -1 || new_name.find("\\") != -1 || new_name.find(":") != -1) {
EditorNode::get_singleton()->show_warning(TTR("Name contains invalid characters."));
return;
}
names.set(renamed_layer_index, new_name);
tooltips.set(renamed_layer_index, new_name + "\n" + vformat(TTR("Bit %d, value %d"), renamed_layer_index, 1 << renamed_layer_index));
emit_signal(SNAME("rename_confirmed"), renamed_layer_index, new_name);
}
Size2 get_grid_size() const {
EditorPropertyLayersGrid::EditorPropertyLayersGrid() {
rename_dialog = memnew(ConfirmationDialog);
VBoxContainer *rename_dialog_vb = memnew(VBoxContainer);
rename_dialog->add_child(rename_dialog_vb);
rename_dialog_text = memnew(LineEdit);
rename_dialog_vb->add_margin_child(TTR("Name:"), rename_dialog_text);
rename_dialog->get_ok_button()->set_text(TTR("Rename"));
add_child(rename_dialog);
rename_dialog->register_text_enter(rename_dialog_text);
rename_dialog->connect("confirmed", callable_mp(this, &EditorPropertyLayersGrid::_rename_operation_confirm));
layer_rename = memnew(PopupMenu);
layer_rename->add_item(TTR("Rename layer"), 0);
add_child(layer_rename);
layer_rename->connect("id_pressed", callable_mp(this, &EditorPropertyLayersGrid::_rename_pressed));
}
Size2 EditorPropertyLayersGrid::get_grid_size() const {
Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
return Vector2(0, font->get_height(font_size) * 3);
}
}
public:
uint32_t value = 0;
int layer_group_size = 0;
int layer_count = 0;
Vector<String> names;
Vector<String> tooltips;
void set_read_only(bool p_read_only) {
void EditorPropertyLayersGrid::set_read_only(bool p_read_only) {
read_only = p_read_only;
}
}
virtual Size2 get_minimum_size() const override {
Size2 EditorPropertyLayersGrid::get_minimum_size() const {
Size2 min_size = get_grid_size();
// Add extra rows when expanded.
@ -832,18 +856,18 @@ public:
}
return min_size;
}
}
virtual String get_tooltip(const Point2 &p_pos) const override {
String EditorPropertyLayersGrid::get_tooltip(const Point2 &p_pos) const {
for (int i = 0; i < flag_rects.size(); i++) {
if (i < tooltips.size() && flag_rects[i].has_point(p_pos)) {
return tooltips[i];
}
}
return String();
}
}
void gui_input(const Ref<InputEvent> &p_ev) override {
void EditorPropertyLayersGrid::gui_input(const Ref<InputEvent> &p_ev) {
if (read_only) {
return;
}
@ -894,9 +918,17 @@ public:
update();
}
}
if (mb.is_valid() && mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
if (hovered_index >= 0) {
renamed_layer_index = hovered_index;
layer_rename->set_position(get_screen_position() + mb->get_position());
layer_rename->reset_size();
layer_rename->popup();
}
}
}
void _notification(int p_what) {
void EditorPropertyLayersGrid::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_DRAW: {
Size2 grid_size = get_grid_size();
@ -1035,17 +1067,17 @@ public:
default:
break;
}
}
}
void set_flag(uint32_t p_flag) {
void EditorPropertyLayersGrid::set_flag(uint32_t p_flag) {
value = p_flag;
update();
}
}
static void _bind_methods() {
void EditorPropertyLayersGrid::_bind_methods() {
ADD_SIGNAL(MethodInfo("flag_changed", PropertyInfo(Variant::INT, "flag")));
}
};
ADD_SIGNAL(MethodInfo("rename_confirmed", PropertyInfo(Variant::INT, "layer_id"), PropertyInfo(Variant::STRING, "new_name")));
}
void EditorPropertyLayers::_set_read_only(bool p_read_only) {
button->set_disabled(p_read_only);
@ -1063,7 +1095,7 @@ void EditorPropertyLayers::update_property() {
}
void EditorPropertyLayers::setup(LayerType p_layer_type) {
String basename;
layer_type = p_layer_type;
int layer_group_size = 0;
int layer_count = 0;
switch (p_layer_type) {
@ -1127,6 +1159,13 @@ void EditorPropertyLayers::setup(LayerType p_layer_type) {
grid->layer_count = layer_count;
}
void EditorPropertyLayers::set_layer_name(int p_index, const String &p_name) {
if (ProjectSettings::get_singleton()->has_setting(basename + vformat("/layer_%d", p_index + 1))) {
ProjectSettings::get_singleton()->set(basename + vformat("/layer_%d", p_index + 1), p_name);
ProjectSettings::get_singleton()->save();
}
}
void EditorPropertyLayers::_button_pressed() {
int layer_count = grid->layer_count;
int layer_group_size = grid->layer_group_size;
@ -1159,6 +1198,10 @@ void EditorPropertyLayers::_menu_pressed(int p_menu) {
_grid_changed(grid->value);
}
void EditorPropertyLayers::_refresh_names() {
setup(layer_type);
}
void EditorPropertyLayers::_bind_methods() {
}
@ -1168,6 +1211,7 @@ EditorPropertyLayers::EditorPropertyLayers() {
add_child(hb);
grid = memnew(EditorPropertyLayersGrid);
grid->connect("flag_changed", callable_mp(this, &EditorPropertyLayers::_grid_changed));
grid->connect("rename_confirmed", callable_mp(this, &EditorPropertyLayers::set_layer_name));
grid->set_h_size_flags(SIZE_EXPAND_FILL);
hb->add_child(grid);
@ -1184,6 +1228,7 @@ EditorPropertyLayers::EditorPropertyLayers() {
layers->set_hide_on_checkable_item_selection(false);
layers->connect("id_pressed", callable_mp(this, &EditorPropertyLayers::_menu_pressed));
layers->connect("popup_hide", callable_mp((BaseButton *)button, &BaseButton::set_pressed), varray(false));
EditorNode::get_singleton()->connect("project_settings_changed", callable_mp(this, &EditorPropertyLayers::_refresh_names));
}
///////////////////// INT /////////////////////////

View file

@ -279,7 +279,46 @@ public:
EditorPropertyFlags();
};
class EditorPropertyLayersGrid;
///////////////////// LAYERS /////////////////////////
class EditorPropertyLayersGrid : public Control {
GDCLASS(EditorPropertyLayersGrid, Control);
private:
Vector<Rect2> flag_rects;
Rect2 expand_rect;
bool expand_hovered = false;
bool expanded = false;
int expansion_rows = 0;
int hovered_index = -1;
bool read_only = false;
int renamed_layer_index = -1;
PopupMenu *layer_rename;
ConfirmationDialog *rename_dialog;
LineEdit *rename_dialog_text;
void _rename_pressed(int p_menu);
void _rename_operation_confirm();
Size2 get_grid_size() const;
protected:
void _notification(int p_what);
static void _bind_methods();
public:
uint32_t value = 0;
int layer_group_size = 0;
int layer_count = 0;
Vector<String> names;
Vector<String> tooltips;
void set_read_only(bool p_read_only);
virtual Size2 get_minimum_size() const override;
virtual String get_tooltip(const Point2 &p_pos) const override;
void gui_input(const Ref<InputEvent> &p_ev) override;
void set_flag(uint32_t p_flag);
EditorPropertyLayersGrid();
};
class EditorPropertyLayers : public EditorProperty {
GDCLASS(EditorPropertyLayers, EditorProperty);
@ -297,12 +336,14 @@ public:
private:
EditorPropertyLayersGrid *grid;
void _grid_changed(uint32_t p_grid);
String basename;
LayerType layer_type;
PopupMenu *layers;
Button *button;
void _button_pressed();
void _menu_pressed(int p_menu);
void _refresh_names();
protected:
virtual void _set_read_only(bool p_read_only) override;
@ -310,6 +351,7 @@ protected:
public:
void setup(LayerType p_layer_type);
void set_layer_name(int p_index, const String &p_name);
virtual void update_property() override;
EditorPropertyLayers();
};