create_dialog.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /*************************************************************************/
  2. /* create_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #include "create_dialog.h"
  31. #include "core/object/class_db.h"
  32. #include "core/os/keyboard.h"
  33. #include "editor_feature_profile.h"
  34. #include "editor_node.h"
  35. #include "editor_scale.h"
  36. #include "editor_settings.h"
  37. void CreateDialog::popup_create(bool p_dont_clear, bool p_replace_mode, const String &p_select_type) {
  38. _fill_type_list();
  39. icon_fallback = search_options->has_theme_icon(base_type, "EditorIcons") ? base_type : "Object";
  40. if (p_dont_clear) {
  41. search_box->select_all();
  42. } else {
  43. search_box->clear();
  44. }
  45. if (p_replace_mode) {
  46. search_box->set_text(p_select_type);
  47. }
  48. search_box->grab_focus();
  49. _update_search();
  50. if (p_replace_mode) {
  51. set_title(vformat(TTR("Change %s Type"), base_type));
  52. get_ok_button()->set_text(TTR("Change"));
  53. } else {
  54. set_title(vformat(TTR("Create New %s"), base_type));
  55. get_ok_button()->set_text(TTR("Create"));
  56. }
  57. _load_favorites_and_history();
  58. _save_and_update_favorite_list();
  59. // Restore valid window bounds or pop up at default size.
  60. Rect2 saved_size = EditorSettings::get_singleton()->get_project_metadata("dialog_bounds", "create_new_node", Rect2());
  61. if (saved_size != Rect2()) {
  62. popup(saved_size);
  63. } else {
  64. popup_centered_clamped(Size2(900, 700) * EDSCALE, 0.8);
  65. }
  66. }
  67. void CreateDialog::_fill_type_list() {
  68. List<StringName> complete_type_list;
  69. ClassDB::get_class_list(&complete_type_list);
  70. ScriptServer::get_global_class_list(&complete_type_list);
  71. EditorData &ed = EditorNode::get_editor_data();
  72. for (List<StringName>::Element *I = complete_type_list.front(); I; I = I->next()) {
  73. String type = I->get();
  74. if (!_should_hide_type(type)) {
  75. type_list.push_back(type);
  76. if (!ed.get_custom_types().has(type)) {
  77. continue;
  78. }
  79. const Vector<EditorData::CustomType> &ct = ed.get_custom_types()[type];
  80. for (int i = 0; i < ct.size(); i++) {
  81. custom_type_parents[ct[i].name] = type;
  82. custom_type_indices[ct[i].name] = i;
  83. type_list.push_back(ct[i].name);
  84. }
  85. }
  86. }
  87. type_list.sort_custom<StringName::AlphCompare>();
  88. }
  89. bool CreateDialog::_is_type_preferred(const String &p_type) const {
  90. if (ClassDB::class_exists(p_type)) {
  91. return ClassDB::is_parent_class(p_type, preferred_search_result_type);
  92. }
  93. return EditorNode::get_editor_data().script_class_is_parent(p_type, preferred_search_result_type);
  94. }
  95. bool CreateDialog::_is_class_disabled_by_feature_profile(const StringName &p_class) const {
  96. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  97. return !profile.is_null() && profile->is_class_disabled(p_class);
  98. }
  99. bool CreateDialog::_should_hide_type(const String &p_type) const {
  100. if (_is_class_disabled_by_feature_profile(p_type)) {
  101. return true;
  102. }
  103. if (base_type == "Node" && p_type.begins_with("Editor")) {
  104. return true; // Do not show editor nodes.
  105. }
  106. if (p_type == base_type) {
  107. return true; // Root is already added.
  108. }
  109. if (ClassDB::class_exists(p_type)) {
  110. if (!ClassDB::can_instance(p_type)) {
  111. return true; // Can't create abstract class.
  112. }
  113. if (!ClassDB::is_parent_class(p_type, base_type)) {
  114. return true; // Wrong inheritance.
  115. }
  116. for (Set<StringName>::Element *E = type_blacklist.front(); E; E = E->next()) {
  117. if (ClassDB::is_parent_class(p_type, E->get())) {
  118. return true; // Parent type is blacklisted.
  119. }
  120. }
  121. } else {
  122. if (!EditorNode::get_editor_data().script_class_is_parent(p_type, base_type)) {
  123. return true; // Wrong inheritance.
  124. }
  125. if (!ScriptServer::is_global_class(p_type)) {
  126. return true;
  127. }
  128. String script_path = ScriptServer::get_global_class_path(p_type);
  129. if (script_path.begins_with("res://addons/")) {
  130. if (!EditorNode::get_singleton()->is_addon_plugin_enabled(script_path.get_slicec('/', 3))) {
  131. return true; // Plugin is not enabled.
  132. }
  133. }
  134. }
  135. return false;
  136. }
  137. void CreateDialog::_update_search() {
  138. search_options->clear();
  139. search_options_types.clear();
  140. TreeItem *root = search_options->create_item();
  141. root->set_text(0, base_type);
  142. root->set_icon(0, search_options->get_theme_icon(icon_fallback, "EditorIcons"));
  143. search_options_types[base_type] = root;
  144. const String search_text = search_box->get_text();
  145. bool empty_search = search_text == "";
  146. // Filter all candidate results.
  147. Vector<String> candidates;
  148. for (List<StringName>::Element *I = type_list.front(); I; I = I->next()) {
  149. if (empty_search || search_text.is_subsequence_ofi(I->get())) {
  150. candidates.push_back(I->get());
  151. }
  152. }
  153. // Build the type tree.
  154. for (int i = 0; i < candidates.size(); i++) {
  155. _add_type(candidates[i], ClassDB::class_exists(candidates[i]));
  156. }
  157. // Select the best result.
  158. if (empty_search) {
  159. select_type(base_type);
  160. } else if (candidates.size() > 0) {
  161. select_type(_top_result(candidates, search_text));
  162. } else {
  163. favorite->set_disabled(true);
  164. help_bit->set_text("");
  165. get_ok_button()->set_disabled(true);
  166. search_options->deselect_all();
  167. }
  168. }
  169. void CreateDialog::_add_type(const String &p_type, bool p_cpp_type) {
  170. if (search_options_types.has(p_type)) {
  171. return;
  172. }
  173. String inherits;
  174. if (p_cpp_type) {
  175. inherits = ClassDB::get_parent_class(p_type);
  176. } else if (ScriptServer::is_global_class(p_type)) {
  177. inherits = EditorNode::get_editor_data().script_class_get_base(p_type);
  178. } else {
  179. inherits = custom_type_parents[p_type];
  180. }
  181. _add_type(inherits, p_cpp_type || ClassDB::class_exists(inherits));
  182. TreeItem *item = search_options->create_item(search_options_types[inherits]);
  183. search_options_types[p_type] = item;
  184. _configure_search_option_item(item, p_type, p_cpp_type);
  185. }
  186. void CreateDialog::_configure_search_option_item(TreeItem *r_item, const String &p_type, const bool p_cpp_type) {
  187. bool script_type = ScriptServer::is_global_class(p_type);
  188. if (p_cpp_type) {
  189. r_item->set_text(0, p_type);
  190. } else if (script_type) {
  191. r_item->set_metadata(0, p_type);
  192. r_item->set_text(0, p_type + " (" + ScriptServer::get_global_class_path(p_type).get_file() + ")");
  193. } else {
  194. r_item->set_metadata(0, custom_type_parents[p_type]);
  195. r_item->set_text(0, p_type);
  196. }
  197. bool can_instance = (p_cpp_type && ClassDB::can_instance(p_type)) || !p_cpp_type;
  198. if (!can_instance) {
  199. r_item->set_custom_color(0, search_options->get_theme_color("disabled_font_color", "Editor"));
  200. r_item->set_selectable(0, false);
  201. }
  202. if (search_box->get_text() != "") {
  203. r_item->set_collapsed(false);
  204. } else {
  205. // Don't collapse the root node or an abstract node on the first tree level.
  206. bool should_collapse = p_type != base_type && (r_item->get_parent()->get_text(0) != base_type || can_instance);
  207. if (should_collapse && bool(EditorSettings::get_singleton()->get("docks/scene_tree/start_create_dialog_fully_expanded"))) {
  208. should_collapse = false; // Collapse all nodes anyway.
  209. }
  210. r_item->set_collapsed(should_collapse);
  211. }
  212. const String &description = DTR(EditorHelp::get_doc_data()->class_list[p_type].brief_description);
  213. r_item->set_tooltip(0, description);
  214. r_item->set_icon(0, EditorNode::get_singleton()->get_class_icon(p_type, icon_fallback));
  215. if (!p_cpp_type && !script_type) {
  216. Ref<Texture2D> icon = EditorNode::get_editor_data().get_custom_types()[custom_type_parents[p_type]][custom_type_indices[p_type]].icon;
  217. if (icon.is_valid()) {
  218. r_item->set_icon(0, icon);
  219. }
  220. }
  221. }
  222. String CreateDialog::_top_result(const Vector<String> p_candidates, const String &p_search_text) const {
  223. float highest_score = 0;
  224. int highest_index = 0;
  225. for (int i = 0; i < p_candidates.size(); i++) {
  226. float score = _score_type(p_candidates[i].get_slicec(' ', 0), p_search_text);
  227. if (score > highest_score) {
  228. highest_score = score;
  229. highest_index = i;
  230. }
  231. }
  232. return p_candidates[highest_index];
  233. }
  234. float CreateDialog::_score_type(const String &p_type, const String &p_search) const {
  235. float inverse_length = 1.f / float(p_type.length());
  236. // Favor types where search term is a substring close to the start of the type.
  237. float w = 0.5f;
  238. int pos = p_type.findn(p_search);
  239. float score = (pos > -1) ? 1.0f - w * MIN(1, 3 * pos * inverse_length) : MAX(0.f, .9f - w);
  240. // Favor shorter items: they resemble the search term more.
  241. w = 0.1f;
  242. score *= (1 - w) + w * (p_search.length() * inverse_length);
  243. score *= _is_type_preferred(p_type) ? 1.0f : 0.8f;
  244. // Add score for being a favorite type.
  245. score *= (favorite_list.find(p_type) > -1) ? 1.0f : 0.7f;
  246. // Look through at most 5 recent items
  247. bool in_recent = false;
  248. for (int i = 0; i < MIN(5, recent->get_item_count()); i++) {
  249. if (recent->get_item_text(i) == p_type) {
  250. in_recent = true;
  251. break;
  252. }
  253. }
  254. score *= in_recent ? 1.0f : 0.8f;
  255. return score;
  256. }
  257. void CreateDialog::_cleanup() {
  258. type_list.clear();
  259. favorite_list.clear();
  260. favorites->clear();
  261. recent->clear();
  262. custom_type_parents.clear();
  263. custom_type_indices.clear();
  264. }
  265. void CreateDialog::_confirmed() {
  266. String selected_item = get_selected_type();
  267. if (selected_item == String()) {
  268. return;
  269. }
  270. FileAccess *f = FileAccess::open(EditorSettings::get_singleton()->get_project_settings_dir().plus_file("create_recent." + base_type), FileAccess::WRITE);
  271. if (f) {
  272. f->store_line(selected_item);
  273. for (int i = 0; i < MIN(32, recent->get_item_count()); i++) {
  274. if (recent->get_item_text(i) != selected_item) {
  275. f->store_line(recent->get_item_text(i));
  276. }
  277. }
  278. memdelete(f);
  279. }
  280. emit_signal("create");
  281. hide();
  282. _cleanup();
  283. }
  284. void CreateDialog::_text_changed(const String &p_newtext) {
  285. _update_search();
  286. }
  287. void CreateDialog::_sbox_input(const Ref<InputEvent> &p_ie) {
  288. Ref<InputEventKey> k = p_ie;
  289. if (k.is_valid()) {
  290. switch (k->get_keycode()) {
  291. case KEY_UP:
  292. case KEY_DOWN:
  293. case KEY_PAGEUP:
  294. case KEY_PAGEDOWN: {
  295. search_options->call("_gui_input", k);
  296. search_box->accept_event();
  297. } break;
  298. }
  299. }
  300. }
  301. void CreateDialog::_notification(int p_what) {
  302. switch (p_what) {
  303. case NOTIFICATION_ENTER_TREE: {
  304. connect("confirmed", callable_mp(this, &CreateDialog::_confirmed));
  305. search_box->set_right_icon(search_options->get_theme_icon("Search", "EditorIcons"));
  306. search_box->set_clear_button_enabled(true);
  307. favorite->set_icon(search_options->get_theme_icon("Favorites", "EditorIcons"));
  308. } break;
  309. case NOTIFICATION_EXIT_TREE: {
  310. disconnect("confirmed", callable_mp(this, &CreateDialog::_confirmed));
  311. } break;
  312. case NOTIFICATION_VISIBILITY_CHANGED: {
  313. if (is_visible()) {
  314. search_box->call_deferred("grab_focus"); // still not visible
  315. search_box->select_all();
  316. } else {
  317. EditorSettings::get_singleton()->get_project_metadata("dialog_bounds", "create_new_node", Rect2(get_position(), get_size()));
  318. }
  319. } break;
  320. }
  321. }
  322. void CreateDialog::select_type(const String &p_type) {
  323. if (!search_options_types.has(p_type)) {
  324. return;
  325. }
  326. TreeItem *to_select = search_options_types[p_type];
  327. to_select->select(0);
  328. search_options->scroll_to_item(to_select);
  329. if (EditorHelp::get_doc_data()->class_list.has(p_type)) {
  330. help_bit->set_text(DTR(EditorHelp::get_doc_data()->class_list[p_type].brief_description));
  331. }
  332. favorite->set_disabled(false);
  333. favorite->set_pressed(favorite_list.find(p_type) != -1);
  334. get_ok_button()->set_disabled(false);
  335. }
  336. String CreateDialog::get_selected_type() {
  337. TreeItem *selected = search_options->get_selected();
  338. if (!selected) {
  339. return String();
  340. }
  341. return selected->get_text(0);
  342. }
  343. Variant CreateDialog::instance_selected() {
  344. TreeItem *selected = search_options->get_selected();
  345. if (!selected) {
  346. return Variant();
  347. }
  348. Variant md = selected->get_metadata(0);
  349. Variant obj;
  350. if (md.get_type() != Variant::NIL) {
  351. String custom = md;
  352. if (ScriptServer::is_global_class(custom)) {
  353. obj = EditorNode::get_editor_data().script_class_instance(custom);
  354. Node *n = Object::cast_to<Node>(obj);
  355. if (n) {
  356. n->set_name(custom);
  357. }
  358. } else {
  359. obj = EditorNode::get_editor_data().instance_custom_type(selected->get_text(0), custom);
  360. }
  361. } else {
  362. obj = ClassDB::instance(selected->get_text(0));
  363. }
  364. // Check if any Object-type property should be instantiated.
  365. List<PropertyInfo> pinfo;
  366. ((Object *)obj)->get_property_list(&pinfo);
  367. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  368. PropertyInfo pi = E->get();
  369. if (pi.type == Variant::OBJECT && pi.usage & PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT) {
  370. Object *prop = ClassDB::instance(pi.class_name);
  371. ((Object *)obj)->set(pi.name, prop);
  372. }
  373. }
  374. return obj;
  375. }
  376. void CreateDialog::_item_selected() {
  377. String name = get_selected_type();
  378. select_type(name);
  379. }
  380. void CreateDialog::_hide_requested() {
  381. _cancel_pressed(); // From AcceptDialog.
  382. }
  383. void CreateDialog::cancel_pressed() {
  384. _cleanup();
  385. }
  386. void CreateDialog::_favorite_toggled() {
  387. TreeItem *item = search_options->get_selected();
  388. if (!item) {
  389. return;
  390. }
  391. String name = item->get_text(0);
  392. if (favorite_list.find(name) == -1) {
  393. favorite_list.push_back(name);
  394. favorite->set_pressed(true);
  395. } else {
  396. favorite_list.erase(name);
  397. favorite->set_pressed(false);
  398. }
  399. _save_and_update_favorite_list();
  400. }
  401. void CreateDialog::_history_selected(int p_idx) {
  402. search_box->set_text(recent->get_item_text(p_idx).get_slicec(' ', 0));
  403. favorites->deselect_all();
  404. _update_search();
  405. }
  406. void CreateDialog::_favorite_selected() {
  407. TreeItem *item = favorites->get_selected();
  408. if (!item) {
  409. return;
  410. }
  411. search_box->set_text(item->get_text(0).get_slicec(' ', 0));
  412. recent->deselect_all();
  413. _update_search();
  414. }
  415. void CreateDialog::_history_activated(int p_idx) {
  416. _history_selected(p_idx);
  417. _confirmed();
  418. }
  419. void CreateDialog::_favorite_activated() {
  420. _favorite_selected();
  421. _confirmed();
  422. }
  423. Variant CreateDialog::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  424. TreeItem *ti = favorites->get_item_at_position(p_point);
  425. if (ti) {
  426. Dictionary d;
  427. d["type"] = "create_favorite_drag";
  428. d["class"] = ti->get_text(0);
  429. Button *tb = memnew(Button);
  430. tb->set_flat(true);
  431. tb->set_icon(ti->get_icon(0));
  432. tb->set_text(ti->get_text(0));
  433. favorites->set_drag_preview(tb);
  434. return d;
  435. }
  436. return Variant();
  437. }
  438. bool CreateDialog::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  439. Dictionary d = p_data;
  440. if (d.has("type") && String(d["type"]) == "create_favorite_drag") {
  441. favorites->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
  442. return true;
  443. }
  444. return false;
  445. }
  446. void CreateDialog::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  447. Dictionary d = p_data;
  448. TreeItem *ti = favorites->get_item_at_position(p_point);
  449. if (!ti) {
  450. return;
  451. }
  452. String drop_at = ti->get_text(0);
  453. int ds = favorites->get_drop_section_at_position(p_point);
  454. int drop_idx = favorite_list.find(drop_at);
  455. if (drop_idx < 0) {
  456. return;
  457. }
  458. String type = d["class"];
  459. int from_idx = favorite_list.find(type);
  460. if (from_idx < 0) {
  461. return;
  462. }
  463. if (drop_idx == from_idx) {
  464. ds = -1; //cause it will be gone
  465. } else if (drop_idx > from_idx) {
  466. drop_idx--;
  467. }
  468. favorite_list.remove(from_idx);
  469. if (ds < 0) {
  470. favorite_list.insert(drop_idx, type);
  471. } else {
  472. if (drop_idx >= favorite_list.size() - 1) {
  473. favorite_list.push_back(type);
  474. } else {
  475. favorite_list.insert(drop_idx + 1, type);
  476. }
  477. }
  478. _save_and_update_favorite_list();
  479. }
  480. void CreateDialog::_save_and_update_favorite_list() {
  481. favorites->clear();
  482. TreeItem *root = favorites->create_item();
  483. FileAccess *f = FileAccess::open(EditorSettings::get_singleton()->get_project_settings_dir().plus_file("favorites." + base_type), FileAccess::WRITE);
  484. if (f) {
  485. for (int i = 0; i < favorite_list.size(); i++) {
  486. String l = favorite_list[i];
  487. String name = l.get_slicec(' ', 0);
  488. if (!(ClassDB::class_exists(name) || ScriptServer::is_global_class(name))) {
  489. continue;
  490. }
  491. f->store_line(l);
  492. if (_is_class_disabled_by_feature_profile(name)) {
  493. continue;
  494. }
  495. TreeItem *ti = favorites->create_item(root);
  496. ti->set_text(0, l);
  497. ti->set_icon(0, EditorNode::get_singleton()->get_class_icon(name, icon_fallback));
  498. }
  499. memdelete(f);
  500. }
  501. emit_signal("favorites_updated");
  502. }
  503. void CreateDialog::_load_favorites_and_history() {
  504. String dir = EditorSettings::get_singleton()->get_project_settings_dir();
  505. FileAccess *f = FileAccess::open(dir.plus_file("create_recent." + base_type), FileAccess::READ);
  506. if (f) {
  507. while (!f->eof_reached()) {
  508. String l = f->get_line().strip_edges();
  509. String name = l.get_slicec(' ', 0);
  510. if ((ClassDB::class_exists(name) || ScriptServer::is_global_class(name)) && !_is_class_disabled_by_feature_profile(name)) {
  511. recent->add_item(l, EditorNode::get_singleton()->get_class_icon(name, icon_fallback));
  512. }
  513. }
  514. memdelete(f);
  515. }
  516. f = FileAccess::open(dir.plus_file("favorites." + base_type), FileAccess::READ);
  517. if (f) {
  518. while (!f->eof_reached()) {
  519. String l = f->get_line().strip_edges();
  520. if (l != String()) {
  521. favorite_list.push_back(l);
  522. }
  523. }
  524. memdelete(f);
  525. }
  526. }
  527. void CreateDialog::_bind_methods() {
  528. ClassDB::bind_method(D_METHOD("_save_and_update_favorite_list"), &CreateDialog::_save_and_update_favorite_list);
  529. ClassDB::bind_method("get_drag_data_fw", &CreateDialog::get_drag_data_fw);
  530. ClassDB::bind_method("can_drop_data_fw", &CreateDialog::can_drop_data_fw);
  531. ClassDB::bind_method("drop_data_fw", &CreateDialog::drop_data_fw);
  532. ADD_SIGNAL(MethodInfo("create"));
  533. ADD_SIGNAL(MethodInfo("favorites_updated"));
  534. }
  535. CreateDialog::CreateDialog() {
  536. base_type = "Object";
  537. preferred_search_result_type = "";
  538. type_blacklist.insert("PluginScript"); // PluginScript must be initialized before use, which is not possible here.
  539. type_blacklist.insert("ScriptCreateDialog"); // This is an exposed editor Node that doesn't have an Editor prefix.
  540. HSplitContainer *hsc = memnew(HSplitContainer);
  541. add_child(hsc);
  542. VSplitContainer *vsc = memnew(VSplitContainer);
  543. hsc->add_child(vsc);
  544. VBoxContainer *fav_vb = memnew(VBoxContainer);
  545. fav_vb->set_custom_minimum_size(Size2(150, 100) * EDSCALE);
  546. fav_vb->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  547. vsc->add_child(fav_vb);
  548. favorites = memnew(Tree);
  549. favorites->set_hide_root(true);
  550. favorites->set_hide_folding(true);
  551. favorites->set_allow_reselect(true);
  552. favorites->connect("cell_selected", callable_mp(this, &CreateDialog::_favorite_selected));
  553. favorites->connect("item_activated", callable_mp(this, &CreateDialog::_favorite_activated));
  554. favorites->add_theme_constant_override("draw_guides", 1);
  555. #ifndef _MSC_VER
  556. #warning cannot forward drag data to a non control, must be fixed
  557. #endif
  558. //favorites->set_drag_forwarding(this);
  559. fav_vb->add_margin_child(TTR("Favorites:"), favorites, true);
  560. VBoxContainer *rec_vb = memnew(VBoxContainer);
  561. vsc->add_child(rec_vb);
  562. rec_vb->set_custom_minimum_size(Size2(150, 100) * EDSCALE);
  563. rec_vb->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  564. recent = memnew(ItemList);
  565. rec_vb->add_margin_child(TTR("Recent:"), recent, true);
  566. recent->set_allow_reselect(true);
  567. recent->connect("item_selected", callable_mp(this, &CreateDialog::_history_selected));
  568. recent->connect("item_activated", callable_mp(this, &CreateDialog::_history_activated));
  569. recent->add_theme_constant_override("draw_guides", 1);
  570. VBoxContainer *vbc = memnew(VBoxContainer);
  571. vbc->set_custom_minimum_size(Size2(300, 0) * EDSCALE);
  572. vbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  573. hsc->add_child(vbc);
  574. search_box = memnew(LineEdit);
  575. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  576. search_box->connect("text_changed", callable_mp(this, &CreateDialog::_text_changed));
  577. search_box->connect("gui_input", callable_mp(this, &CreateDialog::_sbox_input));
  578. HBoxContainer *search_hb = memnew(HBoxContainer);
  579. search_hb->add_child(search_box);
  580. favorite = memnew(Button);
  581. favorite->set_flat(true);
  582. favorite->set_toggle_mode(true);
  583. favorite->set_tooltip(TTR("(Un)favorite selected item."));
  584. favorite->connect("pressed", callable_mp(this, &CreateDialog::_favorite_toggled));
  585. search_hb->add_child(favorite);
  586. vbc->add_margin_child(TTR("Search:"), search_hb);
  587. search_options = memnew(Tree);
  588. search_options->connect("item_activated", callable_mp(this, &CreateDialog::_confirmed));
  589. search_options->connect("cell_selected", callable_mp(this, &CreateDialog::_item_selected));
  590. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  591. help_bit = memnew(EditorHelpBit);
  592. help_bit->connect("request_hide", callable_mp(this, &CreateDialog::_hide_requested));
  593. vbc->add_margin_child(TTR("Description:"), help_bit);
  594. register_text_enter(search_box);
  595. set_hide_on_ok(false);
  596. }