create_dialog.cpp 24 KB

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