create_dialog.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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 "editor_node.h"
  32. #include "object_type_db.h"
  33. #include "print_string.h"
  34. #include "scene/gui/box_container.h"
  35. #if 1
  36. #include "editor_help.h"
  37. #include "editor_settings.h"
  38. #include "os/keyboard.h"
  39. void CreateDialog::popup(bool p_dontclear) {
  40. recent->clear();
  41. FileAccess *f = FileAccess::open(EditorSettings::get_singleton()->get_project_settings_path().plus_file("create_recent." + base_type), FileAccess::READ);
  42. if (f) {
  43. TreeItem *root = recent->create_item();
  44. while (!f->eof_reached()) {
  45. String l = f->get_line().strip_edges();
  46. if (l != String()) {
  47. TreeItem *ti = recent->create_item(root);
  48. ti->set_text(0, l);
  49. if (has_icon(l, "EditorIcons")) {
  50. ti->set_icon(0, get_icon(l, "EditorIcons"));
  51. } else {
  52. ti->set_icon(0, get_icon("Object", "EditorIcons"));
  53. }
  54. }
  55. }
  56. memdelete(f);
  57. }
  58. favorites->clear();
  59. f = FileAccess::open(EditorSettings::get_singleton()->get_project_settings_path().plus_file("favorites." + base_type), FileAccess::READ);
  60. favorite_list.clear();
  61. if (f) {
  62. while (!f->eof_reached()) {
  63. String l = f->get_line().strip_edges();
  64. if (l != String()) {
  65. favorite_list.push_back(l);
  66. }
  67. }
  68. memdelete(f);
  69. } else {
  70. #if 0
  71. // I think this was way too confusing
  72. if (base_type=="Node") {
  73. //harcode some favorites :D
  74. favorite_list.push_back("Panel");
  75. favorite_list.push_back("Button");
  76. favorite_list.push_back("Label");
  77. favorite_list.push_back("LineEdit");
  78. favorite_list.push_back("Node2D");
  79. favorite_list.push_back("Sprite");
  80. favorite_list.push_back("Camera2D");
  81. favorite_list.push_back("Area2D");
  82. favorite_list.push_back("CollisionShape2D");
  83. favorite_list.push_back("Spatial");
  84. favorite_list.push_back("Camera");
  85. favorite_list.push_back("Area");
  86. favorite_list.push_back("CollisionShape");
  87. favorite_list.push_back("TestCube");
  88. favorite_list.push_back("AnimationPlayer");
  89. }
  90. #endif
  91. }
  92. _update_favorite_list();
  93. popup_centered_ratio();
  94. if (p_dontclear)
  95. search_box->select_all();
  96. else {
  97. search_box->clear();
  98. }
  99. search_box->grab_focus();
  100. _update_search();
  101. }
  102. void CreateDialog::_text_changed(const String &p_newtext) {
  103. _update_search();
  104. }
  105. void CreateDialog::_sbox_input(const InputEvent &p_ie) {
  106. if (p_ie.type == InputEvent::KEY && (p_ie.key.scancode == KEY_UP ||
  107. p_ie.key.scancode == KEY_DOWN ||
  108. p_ie.key.scancode == KEY_PAGEUP ||
  109. p_ie.key.scancode == KEY_PAGEDOWN)) {
  110. search_options->call("_input_event", p_ie);
  111. search_box->accept_event();
  112. }
  113. }
  114. void CreateDialog::add_type(const String &p_type, HashMap<String, TreeItem *> &p_types, TreeItem *p_root, TreeItem **to_select) {
  115. if (p_types.has(p_type))
  116. return;
  117. if (!ObjectTypeDB::is_type(p_type, base_type) || p_type == base_type)
  118. return;
  119. String inherits = ObjectTypeDB::type_inherits_from(p_type);
  120. TreeItem *parent = p_root;
  121. if (inherits.length()) {
  122. if (!p_types.has(inherits)) {
  123. add_type(inherits, p_types, p_root, to_select);
  124. }
  125. if (p_types.has(inherits))
  126. parent = p_types[inherits];
  127. }
  128. TreeItem *item = search_options->create_item(parent);
  129. item->set_text(0, p_type);
  130. if (!ObjectTypeDB::can_instance(p_type)) {
  131. item->set_custom_color(0, Color(0.5, 0.5, 0.5));
  132. item->set_selectable(0, false);
  133. } else {
  134. if ((!*to_select && (search_box->get_text().is_subsequence_ofi(p_type))) || search_box->get_text() == p_type) {
  135. *to_select = item;
  136. }
  137. }
  138. if (bool(EditorSettings::get_singleton()->get("scenetree_editor/start_create_dialog_fully_expanded"))) {
  139. item->set_collapsed(false);
  140. } else {
  141. // don't collapse search results
  142. bool collapse = (search_box->get_text() == "");
  143. // don't collapse the root node
  144. collapse &= (item != p_root);
  145. // don't collapse abstract nodes on the first tree level
  146. collapse &= ((parent != p_root) || (ObjectTypeDB::can_instance(p_type)));
  147. item->set_collapsed(collapse);
  148. }
  149. const String &description = EditorHelp::get_doc_data()->class_list[p_type].brief_description;
  150. item->set_tooltip(0, description);
  151. if (has_icon(p_type, "EditorIcons")) {
  152. item->set_icon(0, get_icon(p_type, "EditorIcons"));
  153. }
  154. p_types[p_type] = item;
  155. }
  156. void CreateDialog::_update_search() {
  157. search_options->clear();
  158. favorite->set_disabled(true);
  159. help_bit->set_text("");
  160. /*
  161. TreeItem *root = search_options->create_item();
  162. _parse_fs(EditorFileSystem::get_singleton()->get_filesystem());
  163. */
  164. List<StringName> type_list;
  165. ObjectTypeDB::get_type_list(&type_list);
  166. HashMap<String, TreeItem *> types;
  167. TreeItem *root = search_options->create_item();
  168. root->set_text(0, base_type);
  169. if (has_icon(base_type, "EditorIcons")) {
  170. root->set_icon(0, get_icon(base_type, "EditorIcons"));
  171. }
  172. List<StringName>::Element *I = type_list.front();
  173. TreeItem *to_select = search_box->get_text() == base_type ? root : NULL;
  174. for (; I; I = I->next()) {
  175. String type = I->get();
  176. if (base_type == "Node" && type.begins_with("Editor"))
  177. continue; // do not show editor nodes
  178. if (!ObjectTypeDB::can_instance(type))
  179. continue; // cant create what can't be instanced
  180. if (search_box->get_text() == "") {
  181. add_type(type, types, root, &to_select);
  182. } else {
  183. bool found = false;
  184. String type = I->get();
  185. while (type != "" && ObjectTypeDB::is_type(type, base_type) && type != base_type) {
  186. if (search_box->get_text().is_subsequence_ofi(type)) {
  187. found = true;
  188. break;
  189. }
  190. type = ObjectTypeDB::type_inherits_from(type);
  191. }
  192. if (found)
  193. add_type(I->get(), types, root, &to_select);
  194. }
  195. if (EditorNode::get_editor_data().get_custom_types().has(type) && ObjectTypeDB::is_type(type, base_type)) {
  196. //there are custom types based on this... cool.
  197. //print_line("there are custom types");
  198. const Vector<EditorData::CustomType> &ct = EditorNode::get_editor_data().get_custom_types()[type];
  199. for (int i = 0; i < ct.size(); i++) {
  200. bool show = search_box->get_text().is_subsequence_ofi(ct[i].name);
  201. if (!show)
  202. continue;
  203. if (!types.has(type))
  204. add_type(type, types, root, &to_select);
  205. TreeItem *ti;
  206. if (types.has(type))
  207. ti = types[type];
  208. else
  209. ti = search_options->get_root();
  210. TreeItem *item = search_options->create_item(ti);
  211. item->set_metadata(0, type);
  212. item->set_text(0, ct[i].name);
  213. if (ct[i].icon.is_valid()) {
  214. item->set_icon(0, ct[i].icon);
  215. }
  216. if (!to_select || ct[i].name == search_box->get_text()) {
  217. to_select = item;
  218. }
  219. }
  220. }
  221. }
  222. if (to_select) {
  223. to_select->select(0);
  224. favorite->set_disabled(false);
  225. favorite->set_pressed(favorite_list.find(to_select->get_text(0)) != -1);
  226. }
  227. get_ok()->set_disabled(root->get_children() == NULL);
  228. }
  229. void CreateDialog::_confirmed() {
  230. TreeItem *ti = search_options->get_selected();
  231. if (!ti)
  232. return;
  233. FileAccess *f = FileAccess::open(EditorSettings::get_singleton()->get_project_settings_path().plus_file("create_recent." + base_type), FileAccess::WRITE);
  234. if (f) {
  235. f->store_line(get_selected_type());
  236. TreeItem *t = recent->get_root();
  237. if (t)
  238. t = t->get_children();
  239. int count = 0;
  240. while (t) {
  241. if (t->get_text(0) != get_selected_type()) {
  242. f->store_line(t->get_text(0));
  243. }
  244. if (count > 32) {
  245. //limit it to 32 entries..
  246. break;
  247. }
  248. t = t->get_next();
  249. count++;
  250. }
  251. memdelete(f);
  252. }
  253. emit_signal("create");
  254. hide();
  255. }
  256. void CreateDialog::_notification(int p_what) {
  257. if (p_what == NOTIFICATION_ENTER_TREE) {
  258. connect("confirmed", this, "_confirmed");
  259. favorite->set_icon(get_icon("Favorites", "EditorIcons"));
  260. }
  261. if (p_what == NOTIFICATION_EXIT_TREE) {
  262. disconnect("confirmed", this, "_confirmed");
  263. }
  264. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  265. if (is_visible()) {
  266. search_box->call_deferred("grab_focus"); // still not visible
  267. search_box->select_all();
  268. }
  269. }
  270. }
  271. void CreateDialog::set_base_type(const String &p_base) {
  272. base_type = p_base;
  273. set_title(TTR("Create New") + " " + p_base);
  274. _update_search();
  275. }
  276. String CreateDialog::get_selected_type() {
  277. TreeItem *selected = search_options->get_selected();
  278. if (selected)
  279. return selected->get_text(0);
  280. else
  281. return String();
  282. }
  283. Object *CreateDialog::instance_selected() {
  284. TreeItem *selected = search_options->get_selected();
  285. if (selected) {
  286. String custom = selected->get_metadata(0);
  287. if (custom != String()) {
  288. if (EditorNode::get_editor_data().get_custom_types().has(custom)) {
  289. for (int i = 0; i < EditorNode::get_editor_data().get_custom_types()[custom].size(); i++) {
  290. if (EditorNode::get_editor_data().get_custom_types()[custom][i].name == selected->get_text(0)) {
  291. Ref<Texture> icon = EditorNode::get_editor_data().get_custom_types()[custom][i].icon;
  292. Ref<Script> script = EditorNode::get_editor_data().get_custom_types()[custom][i].script;
  293. String name = selected->get_text(0);
  294. Object *ob = ObjectTypeDB::instance(custom);
  295. ERR_FAIL_COND_V(!ob, NULL);
  296. if (ob->is_type("Node")) {
  297. ob->call("set_name", name);
  298. }
  299. ob->set_script(script.get_ref_ptr());
  300. if (icon.is_valid())
  301. ob->set_meta("_editor_icon", icon);
  302. return ob;
  303. }
  304. }
  305. }
  306. } else {
  307. return ObjectTypeDB::instance(selected->get_text(0));
  308. }
  309. }
  310. return NULL;
  311. }
  312. String CreateDialog::get_base_type() const {
  313. return base_type;
  314. }
  315. void CreateDialog::_item_selected() {
  316. TreeItem *item = search_options->get_selected();
  317. if (!item)
  318. return;
  319. String name = item->get_text(0);
  320. favorite->set_disabled(false);
  321. favorite->set_pressed(favorite_list.find(name) != -1);
  322. if (!EditorHelp::get_doc_data()->class_list.has(name))
  323. return;
  324. help_bit->set_text(EditorHelp::get_doc_data()->class_list[name].brief_description);
  325. }
  326. void CreateDialog::_favorite_toggled() {
  327. TreeItem *item = search_options->get_selected();
  328. if (!item)
  329. return;
  330. String name = item->get_text(0);
  331. if (favorite_list.find(name) == -1) {
  332. favorite_list.push_back(name);
  333. favorite->set_pressed(true);
  334. } else {
  335. favorite_list.erase(name);
  336. favorite->set_pressed(false);
  337. }
  338. _save_favorite_list();
  339. _update_favorite_list();
  340. }
  341. void CreateDialog::_save_favorite_list() {
  342. FileAccess *f = FileAccess::open(EditorSettings::get_singleton()->get_project_settings_path().plus_file("favorites." + base_type), FileAccess::WRITE);
  343. if (f) {
  344. for (int i = 0; i < favorite_list.size(); i++) {
  345. f->store_line(favorite_list[i]);
  346. }
  347. memdelete(f);
  348. }
  349. }
  350. void CreateDialog::_update_favorite_list() {
  351. favorites->clear();
  352. TreeItem *root = favorites->create_item();
  353. for (int i = 0; i < favorite_list.size(); i++) {
  354. TreeItem *ti = favorites->create_item(root);
  355. String l = favorite_list[i];
  356. ti->set_text(0, l);
  357. if (has_icon(l, "EditorIcons")) {
  358. ti->set_icon(0, get_icon(l, "EditorIcons"));
  359. } else {
  360. ti->set_icon(0, get_icon("Object", "EditorIcons"));
  361. }
  362. }
  363. }
  364. void CreateDialog::_history_selected() {
  365. TreeItem *item = recent->get_selected();
  366. if (!item)
  367. return;
  368. search_box->set_text(item->get_text(0));
  369. _update_search();
  370. }
  371. void CreateDialog::_favorite_selected() {
  372. TreeItem *item = favorites->get_selected();
  373. if (!item)
  374. return;
  375. search_box->set_text(item->get_text(0));
  376. _update_search();
  377. }
  378. void CreateDialog::_history_activated() {
  379. _history_selected();
  380. _confirmed();
  381. }
  382. void CreateDialog::_favorite_activated() {
  383. _favorite_selected();
  384. _confirmed();
  385. }
  386. Variant CreateDialog::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  387. TreeItem *ti = favorites->get_item_at_pos(p_point);
  388. if (ti) {
  389. Dictionary d;
  390. d["type"] = "create_favorite_drag";
  391. d["class"] = ti->get_text(0);
  392. ToolButton *tb = memnew(ToolButton);
  393. tb->set_icon(ti->get_icon(0));
  394. tb->set_text(ti->get_text(0));
  395. set_drag_preview(tb);
  396. return d;
  397. }
  398. return Variant();
  399. }
  400. bool CreateDialog::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  401. Dictionary d = p_data;
  402. if (d.has("type") && String(d["type"]) == "create_favorite_drag") {
  403. favorites->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
  404. return true;
  405. }
  406. return false;
  407. }
  408. void CreateDialog::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  409. Dictionary d = p_data;
  410. TreeItem *ti = favorites->get_item_at_pos(p_point);
  411. if (!ti)
  412. return;
  413. String drop_at = ti->get_text(0);
  414. int ds = favorites->get_drop_section_at_pos(p_point);
  415. int drop_idx = favorite_list.find(drop_at);
  416. if (drop_idx < 0)
  417. return;
  418. String type = d["class"];
  419. int from_idx = favorite_list.find(type);
  420. if (from_idx < 0)
  421. return;
  422. if (drop_idx == from_idx) {
  423. ds = -1; //cause it will be gone
  424. } else if (drop_idx > from_idx) {
  425. drop_idx--;
  426. }
  427. favorite_list.remove(from_idx);
  428. if (ds < 0) {
  429. favorite_list.insert(drop_idx, type);
  430. } else {
  431. if (drop_idx >= favorite_list.size() - 1) {
  432. favorite_list.push_back(type);
  433. } else {
  434. favorite_list.insert(drop_idx + 1, type);
  435. }
  436. }
  437. _save_favorite_list();
  438. _update_favorite_list();
  439. }
  440. void CreateDialog::_bind_methods() {
  441. ObjectTypeDB::bind_method(_MD("_text_changed"), &CreateDialog::_text_changed);
  442. ObjectTypeDB::bind_method(_MD("_confirmed"), &CreateDialog::_confirmed);
  443. ObjectTypeDB::bind_method(_MD("_sbox_input"), &CreateDialog::_sbox_input);
  444. ObjectTypeDB::bind_method(_MD("_item_selected"), &CreateDialog::_item_selected);
  445. ObjectTypeDB::bind_method(_MD("_favorite_toggled"), &CreateDialog::_favorite_toggled);
  446. ObjectTypeDB::bind_method(_MD("_history_selected"), &CreateDialog::_history_selected);
  447. ObjectTypeDB::bind_method(_MD("_favorite_selected"), &CreateDialog::_favorite_selected);
  448. ObjectTypeDB::bind_method(_MD("_history_activated"), &CreateDialog::_history_activated);
  449. ObjectTypeDB::bind_method(_MD("_favorite_activated"), &CreateDialog::_favorite_activated);
  450. ObjectTypeDB::bind_method("get_drag_data_fw", &CreateDialog::get_drag_data_fw);
  451. ObjectTypeDB::bind_method("can_drop_data_fw", &CreateDialog::can_drop_data_fw);
  452. ObjectTypeDB::bind_method("drop_data_fw", &CreateDialog::drop_data_fw);
  453. ADD_SIGNAL(MethodInfo("create"));
  454. }
  455. CreateDialog::CreateDialog() {
  456. HSplitContainer *hbc = memnew(HSplitContainer);
  457. add_child(hbc);
  458. set_child_rect(hbc);
  459. VBoxContainer *lvbc = memnew(VBoxContainer);
  460. hbc->add_child(lvbc);
  461. lvbc->set_custom_minimum_size(Size2(150, 0) * EDSCALE);
  462. favorites = memnew(Tree);
  463. lvbc->add_margin_child(TTR("Favorites:"), favorites, true);
  464. favorites->set_hide_root(true);
  465. favorites->set_hide_folding(true);
  466. favorites->connect("cell_selected", this, "_favorite_selected");
  467. favorites->connect("item_activated", this, "_favorite_activated");
  468. favorites->set_drag_forwarding(this);
  469. recent = memnew(Tree);
  470. lvbc->add_margin_child(TTR("Recent:"), recent, true);
  471. recent->set_hide_root(true);
  472. recent->set_hide_folding(true);
  473. recent->connect("cell_selected", this, "_history_selected");
  474. recent->connect("item_activated", this, "_history_activated");
  475. VBoxContainer *vbc = memnew(VBoxContainer);
  476. hbc->add_child(vbc);
  477. vbc->set_h_size_flags(SIZE_EXPAND_FILL);
  478. HBoxContainer *search_hb = memnew(HBoxContainer);
  479. search_box = memnew(LineEdit);
  480. search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  481. search_hb->add_child(search_box);
  482. favorite = memnew(Button);
  483. favorite->set_toggle_mode(true);
  484. search_hb->add_child(favorite);
  485. favorite->connect("pressed", this, "_favorite_toggled");
  486. vbc->add_margin_child(TTR("Search:"), search_hb);
  487. search_box->connect("text_changed", this, "_text_changed");
  488. search_box->connect("input_event", this, "_sbox_input");
  489. search_options = memnew(Tree);
  490. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  491. get_ok()->set_text(TTR("Create"));
  492. get_ok()->set_disabled(true);
  493. register_text_enter(search_box);
  494. set_hide_on_ok(false);
  495. search_options->connect("item_activated", this, "_confirmed");
  496. search_options->connect("cell_selected", this, "_item_selected");
  497. // search_options->set_hide_root(true);
  498. base_type = "Object";
  499. help_bit = memnew(EditorHelpBit);
  500. vbc->add_margin_child(TTR("Description:"), help_bit);
  501. help_bit->connect("request_hide", this, "_closed");
  502. }
  503. #else
  504. //old create dialog, disabled
  505. void CreateDialog::_notification(int p_what) {
  506. if (p_what == NOTIFICATION_READY) {
  507. connect("confirmed", this, "_create");
  508. update_tree();
  509. }
  510. if (p_what == NOTIFICATION_DRAW) {
  511. //RID ci = get_canvas_item();
  512. //get_stylebox("panel","PopupMenu")->draw(ci,Rect2(Point2(),get_size()));
  513. }
  514. }
  515. void CreateDialog::_create() {
  516. if (tree->get_selected())
  517. emit_signal("create");
  518. hide();
  519. }
  520. void CreateDialog::_cancel() {
  521. hide();
  522. }
  523. void CreateDialog::_text_changed(String p_text) {
  524. update_tree();
  525. }
  526. void CreateDialog::add_type(const String &p_type, HashMap<String, TreeItem *> &p_types, TreeItem *p_root) {
  527. if (p_types.has(p_type))
  528. return;
  529. if (!ObjectTypeDB::is_type(p_type, base) || p_type == base)
  530. return;
  531. String inherits = ObjectTypeDB::type_inherits_from(p_type);
  532. TreeItem *parent = p_root;
  533. if (inherits.length()) {
  534. if (!p_types.has(inherits)) {
  535. add_type(inherits, p_types, p_root);
  536. }
  537. if (p_types.has(inherits))
  538. parent = p_types[inherits];
  539. }
  540. TreeItem *item = tree->create_item(parent);
  541. item->set_text(0, p_type);
  542. if (!ObjectTypeDB::can_instance(p_type)) {
  543. item->set_custom_color(0, Color(0.5, 0.5, 0.5));
  544. item->set_selectable(0, false);
  545. }
  546. if (has_icon(p_type, "EditorIcons")) {
  547. item->set_icon(0, get_icon(p_type, "EditorIcons"));
  548. }
  549. p_types[p_type] = item;
  550. }
  551. void CreateDialog::update_tree() {
  552. tree->clear();
  553. List<String> type_list;
  554. ObjectTypeDB::get_type_list(&type_list);
  555. HashMap<String, TreeItem *> types;
  556. TreeItem *root = tree->create_item();
  557. root->set_text(0, base);
  558. List<String>::Element *I = type_list.front();
  559. for (; I; I = I->next()) {
  560. String type = I->get();
  561. if (!ObjectTypeDB::can_instance(type))
  562. continue; // cant create what can't be instanced
  563. if (filter->get_text() == "")
  564. add_type(type, types, root);
  565. else {
  566. bool found = false;
  567. String type = I->get();
  568. while (type != "" && ObjectTypeDB::is_type(type, base) && type != base) {
  569. if (type.findn(filter->get_text()) != -1) {
  570. found = true;
  571. break;
  572. }
  573. type = ObjectTypeDB::type_inherits_from(type);
  574. }
  575. if (found)
  576. add_type(I->get(), types, root);
  577. }
  578. if (EditorNode::get_editor_data().get_custom_types().has(type)) {
  579. //there are custom types based on this... cool.
  580. const Vector<EditorData::CustomType> &ct = EditorNode::get_editor_data().get_custom_types()[type];
  581. for (int i = 0; i < ct.size(); i++) {
  582. bool show = filter->get_text() == "" || ct[i].name.findn(filter->get_text()) != -1;
  583. if (!show)
  584. continue;
  585. if (!types.has(type))
  586. add_type(type, types, root);
  587. TreeItem *ti;
  588. if (types.has(type))
  589. ti = types[type];
  590. else
  591. ti = tree->get_root();
  592. TreeItem *item = tree->create_item(ti);
  593. item->set_metadata(0, type);
  594. item->set_text(0, ct[i].name);
  595. if (ct[i].icon.is_valid()) {
  596. item->set_icon(0, ct[i].icon);
  597. }
  598. }
  599. }
  600. }
  601. }
  602. Object *CreateDialog::instance_selected() {
  603. if (!tree->get_selected())
  604. return NULL;
  605. String base = String(tree->get_selected()->get_metadata(0));
  606. if (base != "") {
  607. String name = tree->get_selected()->get_text(0);
  608. if (EditorNode::get_editor_data().get_custom_types().has(base)) {
  609. const Vector<EditorData::CustomType> &ct = EditorNode::get_editor_data().get_custom_types()[base];
  610. for (int i = 0; i < ct.size(); i++) {
  611. if (ct[i].name == name) {
  612. Object *obj = ObjectTypeDB::instance(base);
  613. ERR_FAIL_COND_V(!obj, NULL);
  614. obj->set_script(ct[i].script.get_ref_ptr());
  615. if (ct[i].icon.is_valid())
  616. obj->set_meta("_editor_icon", ct[i].icon);
  617. return obj;
  618. }
  619. }
  620. }
  621. ERR_FAIL_V(NULL);
  622. }
  623. return ObjectTypeDB::instance(tree->get_selected()->get_text(0));
  624. }
  625. void CreateDialog::_bind_methods() {
  626. ObjectTypeDB::bind_method("_create", &CreateDialog::_create);
  627. ObjectTypeDB::bind_method("_cancel", &CreateDialog::_cancel);
  628. ObjectTypeDB::bind_method("_text_changed", &CreateDialog::_text_changed);
  629. ADD_SIGNAL(MethodInfo("create"));
  630. }
  631. void CreateDialog::set_base_type(const String &p_base) {
  632. set_title(vformat("Create %s Type", p_base));
  633. if (base == p_base)
  634. return;
  635. base = p_base;
  636. if (is_inside_scene())
  637. update_tree();
  638. }
  639. String CreateDialog::get_base_type() const {
  640. return base;
  641. }
  642. CreateDialog::CreateDialog() {
  643. VBoxContainer *vbc = memnew(VBoxContainer);
  644. add_child(vbc);
  645. set_child_rect(vbc);
  646. get_ok()->set_text("Create");
  647. tree = memnew(Tree);
  648. vbc->add_margin_child("Type:", tree, true);
  649. //tree->set_hide_root(true);
  650. filter = memnew(LineEdit);
  651. vbc->add_margin_child("Filter:", filter);
  652. base = "Node";
  653. set_as_toplevel(true);
  654. tree->connect("item_activated", this, "_create");
  655. filter->connect("text_changed", this, "_text_changed");
  656. }
  657. CreateDialog::~CreateDialog() {
  658. }
  659. #endif