1 | /**************************************************************************/ |
2 | /* create_dialog.cpp */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
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 "create_dialog.h" |
32 | |
33 | #include "core/object/class_db.h" |
34 | #include "core/os/keyboard.h" |
35 | #include "editor/editor_feature_profile.h" |
36 | #include "editor/editor_node.h" |
37 | #include "editor/editor_paths.h" |
38 | #include "editor/editor_scale.h" |
39 | #include "editor/editor_settings.h" |
40 | #include "editor/editor_string_names.h" |
41 | |
42 | void CreateDialog::(bool p_dont_clear, bool p_replace_mode, const String &p_current_type, const String &p_current_name) { |
43 | _fill_type_list(); |
44 | |
45 | icon_fallback = search_options->has_theme_icon(base_type, EditorStringName(EditorIcons)) ? base_type : "Object" ; |
46 | |
47 | if (p_dont_clear) { |
48 | search_box->select_all(); |
49 | } else { |
50 | search_box->clear(); |
51 | } |
52 | |
53 | if (p_replace_mode) { |
54 | search_box->set_text(p_current_type); |
55 | } |
56 | |
57 | search_box->grab_focus(); |
58 | _update_search(); |
59 | |
60 | if (p_replace_mode) { |
61 | set_title(vformat(TTR("Change Type of \"%s\"" ), p_current_name)); |
62 | set_ok_button_text(TTR("Change" )); |
63 | } else { |
64 | set_title(vformat(TTR("Create New %s" ), base_type)); |
65 | set_ok_button_text(TTR("Create" )); |
66 | } |
67 | |
68 | _load_favorites_and_history(); |
69 | _save_and_update_favorite_list(); |
70 | |
71 | // Restore valid window bounds or pop up at default size. |
72 | Rect2 saved_size = EditorSettings::get_singleton()->get_project_metadata("dialog_bounds" , "create_new_node" , Rect2()); |
73 | if (saved_size != Rect2()) { |
74 | popup(saved_size); |
75 | } else { |
76 | popup_centered_clamped(Size2(900, 700) * EDSCALE, 0.8); |
77 | } |
78 | } |
79 | |
80 | void CreateDialog::_fill_type_list() { |
81 | List<StringName> complete_type_list; |
82 | ClassDB::get_class_list(&complete_type_list); |
83 | ScriptServer::get_global_class_list(&complete_type_list); |
84 | |
85 | EditorData &ed = EditorNode::get_editor_data(); |
86 | |
87 | for (List<StringName>::Element *I = complete_type_list.front(); I; I = I->next()) { |
88 | String type = I->get(); |
89 | if (!_should_hide_type(type)) { |
90 | type_list.push_back(type); |
91 | |
92 | if (!ed.get_custom_types().has(type)) { |
93 | continue; |
94 | } |
95 | |
96 | const Vector<EditorData::CustomType> &ct = ed.get_custom_types()[type]; |
97 | for (int i = 0; i < ct.size(); i++) { |
98 | custom_type_parents[ct[i].name] = type; |
99 | custom_type_indices[ct[i].name] = i; |
100 | type_list.push_back(ct[i].name); |
101 | } |
102 | } |
103 | } |
104 | type_list.sort_custom<StringName::AlphCompare>(); |
105 | } |
106 | |
107 | bool CreateDialog::_is_type_preferred(const String &p_type) const { |
108 | if (ClassDB::class_exists(p_type)) { |
109 | return ClassDB::is_parent_class(p_type, preferred_search_result_type); |
110 | } |
111 | |
112 | return EditorNode::get_editor_data().script_class_is_parent(p_type, preferred_search_result_type); |
113 | } |
114 | |
115 | bool CreateDialog::_is_class_disabled_by_feature_profile(const StringName &p_class) const { |
116 | Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile(); |
117 | |
118 | return !profile.is_null() && profile->is_class_disabled(p_class); |
119 | } |
120 | |
121 | bool CreateDialog::_should_hide_type(const String &p_type) const { |
122 | if (_is_class_disabled_by_feature_profile(p_type)) { |
123 | return true; |
124 | } |
125 | |
126 | if (is_base_type_node && p_type.begins_with("Editor" )) { |
127 | return true; // Do not show editor nodes. |
128 | } |
129 | |
130 | if (ClassDB::class_exists(p_type)) { |
131 | if (!ClassDB::can_instantiate(p_type) || ClassDB::is_virtual(p_type)) { |
132 | return true; // Can't create abstract or virtual class. |
133 | } |
134 | |
135 | if (!ClassDB::is_parent_class(p_type, base_type)) { |
136 | return true; // Wrong inheritance. |
137 | } |
138 | |
139 | if (!ClassDB::is_class_exposed(p_type)) { |
140 | return true; // Unexposed types. |
141 | } |
142 | |
143 | for (const StringName &E : type_blacklist) { |
144 | if (ClassDB::is_parent_class(p_type, E)) { |
145 | return true; // Parent type is blacklisted. |
146 | } |
147 | } |
148 | } else { |
149 | if (!EditorNode::get_editor_data().script_class_is_parent(p_type, base_type)) { |
150 | return true; // Wrong inheritance. |
151 | } |
152 | if (!ScriptServer::is_global_class(p_type)) { |
153 | return true; |
154 | } |
155 | |
156 | StringName native_type = ScriptServer::get_global_class_native_base(p_type); |
157 | if (ClassDB::class_exists(native_type) && !ClassDB::can_instantiate(native_type)) { |
158 | return true; |
159 | } |
160 | |
161 | String script_path = ScriptServer::get_global_class_path(p_type); |
162 | if (script_path.begins_with("res://addons/" )) { |
163 | if (!EditorNode::get_singleton()->is_addon_plugin_enabled(script_path.get_slicec('/', 3))) { |
164 | return true; // Plugin is not enabled. |
165 | } |
166 | } |
167 | } |
168 | |
169 | return false; |
170 | } |
171 | |
172 | void CreateDialog::_update_search() { |
173 | search_options->clear(); |
174 | search_options_types.clear(); |
175 | |
176 | TreeItem *root = search_options->create_item(); |
177 | root->set_text(0, base_type); |
178 | root->set_icon(0, search_options->get_editor_theme_icon(icon_fallback)); |
179 | search_options_types[base_type] = root; |
180 | _configure_search_option_item(root, base_type, ClassDB::class_exists(base_type) ? TypeCategory::CPP_TYPE : TypeCategory::OTHER_TYPE); |
181 | |
182 | const String search_text = search_box->get_text(); |
183 | bool empty_search = search_text.is_empty(); |
184 | |
185 | // Filter all candidate results. |
186 | Vector<String> candidates; |
187 | for (List<StringName>::Element *I = type_list.front(); I; I = I->next()) { |
188 | if (empty_search || search_text.is_subsequence_ofn(I->get())) { |
189 | candidates.push_back(I->get()); |
190 | } |
191 | } |
192 | |
193 | // Build the type tree. |
194 | for (int i = 0; i < candidates.size(); i++) { |
195 | _add_type(candidates[i], ClassDB::class_exists(candidates[i]) ? TypeCategory::CPP_TYPE : TypeCategory::OTHER_TYPE); |
196 | } |
197 | |
198 | // Select the best result. |
199 | if (empty_search) { |
200 | select_type(base_type); |
201 | } else if (candidates.size() > 0) { |
202 | select_type(_top_result(candidates, search_text)); |
203 | } else { |
204 | favorite->set_disabled(true); |
205 | help_bit->set_text(vformat(TTR("No results for \"%s\"." ), search_text)); |
206 | help_bit->get_rich_text()->set_self_modulate(Color(1, 1, 1, 0.5)); |
207 | get_ok_button()->set_disabled(true); |
208 | search_options->deselect_all(); |
209 | } |
210 | } |
211 | |
212 | void CreateDialog::_add_type(const String &p_type, const TypeCategory p_type_category) { |
213 | if (search_options_types.has(p_type)) { |
214 | return; |
215 | } |
216 | |
217 | String inherits; |
218 | |
219 | TypeCategory inherited_type = TypeCategory::OTHER_TYPE; |
220 | |
221 | if (p_type_category == TypeCategory::CPP_TYPE) { |
222 | inherits = ClassDB::get_parent_class(p_type); |
223 | inherited_type = TypeCategory::CPP_TYPE; |
224 | } else if (p_type_category == TypeCategory::PATH_TYPE) { |
225 | ERR_FAIL_COND(!ResourceLoader::exists(p_type, "Script" )); |
226 | Ref<Script> scr = ResourceLoader::load(p_type, "Script" ); |
227 | ERR_FAIL_COND(scr.is_null()); |
228 | |
229 | Ref<Script> base = scr->get_base_script(); |
230 | if (base.is_null()) { |
231 | String extends; |
232 | scr->get_language()->get_global_class_name(scr->get_path(), &extends); |
233 | |
234 | inherits = extends; |
235 | inherited_type = TypeCategory::CPP_TYPE; |
236 | } else { |
237 | inherits = scr->get_language()->get_global_class_name(base->get_path()); |
238 | if (inherits.is_empty()) { |
239 | inherits = base->get_path(); |
240 | inherited_type = TypeCategory::PATH_TYPE; |
241 | } |
242 | } |
243 | } else { |
244 | if (ScriptServer::is_global_class(p_type)) { |
245 | Ref<Script> scr = EditorNode::get_editor_data().script_class_load_script(p_type); |
246 | ERR_FAIL_COND(scr.is_null()); |
247 | |
248 | Ref<Script> base = scr->get_base_script(); |
249 | if (base.is_null()) { |
250 | String extends; |
251 | scr->get_language()->get_global_class_name(scr->get_path(), &extends); |
252 | |
253 | inherits = extends; |
254 | inherited_type = TypeCategory::CPP_TYPE; |
255 | } else { |
256 | inherits = scr->get_language()->get_global_class_name(base->get_path()); |
257 | if (inherits.is_empty()) { |
258 | inherits = base->get_path(); |
259 | inherited_type = TypeCategory::PATH_TYPE; |
260 | } |
261 | } |
262 | } else { |
263 | inherits = custom_type_parents[p_type]; |
264 | if (ClassDB::class_exists(inherits)) { |
265 | inherited_type = TypeCategory::CPP_TYPE; |
266 | } |
267 | } |
268 | } |
269 | |
270 | // Should never happen, but just in case... |
271 | ERR_FAIL_COND(inherits.is_empty()); |
272 | |
273 | _add_type(inherits, inherited_type); |
274 | |
275 | TreeItem *item = search_options->create_item(search_options_types[inherits]); |
276 | search_options_types[p_type] = item; |
277 | _configure_search_option_item(item, p_type, p_type_category); |
278 | } |
279 | |
280 | void CreateDialog::_configure_search_option_item(TreeItem *r_item, const String &p_type, const TypeCategory p_type_category) { |
281 | bool script_type = ScriptServer::is_global_class(p_type); |
282 | if (p_type_category == TypeCategory::CPP_TYPE) { |
283 | r_item->set_text(0, p_type); |
284 | } else if (p_type_category == TypeCategory::PATH_TYPE) { |
285 | r_item->set_text(0, "\"" + p_type + "\"" ); |
286 | } else if (script_type) { |
287 | r_item->set_metadata(0, p_type); |
288 | r_item->set_text(0, p_type); |
289 | r_item->set_suffix(0, "(" + ScriptServer::get_global_class_path(p_type).get_file() + ")" ); |
290 | } else { |
291 | r_item->set_metadata(0, custom_type_parents[p_type]); |
292 | r_item->set_text(0, p_type); |
293 | } |
294 | |
295 | bool can_instantiate = (p_type_category == TypeCategory::CPP_TYPE && ClassDB::can_instantiate(p_type)) || |
296 | p_type_category == TypeCategory::OTHER_TYPE; |
297 | bool instantiable = can_instantiate && !(ClassDB::class_exists(p_type) && ClassDB::is_virtual(p_type)); |
298 | |
299 | r_item->set_meta(SNAME("__instantiable" ), instantiable); |
300 | |
301 | r_item->set_icon(0, EditorNode::get_singleton()->get_class_icon(p_type)); |
302 | if (!instantiable) { |
303 | r_item->set_custom_color(0, search_options->get_theme_color(SNAME("disabled_font_color" ), EditorStringName(Editor))); |
304 | } |
305 | |
306 | bool is_deprecated = EditorHelp::get_doc_data()->class_list[p_type].is_deprecated; |
307 | bool is_experimental = EditorHelp::get_doc_data()->class_list[p_type].is_experimental; |
308 | |
309 | if (is_deprecated) { |
310 | r_item->add_button(0, get_editor_theme_icon("StatusError" ), 0, false, TTR("This class is marked as deprecated." )); |
311 | } else if (is_experimental) { |
312 | r_item->add_button(0, get_editor_theme_icon("NodeWarning" ), 0, false, TTR("This class is marked as experimental." )); |
313 | } |
314 | |
315 | if (!search_box->get_text().is_empty()) { |
316 | r_item->set_collapsed(false); |
317 | } else { |
318 | // Don't collapse the root node or an abstract node on the first tree level. |
319 | bool should_collapse = p_type != base_type && (r_item->get_parent()->get_text(0) != base_type || can_instantiate); |
320 | |
321 | if (should_collapse && bool(EDITOR_GET("docks/scene_tree/start_create_dialog_fully_expanded" ))) { |
322 | should_collapse = false; // Collapse all nodes anyway. |
323 | } |
324 | r_item->set_collapsed(should_collapse); |
325 | } |
326 | |
327 | const String &description = DTR(EditorHelp::get_doc_data()->class_list[p_type].brief_description); |
328 | r_item->set_tooltip_text(0, description); |
329 | |
330 | if (p_type_category == TypeCategory::OTHER_TYPE && !script_type) { |
331 | Ref<Texture2D> icon = EditorNode::get_editor_data().get_custom_types()[custom_type_parents[p_type]][custom_type_indices[p_type]].icon; |
332 | if (icon.is_valid()) { |
333 | r_item->set_icon(0, icon); |
334 | } |
335 | } |
336 | } |
337 | |
338 | String CreateDialog::_top_result(const Vector<String> p_candidates, const String &p_search_text) const { |
339 | float highest_score = 0; |
340 | int highest_index = 0; |
341 | for (int i = 0; i < p_candidates.size(); i++) { |
342 | float score = _score_type(p_candidates[i].get_slicec(' ', 0), p_search_text); |
343 | if (score > highest_score) { |
344 | highest_score = score; |
345 | highest_index = i; |
346 | } |
347 | } |
348 | |
349 | return p_candidates[highest_index]; |
350 | } |
351 | |
352 | float CreateDialog::_score_type(const String &p_type, const String &p_search) const { |
353 | if (p_type == p_search) { |
354 | // Always favor an exact match (case-sensitive), since clicking a favorite will set the search text to the type. |
355 | return 1.0f; |
356 | } |
357 | |
358 | float inverse_length = 1.f / float(p_type.length()); |
359 | |
360 | // Favor types where search term is a substring close to the start of the type. |
361 | float w = 0.5f; |
362 | int pos = p_type.findn(p_search); |
363 | float score = (pos > -1) ? 1.0f - w * MIN(1, 3 * pos * inverse_length) : MAX(0.f, .9f - w); |
364 | |
365 | // Favor shorter items: they resemble the search term more. |
366 | w = 0.9f; |
367 | score *= (1 - w) + w * MIN(1.0f, p_search.length() * inverse_length); |
368 | |
369 | score *= _is_type_preferred(p_type) ? 1.0f : 0.9f; |
370 | |
371 | // Add score for being a favorite type. |
372 | score *= (favorite_list.find(p_type) > -1) ? 1.0f : 0.8f; |
373 | |
374 | // Look through at most 5 recent items |
375 | bool in_recent = false; |
376 | constexpr int RECENT_COMPLETION_SIZE = 5; |
377 | for (int i = 0; i < MIN(RECENT_COMPLETION_SIZE - 1, recent->get_item_count()); i++) { |
378 | if (recent->get_item_text(i) == p_type) { |
379 | in_recent = true; |
380 | break; |
381 | } |
382 | } |
383 | score *= in_recent ? 1.0f : 0.9f; |
384 | |
385 | return score; |
386 | } |
387 | |
388 | void CreateDialog::_cleanup() { |
389 | type_list.clear(); |
390 | favorite_list.clear(); |
391 | favorites->clear(); |
392 | recent->clear(); |
393 | custom_type_parents.clear(); |
394 | custom_type_indices.clear(); |
395 | } |
396 | |
397 | void CreateDialog::_confirmed() { |
398 | String selected_item = get_selected_type(); |
399 | if (selected_item.is_empty()) { |
400 | return; |
401 | } |
402 | |
403 | TreeItem *selected = search_options->get_selected(); |
404 | if (!selected->get_meta("__instantiable" , true)) { |
405 | return; |
406 | } |
407 | |
408 | { |
409 | Ref<FileAccess> f = FileAccess::open(EditorPaths::get_singleton()->get_project_settings_dir().path_join("create_recent." + base_type), FileAccess::WRITE); |
410 | if (f.is_valid()) { |
411 | f->store_line(selected_item); |
412 | |
413 | constexpr int RECENT_HISTORY_SIZE = 15; |
414 | for (int i = 0; i < MIN(RECENT_HISTORY_SIZE - 1, recent->get_item_count()); i++) { |
415 | if (recent->get_item_text(i) != selected_item) { |
416 | f->store_line(recent->get_item_text(i)); |
417 | } |
418 | } |
419 | } |
420 | } |
421 | |
422 | // To prevent, emitting an error from the transient window (shader dialog for example) hide this dialog before emitting the "create" signal. |
423 | hide(); |
424 | |
425 | emit_signal(SNAME("create" )); |
426 | _cleanup(); |
427 | } |
428 | |
429 | void CreateDialog::_text_changed(const String &p_newtext) { |
430 | _update_search(); |
431 | } |
432 | |
433 | void CreateDialog::_sbox_input(const Ref<InputEvent> &p_ie) { |
434 | Ref<InputEventKey> k = p_ie; |
435 | if (k.is_valid() && k->is_pressed()) { |
436 | switch (k->get_keycode()) { |
437 | case Key::UP: |
438 | case Key::DOWN: |
439 | case Key::PAGEUP: |
440 | case Key::PAGEDOWN: { |
441 | search_options->gui_input(k); |
442 | search_box->accept_event(); |
443 | } break; |
444 | case Key::SPACE: { |
445 | TreeItem *ti = search_options->get_selected(); |
446 | if (ti) { |
447 | ti->set_collapsed(!ti->is_collapsed()); |
448 | } |
449 | search_box->accept_event(); |
450 | } break; |
451 | default: |
452 | break; |
453 | } |
454 | } |
455 | } |
456 | |
457 | void CreateDialog::_notification(int p_what) { |
458 | switch (p_what) { |
459 | case NOTIFICATION_ENTER_TREE: { |
460 | connect("confirmed" , callable_mp(this, &CreateDialog::_confirmed)); |
461 | } break; |
462 | |
463 | case NOTIFICATION_EXIT_TREE: { |
464 | disconnect("confirmed" , callable_mp(this, &CreateDialog::_confirmed)); |
465 | } break; |
466 | |
467 | case NOTIFICATION_VISIBILITY_CHANGED: { |
468 | if (is_visible()) { |
469 | search_box->call_deferred(SNAME("grab_focus" )); // still not visible |
470 | search_box->select_all(); |
471 | } else { |
472 | EditorSettings::get_singleton()->set_project_metadata("dialog_bounds" , "create_new_node" , Rect2(get_position(), get_size())); |
473 | } |
474 | } break; |
475 | |
476 | case NOTIFICATION_THEME_CHANGED: { |
477 | const int icon_width = get_theme_constant(SNAME("class_icon_size" ), EditorStringName(Editor)); |
478 | search_options->add_theme_constant_override("icon_max_width" , icon_width); |
479 | favorites->add_theme_constant_override("icon_max_width" , icon_width); |
480 | recent->set_fixed_icon_size(Size2(icon_width, icon_width)); |
481 | |
482 | search_box->set_right_icon(get_editor_theme_icon(SNAME("Search" ))); |
483 | favorite->set_icon(get_editor_theme_icon(SNAME("Favorites" ))); |
484 | } break; |
485 | } |
486 | } |
487 | |
488 | void CreateDialog::select_type(const String &p_type, bool p_center_on_item) { |
489 | if (!search_options_types.has(p_type)) { |
490 | return; |
491 | } |
492 | |
493 | TreeItem *to_select = search_options_types[p_type]; |
494 | to_select->select(0); |
495 | search_options->scroll_to_item(to_select, p_center_on_item); |
496 | |
497 | if (EditorHelp::get_doc_data()->class_list.has(p_type) && !DTR(EditorHelp::get_doc_data()->class_list[p_type].brief_description).is_empty()) { |
498 | // Display both class name and description, since the help bit may be displayed |
499 | // far away from the location (especially if the dialog was resized to be taller). |
500 | help_bit->set_text(vformat("[b]%s[/b]: %s" , p_type, DTR(EditorHelp::get_doc_data()->class_list[p_type].brief_description))); |
501 | help_bit->get_rich_text()->set_self_modulate(Color(1, 1, 1, 1)); |
502 | } else { |
503 | // Use nested `vformat()` as translators shouldn't interfere with BBCode tags. |
504 | help_bit->set_text(vformat(TTR("No description available for %s." ), vformat("[b]%s[/b]" , p_type))); |
505 | help_bit->get_rich_text()->set_self_modulate(Color(1, 1, 1, 0.5)); |
506 | } |
507 | |
508 | favorite->set_disabled(false); |
509 | favorite->set_pressed(favorite_list.has(p_type)); |
510 | get_ok_button()->set_disabled(false); |
511 | } |
512 | |
513 | void CreateDialog::select_base() { |
514 | if (search_options_types.is_empty()) { |
515 | _update_search(); |
516 | } |
517 | select_type(base_type, false); |
518 | } |
519 | |
520 | String CreateDialog::get_selected_type() { |
521 | TreeItem *selected = search_options->get_selected(); |
522 | if (!selected) { |
523 | return String(); |
524 | } |
525 | |
526 | return selected->get_text(0); |
527 | } |
528 | |
529 | void CreateDialog::set_base_type(const String &p_base) { |
530 | base_type = p_base; |
531 | is_base_type_node = ClassDB::is_parent_class(p_base, "Node" ); |
532 | } |
533 | |
534 | Variant CreateDialog::instantiate_selected() { |
535 | TreeItem *selected = search_options->get_selected(); |
536 | |
537 | if (!selected) { |
538 | return Variant(); |
539 | } |
540 | |
541 | Variant md = selected->get_metadata(0); |
542 | Variant obj; |
543 | if (md.get_type() != Variant::NIL) { |
544 | String custom = md; |
545 | if (ScriptServer::is_global_class(custom)) { |
546 | obj = EditorNode::get_editor_data().script_class_instance(custom); |
547 | Node *n = Object::cast_to<Node>(obj); |
548 | if (n) { |
549 | n->set_name(custom); |
550 | } |
551 | } else { |
552 | obj = EditorNode::get_editor_data().instantiate_custom_type(selected->get_text(0), custom); |
553 | } |
554 | } else { |
555 | obj = ClassDB::instantiate(selected->get_text(0)); |
556 | } |
557 | EditorNode::get_editor_data().instantiate_object_properties(obj); |
558 | |
559 | return obj; |
560 | } |
561 | |
562 | void CreateDialog::_item_selected() { |
563 | String name = get_selected_type(); |
564 | select_type(name, false); |
565 | } |
566 | |
567 | void CreateDialog::_hide_requested() { |
568 | _cancel_pressed(); // From AcceptDialog. |
569 | } |
570 | |
571 | void CreateDialog::cancel_pressed() { |
572 | _cleanup(); |
573 | } |
574 | |
575 | void CreateDialog::_favorite_toggled() { |
576 | TreeItem *item = search_options->get_selected(); |
577 | if (!item) { |
578 | return; |
579 | } |
580 | |
581 | String name = item->get_text(0); |
582 | |
583 | if (favorite_list.has(name)) { |
584 | favorite_list.erase(name); |
585 | favorite->set_pressed(false); |
586 | } else { |
587 | favorite_list.push_back(name); |
588 | favorite->set_pressed(true); |
589 | } |
590 | |
591 | _save_and_update_favorite_list(); |
592 | } |
593 | |
594 | void CreateDialog::_history_selected(int p_idx) { |
595 | search_box->set_text(recent->get_item_text(p_idx).get_slicec(' ', 0)); |
596 | favorites->deselect_all(); |
597 | _update_search(); |
598 | } |
599 | |
600 | void CreateDialog::_favorite_selected() { |
601 | TreeItem *item = favorites->get_selected(); |
602 | if (!item) { |
603 | return; |
604 | } |
605 | |
606 | search_box->set_text(item->get_text(0).get_slicec(' ', 0)); |
607 | recent->deselect_all(); |
608 | _update_search(); |
609 | } |
610 | |
611 | void CreateDialog::_history_activated(int p_idx) { |
612 | _history_selected(p_idx); |
613 | _confirmed(); |
614 | } |
615 | |
616 | void CreateDialog::_favorite_activated() { |
617 | _favorite_selected(); |
618 | _confirmed(); |
619 | } |
620 | |
621 | Variant CreateDialog::get_drag_data_fw(const Point2 &p_point, Control *p_from) { |
622 | TreeItem *ti = favorites->get_item_at_position(p_point); |
623 | if (ti) { |
624 | Dictionary d; |
625 | d["type" ] = "create_favorite_drag" ; |
626 | d["class" ] = ti->get_text(0); |
627 | |
628 | Button *tb = memnew(Button); |
629 | tb->set_flat(true); |
630 | tb->set_icon(ti->get_icon(0)); |
631 | tb->set_text(ti->get_text(0)); |
632 | favorites->set_drag_preview(tb); |
633 | |
634 | return d; |
635 | } |
636 | |
637 | return Variant(); |
638 | } |
639 | |
640 | bool CreateDialog::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const { |
641 | Dictionary d = p_data; |
642 | if (d.has("type" ) && String(d["type" ]) == "create_favorite_drag" ) { |
643 | favorites->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN); |
644 | return true; |
645 | } |
646 | |
647 | return false; |
648 | } |
649 | |
650 | void CreateDialog::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) { |
651 | Dictionary d = p_data; |
652 | |
653 | TreeItem *ti = favorites->get_item_at_position(p_point); |
654 | if (!ti) { |
655 | return; |
656 | } |
657 | |
658 | String drop_at = ti->get_text(0); |
659 | int ds = favorites->get_drop_section_at_position(p_point); |
660 | |
661 | int drop_idx = favorite_list.find(drop_at); |
662 | if (drop_idx < 0) { |
663 | return; |
664 | } |
665 | |
666 | String type = d["class" ]; |
667 | |
668 | int from_idx = favorite_list.find(type); |
669 | if (from_idx < 0) { |
670 | return; |
671 | } |
672 | |
673 | if (drop_idx == from_idx) { |
674 | ds = -1; //cause it will be gone |
675 | } else if (drop_idx > from_idx) { |
676 | drop_idx--; |
677 | } |
678 | |
679 | favorite_list.remove_at(from_idx); |
680 | |
681 | if (ds < 0) { |
682 | favorite_list.insert(drop_idx, type); |
683 | } else { |
684 | if (drop_idx >= favorite_list.size() - 1) { |
685 | favorite_list.push_back(type); |
686 | } else { |
687 | favorite_list.insert(drop_idx + 1, type); |
688 | } |
689 | } |
690 | |
691 | _save_and_update_favorite_list(); |
692 | } |
693 | |
694 | void CreateDialog::_save_and_update_favorite_list() { |
695 | favorites->clear(); |
696 | TreeItem *root = favorites->create_item(); |
697 | |
698 | { |
699 | Ref<FileAccess> f = FileAccess::open(EditorPaths::get_singleton()->get_project_settings_dir().path_join("favorites." + base_type), FileAccess::WRITE); |
700 | if (f.is_valid()) { |
701 | for (int i = 0; i < favorite_list.size(); i++) { |
702 | String l = favorite_list[i]; |
703 | String name = l.get_slicec(' ', 0); |
704 | if (!EditorNode::get_editor_data().is_type_recognized(name)) { |
705 | continue; |
706 | } |
707 | f->store_line(l); |
708 | |
709 | if (_is_class_disabled_by_feature_profile(name)) { |
710 | continue; |
711 | } |
712 | |
713 | TreeItem *ti = favorites->create_item(root); |
714 | ti->set_text(0, l); |
715 | ti->set_icon(0, EditorNode::get_singleton()->get_class_icon(name)); |
716 | } |
717 | } |
718 | } |
719 | |
720 | emit_signal(SNAME("favorites_updated" )); |
721 | } |
722 | |
723 | void CreateDialog::_load_favorites_and_history() { |
724 | String dir = EditorPaths::get_singleton()->get_project_settings_dir(); |
725 | Ref<FileAccess> f = FileAccess::open(dir.path_join("create_recent." + base_type), FileAccess::READ); |
726 | if (f.is_valid()) { |
727 | while (!f->eof_reached()) { |
728 | String l = f->get_line().strip_edges(); |
729 | String name = l.get_slicec(' ', 0); |
730 | |
731 | if (EditorNode::get_editor_data().is_type_recognized(name) && !_is_class_disabled_by_feature_profile(name)) { |
732 | recent->add_item(l, EditorNode::get_singleton()->get_class_icon(name)); |
733 | } |
734 | } |
735 | } |
736 | |
737 | f = FileAccess::open(dir.path_join("favorites." + base_type), FileAccess::READ); |
738 | if (f.is_valid()) { |
739 | while (!f->eof_reached()) { |
740 | String l = f->get_line().strip_edges(); |
741 | |
742 | if (!l.is_empty()) { |
743 | favorite_list.push_back(l); |
744 | } |
745 | } |
746 | } |
747 | } |
748 | |
749 | void CreateDialog::_bind_methods() { |
750 | ADD_SIGNAL(MethodInfo("create" )); |
751 | ADD_SIGNAL(MethodInfo("favorites_updated" )); |
752 | } |
753 | |
754 | CreateDialog::CreateDialog() { |
755 | base_type = "Object" ; |
756 | preferred_search_result_type = "" ; |
757 | |
758 | type_blacklist.insert("PluginScript" ); // PluginScript must be initialized before use, which is not possible here. |
759 | type_blacklist.insert("ScriptCreateDialog" ); // This is an exposed editor Node that doesn't have an Editor prefix. |
760 | |
761 | HSplitContainer *hsc = memnew(HSplitContainer); |
762 | add_child(hsc); |
763 | |
764 | VSplitContainer *vsc = memnew(VSplitContainer); |
765 | hsc->add_child(vsc); |
766 | |
767 | VBoxContainer *fav_vb = memnew(VBoxContainer); |
768 | fav_vb->set_custom_minimum_size(Size2(150, 100) * EDSCALE); |
769 | fav_vb->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
770 | vsc->add_child(fav_vb); |
771 | |
772 | favorites = memnew(Tree); |
773 | favorites->set_hide_root(true); |
774 | favorites->set_hide_folding(true); |
775 | favorites->set_allow_reselect(true); |
776 | favorites->connect("cell_selected" , callable_mp(this, &CreateDialog::_favorite_selected)); |
777 | favorites->connect("item_activated" , callable_mp(this, &CreateDialog::_favorite_activated)); |
778 | favorites->add_theme_constant_override("draw_guides" , 1); |
779 | SET_DRAG_FORWARDING_GCD(favorites, CreateDialog); |
780 | fav_vb->add_margin_child(TTR("Favorites:" ), favorites, true); |
781 | |
782 | VBoxContainer *rec_vb = memnew(VBoxContainer); |
783 | vsc->add_child(rec_vb); |
784 | rec_vb->set_custom_minimum_size(Size2(150, 100) * EDSCALE); |
785 | rec_vb->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
786 | |
787 | recent = memnew(ItemList); |
788 | rec_vb->add_margin_child(TTR("Recent:" ), recent, true); |
789 | recent->set_allow_reselect(true); |
790 | recent->connect("item_selected" , callable_mp(this, &CreateDialog::_history_selected)); |
791 | recent->connect("item_activated" , callable_mp(this, &CreateDialog::_history_activated)); |
792 | recent->add_theme_constant_override("draw_guides" , 1); |
793 | |
794 | VBoxContainer *vbc = memnew(VBoxContainer); |
795 | vbc->set_custom_minimum_size(Size2(300, 0) * EDSCALE); |
796 | vbc->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
797 | hsc->add_child(vbc); |
798 | |
799 | search_box = memnew(LineEdit); |
800 | search_box->set_clear_button_enabled(true); |
801 | search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
802 | search_box->connect("text_changed" , callable_mp(this, &CreateDialog::_text_changed)); |
803 | search_box->connect("gui_input" , callable_mp(this, &CreateDialog::_sbox_input)); |
804 | |
805 | HBoxContainer *search_hb = memnew(HBoxContainer); |
806 | search_hb->add_child(search_box); |
807 | |
808 | favorite = memnew(Button); |
809 | favorite->set_toggle_mode(true); |
810 | favorite->set_tooltip_text(TTR("(Un)favorite selected item." )); |
811 | favorite->connect("pressed" , callable_mp(this, &CreateDialog::_favorite_toggled)); |
812 | search_hb->add_child(favorite); |
813 | vbc->add_margin_child(TTR("Search:" ), search_hb); |
814 | |
815 | search_options = memnew(Tree); |
816 | search_options->connect("item_activated" , callable_mp(this, &CreateDialog::_confirmed)); |
817 | search_options->connect("cell_selected" , callable_mp(this, &CreateDialog::_item_selected)); |
818 | vbc->add_margin_child(TTR("Matches:" ), search_options, true); |
819 | |
820 | help_bit = memnew(EditorHelpBit); |
821 | help_bit->connect("request_hide" , callable_mp(this, &CreateDialog::_hide_requested)); |
822 | vbc->add_margin_child(TTR("Description:" ), help_bit); |
823 | |
824 | register_text_enter(search_box); |
825 | set_hide_on_ok(false); |
826 | set_clamp_to_embedder(true); |
827 | } |
828 | |