2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* editor_sub_scene.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 20:16:55 +08:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
2017-01-02 05:01:57 +08:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 06:11:42 +08:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 09:10:30 +08: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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "editor_sub_scene.h"
|
2017-01-16 15:04:19 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
#include "scene/gui/margin_container.h"
|
|
|
|
#include "scene/resources/packed_scene.h"
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void EditorSubScene::_path_selected(const String &p_path) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
path->set_text(p_path);
|
|
|
|
_path_changed(p_path);
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void EditorSubScene::_path_changed(const String &p_path) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
tree->clear();
|
|
|
|
|
|
|
|
if (scene) {
|
|
|
|
memdelete(scene);
|
2017-03-05 23:44:50 +08:00
|
|
|
scene = NULL;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (p_path == "")
|
2014-02-10 09:10:30 +08:00
|
|
|
return;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Ref<PackedScene> ps = ResourceLoader::load(p_path, "PackedScene");
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
if (ps.is_null())
|
|
|
|
return;
|
|
|
|
|
|
|
|
scene = ps->instance();
|
|
|
|
if (!scene)
|
|
|
|
return;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
_fill_tree(scene, NULL);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditorSubScene::_path_browse() {
|
|
|
|
|
|
|
|
file_dialog->popup_centered_ratio();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorSubScene::_notification(int p_what) {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-01-13 21:45:50 +08:00
|
|
|
if (!is_visible_in_tree()) {
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void EditorSubScene::_fill_tree(Node *p_node, TreeItem *p_parent) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
TreeItem *it = tree->create_item(p_parent);
|
2017-03-05 23:44:50 +08: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);
|
|
|
|
if (has_icon(p_node->get_class(), "EditorIcons")) {
|
|
|
|
it->set_icon(0, get_icon(p_node->get_class(), "EditorIcons"));
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < p_node->get_child_count(); i++) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
Node *c = p_node->get_child(i);
|
2017-03-05 23:44:50 +08:00
|
|
|
if (c->get_owner() != scene)
|
2014-02-10 09:10:30 +08:00
|
|
|
continue;
|
2017-03-05 23:44:50 +08:00
|
|
|
_fill_tree(c, it);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorSubScene::ok_pressed() {
|
|
|
|
|
|
|
|
TreeItem *s = tree->get_selected();
|
|
|
|
if (!s)
|
|
|
|
return;
|
|
|
|
Node *selnode = s->get_metadata(0);
|
|
|
|
if (!selnode)
|
|
|
|
return;
|
|
|
|
emit_signal("subscene_selected");
|
|
|
|
hide();
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void EditorSubScene::_reown(Node *p_node, List<Node *> *p_to_reown) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (p_node == scene) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
scene->set_filename("");
|
|
|
|
p_to_reown->push_back(p_node);
|
2017-03-05 23:44:50 +08:00
|
|
|
} else if (p_node->get_owner() == scene) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
p_to_reown->push_back(p_node);
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08: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 09:10:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void EditorSubScene::move(Node *p_new_parent, Node *p_new_owner) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
if (!scene) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
TreeItem *s = tree->get_selected();
|
|
|
|
if (!s) {
|
2016-03-09 07:00:52 +08:00
|
|
|
return;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Node *selnode = s->get_metadata(0);
|
|
|
|
if (!selnode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
List<Node *> to_reown;
|
|
|
|
_reown(selnode, &to_reown);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (selnode != scene) {
|
2014-02-10 09:10:30 +08:00
|
|
|
selnode->get_parent()->remove_child(selnode);
|
|
|
|
}
|
|
|
|
|
|
|
|
p_new_parent->add_child(selnode);
|
2017-03-05 23:44:50 +08:00
|
|
|
for (List<Node *>::Element *E = to_reown.front(); E; E = E->next()) {
|
2014-02-10 09:10:30 +08:00
|
|
|
E->get()->set_owner(p_new_owner);
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (selnode != scene) {
|
2014-02-10 09:10:30 +08:00
|
|
|
memdelete(scene);
|
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
scene = NULL;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
//return selnode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorSubScene::clear() {
|
|
|
|
|
|
|
|
path->set_text("");
|
|
|
|
_path_changed("");
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorSubScene::_bind_methods() {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("_path_selected"), &EditorSubScene::_path_selected);
|
|
|
|
ClassDB::bind_method(D_METHOD("_path_changed"), &EditorSubScene::_path_changed);
|
|
|
|
ClassDB::bind_method(D_METHOD("_path_browse"), &EditorSubScene::_path_browse);
|
|
|
|
ADD_SIGNAL(MethodInfo("subscene_selected"));
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
EditorSubScene::EditorSubScene() {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
scene = NULL;
|
2015-12-14 19:43:56 +08:00
|
|
|
|
2016-05-19 06:08:12 +08:00
|
|
|
set_title(TTR("Select Node(s) to Import"));
|
2015-12-14 19:43:56 +08:00
|
|
|
set_hide_on_ok(false);
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
VBoxContainer *vb = memnew(VBoxContainer);
|
2014-02-10 09:10:30 +08:00
|
|
|
add_child(vb);
|
2017-01-14 19:26:56 +08:00
|
|
|
//set_child_rect(vb);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
HBoxContainer *hb = memnew(HBoxContainer);
|
|
|
|
path = memnew(LineEdit);
|
|
|
|
path->connect("text_entered", this, "_path_changed");
|
2014-02-10 09:10:30 +08:00
|
|
|
hb->add_child(path);
|
|
|
|
path->set_h_size_flags(SIZE_EXPAND_FILL);
|
2017-03-05 23:44:50 +08:00
|
|
|
Button *b = memnew(Button);
|
2014-02-10 09:10:30 +08:00
|
|
|
b->set_text(" .. ");
|
|
|
|
hb->add_child(b);
|
2017-03-05 23:44:50 +08:00
|
|
|
b->connect("pressed", this, "_path_browse");
|
|
|
|
vb->add_margin_child(TTR("Scene Path:"), hb);
|
2015-12-14 19:43:56 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
tree = memnew(Tree);
|
2014-02-10 09:10:30 +08:00
|
|
|
tree->set_v_size_flags(SIZE_EXPAND_FILL);
|
2017-03-05 23:44:50 +08:00
|
|
|
vb->add_margin_child(TTR("Import From Node:"), tree, true);
|
|
|
|
tree->connect("item_activated", this, "_ok", make_binds(), CONNECT_DEFERRED);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
file_dialog = memnew(EditorFileDialog);
|
2014-02-10 09:10:30 +08:00
|
|
|
List<String> extensions;
|
2017-03-05 23:44:50 +08:00
|
|
|
ResourceLoader::get_recognized_extensions_for_type("PackedScene", &extensions);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
file_dialog->add_filter("*." + E->get());
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2015-06-06 20:44:38 +08:00
|
|
|
file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
|
2014-02-10 09:10:30 +08:00
|
|
|
add_child(file_dialog);
|
2017-03-05 23:44:50 +08:00
|
|
|
file_dialog->connect("file_selected", this, "_path_selected");
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|