summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <rverschelde@gmail.com>2017-12-09 16:42:25 +0100
committerGitHub <noreply@github.com>2017-12-09 16:42:25 +0100
commit20a566d63a34e5b502a0b46d3cbe655427f1bc76 (patch)
treef31b2c613fbd7005fb4f0150017bcbec51e1cef3
parent4d6f8f89ed0a42b6aae7f5843f2d0a6332018d08 (diff)
parent3cca09d96a8918369638813db3a3d0e546369503 (diff)
Merge pull request #13166 from Krakean/change_addrootifnone_v2
When drag'n'drop, automatically create dragged resource as a root node if you haven't any yet (v2)
-rw-r--r--editor/plugins/canvas_item_editor_plugin.cpp57
-rw-r--r--editor/plugins/canvas_item_editor_plugin.h1
2 files changed, 35 insertions, 23 deletions
diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp
index 8eee9c7e7c..213f293bea 100644
--- a/editor/plugins/canvas_item_editor_plugin.cpp
+++ b/editor/plugins/canvas_item_editor_plugin.cpp
@@ -4486,15 +4486,24 @@ void CanvasItemEditorViewport::_create_nodes(Node *parent, Node *child, String &
Ref<Texture> texture = Ref<Texture>(Object::cast_to<Texture>(ResourceCache::get(path)));
Size2 texture_size = texture->get_size();
- editor_data->get_undo_redo().add_do_method(parent, "add_child", child);
- editor_data->get_undo_redo().add_do_method(child, "set_owner", editor->get_edited_scene());
- editor_data->get_undo_redo().add_do_reference(child);
- editor_data->get_undo_redo().add_undo_method(parent, "remove_child", child);
+ if (parent) {
+ editor_data->get_undo_redo().add_do_method(parent, "add_child", child);
+ editor_data->get_undo_redo().add_do_method(child, "set_owner", editor->get_edited_scene());
+ editor_data->get_undo_redo().add_do_reference(child);
+ editor_data->get_undo_redo().add_undo_method(parent, "remove_child", child);
+ } else { // if we haven't parent, lets try to make a child as a parent.
+ editor_data->get_undo_redo().add_do_method(editor, "set_edited_scene", child);
+ editor_data->get_undo_redo().add_do_method(child, "set_owner", editor->get_edited_scene());
+ editor_data->get_undo_redo().add_do_reference(child);
+ editor_data->get_undo_redo().add_undo_method(editor, "set_edited_scene", (Object *)NULL);
+ }
- String new_name = parent->validate_child_name(child);
- ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
- editor_data->get_undo_redo().add_do_method(sed, "live_debug_create_node", editor->get_edited_scene()->get_path_to(parent), child->get_class(), new_name);
- editor_data->get_undo_redo().add_undo_method(sed, "live_debug_remove_node", NodePath(String(editor->get_edited_scene()->get_path_to(parent)) + "/" + new_name));
+ if (parent) {
+ String new_name = parent->validate_child_name(child);
+ ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
+ editor_data->get_undo_redo().add_do_method(sed, "live_debug_create_node", editor->get_edited_scene()->get_path_to(parent), child->get_class(), new_name);
+ editor_data->get_undo_redo().add_undo_method(sed, "live_debug_remove_node", NodePath(String(editor->get_edited_scene()->get_path_to(parent)) + "/" + new_name));
+ }
// handle with different property for texture
String property = "texture";
@@ -4527,8 +4536,8 @@ void CanvasItemEditorViewport::_create_nodes(Node *parent, Node *child, String &
}
// locate at preview position
- Point2 pos;
- if (parent->has_method("get_global_position")) {
+ Point2 pos = Point2(0, 0);
+ if (parent && parent->has_method("get_global_position")) {
pos = parent->call("get_global_position");
}
Transform2D trans = canvas->get_canvas_transform();
@@ -4689,6 +4698,18 @@ bool CanvasItemEditorViewport::can_drop_data(const Point2 &p_point, const Varian
return false;
}
+void CanvasItemEditorViewport::_show_resource_type_selector() {
+ List<BaseButton *> btn_list;
+ button_group->get_buttons(&btn_list);
+
+ for (int i = 0; i < btn_list.size(); i++) {
+ CheckBox *check = Object::cast_to<CheckBox>(btn_list[i]);
+ check->set_pressed(check->get_text() == default_type);
+ }
+ selector->set_title(vformat(TTR("Add %s"), default_type));
+ selector->popup_centered_minsize();
+}
+
void CanvasItemEditorViewport::drop_data(const Point2 &p_point, const Variant &p_data) {
bool is_shift = Input::get_singleton()->is_key_pressed(KEY_SHIFT);
bool is_alt = Input::get_singleton()->is_key_pressed(KEY_ALT);
@@ -4705,10 +4726,8 @@ void CanvasItemEditorViewport::drop_data(const Point2 &p_point, const Variant &p
if (root_node) {
list.push_back(root_node);
} else {
- accept->get_ok()->set_text(TTR("OK :("));
- accept->set_text(TTR("No parent to instance a child at."));
- accept->popup_centered_minsize();
- _remove_preview();
+ drop_pos = p_point;
+ _show_resource_type_selector();
return;
}
}
@@ -4727,15 +4746,7 @@ void CanvasItemEditorViewport::drop_data(const Point2 &p_point, const Variant &p
drop_pos = p_point;
if (is_alt) {
- List<BaseButton *> btn_list;
- button_group->get_buttons(&btn_list);
-
- for (int i = 0; i < btn_list.size(); i++) {
- CheckBox *check = Object::cast_to<CheckBox>(btn_list[i]);
- check->set_pressed(check->get_text() == default_type);
- }
- selector->set_title(vformat(TTR("Add %s"), default_type));
- selector->popup_centered_minsize();
+ _show_resource_type_selector();
} else {
_perform_drop_data();
}
diff --git a/editor/plugins/canvas_item_editor_plugin.h b/editor/plugins/canvas_item_editor_plugin.h
index 457833e1a7..4be09a16e9 100644
--- a/editor/plugins/canvas_item_editor_plugin.h
+++ b/editor/plugins/canvas_item_editor_plugin.h
@@ -578,6 +578,7 @@ class CanvasItemEditorViewport : public Control {
void _create_nodes(Node *parent, Node *child, String &path, const Point2 &p_point);
bool _create_instance(Node *parent, String &path, const Point2 &p_point);
void _perform_drop_data();
+ void _show_resource_type_selector();
static void _bind_methods();