Merge pull request #51035 from foxydevloper/drag-drop-more-support

Improve drag and dropping files into viewport by supporting more types
This commit is contained in:
Rémi Verschelde 2021-08-13 14:57:10 +02:00 committed by GitHub
commit 62c6347a27
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 62 deletions

View file

@ -41,6 +41,7 @@
#include "editor/editor_settings.h"
#include "editor/plugins/animation_player_editor_plugin.h"
#include "editor/plugins/script_editor_plugin.h"
#include "scene/2d/cpu_particles_2d.h"
#include "scene/2d/gpu_particles_2d.h"
#include "scene/2d/light_2d.h"
#include "scene/2d/polygon_2d.h"
@ -5740,7 +5741,7 @@ void CanvasItemEditorViewport::_on_change_type_confirmed() {
}
CheckBox *check = Object::cast_to<CheckBox>(button_group->get_pressed_button());
default_type = check->get_text();
default_texture_node_type = check->get_text();
_perform_drop_data();
selector->hide();
}
@ -5832,14 +5833,13 @@ void CanvasItemEditorViewport::_create_nodes(Node *parent, Node *child, String &
child->set_name(name);
Ref<Texture2D> texture = Ref<Texture2D>(Object::cast_to<Texture2D>(ResourceCache::get(path)));
Size2 texture_size = texture->get_size();
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.
} else { // If no parent is selected, set as root node of the scene.
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);
@ -5853,28 +5853,23 @@ void CanvasItemEditorViewport::_create_nodes(Node *parent, Node *child, String &
editor_data->get_undo_redo().add_undo_method(ed, "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";
List<PropertyInfo> props;
child->get_property_list(&props);
for (const PropertyInfo &E : props) {
if (E.name == "config/texture") { // Particles2D
property = "config/texture";
break;
} else if (E.name == "texture/texture") { // Polygon2D
property = "texture/texture";
break;
} else if (E.name == "normal") { // TouchScreenButton
property = "normal";
break;
}
String node_class = child->get_class();
if (node_class == "Polygon2D") {
editor_data->get_undo_redo().add_do_property(child, "texture/texture", texture);
} else if (node_class == "TouchScreenButton") {
editor_data->get_undo_redo().add_do_property(child, "normal", texture);
} else if (node_class == "TextureButton") {
editor_data->get_undo_redo().add_do_property(child, "texture_button", texture);
} else {
editor_data->get_undo_redo().add_do_property(child, "texture", texture);
}
editor_data->get_undo_redo().add_do_property(child, property, texture);
// make visible for certain node type
if (default_type == "NinePatchRect") {
editor_data->get_undo_redo().add_do_property(child, "rect/size", texture_size);
} else if (default_type == "Polygon2D") {
if (ClassDB::is_parent_class(node_class, "Control")) {
Size2 texture_size = texture->get_size();
editor_data->get_undo_redo().add_do_property(child, "rect_size", texture_size);
} else if (node_class == "Polygon2D") {
Size2 texture_size = texture->get_size();
Vector<Vector2> list;
list.push_back(Vector2(0, 0));
list.push_back(Vector2(texture_size.width, 0));
@ -5975,23 +5970,7 @@ void CanvasItemEditorViewport::_perform_drop_data() {
} else {
Ref<Texture2D> texture = Ref<Texture2D>(Object::cast_to<Texture2D>(*res));
if (texture != nullptr && texture.is_valid()) {
Node *child;
if (default_type == "Light2D") {
child = memnew(Light2D);
} else if (default_type == "GPUParticles2D") {
child = memnew(GPUParticles2D);
} else if (default_type == "Polygon2D") {
child = memnew(Polygon2D);
} else if (default_type == "TouchScreenButton") {
child = memnew(TouchScreenButton);
} else if (default_type == "TextureRect") {
child = memnew(TextureRect);
} else if (default_type == "NinePatchRect") {
child = memnew(NinePatchRect);
} else {
child = memnew(Sprite2D); // default
}
Node *child = _make_texture_node_type(default_texture_node_type);
_create_nodes(target_node, child, path, drop_pos);
}
}
@ -6029,13 +6008,7 @@ bool CanvasItemEditorViewport::can_drop_data(const Point2 &p_point, const Varian
continue;
}
memdelete(instantiated_scene);
} else if (type == "Texture2D" ||
type == "ImageTexture" ||
type == "ViewportTexture" ||
type == "CurveTexture" ||
type == "GradientTexture" ||
type == "StreamTexture2D" ||
type == "AtlasTexture") {
} else if (ClassDB::is_parent_class(type, "Texture2D")) {
Ref<Texture2D> texture = Ref<Texture2D>(Object::cast_to<Texture2D>(*res));
if (!texture.is_valid()) {
continue;
@ -6052,7 +6025,7 @@ bool CanvasItemEditorViewport::can_drop_data(const Point2 &p_point, const Varian
}
Transform2D trans = canvas_item_editor->get_canvas_transform();
preview_node->set_position((p_point - trans.get_origin()) / trans.get_scale().x);
label->set_text(vformat(TTR("Adding %s..."), default_type));
label->set_text(vformat(TTR("Adding %s..."), default_texture_node_type));
}
return can_instantiate;
}
@ -6068,9 +6041,9 @@ void CanvasItemEditorViewport::_show_resource_type_selector() {
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);
check->set_pressed(check->get_text() == default_texture_node_type);
}
selector->set_title(vformat(TTR("Add %s"), default_type));
selector->set_title(vformat(TTR("Add %s"), default_texture_node_type));
selector->popup_centered();
}
@ -6126,6 +6099,30 @@ void CanvasItemEditorViewport::drop_data(const Point2 &p_point, const Variant &p
}
}
Node *CanvasItemEditorViewport::_make_texture_node_type(String texture_node_type) {
Node *node = nullptr;
if (texture_node_type == "Sprite2D") {
node = memnew(Sprite2D);
} else if (texture_node_type == "PointLight2D") {
node = memnew(PointLight2D);
} else if (texture_node_type == "CPUParticles2D") {
node = memnew(CPUParticles2D);
} else if (texture_node_type == "GPUParticles2D") {
node = memnew(GPUParticles2D);
} else if (texture_node_type == "Polygon2D") {
node = memnew(Polygon2D);
} else if (texture_node_type == "TouchScreenButton") {
node = memnew(TouchScreenButton);
} else if (texture_node_type == "TextureRect") {
node = memnew(TextureRect);
} else if (texture_node_type == "TextureButton") {
node = memnew(TextureButton);
} else if (texture_node_type == "NinePatchRect") {
node = memnew(NinePatchRect);
}
return node;
}
void CanvasItemEditorViewport::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
@ -6145,16 +6142,18 @@ void CanvasItemEditorViewport::_bind_methods() {
}
CanvasItemEditorViewport::CanvasItemEditorViewport(EditorNode *p_node, CanvasItemEditor *p_canvas_item_editor) {
default_type = "Sprite2D";
default_texture_node_type = "Sprite2D";
// Node2D
types.push_back("Sprite2D");
types.push_back("Light2D");
types.push_back("GPUParticles2D");
types.push_back("Polygon2D");
types.push_back("TouchScreenButton");
texture_node_types.push_back("Sprite2D");
texture_node_types.push_back("PointLight2D");
texture_node_types.push_back("CPUParticles2D");
texture_node_types.push_back("GPUParticles2D");
texture_node_types.push_back("Polygon2D");
texture_node_types.push_back("TouchScreenButton");
// Control
types.push_back("TextureRect");
types.push_back("NinePatchRect");
texture_node_types.push_back("TextureRect");
texture_node_types.push_back("TextureButton");
texture_node_types.push_back("NinePatchRect");
target_node = nullptr;
editor = p_node;
@ -6182,10 +6181,10 @@ CanvasItemEditorViewport::CanvasItemEditorViewport(EditorNode *p_node, CanvasIte
btn_group->set_h_size_flags(0);
button_group.instantiate();
for (int i = 0; i < types.size(); i++) {
for (int i = 0; i < texture_node_types.size(); i++) {
CheckBox *check = memnew(CheckBox);
btn_group->add_child(check);
check->set_text(types[i]);
check->set_text(texture_node_types[i]);
check->connect("button_down", callable_mp(this, &CanvasItemEditorViewport::_on_select_type), varray(check));
check->set_button_group(button_group);
}

View file

@ -639,8 +639,10 @@ public:
class CanvasItemEditorViewport : public Control {
GDCLASS(CanvasItemEditorViewport, Control);
String default_type;
Vector<String> types;
// The type of node that will be created when dropping texture into the viewport.
String default_texture_node_type;
// Node types that are available to select from when dropping texture into viewport.
Vector<String> texture_node_types;
Vector<String> selected_files;
Node *target_node;
@ -662,6 +664,7 @@ class CanvasItemEditorViewport : public Control {
void _on_select_type(Object *selected);
void _on_change_type_confirmed();
void _on_change_type_closed();
Node *_make_texture_node_type(String texture_node_type);
void _create_preview(const Vector<String> &files) const;
void _remove_preview();

View file

@ -4030,7 +4030,7 @@ bool Node3DEditorViewport::can_drop_data_fw(const Point2 &p_point, const Variant
continue;
}
memdelete(instantiated_scene);
} else if (type == "Mesh" || type == "ArrayMesh" || type == "PrimitiveMesh") {
} else if (ClassDB::is_parent_class(type, "Mesh")) {
Ref<Mesh> mesh = ResourceLoader::load(files[i]);
if (!mesh.is_valid()) {
continue;