Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add buttons to remove keys\items from dictionaries\arrays. #29656

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add option to remove array item and button to remove typed array item
  • Loading branch information
nhold committed Jul 2, 2019
commit bd9cc84fdc89d37e184e0a9134c994b11ca6008d
63 changes: 45 additions & 18 deletions editor/editor_properties_array_dict.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,20 @@ void EditorPropertyArray::_property_changed(const String &p_prop, Variant p_valu
void EditorPropertyArray::_change_type(Object *p_button, int p_index) {

Button *button = Object::cast_to<Button>(p_button);

changing_type_idx = p_index;
Rect2 rect = button->get_global_rect();
change_type->set_as_minsize();
change_type->set_global_position(rect.position + rect.size - Vector2(change_type->get_combined_minimum_size().x, 0));
change_type->popup();
changing_type_idx = p_index;
}

void EditorPropertyArray::_change_type_menu(int p_index) {

if (p_index == Variant::VARIANT_MAX) {
_remove_pressed(changing_type_idx);
return;
}

Variant value;
Variant::CallError ce;
value = Variant::construct(Variant::Type(p_index), NULL, 0, ce);
Expand All @@ -204,6 +208,7 @@ void EditorPropertyArray::_change_type_menu(int p_index) {
if (array.get_type() == Variant::ARRAY) {
array = array.call("duplicate"); //dupe, so undo/redo works better
}

object->set_array(array);
update_property();
}
Expand Down Expand Up @@ -356,21 +361,27 @@ void EditorPropertyArray::update_property() {
prop->set_selectable(false);
prop->connect("property_changed", this, "_property_changed");
prop->connect("object_id_selected", this, "_object_id_selected");
if (array.get_type() == Variant::ARRAY) {
HBoxContainer *hb = memnew(HBoxContainer);
vbox->add_child(hb);
hb->add_child(prop);
prop->set_h_size_flags(SIZE_EXPAND_FILL);

if (subtype == Variant::NIL) {
Button *edit = memnew(Button);
edit->set_icon(get_icon("Edit", "EditorIcons"));
hb->add_child(edit);
edit->connect("pressed", this, "_change_type", varray(edit, i + offset));
}
prop->set_h_size_flags(SIZE_EXPAND_FILL);

HBoxContainer *hb = memnew(HBoxContainer);

vbox->add_child(hb);
hb->add_child(prop);

bool is_untyped_array = array.get_type() == Variant::ARRAY && subtype == Variant::NIL;

if (is_untyped_array) {

Button *edit = memnew(Button);
edit->set_icon(get_icon("Edit", "EditorIcons"));
hb->add_child(edit);
edit->connect("pressed", this, "_change_type", varray(edit, i + offset));
} else {
vbox->add_child(prop);

Button *remove = memnew(Button);
remove->set_icon(get_icon("Remove", "EditorIcons"));
remove->connect("pressed", this, "_remove_pressed", varray(i + offset));
hb->add_child(remove);
}

prop->update_property();
Expand All @@ -388,8 +399,21 @@ void EditorPropertyArray::update_property() {
#endif
}

void EditorPropertyArray::_notification(int p_what) {
void EditorPropertyArray::_remove_pressed(int p_index) {

Variant array = object->get_array();
array.call("remove", p_index);

if (array.get_type() == Variant::ARRAY) {
array = array.call("duplicate");
}

emit_changed(get_edited_property(), array, "", false);
object->set_array(array);
update_property();
}

void EditorPropertyArray::_notification(int p_what) {
if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
}
}
Expand Down Expand Up @@ -476,6 +500,7 @@ void EditorPropertyArray::_bind_methods() {
ClassDB::bind_method("_change_type", &EditorPropertyArray::_change_type);
ClassDB::bind_method("_change_type_menu", &EditorPropertyArray::_change_type_menu);
ClassDB::bind_method("_object_id_selected", &EditorPropertyArray::_object_id_selected);
ClassDB::bind_method("_remove_pressed", &EditorPropertyArray::_remove_pressed);
}

EditorPropertyArray::EditorPropertyArray() {
Expand All @@ -498,11 +523,13 @@ EditorPropertyArray::EditorPropertyArray() {
change_type = memnew(PopupMenu);
add_child(change_type);
change_type->connect("id_pressed", this, "_change_type_menu");
changing_type_idx = -1;

for (int i = 0; i < Variant::VARIANT_MAX; i++) {
String type = Variant::get_type_name(Variant::Type(i));
change_type->add_item(type, i);
}
change_type->add_separator();
change_type->add_item(TTR("Remove Item"), Variant::VARIANT_MAX);
changing_type_idx = -1;

subtype = Variant::NIL;
Expand Down Expand Up @@ -995,7 +1022,7 @@ EditorPropertyDictionary::EditorPropertyDictionary() {
change_type = memnew(PopupMenu);
add_child(change_type);
change_type->connect("id_pressed", this, "_change_type_menu");
changing_type_idx = -1;

for (int i = 0; i < Variant::VARIANT_MAX; i++) {
String type = Variant::get_type_name(Variant::Type(i));
change_type->add_item(type, i);
Expand Down
1 change: 1 addition & 0 deletions editor/editor_properties_array_dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class EditorPropertyArray : public EditorProperty {
void _change_type_menu(int p_index);

void _object_id_selected(const String &p_property, ObjectID p_id);
void _remove_pressed(int p_index);

protected:
static void _bind_methods();
Expand Down