summaryrefslogtreecommitdiff
path: root/tools/editor/editor_sub_scene.cpp
blob: 8f1f24f7691160fcb76cf85a41759ab6fe671089 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*************************************************************************/
/*  editor_sub_scene.cpp                                                 */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                    http://www.godotengine.org                         */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur.                 */
/*                                                                       */
/* 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"
#include "scene/gui/margin_container.h"
#include "scene/resources/packed_scene.h"
void EditorSubScene::_path_selected(const String& p_path) {


	path->set_text(p_path);
	_path_changed(p_path);

}

void EditorSubScene::_path_changed(const String& p_path) {

	tree->clear();


	if (scene) {
		memdelete(scene);
		scene=NULL;
	}

	if (p_path=="")
		return;

	Ref<PackedScene> ps = ResourceLoader::load(p_path,"PackedScene");

	if (ps.is_null())
		return;

	scene = ps->instance();
	if (!scene)
		return;

	_fill_tree(scene,NULL);



}

void EditorSubScene::_path_browse() {

	file_dialog->popup_centered_ratio();
}


void EditorSubScene::_notification(int p_what) {

	if (p_what==NOTIFICATION_VISIBILITY_CHANGED) {

		if (!is_visible()) {


		}
	}
}


void EditorSubScene::_fill_tree(Node* p_node,TreeItem *p_parent) {

	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);
	if (has_icon(p_node->get_class(),"EditorIcons")) {
		it->set_icon(0,get_icon(p_node->get_class(),"EditorIcons"));

	}

	for(int i=0;i<p_node->get_child_count();i++) {

		Node *c = p_node->get_child(i);
		if (c->get_owner()!=scene)
			continue;
		_fill_tree(c,it);
	}

}


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();


}


void EditorSubScene::_reown(Node* p_node,List<Node*> *p_to_reown) {

	if (p_node==scene) {

		scene->set_filename("");
		p_to_reown->push_back(p_node);
	} else if (p_node->get_owner()==scene){

		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);
	}
}

void EditorSubScene::move(Node* p_new_parent, Node* p_new_owner) {

	if (!scene) {
		return;
	}
	TreeItem *s = tree->get_selected();
	if (!s) {
		return;
	}

	Node *selnode = s->get_metadata(0);
	if (!selnode) {
		return;
	}

	List<Node*> to_reown;
	_reown(selnode,&to_reown);

	if (selnode!=scene) {
		selnode->get_parent()->remove_child(selnode);
	}

	p_new_parent->add_child(selnode);
	for (List<Node*>::Element *E=to_reown.front();E;E=E->next()) {
		E->get()->set_owner(p_new_owner);
	}

	if (selnode!=scene) {
		memdelete(scene);
	}
	scene=NULL;


	//return selnode;


}

void EditorSubScene::clear() {

	path->set_text("");
	_path_changed("");
}

void EditorSubScene::_bind_methods() {

	ClassDB::bind_method(_MD("_path_selected"),&EditorSubScene::_path_selected);
	ClassDB::bind_method(_MD("_path_changed"),&EditorSubScene::_path_changed);
	ClassDB::bind_method(_MD("_path_browse"),&EditorSubScene::_path_browse);
	ADD_SIGNAL( MethodInfo("subscene_selected"));

}


EditorSubScene::EditorSubScene() {

	scene=NULL;

	set_title(TTR("Select Node(s) to Import"));
	set_hide_on_ok(false);

	VBoxContainer *vb = memnew( VBoxContainer );
	add_child(vb);
//	set_child_rect(vb);

	HBoxContainer *hb = memnew( HBoxContainer );
	path = memnew( LineEdit );
	path->connect("text_entered",this,"_path_changed");
	hb->add_child(path);
	path->set_h_size_flags(SIZE_EXPAND_FILL);
	Button *b = memnew( Button );
	b->set_text(" .. ");
	hb->add_child(b);
	b->connect("pressed",this,"_path_browse");
	vb->add_margin_child(TTR("Scene Path:"),hb);

	tree = memnew( Tree );
	tree->set_v_size_flags(SIZE_EXPAND_FILL);
	vb->add_margin_child(TTR("Import From Node:"),tree,true);
	tree->connect("item_activated",this,"_ok",make_binds(),CONNECT_DEFERRED);

	file_dialog = memnew( EditorFileDialog );
	List<String> extensions;
	ResourceLoader::get_recognized_extensions_for_type("PackedScene",&extensions);

	for(List<String>::Element *E = extensions.front();E;E=E->next() ) {

		file_dialog->add_filter("*."+E->get());
	}

	file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
	add_child(file_dialog);
	file_dialog->connect("file_selected",this,"_path_selected");

}