1 /*************************************************************************/
2 /* editor_autoload_settings.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /*************************************************************************/
30
31 #include "editor_autoload_settings.h"
32
33 #include "global_constants.h"
34 #include "globals.h"
35
36 #include "editor_node.h"
37
38 #define PREVIEW_LIST_MAX_SIZE 10
39
_notification(int p_what)40 void EditorAutoloadSettings::_notification(int p_what) {
41
42 if (p_what == NOTIFICATION_ENTER_TREE) {
43
44 List<String> afn;
45 ResourceLoader::get_recognized_extensions_for_type("Script", &afn);
46 ResourceLoader::get_recognized_extensions_for_type("PackedScene", &afn);
47
48 EditorFileDialog *file_dialog = autoload_add_path->get_file_dialog();
49
50 for (List<String>::Element *E = afn.front(); E; E = E->next()) {
51
52 file_dialog->add_filter("*." + E->get());
53 }
54 }
55 }
56
_autoload_name_is_valid(const String & p_name,String * r_error)57 bool EditorAutoloadSettings::_autoload_name_is_valid(const String &p_name, String *r_error) {
58
59 if (!p_name.is_valid_identifier()) {
60 if (r_error)
61 *r_error = TTR("Invalid name.") + "\n" + TTR("Valid characters:") + " a-z, A-Z, 0-9 or _";
62
63 return false;
64 }
65
66 if (ObjectTypeDB::type_exists(p_name)) {
67 if (r_error)
68 *r_error = TTR("Invalid name. Must not collide with an existing engine class name.");
69
70 return false;
71 }
72
73 for (int i = 0; i < Variant::VARIANT_MAX; i++) {
74 if (Variant::get_type_name(Variant::Type(i)) == p_name) {
75 if (r_error)
76 *r_error = TTR("Invalid name. Must not collide with an existing buit-in type name.");
77
78 return false;
79 }
80 }
81
82 for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
83 if (GlobalConstants::get_global_constant_name(i) == p_name) {
84 if (r_error)
85 *r_error = TTR("Invalid name. Must not collide with an existing global constant name.");
86
87 return false;
88 }
89 }
90
91 return true;
92 }
93
_autoload_add()94 void EditorAutoloadSettings::_autoload_add() {
95
96 String name = autoload_add_name->get_text();
97
98 String error;
99 if (!_autoload_name_is_valid(name, &error)) {
100 EditorNode::get_singleton()->show_warning(error);
101 return;
102 }
103
104 String path = autoload_add_path->get_line_edit()->get_text();
105 if (!FileAccess::exists(path)) {
106 EditorNode::get_singleton()->show_warning(TTR("Invalid Path.") + "\n" + TTR("File does not exist."));
107 return;
108 }
109
110 if (!path.begins_with("res://")) {
111 EditorNode::get_singleton()->show_warning(TTR("Invalid Path.") + "\n" + TTR("Not in resource path."));
112 return;
113 }
114
115 name = "autoload/" + name;
116
117 UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
118
119 undo_redo->create_action(TTR("Add AutoLoad"));
120 undo_redo->add_do_property(Globals::get_singleton(), name, "*" + path);
121 undo_redo->add_do_method(Globals::get_singleton(), "set_persisting", name, true);
122
123 if (Globals::get_singleton()->has(name)) {
124 undo_redo->add_undo_property(Globals::get_singleton(), name, Globals::get_singleton()->get(name));
125 } else {
126 undo_redo->add_undo_property(Globals::get_singleton(), name, Variant());
127 }
128
129 undo_redo->add_do_method(this, "update_autoload");
130 undo_redo->add_undo_method(this, "update_autoload");
131
132 undo_redo->add_do_method(this, "emit_signal", autoload_changed);
133 undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
134
135 undo_redo->commit_action();
136
137 autoload_add_path->get_line_edit()->set_text("");
138 autoload_add_name->set_text("");
139 }
140
_autoload_selected()141 void EditorAutoloadSettings::_autoload_selected() {
142
143 TreeItem *ti = tree->get_selected();
144
145 if (!ti)
146 return;
147
148 selected_autoload = "autoload/" + ti->get_text(0);
149 }
150
_autoload_edited()151 void EditorAutoloadSettings::_autoload_edited() {
152
153 if (updating_autoload)
154 return;
155
156 TreeItem *ti = tree->get_edited();
157 int column = tree->get_edited_column();
158
159 UndoRedo *undo_redo = EditorNode::get_undo_redo();
160
161 if (column == 0) {
162 String name = ti->get_text(0);
163 String old_name = selected_autoload.get_slice("/", 1);
164
165 if (name == old_name)
166 return;
167
168 String error;
169 if (!_autoload_name_is_valid(name, &error)) {
170 ti->set_text(0, old_name);
171 EditorNode::get_singleton()->show_warning(error);
172 return;
173 }
174
175 if (Globals::get_singleton()->has("autoload/" + name)) {
176 ti->set_text(0, old_name);
177 EditorNode::get_singleton()->show_warning(vformat(TTR("Autoload '%s' already exists!"), name));
178 return;
179 }
180
181 updating_autoload = true;
182
183 name = "autoload/" + name;
184
185 bool persisting = Globals::get_singleton()->get(selected_autoload);
186 int order = Globals::get_singleton()->get(selected_autoload);
187 String path = Globals::get_singleton()->get(selected_autoload);
188
189 undo_redo->create_action(TTR("Rename Autoload"));
190
191 undo_redo->add_do_property(Globals::get_singleton(), name, path);
192 undo_redo->add_do_method(Globals::get_singleton(), "set_persisting", name, persisting);
193 undo_redo->add_do_method(Globals::get_singleton(), "set_order", name, order);
194 undo_redo->add_do_method(Globals::get_singleton(), "clear", selected_autoload);
195
196 undo_redo->add_undo_property(Globals::get_singleton(), selected_autoload, path);
197 undo_redo->add_undo_method(Globals::get_singleton(), "set_persisting", selected_autoload, persisting);
198 undo_redo->add_undo_method(Globals::get_singleton(), "set_order", selected_autoload, order);
199 undo_redo->add_undo_method(Globals::get_singleton(), "clear", name);
200
201 undo_redo->add_do_method(this, "update_autoload");
202 undo_redo->add_undo_method(this, "update_autoload");
203
204 undo_redo->add_do_method(this, "emit_signal", autoload_changed);
205 undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
206
207 undo_redo->commit_action();
208
209 selected_autoload = name;
210 } else if (column == 2) {
211 updating_autoload = true;
212
213 bool checked = ti->is_checked(2);
214 String base = "autoload/" + ti->get_text(0);
215
216 int order = Globals::get_singleton()->get_order(base);
217 String path = Globals::get_singleton()->get(base);
218
219 if (path.begins_with("*"))
220 path = path.substr(1, path.length());
221
222 if (checked)
223 path = "*" + path;
224
225 undo_redo->create_action(TTR("Toggle AutoLoad Globals"));
226
227 undo_redo->add_do_property(Globals::get_singleton(), base, path);
228 undo_redo->add_undo_property(Globals::get_singleton(), base, Globals::get_singleton()->get(base));
229
230 undo_redo->add_do_method(Globals::get_singleton(), "set_order", base, order);
231 undo_redo->add_undo_method(Globals::get_singleton(), "set_order", base, order);
232
233 undo_redo->add_do_method(this, "update_autoload");
234 undo_redo->add_undo_method(this, "update_autoload");
235
236 undo_redo->add_do_method(this, "emit_signal", autoload_changed);
237 undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
238
239 undo_redo->commit_action();
240 }
241
242 updating_autoload = false;
243 }
244
_autoload_button_pressed(Object * p_item,int p_column,int p_button)245 void EditorAutoloadSettings::_autoload_button_pressed(Object *p_item, int p_column, int p_button) {
246
247 TreeItem *ti = p_item->cast_to<TreeItem>();
248
249 String name = "autoload/" + ti->get_text(0);
250
251 UndoRedo *undo_redo = EditorNode::get_undo_redo();
252
253 switch (p_button) {
254 case BUTTON_OPEN: {
255 _autoload_open(ti->get_text(1));
256 } break;
257 case BUTTON_MOVE_UP:
258 case BUTTON_MOVE_DOWN: {
259
260 TreeItem *swap = NULL;
261
262 if (p_button == BUTTON_MOVE_UP) {
263 swap = ti->get_prev();
264 } else {
265 swap = ti->get_next();
266 }
267
268 if (!swap)
269 return;
270
271 String swap_name = "autoload/" + swap->get_text(0);
272
273 int order = Globals::get_singleton()->get_order(name);
274 int swap_order = Globals::get_singleton()->get_order(swap_name);
275
276 undo_redo->create_action(TTR("Move Autoload"));
277
278 undo_redo->add_do_method(Globals::get_singleton(), "set_order", name, swap_order);
279 undo_redo->add_undo_method(Globals::get_singleton(), "set_order", name, order);
280
281 undo_redo->add_do_method(Globals::get_singleton(), "set_order", swap_name, order);
282 undo_redo->add_undo_method(Globals::get_singleton(), "set_order", swap_name, swap_order);
283
284 undo_redo->add_do_method(this, "update_autoload");
285 undo_redo->add_undo_method(this, "update_autoload");
286
287 undo_redo->add_do_method(this, "emit_signal", autoload_changed);
288 undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
289
290 undo_redo->commit_action();
291 } break;
292 case BUTTON_DELETE: {
293
294 int order = Globals::get_singleton()->get_order(name);
295
296 undo_redo->create_action(TTR("Remove Autoload"));
297
298 undo_redo->add_do_property(Globals::get_singleton(), name, Variant());
299
300 undo_redo->add_undo_property(Globals::get_singleton(), name, Globals::get_singleton()->get(name));
301 undo_redo->add_undo_method(Globals::get_singleton(), "set_persisting", name, true);
302 undo_redo->add_undo_method(Globals::get_singleton(), "set_order", order);
303
304 undo_redo->add_do_method(this, "update_autoload");
305 undo_redo->add_undo_method(this, "update_autoload");
306
307 undo_redo->add_do_method(this, "emit_signal", autoload_changed);
308 undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
309
310 undo_redo->commit_action();
311 } break;
312 }
313 }
314
_autoload_file_callback(const String & p_path)315 void EditorAutoloadSettings::_autoload_file_callback(const String &p_path) {
316
317 autoload_add_name->set_text(p_path.get_file().basename());
318 }
319
_autoload_activated()320 void EditorAutoloadSettings::_autoload_activated() {
321 TreeItem *ti = tree->get_selected();
322 if (!ti)
323 return;
324 _autoload_open(ti->get_text(1));
325 }
326
_autoload_open(const String & fpath)327 void EditorAutoloadSettings::_autoload_open(const String &fpath) {
328 if (ResourceLoader::get_resource_type(fpath) == "PackedScene") {
329 EditorNode::get_singleton()->open_request(fpath);
330 } else {
331 EditorNode::get_singleton()->load_resource(fpath);
332 }
333 ProjectSettings::get_singleton()->hide();
334 }
335
update_autoload()336 void EditorAutoloadSettings::update_autoload() {
337
338 if (updating_autoload)
339 return;
340
341 updating_autoload = true;
342
343 autoload_cache.clear();
344
345 tree->clear();
346 TreeItem *root = tree->create_item();
347
348 List<PropertyInfo> props;
349 Globals::get_singleton()->get_property_list(&props);
350
351 for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
352
353 const PropertyInfo &pi = E->get();
354
355 if (!pi.name.begins_with("autoload/"))
356 continue;
357
358 String name = pi.name.get_slice("/", 1);
359 String path = Globals::get_singleton()->get(pi.name);
360
361 if (name.empty())
362 continue;
363
364 AutoLoadInfo info;
365 info.name = pi.name;
366 info.order = Globals::get_singleton()->get_order(pi.name);
367
368 autoload_cache.push_back(info);
369
370 bool global = false;
371
372 if (path.begins_with("*")) {
373 global = true;
374 path = path.substr(1, path.length());
375 }
376
377 TreeItem *item = tree->create_item(root);
378 item->set_text(0, name);
379 item->set_editable(0, true);
380
381 item->set_text(1, path);
382 item->set_selectable(1, true);
383
384 item->set_cell_mode(2, TreeItem::CELL_MODE_CHECK);
385 item->set_editable(2, true);
386 item->set_text(2, TTR("Enable"));
387 item->set_checked(2, global);
388 item->add_button(3, get_icon("FileList", "EditorIcons"), BUTTON_OPEN);
389 item->add_button(3, get_icon("MoveUp", "EditorIcons"), BUTTON_MOVE_UP);
390 item->add_button(3, get_icon("MoveDown", "EditorIcons"), BUTTON_MOVE_DOWN);
391 item->add_button(3, get_icon("Del", "EditorIcons"), BUTTON_DELETE);
392 item->set_selectable(3, false);
393 }
394
395 updating_autoload = false;
396 }
397
get_drag_data_fw(const Point2 & p_point,Control * p_control)398 Variant EditorAutoloadSettings::get_drag_data_fw(const Point2 &p_point, Control *p_control) {
399
400 if (autoload_cache.size() <= 1)
401 return false;
402
403 StringArray autoloads;
404
405 TreeItem *next = tree->get_next_selected(NULL);
406
407 while (next) {
408 autoloads.push_back(next->get_text(0));
409 next = tree->get_next_selected(next);
410 }
411
412 if (autoloads.size() == 0 || autoloads.size() == autoload_cache.size())
413 return Variant();
414
415 VBoxContainer *preview = memnew(VBoxContainer);
416
417 int max_size = MIN(PREVIEW_LIST_MAX_SIZE, autoloads.size());
418
419 for (int i = 0; i < max_size; i++) {
420 Label *label = memnew(Label(autoloads[i]));
421 label->set_self_opacity(Math::lerp(1, 0, float(i) / PREVIEW_LIST_MAX_SIZE));
422
423 preview->add_child(label);
424 }
425
426 tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
427 tree->set_drag_preview(preview);
428
429 Dictionary drop_data;
430 drop_data["type"] = "autoload";
431 drop_data["autoloads"] = autoloads;
432
433 return drop_data;
434 }
435
can_drop_data_fw(const Point2 & p_point,const Variant & p_data,Control * p_control) const436 bool EditorAutoloadSettings::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_control) const {
437 if (updating_autoload)
438 return false;
439
440 Dictionary drop_data = p_data;
441
442 if (!drop_data.has("type"))
443 return false;
444
445 if (drop_data.has("type")) {
446 TreeItem *ti = tree->get_item_at_pos(p_point);
447
448 if (!ti)
449 return false;
450
451 int section = tree->get_drop_section_at_pos(p_point);
452
453 if (section < -1)
454 return false;
455
456 return true;
457 }
458
459 return false;
460 }
461
drop_data_fw(const Point2 & p_point,const Variant & p_data,Control * p_control)462 void EditorAutoloadSettings::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_control) {
463
464 TreeItem *ti = tree->get_item_at_pos(p_point);
465
466 if (!ti)
467 return;
468
469 int section = tree->get_drop_section_at_pos(p_point);
470
471 if (section < -1)
472 return;
473
474 String name;
475 bool move_to_back = false;
476
477 if (section < 0) {
478 name = ti->get_text(0);
479 } else if (ti->get_next()) {
480 name = ti->get_next()->get_text(0);
481 } else {
482 name = ti->get_text(0);
483 move_to_back = true;
484 }
485
486 int order = Globals::get_singleton()->get_order("autoload/" + name);
487
488 AutoLoadInfo aux;
489 List<AutoLoadInfo>::Element *E = NULL;
490
491 if (!move_to_back) {
492 aux.order = order;
493 E = autoload_cache.find(aux);
494 }
495
496 Dictionary drop_data = p_data;
497 StringArray autoloads = drop_data["autoloads"];
498
499 Vector<int> orders;
500 orders.resize(autoload_cache.size());
501
502 for (int i = 0; i < autoloads.size(); i++) {
503 aux.order = Globals::get_singleton()->get_order("autoload/" + autoloads[i]);
504
505 List<AutoLoadInfo>::Element *I = autoload_cache.find(aux);
506
507 if (move_to_back) {
508 autoload_cache.move_to_back(I);
509 } else if (E != I) {
510 autoload_cache.move_before(I, E);
511 } else if (E->next()) {
512 E = E->next();
513 } else {
514 break;
515 }
516 }
517
518 int i = 0;
519
520 for (List<AutoLoadInfo>::Element *E = autoload_cache.front(); E; E = E->next()) {
521 orders[i++] = E->get().order;
522 }
523
524 orders.sort();
525
526 UndoRedo *undo_redo = EditorNode::get_undo_redo();
527
528 undo_redo->create_action(TTR("Rearrange Autoloads"));
529
530 i = 0;
531
532 for (List<AutoLoadInfo>::Element *E = autoload_cache.front(); E; E = E->next()) {
533 undo_redo->add_do_method(Globals::get_singleton(), "set_order", E->get().name, orders[i++]);
534 undo_redo->add_undo_method(Globals::get_singleton(), "set_order", E->get().name, E->get().order);
535 }
536
537 orders.clear();
538
539 undo_redo->add_do_method(this, "update_autoload");
540 undo_redo->add_undo_method(this, "update_autoload");
541
542 undo_redo->add_do_method(this, "emit_signal", autoload_changed);
543 undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
544
545 undo_redo->commit_action();
546 }
547
_bind_methods()548 void EditorAutoloadSettings::_bind_methods() {
549
550 ObjectTypeDB::bind_method("_autoload_add", &EditorAutoloadSettings::_autoload_add);
551 ObjectTypeDB::bind_method("_autoload_selected", &EditorAutoloadSettings::_autoload_selected);
552 ObjectTypeDB::bind_method("_autoload_edited", &EditorAutoloadSettings::_autoload_edited);
553 ObjectTypeDB::bind_method("_autoload_button_pressed", &EditorAutoloadSettings::_autoload_button_pressed);
554 ObjectTypeDB::bind_method("_autoload_file_callback", &EditorAutoloadSettings::_autoload_file_callback);
555 ObjectTypeDB::bind_method("_autoload_activated", &EditorAutoloadSettings::_autoload_activated);
556 ObjectTypeDB::bind_method("_autoload_open", &EditorAutoloadSettings::_autoload_open);
557
558 ObjectTypeDB::bind_method("get_drag_data_fw", &EditorAutoloadSettings::get_drag_data_fw);
559 ObjectTypeDB::bind_method("can_drop_data_fw", &EditorAutoloadSettings::can_drop_data_fw);
560 ObjectTypeDB::bind_method("drop_data_fw", &EditorAutoloadSettings::drop_data_fw);
561
562 ObjectTypeDB::bind_method("update_autoload", &EditorAutoloadSettings::update_autoload);
563
564 ADD_SIGNAL(MethodInfo("autoload_changed"));
565 }
566
EditorAutoloadSettings()567 EditorAutoloadSettings::EditorAutoloadSettings() {
568
569 autoload_changed = "autoload_changed";
570
571 updating_autoload = false;
572 selected_autoload = "";
573
574 HBoxContainer *hbc = memnew(HBoxContainer);
575 add_child(hbc);
576
577 VBoxContainer *vbc_path = memnew(VBoxContainer);
578 vbc_path->set_h_size_flags(SIZE_EXPAND_FILL);
579
580 autoload_add_path = memnew(EditorLineEditFileChooser);
581 autoload_add_path->set_h_size_flags(SIZE_EXPAND_FILL);
582
583 autoload_add_path->get_file_dialog()->set_mode(EditorFileDialog::MODE_OPEN_FILE);
584 autoload_add_path->get_file_dialog()->connect("file_selected", this, "_autoload_file_callback");
585
586 vbc_path->add_margin_child(TTR("Path:"), autoload_add_path);
587 hbc->add_child(vbc_path);
588
589 VBoxContainer *vbc_name = memnew(VBoxContainer);
590 vbc_name->set_h_size_flags(SIZE_EXPAND_FILL);
591
592 HBoxContainer *hbc_name = memnew(HBoxContainer);
593
594 autoload_add_name = memnew(LineEdit);
595 autoload_add_name->set_h_size_flags(SIZE_EXPAND_FILL);
596 hbc_name->add_child(autoload_add_name);
597
598 Button *add_autoload = memnew(Button);
599 add_autoload->set_text(TTR("Add"));
600 hbc_name->add_child(add_autoload);
601 add_autoload->connect("pressed", this, "_autoload_add");
602
603 vbc_name->add_margin_child(TTR("Node Name:"), hbc_name);
604 hbc->add_child(vbc_name);
605
606 tree = memnew(Tree);
607 tree->set_hide_root(true);
608 tree->set_select_mode(Tree::SELECT_MULTI);
609 tree->set_single_select_cell_editing_only_when_already_selected(true);
610
611 tree->set_drag_forwarding(this);
612
613 tree->set_columns(4);
614 tree->set_column_titles_visible(true);
615
616 tree->set_column_title(0, TTR("Name"));
617 tree->set_column_expand(0, true);
618 tree->set_column_min_width(0, 100);
619
620 tree->set_column_title(1, TTR("Path"));
621 tree->set_column_expand(1, true);
622 tree->set_column_min_width(1, 100);
623
624 tree->set_column_title(2, TTR("Singleton"));
625 tree->set_column_expand(2, false);
626 tree->set_column_min_width(2, 80);
627
628 tree->set_column_expand(3, false);
629 tree->set_column_min_width(3, 120);
630
631 tree->connect("cell_selected", this, "_autoload_selected");
632 tree->connect("item_edited", this, "_autoload_edited");
633 tree->connect("button_pressed", this, "_autoload_button_pressed");
634 tree->connect("item_activated", this, "_autoload_activated");
635
636 add_margin_child(TTR("List:"), tree, true);
637 }
638