godot/editor/editor_sub_scene.cpp

273 lines
7.6 KiB
C++
Raw Normal View History

2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* editor_sub_scene.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
2014-02-10 01:10:30 +00:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2014-02-10 01:10:30 +00:00
#include "editor_sub_scene.h"
2018-09-02 21:40:51 +00:00
#include "editor/editor_node.h"
2014-02-10 01:10:30 +00:00
#include "scene/gui/margin_container.h"
#include "scene/resources/packed_scene.h"
void EditorSubScene::_path_selected(const String &p_path) {
2014-02-10 01:10:30 +00:00
path->set_text(p_path);
_path_changed(p_path);
}
void EditorSubScene::_path_changed(const String &p_path) {
2014-02-10 01:10:30 +00:00
tree->clear();
if (scene) {
memdelete(scene);
2020-04-01 23:20:12 +00:00
scene = nullptr;
2014-02-10 01:10:30 +00:00
}
if (p_path == "")
2014-02-10 01:10:30 +00:00
return;
Ref<PackedScene> ps = ResourceLoader::load(p_path, "PackedScene");
2014-02-10 01:10:30 +00:00
if (ps.is_null())
return;
scene = ps->instance();
if (!scene)
return;
2020-04-01 23:20:12 +00:00
_fill_tree(scene, nullptr);
2014-02-10 01:10:30 +00:00
}
void EditorSubScene::_path_browse() {
file_dialog->popup_centered_ratio();
}
void EditorSubScene::_notification(int p_what) {
if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
2014-02-10 01:10:30 +00:00
2020-04-01 23:20:12 +00:00
if (is_visible() && scene == nullptr) {
2019-04-22 01:29:45 +00:00
_path_browse();
}
2014-02-10 01:10:30 +00:00
}
}
void EditorSubScene::_fill_tree(Node *p_node, TreeItem *p_parent) {
2014-02-10 01:10:30 +00:00
TreeItem *it = tree->create_item(p_parent);
it->set_metadata(0, p_node);
it->set_text(0, p_node->get_name());
it->set_editable(0, false);
it->set_selectable(0, true);
2018-09-02 21:40:51 +00:00
it->set_icon(0, EditorNode::get_singleton()->get_object_icon(p_node, "Node"));
2014-02-10 01:10:30 +00:00
for (int i = 0; i < p_node->get_child_count(); i++) {
2014-02-10 01:10:30 +00:00
Node *c = p_node->get_child(i);
if (c->get_owner() != scene)
2014-02-10 01:10:30 +00:00
continue;
_fill_tree(c, it);
2014-02-10 01:10:30 +00:00
}
}
2017-12-12 12:55:16 +00:00
void EditorSubScene::_selected_changed() {
TreeItem *item = tree->get_selected();
ERR_FAIL_COND(!item);
Node *n = item->get_metadata(0);
if (!n || !selection.find(n)) {
selection.clear();
is_root = false;
}
2017-12-12 12:55:16 +00:00
}
2014-02-10 01:10:30 +00:00
2017-12-12 12:55:16 +00:00
void EditorSubScene::_item_multi_selected(Object *p_object, int p_cell, bool p_selected) {
if (!is_root) {
TreeItem *item = Object::cast_to<TreeItem>(p_object);
ERR_FAIL_COND(!item);
Node *n = item->get_metadata(0);
if (!n)
return;
if (p_selected) {
if (n == scene) {
is_root = true;
selection.clear();
}
selection.push_back(n);
} else {
List<Node *>::Element *E = selection.find(n);
if (E)
selection.erase(E);
2017-12-12 12:55:16 +00:00
}
}
}
void EditorSubScene::_item_activated() {
_ok_pressed(); // From AcceptDialog.
}
void EditorSubScene::_remove_selection_child(Node *p_node) {
if (p_node->get_child_count() > 0) {
for (int i = 0; i < p_node->get_child_count(); i++) {
Node *c = p_node->get_child(i);
2017-12-12 12:55:16 +00:00
List<Node *>::Element *E = selection.find(c);
if (E) {
selection.move_to_back(E);
selection.pop_back();
}
if (c->get_child_count() > 0) {
_remove_selection_child(c);
}
}
}
}
void EditorSubScene::ok_pressed() {
if (selection.size() <= 0) {
2014-02-10 01:10:30 +00:00
return;
2017-12-12 12:55:16 +00:00
}
for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
Node *c = E->get();
_remove_selection_child(c);
}
2014-02-10 01:10:30 +00:00
emit_signal("subscene_selected");
hide();
clear();
}
void EditorSubScene::_reown(Node *p_node, List<Node *> *p_to_reown) {
2014-02-10 01:10:30 +00:00
if (p_node == scene) {
2014-02-10 01:10:30 +00:00
scene->set_filename("");
p_to_reown->push_back(p_node);
} else if (p_node->get_owner() == scene) {
2014-02-10 01:10:30 +00:00
p_to_reown->push_back(p_node);
}
for (int i = 0; i < p_node->get_child_count(); i++) {
Node *c = p_node->get_child(i);
_reown(c, p_to_reown);
2014-02-10 01:10:30 +00:00
}
}
void EditorSubScene::move(Node *p_new_parent, Node *p_new_owner) {
2014-02-10 01:10:30 +00:00
if (!scene) {
return;
}
2017-12-12 12:55:16 +00:00
if (selection.size() <= 0) {
2014-02-10 01:10:30 +00:00
return;
}
2017-12-12 12:55:16 +00:00
for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
Node *selnode = E->get();
if (!selnode) {
return;
}
List<Node *> to_reown;
_reown(selnode, &to_reown);
if (selnode != scene) {
selnode->get_parent()->remove_child(selnode);
}
2014-02-10 01:10:30 +00:00
2017-12-12 12:55:16 +00:00
p_new_parent->add_child(selnode);
for (List<Node *>::Element *F = to_reown.front(); F; F = F->next()) {
F->get()->set_owner(p_new_owner);
2017-12-12 12:55:16 +00:00
}
2014-02-10 01:10:30 +00:00
}
2017-12-12 12:55:16 +00:00
if (!is_root) {
2014-02-10 01:10:30 +00:00
memdelete(scene);
}
2020-04-01 23:20:12 +00:00
scene = nullptr;
2014-02-10 01:10:30 +00:00
//return selnode;
}
void EditorSubScene::clear() {
path->set_text("");
_path_changed("");
}
void EditorSubScene::_bind_methods() {
ADD_SIGNAL(MethodInfo("subscene_selected"));
2014-02-10 01:10:30 +00:00
}
EditorSubScene::EditorSubScene() {
2020-04-01 23:20:12 +00:00
scene = nullptr;
2017-12-12 12:55:16 +00:00
is_root = false;
set_title(TTR("Select Node(s) to Import"));
set_hide_on_ok(false);
VBoxContainer *vb = memnew(VBoxContainer);
2014-02-10 01:10:30 +00:00
add_child(vb);
//set_child_rect(vb);
2014-02-10 01:10:30 +00:00
HBoxContainer *hb = memnew(HBoxContainer);
path = memnew(LineEdit);
path->connect("text_entered", callable_mp(this, &EditorSubScene::_path_changed));
2014-02-10 01:10:30 +00:00
hb->add_child(path);
path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
Button *b = memnew(Button);
2019-04-22 01:29:45 +00:00
b->set_text(TTR("Browse"));
2014-02-10 01:10:30 +00:00
hb->add_child(b);
b->connect("pressed", callable_mp(this, &EditorSubScene::_path_browse));
vb->add_margin_child(TTR("Scene Path:"), hb);
tree = memnew(Tree);
tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
vb->add_margin_child(TTR("Import From Node:"), tree, true);
2017-12-12 12:55:16 +00:00
tree->set_select_mode(Tree::SELECT_MULTI);
tree->connect("multi_selected", callable_mp(this, &EditorSubScene::_item_multi_selected));
2017-12-12 12:55:16 +00:00
//tree->connect("nothing_selected", this, "_deselect_items");
tree->connect("cell_selected", callable_mp(this, &EditorSubScene::_selected_changed));
2017-12-12 12:55:16 +00:00
tree->connect("item_activated", callable_mp(this, &EditorSubScene::_item_activated), make_binds(), CONNECT_DEFERRED);
2014-02-10 01:10:30 +00:00
file_dialog = memnew(EditorFileDialog);
2014-02-10 01:10:30 +00:00
List<String> extensions;
ResourceLoader::get_recognized_extensions_for_type("PackedScene", &extensions);
2014-02-10 01:10:30 +00:00
for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
2014-02-10 01:10:30 +00:00
file_dialog->add_filter("*." + E->get());
2014-02-10 01:10:30 +00:00
}
file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
2014-02-10 01:10:30 +00:00
add_child(file_dialog);
file_dialog->connect("file_selected", callable_mp(this, &EditorSubScene::_path_selected));
2014-02-10 01:10:30 +00:00
}