2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* editor_sub_scene.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2020-01-01 11:16:22 +01:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01: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. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "editor_sub_scene.h"
|
2017-01-16 08:04:19 +01:00
|
|
|
|
2018-09-02 23:40:51 +02:00
|
|
|
#include "editor/editor_node.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "scene/gui/margin_container.h"
|
|
|
|
#include "scene/resources/packed_scene.h"
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void EditorSubScene::_path_selected(const String &p_path) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
path->set_text(p_path);
|
|
|
|
_path_changed(p_path);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void EditorSubScene::_path_changed(const String &p_path) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
tree->clear();
|
|
|
|
|
|
|
|
if (scene) {
|
|
|
|
memdelete(scene);
|
2017-03-05 16:44:50 +01:00
|
|
|
scene = NULL;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_path == "")
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Ref<PackedScene> ps = ResourceLoader::load(p_path, "PackedScene");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (ps.is_null())
|
|
|
|
return;
|
|
|
|
|
|
|
|
scene = ps->instance();
|
|
|
|
if (!scene)
|
|
|
|
return;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_fill_tree(scene, NULL);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorSubScene::_path_browse() {
|
|
|
|
|
|
|
|
file_dialog->popup_centered_ratio();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorSubScene::_notification(int p_what) {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-06 18:00:16 +01:00
|
|
|
if (is_visible() && scene == NULL) {
|
2019-04-22 03:29:45 +02:00
|
|
|
_path_browse();
|
2020-03-06 18:00:16 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void EditorSubScene::_fill_tree(Node *p_node, TreeItem *p_parent) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
TreeItem *it = tree->create_item(p_parent);
|
2017-03-05 16:44:50 +01:00
|
|
|
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 23:40:51 +02:00
|
|
|
it->set_icon(0, EditorNode::get_singleton()->get_object_icon(p_node, "Node"));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < p_node->get_child_count(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Node *c = p_node->get_child(i);
|
2017-03-05 16:44:50 +01:00
|
|
|
if (c->get_owner() != scene)
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
2017-03-05 16:44:50 +01:00
|
|
|
_fill_tree(c, it);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-12 13:55:16 +01:00
|
|
|
void EditorSubScene::_selected_changed() {
|
2019-10-09 04:21:11 +02:00
|
|
|
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 13:55:16 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-12-12 13:55:16 +01: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);
|
2019-10-09 04:21:11 +02:00
|
|
|
} else {
|
|
|
|
List<Node *>::Element *E = selection.find(n);
|
|
|
|
|
|
|
|
if (E)
|
|
|
|
selection.erase(E);
|
2017-12-12 13:55:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-27 22:49:16 +01:00
|
|
|
void EditorSubScene::_item_activated() {
|
|
|
|
_ok_pressed(); // From AcceptDialog.
|
|
|
|
}
|
|
|
|
|
2018-02-01 09:57:10 +01:00
|
|
|
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 13:55:16 +01: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 02:10:30 +01:00
|
|
|
return;
|
2017-12-12 13:55:16 +01:00
|
|
|
}
|
|
|
|
for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
|
|
|
|
Node *c = E->get();
|
|
|
|
_remove_selection_child(c);
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
emit_signal("subscene_selected");
|
|
|
|
hide();
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void EditorSubScene::_reown(Node *p_node, List<Node *> *p_to_reown) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_node == scene) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
scene->set_filename("");
|
|
|
|
p_to_reown->push_back(p_node);
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (p_node->get_owner() == scene) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
p_to_reown->push_back(p_node);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
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 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void EditorSubScene::move(Node *p_new_parent, Node *p_new_owner) {
|
2014-02-10 02:10:30 +01:00
|
|
|
if (!scene) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-12 13:55:16 +01:00
|
|
|
if (selection.size() <= 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-12 13:55:16 +01: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 02:10:30 +01:00
|
|
|
|
2017-12-12 13:55:16 +01:00
|
|
|
p_new_parent->add_child(selnode);
|
2019-02-12 21:10:08 +01:00
|
|
|
for (List<Node *>::Element *F = to_reown.front(); F; F = F->next()) {
|
|
|
|
F->get()->set_owner(p_new_owner);
|
2017-12-12 13:55:16 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-12-12 13:55:16 +01:00
|
|
|
if (!is_root) {
|
2014-02-10 02:10:30 +01:00
|
|
|
memdelete(scene);
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
scene = NULL;
|
2014-02-10 02:10:30 +01:00
|
|
|
//return selnode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorSubScene::clear() {
|
|
|
|
|
|
|
|
path->set_text("");
|
|
|
|
_path_changed("");
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorSubScene::_bind_methods() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_SIGNAL(MethodInfo("subscene_selected"));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
EditorSubScene::EditorSubScene() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
scene = NULL;
|
2017-12-12 13:55:16 +01:00
|
|
|
is_root = false;
|
2015-12-14 12:43:56 +01:00
|
|
|
|
2016-05-19 00:08:12 +02:00
|
|
|
set_title(TTR("Select Node(s) to Import"));
|
2015-12-14 12:43:56 +01:00
|
|
|
set_hide_on_ok(false);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
VBoxContainer *vb = memnew(VBoxContainer);
|
2014-02-10 02:10:30 +01:00
|
|
|
add_child(vb);
|
2017-01-14 12:26:56 +01:00
|
|
|
//set_child_rect(vb);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
HBoxContainer *hb = memnew(HBoxContainer);
|
|
|
|
path = memnew(LineEdit);
|
2020-02-21 18:28:45 +01:00
|
|
|
path->connect("text_entered", callable_mp(this, &EditorSubScene::_path_changed));
|
2014-02-10 02:10:30 +01:00
|
|
|
hb->add_child(path);
|
2020-03-06 18:00:16 +01:00
|
|
|
path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
|
2017-03-05 16:44:50 +01:00
|
|
|
Button *b = memnew(Button);
|
2019-04-22 03:29:45 +02:00
|
|
|
b->set_text(TTR("Browse"));
|
2014-02-10 02:10:30 +01:00
|
|
|
hb->add_child(b);
|
2020-02-21 18:28:45 +01:00
|
|
|
b->connect("pressed", callable_mp(this, &EditorSubScene::_path_browse));
|
2017-03-05 16:44:50 +01:00
|
|
|
vb->add_margin_child(TTR("Scene Path:"), hb);
|
2015-12-14 12:43:56 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
tree = memnew(Tree);
|
2020-03-06 18:00:16 +01:00
|
|
|
tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
|
2017-03-05 16:44:50 +01:00
|
|
|
vb->add_margin_child(TTR("Import From Node:"), tree, true);
|
2017-12-12 13:55:16 +01:00
|
|
|
tree->set_select_mode(Tree::SELECT_MULTI);
|
2020-02-21 18:28:45 +01:00
|
|
|
tree->connect("multi_selected", callable_mp(this, &EditorSubScene::_item_multi_selected));
|
2017-12-12 13:55:16 +01:00
|
|
|
//tree->connect("nothing_selected", this, "_deselect_items");
|
2020-02-21 18:28:45 +01:00
|
|
|
tree->connect("cell_selected", callable_mp(this, &EditorSubScene::_selected_changed));
|
2017-12-12 13:55:16 +01:00
|
|
|
|
2020-02-27 22:49:16 +01:00
|
|
|
tree->connect("item_activated", callable_mp(this, &EditorSubScene::_item_activated), make_binds(), CONNECT_DEFERRED);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
file_dialog = memnew(EditorFileDialog);
|
2014-02-10 02:10:30 +01:00
|
|
|
List<String> extensions;
|
2017-03-05 16:44:50 +01:00
|
|
|
ResourceLoader::get_recognized_extensions_for_type("PackedScene", &extensions);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
file_dialog->add_filter("*." + E->get());
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-03-06 18:00:16 +01:00
|
|
|
file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
|
2014-02-10 02:10:30 +01:00
|
|
|
add_child(file_dialog);
|
2020-02-21 18:28:45 +01:00
|
|
|
file_dialog->connect("file_selected", callable_mp(this, &EditorSubScene::_path_selected));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|