create_dialog.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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. #include "create_dialog.h"
  31. #include "core/class_db.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/print_string.h"
  34. #include "editor_feature_profile.h"
  35. #include "editor_help.h"
  36. #include "editor_node.h"
  37. #include "editor_scale.h"
  38. #include "editor_settings.h"
  39. #include "scene/gui/box_container.h"
  40. void CreateDialog::popup_create(bool p_dont_clear, bool p_replace_mode, const String &p_select_type) {
  41. type_list.clear();
  42. ClassDB::get_class_list(&type_list);
  43. ScriptServer::get_global_class_list(&type_list);
  44. type_list.sort_custom<StringName::AlphCompare>();
  45. recent->clear();
  46. FileAccess *f = FileAccess::open(EditorSettings::get_singleton()->get_project_settings_dir().plus_file("create_recent." + base_type), FileAccess::READ);
  47. if (f) {
  48. TreeItem *root = recent->create_item();
  49. String icon_fallback = has_icon(base_type, "EditorIcons") ? base_type : "Object";
  50. while (!f->eof_reached()) {
  51. String l = f->get_line().strip_edges();
  52. String name = l.split(" ")[0];
  53. if ((ClassDB::class_exists(name) || ScriptServer::is_global_class(name)) && !_is_class_disabled_by_feature_profile(name)) {
  54. TreeItem *ti = recent->create_item(root);
  55. ti->set_text(0, l);
  56. ti->set_icon(0, EditorNode::get_singleton()->get_class_icon(name, icon_fallback));
  57. }
  58. }
  59. memdelete(f);
  60. }
  61. favorites->clear();
  62. f = FileAccess::open(EditorSettings::get_singleton()->get_project_settings_dir().plus_file("favorites." + base_type), FileAccess::READ);
  63. favorite_list.clear();
  64. if (f) {
  65. while (!f->eof_reached()) {
  66. String l = f->get_line().strip_edges();
  67. if (l != String()) {
  68. favorite_list.push_back(l);
  69. }
  70. }
  71. memdelete(f);
  72. }
  73. _save_and_update_favorite_list();
  74. // Restore valid window bounds or pop up at default size.
  75. Rect2 saved_size = EditorSettings::get_singleton()->get_project_metadata("dialog_bounds", "create_new_node", Rect2());
  76. if (saved_size != Rect2()) {
  77. popup(saved_size);
  78. } else {
  79. popup_centered_clamped(Size2(900, 700) * EDSCALE, 0.8);
  80. }
  81. if (p_dont_clear) {
  82. search_box->select_all();
  83. } else {
  84. search_box->clear();
  85. }
  86. search_box->grab_focus();
  87. _update_search();
  88. is_replace_mode = p_replace_mode;
  89. if (p_replace_mode) {
  90. select_type(p_select_type);
  91. set_title(vformat(TTR("Change %s Type"), base_type));
  92. get_ok()->set_text(TTR("Change"));
  93. } else {
  94. set_title(vformat(TTR("Create New %s"), base_type));
  95. get_ok()->set_text(TTR("Create"));
  96. }
  97. }
  98. void CreateDialog::_text_changed(const String &p_newtext) {
  99. _update_search();
  100. }
  101. void CreateDialog::_sbox_input(const Ref<InputEvent> &p_ie) {
  102. Ref<InputEventKey> k = p_ie;
  103. if (k.is_valid() && (k->get_scancode() == KEY_UP || k->get_scancode() == KEY_DOWN || k->get_scancode() == KEY_PAGEUP || k->get_scancode() == KEY_PAGEDOWN)) {
  104. search_options->call("_gui_input", k);
  105. search_box->accept_event();
  106. }
  107. }
  108. void CreateDialog::add_type(const String &p_type, HashMap<String, TreeItem *> &p_types, TreeItem *p_root, TreeItem **to_select) {
  109. if (p_types.has(p_type)) {
  110. return;
  111. }
  112. bool cpp_type = ClassDB::class_exists(p_type);
  113. EditorData &ed = EditorNode::get_editor_data();
  114. if (p_type == base_type) {
  115. return;
  116. }
  117. if (cpp_type) {
  118. if (!ClassDB::is_parent_class(p_type, base_type)) {
  119. return;
  120. }
  121. } else {
  122. if (!search_loaded_scripts.has(p_type)) {
  123. search_loaded_scripts[p_type] = ed.script_class_load_script(p_type);
  124. }
  125. if (!ScriptServer::is_global_class(p_type) || !ed.script_class_is_parent(p_type, base_type)) {
  126. return;
  127. }
  128. String script_path = ScriptServer::get_global_class_path(p_type);
  129. if (script_path.find("res://addons/", 0) != -1) {
  130. if (!EditorNode::get_singleton()->is_addon_plugin_enabled(script_path.get_slicec('/', 3))) {
  131. return;
  132. }
  133. }
  134. }
  135. String inherits = cpp_type ? ClassDB::get_parent_class(p_type) : ed.script_class_get_base(p_type);
  136. TreeItem *parent = p_root;
  137. if (inherits.length()) {
  138. if (!p_types.has(inherits)) {
  139. add_type(inherits, p_types, p_root, to_select);
  140. }
  141. if (p_types.has(inherits)) {
  142. parent = p_types[inherits];
  143. } else if (ScriptServer::is_global_class(inherits)) {
  144. return;
  145. }
  146. }
  147. bool can_instance = (cpp_type && ClassDB::can_instance(p_type)) || ScriptServer::is_global_class(p_type);
  148. TreeItem *item = search_options->create_item(parent);
  149. item->set_text(0, p_type);
  150. if (cpp_type) {
  151. item->set_tooltip(0, DTR(EditorHelp::get_doc_data()->class_list[p_type].brief_description));
  152. } else {
  153. item->set_tooltip(0, ScriptServer::get_global_class_path(p_type));
  154. item->add_button(0, EditorNode::get_singleton()->get_class_icon("Script"));
  155. item->set_metadata(0, p_type);
  156. }
  157. if (!can_instance) {
  158. item->set_custom_color(0, get_color("disabled_font_color", "Editor"));
  159. item->set_selectable(0, false);
  160. } else if (!(*to_select && (*to_select)->get_text(0) == search_box->get_text())) {
  161. String search_term = search_box->get_text().to_lower();
  162. // if the node name matches exactly as the search, the node should be selected.
  163. // this also fixes when the user clicks on recent nodes.
  164. if (p_type.to_lower() == search_term) {
  165. *to_select = item;
  166. } else {
  167. bool current_type_prefered = _is_type_prefered(p_type);
  168. bool selected_type_prefered = *to_select ? _is_type_prefered((*to_select)->get_text(0).split(" ")[0]) : false;
  169. bool is_subsequence_of_type = search_box->get_text().is_subsequence_ofi(p_type);
  170. bool is_substring_of_type = p_type.to_lower().find(search_term) >= 0;
  171. bool is_substring_of_selected = false;
  172. bool is_subsequence_of_selected = false;
  173. bool is_selected_equal = false;
  174. if (*to_select) {
  175. String name = (*to_select)->get_text(0).split(" ")[0].to_lower();
  176. is_substring_of_selected = name.find(search_term) >= 0;
  177. is_subsequence_of_selected = search_term.is_subsequence_of(name);
  178. is_selected_equal = name == search_term;
  179. }
  180. if (is_subsequence_of_type && !is_selected_equal) {
  181. if (is_substring_of_type) {
  182. if (!is_substring_of_selected || (current_type_prefered && !selected_type_prefered)) {
  183. *to_select = item;
  184. }
  185. } else {
  186. // substring results weigh more than subsequences, so let's make sure we don't override them
  187. if (!is_substring_of_selected) {
  188. if (!is_subsequence_of_selected || (current_type_prefered && !selected_type_prefered)) {
  189. *to_select = item;
  190. }
  191. }
  192. }
  193. }
  194. }
  195. }
  196. if (bool(EditorSettings::get_singleton()->get("docks/scene_tree/start_create_dialog_fully_expanded"))) {
  197. item->set_collapsed(false);
  198. } else {
  199. // don't collapse search results
  200. bool collapse = (search_box->get_text() == "");
  201. // don't collapse the root node
  202. collapse &= (item != p_root);
  203. // don't collapse abstract nodes on the first tree level
  204. collapse &= ((parent != p_root) || (can_instance));
  205. item->set_collapsed(collapse);
  206. }
  207. String icon_fallback = has_icon(base_type, "EditorIcons") ? base_type : "Object";
  208. item->set_icon(0, EditorNode::get_singleton()->get_class_icon(p_type, icon_fallback));
  209. p_types[p_type] = item;
  210. }
  211. bool CreateDialog::_is_type_prefered(const String &type) {
  212. bool cpp_type = ClassDB::class_exists(type);
  213. EditorData &ed = EditorNode::get_editor_data();
  214. if (cpp_type) {
  215. return ClassDB::is_parent_class(type, preferred_search_result_type);
  216. }
  217. return ed.script_class_is_parent(type, preferred_search_result_type);
  218. }
  219. bool CreateDialog::_is_class_disabled_by_feature_profile(const StringName &p_class) {
  220. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  221. if (profile.is_null()) {
  222. return false;
  223. }
  224. return profile->is_class_disabled(p_class);
  225. }
  226. void CreateDialog::select_type(const String &p_type) {
  227. TreeItem *to_select;
  228. if (search_options_types.has(p_type)) {
  229. to_select = search_options_types[p_type];
  230. } else {
  231. to_select = search_options->get_root();
  232. }
  233. // uncollapse from selected type to top level
  234. // TODO: should this be in tree?
  235. TreeItem *cur = to_select;
  236. while (cur) {
  237. cur->set_collapsed(false);
  238. cur = cur->get_parent();
  239. }
  240. to_select->select(0);
  241. search_options->scroll_to_item(to_select);
  242. }
  243. void CreateDialog::_update_search() {
  244. search_options->clear();
  245. favorite->set_disabled(true);
  246. help_bit->set_text(vformat(TTR("No results for \"%s\"."), search_box->get_text()));
  247. help_bit->get_rich_text()->set_self_modulate(Color(1, 1, 1, 0.5));
  248. search_options_types.clear();
  249. TreeItem *root = search_options->create_item();
  250. EditorData &ed = EditorNode::get_editor_data();
  251. root->set_text(0, base_type);
  252. String base_icon = has_icon(base_type, "EditorIcons") ? base_type : "Object";
  253. root->set_icon(0, get_icon(base_icon, "EditorIcons"));
  254. TreeItem *to_select = search_box->get_text() == base_type ? root : nullptr;
  255. for (List<StringName>::Element *I = type_list.front(); I; I = I->next()) {
  256. String type = I->get();
  257. if (_is_class_disabled_by_feature_profile(type)) {
  258. continue;
  259. }
  260. bool cpp_type = ClassDB::class_exists(type);
  261. if (base_type == "Node" && type.begins_with("Editor")) {
  262. continue; // do not show editor nodes
  263. }
  264. if (cpp_type && !ClassDB::can_instance(type)) {
  265. continue; // can't create what can't be instanced
  266. }
  267. if (cpp_type) {
  268. bool skip = false;
  269. for (Set<StringName>::Element *E = type_blacklist.front(); E && !skip; E = E->next()) {
  270. if (ClassDB::is_parent_class(type, E->get())) {
  271. skip = true;
  272. }
  273. }
  274. if (skip) {
  275. continue;
  276. }
  277. }
  278. if (search_box->get_text() == "") {
  279. add_type(type, search_options_types, root, &to_select);
  280. } else {
  281. bool found = false;
  282. String type2 = type;
  283. bool cpp_type2 = cpp_type;
  284. if (!cpp_type && !search_loaded_scripts.has(type)) {
  285. search_loaded_scripts[type] = ed.script_class_load_script(type);
  286. }
  287. while (type2 != "" && (cpp_type2 ? ClassDB::is_parent_class(type2, base_type) : ed.script_class_is_parent(type2, base_type)) && type2 != base_type) {
  288. if (search_box->get_text().is_subsequence_ofi(type2)) {
  289. found = true;
  290. break;
  291. }
  292. type2 = cpp_type2 ? ClassDB::get_parent_class(type2) : ed.script_class_get_base(type2);
  293. cpp_type2 = cpp_type2 || ClassDB::class_exists(type2); // Built-in class can't inherit from custom type, so we can skip the check if it's already true.
  294. if (!cpp_type2 && !search_loaded_scripts.has(type2)) {
  295. search_loaded_scripts[type2] = ed.script_class_load_script(type2);
  296. }
  297. }
  298. if (found) {
  299. add_type(type, search_options_types, root, &to_select);
  300. }
  301. }
  302. if (EditorNode::get_editor_data().get_custom_types().has(type) && ClassDB::is_parent_class(type, base_type)) {
  303. //there are custom types based on this... cool.
  304. const Vector<EditorData::CustomType> &ct = EditorNode::get_editor_data().get_custom_types()[type];
  305. for (int i = 0; i < ct.size(); i++) {
  306. bool show = search_box->get_text().is_subsequence_ofi(ct[i].name);
  307. if (!show) {
  308. continue;
  309. }
  310. if (!search_options_types.has(type)) {
  311. add_type(type, search_options_types, root, &to_select);
  312. }
  313. TreeItem *ti;
  314. if (search_options_types.has(type)) {
  315. ti = search_options_types[type];
  316. } else {
  317. ti = search_options->get_root();
  318. }
  319. TreeItem *item = search_options->create_item(ti);
  320. item->set_metadata(0, type);
  321. item->set_text(0, ct[i].name);
  322. item->set_icon(0, ct[i].icon.is_valid() ? ct[i].icon : get_icon(base_icon, "EditorIcons"));
  323. if (!to_select || ct[i].name == search_box->get_text()) {
  324. to_select = item;
  325. }
  326. }
  327. }
  328. }
  329. if (search_box->get_text() == "") {
  330. to_select = root;
  331. }
  332. if (to_select) {
  333. to_select->select(0);
  334. search_options->scroll_to_item(to_select);
  335. favorite->set_disabled(false);
  336. favorite->set_pressed(favorite_list.find(to_select->get_text(0)) != -1);
  337. }
  338. get_ok()->set_disabled(root->get_children() == nullptr);
  339. }
  340. void CreateDialog::_confirmed() {
  341. TreeItem *ti = search_options->get_selected();
  342. if (!ti) {
  343. return;
  344. }
  345. FileAccess *f = FileAccess::open(EditorSettings::get_singleton()->get_project_settings_dir().plus_file("create_recent." + base_type), FileAccess::WRITE);
  346. if (f) {
  347. f->store_line(get_selected_type());
  348. TreeItem *t = recent->get_root();
  349. if (t) {
  350. t = t->get_children();
  351. }
  352. int count = 0;
  353. while (t) {
  354. if (t->get_text(0) != get_selected_type()) {
  355. f->store_line(t->get_text(0));
  356. }
  357. if (count > 32) {
  358. //limit it to 32 entries..
  359. break;
  360. }
  361. t = t->get_next();
  362. count++;
  363. }
  364. memdelete(f);
  365. }
  366. emit_signal("create");
  367. hide();
  368. }
  369. void CreateDialog::_notification(int p_what) {
  370. switch (p_what) {
  371. case NOTIFICATION_ENTER_TREE: {
  372. connect("confirmed", this, "_confirmed");
  373. search_box->set_right_icon(get_icon("Search", "EditorIcons"));
  374. search_box->set_clear_button_enabled(true);
  375. favorite->set_icon(get_icon("Favorites", "EditorIcons"));
  376. } break;
  377. case NOTIFICATION_EXIT_TREE: {
  378. disconnect("confirmed", this, "_confirmed");
  379. } break;
  380. case NOTIFICATION_VISIBILITY_CHANGED: {
  381. if (is_visible_in_tree()) {
  382. search_box->call_deferred("grab_focus"); // still not visible
  383. search_box->select_all();
  384. }
  385. } break;
  386. case NOTIFICATION_POPUP_HIDE: {
  387. EditorSettings::get_singleton()->get_project_metadata("dialog_bounds", "create_new_node", get_rect());
  388. search_loaded_scripts.clear();
  389. } break;
  390. }
  391. }
  392. void CreateDialog::set_base_type(const String &p_base) {
  393. base_type = p_base;
  394. if (is_replace_mode) {
  395. set_title(vformat(TTR("Change %s Type"), p_base));
  396. } else {
  397. set_title(vformat(TTR("Create New %s"), p_base));
  398. }
  399. _update_search();
  400. }
  401. String CreateDialog::get_base_type() const {
  402. return base_type;
  403. }
  404. void CreateDialog::select_base() {
  405. if (search_options_types.empty()) {
  406. _update_search();
  407. }
  408. select_type(base_type);
  409. }
  410. void CreateDialog::set_preferred_search_result_type(const String &p_preferred_type) {
  411. preferred_search_result_type = p_preferred_type;
  412. }
  413. String CreateDialog::get_preferred_search_result_type() {
  414. return preferred_search_result_type;
  415. }
  416. String CreateDialog::get_selected_type() {
  417. TreeItem *selected = search_options->get_selected();
  418. if (selected) {
  419. return selected->get_text(0);
  420. } else {
  421. return String();
  422. }
  423. }
  424. Variant CreateDialog::instance_selected() {
  425. TreeItem *selected = search_options->get_selected();
  426. if (selected) {
  427. Variant md = selected->get_metadata(0);
  428. String custom;
  429. if (md.get_type() != Variant::NIL) {
  430. custom = md;
  431. }
  432. if (custom != String()) {
  433. if (ScriptServer::is_global_class(custom)) {
  434. Variant obj = EditorNode::get_editor_data().script_class_instance(custom);
  435. Node *n = Object::cast_to<Node>(obj);
  436. if (n) {
  437. n->set_name(custom);
  438. }
  439. return obj;
  440. }
  441. return EditorNode::get_editor_data().instance_custom_type(selected->get_text(0), custom);
  442. } else {
  443. return ClassDB::instance(selected->get_text(0));
  444. }
  445. }
  446. return Variant();
  447. }
  448. void CreateDialog::_item_selected() {
  449. TreeItem *item = search_options->get_selected();
  450. if (!item) {
  451. return;
  452. }
  453. String name = item->get_text(0);
  454. favorite->set_disabled(false);
  455. favorite->set_pressed(favorite_list.find(name) != -1);
  456. const String brief_desc = item->get_tooltip(0);
  457. if (EditorHelp::get_doc_data()->class_list.has(name)) {
  458. help_bit->set_text(vformat("[%s] %s", name, brief_desc));
  459. } else {
  460. help_bit->set_text(vformat("[url=%s]%s[/url]", brief_desc, name));
  461. }
  462. help_bit->get_rich_text()->set_self_modulate(Color(1, 1, 1, 1));
  463. get_ok()->set_disabled(false);
  464. }
  465. void CreateDialog::_script_selected(const Variant &p_object, int p_column, int p_id) {
  466. TreeItem *p_item = Object::cast_to<TreeItem>(p_object);
  467. String script_path = p_item->get_tooltip(0);
  468. if (!script_path.empty()) {
  469. Ref<Script> script = ResourceLoader::load(script_path, "Script");
  470. if (script.is_valid()) {
  471. EditorNode::get_singleton()->edit_resource(script);
  472. } else {
  473. print_error("Failed to load script: " + script_path);
  474. }
  475. hide();
  476. }
  477. }
  478. void CreateDialog::_favorite_toggled() {
  479. TreeItem *item = search_options->get_selected();
  480. if (!item) {
  481. return;
  482. }
  483. String name = item->get_text(0);
  484. if (favorite_list.find(name) == -1) {
  485. favorite_list.push_back(name);
  486. favorite->set_pressed(true);
  487. } else {
  488. favorite_list.erase(name);
  489. favorite->set_pressed(false);
  490. }
  491. _save_and_update_favorite_list();
  492. }
  493. void CreateDialog::_save_favorite_list() {
  494. FileAccess *f = FileAccess::open(EditorSettings::get_singleton()->get_project_settings_dir().plus_file("favorites." + base_type), FileAccess::WRITE);
  495. if (f) {
  496. for (int i = 0; i < favorite_list.size(); i++) {
  497. String l = favorite_list[i];
  498. String name = l.split(" ")[0];
  499. if (!(ClassDB::class_exists(name) || ScriptServer::is_global_class(name))) {
  500. continue;
  501. }
  502. f->store_line(l);
  503. }
  504. memdelete(f);
  505. }
  506. }
  507. void CreateDialog::_update_favorite_list() {
  508. favorites->clear();
  509. TreeItem *root = favorites->create_item();
  510. String icon_fallback = has_icon(base_type, "EditorIcons") ? base_type : "Object";
  511. for (int i = 0; i < favorite_list.size(); i++) {
  512. String l = favorite_list[i];
  513. String name = l.split(" ")[0];
  514. if (!((ClassDB::class_exists(name) || ScriptServer::is_global_class(name)) && !_is_class_disabled_by_feature_profile(name))) {
  515. continue;
  516. }
  517. TreeItem *ti = favorites->create_item(root);
  518. ti->set_text(0, l);
  519. ti->set_icon(0, EditorNode::get_singleton()->get_class_icon(name, icon_fallback));
  520. }
  521. emit_signal("favorites_updated");
  522. }
  523. void CreateDialog::_history_selected() {
  524. TreeItem *item = recent->get_selected();
  525. if (!item) {
  526. return;
  527. }
  528. search_box->set_text(item->get_text(0).get_slicec(' ', 0));
  529. favorites->deselect_all();
  530. _update_search();
  531. }
  532. void CreateDialog::_favorite_selected() {
  533. TreeItem *item = favorites->get_selected();
  534. if (!item) {
  535. return;
  536. }
  537. search_box->set_text(item->get_text(0).get_slicec(' ', 0));
  538. recent->deselect_all();
  539. _update_search();
  540. }
  541. void CreateDialog::_history_activated() {
  542. _history_selected();
  543. _confirmed();
  544. }
  545. void CreateDialog::_favorite_activated() {
  546. _favorite_selected();
  547. _confirmed();
  548. }
  549. Variant CreateDialog::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  550. TreeItem *ti = favorites->get_item_at_position(p_point);
  551. if (ti) {
  552. Dictionary d;
  553. d["type"] = "create_favorite_drag";
  554. d["class"] = ti->get_text(0);
  555. ToolButton *tb = memnew(ToolButton);
  556. tb->set_icon(ti->get_icon(0));
  557. tb->set_text(ti->get_text(0));
  558. set_drag_preview(tb);
  559. return d;
  560. }
  561. return Variant();
  562. }
  563. bool CreateDialog::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  564. Dictionary d = p_data;
  565. if (d.has("type") && String(d["type"]) == "create_favorite_drag") {
  566. favorites->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
  567. return true;
  568. }
  569. return false;
  570. }
  571. void CreateDialog::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  572. Dictionary d = p_data;
  573. TreeItem *ti = favorites->get_item_at_position(p_point);
  574. if (!ti) {
  575. return;
  576. }
  577. String drop_at = ti->get_text(0);
  578. int ds = favorites->get_drop_section_at_position(p_point);
  579. int drop_idx = favorite_list.find(drop_at);
  580. if (drop_idx < 0) {
  581. return;
  582. }
  583. String type = d["class"];
  584. int from_idx = favorite_list.find(type);
  585. if (from_idx < 0) {
  586. return;
  587. }
  588. if (drop_idx == from_idx) {
  589. ds = -1; //cause it will be gone
  590. } else if (drop_idx > from_idx) {
  591. drop_idx--;
  592. }
  593. favorite_list.remove(from_idx);
  594. if (ds < 0) {
  595. favorite_list.insert(drop_idx, type);
  596. } else {
  597. if (drop_idx >= favorite_list.size() - 1) {
  598. favorite_list.push_back(type);
  599. } else {
  600. favorite_list.insert(drop_idx + 1, type);
  601. }
  602. }
  603. _save_and_update_favorite_list();
  604. }
  605. void CreateDialog::_save_and_update_favorite_list() {
  606. _save_favorite_list();
  607. _update_favorite_list();
  608. }
  609. void CreateDialog::_bind_methods() {
  610. ClassDB::bind_method(D_METHOD("_text_changed"), &CreateDialog::_text_changed);
  611. ClassDB::bind_method(D_METHOD("_confirmed"), &CreateDialog::_confirmed);
  612. ClassDB::bind_method(D_METHOD("_sbox_input"), &CreateDialog::_sbox_input);
  613. ClassDB::bind_method(D_METHOD("_item_selected"), &CreateDialog::_item_selected);
  614. ClassDB::bind_method(D_METHOD("_script_selected"), &CreateDialog::_script_selected);
  615. ClassDB::bind_method(D_METHOD("_favorite_toggled"), &CreateDialog::_favorite_toggled);
  616. ClassDB::bind_method(D_METHOD("_history_selected"), &CreateDialog::_history_selected);
  617. ClassDB::bind_method(D_METHOD("_favorite_selected"), &CreateDialog::_favorite_selected);
  618. ClassDB::bind_method(D_METHOD("_history_activated"), &CreateDialog::_history_activated);
  619. ClassDB::bind_method(D_METHOD("_favorite_activated"), &CreateDialog::_favorite_activated);
  620. ClassDB::bind_method(D_METHOD("_save_and_update_favorite_list"), &CreateDialog::_save_and_update_favorite_list);
  621. ClassDB::bind_method("get_drag_data_fw", &CreateDialog::get_drag_data_fw);
  622. ClassDB::bind_method("can_drop_data_fw", &CreateDialog::can_drop_data_fw);
  623. ClassDB::bind_method("drop_data_fw", &CreateDialog::drop_data_fw);
  624. ADD_SIGNAL(MethodInfo("create"));
  625. ADD_SIGNAL(MethodInfo("favorites_updated"));
  626. }
  627. CreateDialog::CreateDialog() {
  628. is_replace_mode = false;
  629. set_resizable(true);
  630. HSplitContainer *hsc = memnew(HSplitContainer);
  631. add_child(hsc);
  632. VSplitContainer *vsc = memnew(VSplitContainer);
  633. hsc->add_child(vsc);
  634. VBoxContainer *fav_vb = memnew(VBoxContainer);
  635. vsc->add_child(fav_vb);
  636. fav_vb->set_custom_minimum_size(Size2(150, 100) * EDSCALE);
  637. fav_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  638. favorites = memnew(Tree);
  639. fav_vb->add_margin_child(TTR("Favorites:"), favorites, true);
  640. favorites->set_hide_root(true);
  641. favorites->set_hide_folding(true);
  642. favorites->set_allow_reselect(true);
  643. favorites->connect("cell_selected", this, "_favorite_selected");
  644. favorites->connect("item_activated", this, "_favorite_activated");
  645. favorites->set_drag_forwarding(this);
  646. favorites->add_constant_override("draw_guides", 1);
  647. VBoxContainer *rec_vb = memnew(VBoxContainer);
  648. vsc->add_child(rec_vb);
  649. rec_vb->set_custom_minimum_size(Size2(150, 100) * EDSCALE);
  650. rec_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  651. recent = memnew(Tree);
  652. rec_vb->add_margin_child(TTR("Recent:"), recent, true);
  653. recent->set_hide_root(true);
  654. recent->set_hide_folding(true);
  655. recent->set_allow_reselect(true);
  656. recent->connect("cell_selected", this, "_history_selected");
  657. recent->connect("item_activated", this, "_history_activated");
  658. recent->add_constant_override("draw_guides", 1);
  659. VBoxContainer *vbc = memnew(VBoxContainer);
  660. hsc->add_child(vbc);
  661. vbc->set_custom_minimum_size(Size2(300, 0) * EDSCALE);
  662. vbc->set_h_size_flags(SIZE_EXPAND_FILL);
  663. HBoxContainer *search_hb = memnew(HBoxContainer);
  664. search_box = memnew(LineEdit);
  665. search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  666. search_hb->add_child(search_box);
  667. favorite = memnew(Button);
  668. favorite->set_flat(true);
  669. favorite->set_toggle_mode(true);
  670. search_hb->add_child(favorite);
  671. favorite->connect("pressed", this, "_favorite_toggled");
  672. vbc->add_margin_child(TTR("Search:"), search_hb);
  673. search_box->connect("text_changed", this, "_text_changed");
  674. search_box->connect("gui_input", this, "_sbox_input");
  675. search_options = memnew(Tree);
  676. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  677. get_ok()->set_disabled(true);
  678. register_text_enter(search_box);
  679. set_hide_on_ok(false);
  680. search_options->connect("item_activated", this, "_confirmed");
  681. search_options->connect("cell_selected", this, "_item_selected");
  682. search_options->connect("button_pressed", this, "_script_selected");
  683. base_type = "Object";
  684. preferred_search_result_type = "";
  685. help_bit = memnew(EditorHelpBit);
  686. vbc->add_margin_child(TTR("Description:"), help_bit);
  687. help_bit->connect("request_hide", this, "_closed");
  688. type_blacklist.insert("PluginScript"); // PluginScript must be initialized before use, which is not possible here
  689. type_blacklist.insert("ScriptCreateDialog"); // This is an exposed editor Node that doesn't have an Editor prefix.
  690. }