script_create_dialog.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /*************************************************************************/
  2. /* script_create_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "script_create_dialog.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_scale.h"
  33. #include "editor_file_system.h"
  34. #include "io/resource_saver.h"
  35. #include "os/file_access.h"
  36. #include "project_settings.h"
  37. #include "script_language.h"
  38. void ScriptCreateDialog::_notification(int p_what) {
  39. switch (p_what) {
  40. case NOTIFICATION_ENTER_TREE: {
  41. path_button->set_icon(get_icon("Folder", "EditorIcons"));
  42. parent_browse_button->set_icon(get_icon("Folder", "EditorIcons"));
  43. }
  44. }
  45. }
  46. void ScriptCreateDialog::config(const String &p_base_name, const String &p_base_path) {
  47. class_name->set_text("");
  48. parent_name->set_text(p_base_name);
  49. if (p_base_path != "") {
  50. initial_bp = p_base_path.get_basename();
  51. file_path->set_text(initial_bp + "." + ScriptServer::get_language(language_menu->get_selected())->get_extension());
  52. } else {
  53. initial_bp = "";
  54. file_path->set_text("");
  55. }
  56. _lang_changed(current_language);
  57. _parent_name_changed(parent_name->get_text());
  58. _class_name_changed("");
  59. _path_changed(file_path->get_text());
  60. }
  61. bool ScriptCreateDialog::_validate(const String &p_string) {
  62. if (p_string.length() == 0)
  63. return false;
  64. String path_chars = "\"res://";
  65. bool is_val_path = ScriptServer::get_language(language_menu->get_selected())->can_inherit_from_file();
  66. for (int i = 0; i < p_string.length(); i++) {
  67. if (i == 0) {
  68. if (p_string[0] >= '0' && p_string[0] <= '9')
  69. return false; // no start with number plz
  70. }
  71. if (i == p_string.length() - 1 && is_val_path)
  72. return p_string[i] == '\"';
  73. if (is_val_path && i < path_chars.length()) {
  74. if (p_string[i] != path_chars[i])
  75. is_val_path = false;
  76. else
  77. continue;
  78. }
  79. bool valid_char = (p_string[i] >= '0' && p_string[i] <= '9') || (p_string[i] >= 'a' && p_string[i] <= 'z') || (p_string[i] >= 'A' && p_string[i] <= 'Z') || p_string[i] == '_' || p_string[i] == '-' || (is_val_path && (p_string[i] == '/' || p_string[i] == '.'));
  80. if (!valid_char)
  81. return false;
  82. }
  83. return true;
  84. }
  85. void ScriptCreateDialog::_class_name_changed(const String &p_name) {
  86. if (_validate(class_name->get_text())) {
  87. is_class_name_valid = true;
  88. } else {
  89. is_class_name_valid = false;
  90. }
  91. _update_dialog();
  92. }
  93. void ScriptCreateDialog::_parent_name_changed(const String &p_parent) {
  94. if (_validate(parent_name->get_text())) {
  95. is_parent_name_valid = true;
  96. } else {
  97. is_parent_name_valid = false;
  98. }
  99. _update_dialog();
  100. }
  101. void ScriptCreateDialog::_template_changed(int p_template) {
  102. String selected_template = p_template == 0 ? "" : template_menu->get_item_text(template_menu->get_selected());
  103. EditorSettings::get_singleton()->set_project_metadata("script_setup", "last_selected_template", selected_template);
  104. if (p_template == 0) {
  105. //default
  106. script_template = "";
  107. return;
  108. }
  109. String ext = ScriptServer::get_language(language_menu->get_selected())->get_extension();
  110. String name = template_list[p_template - 1] + "." + ext;
  111. script_template = EditorSettings::get_singleton()->get_settings_path() + "/script_templates/" + name;
  112. }
  113. void ScriptCreateDialog::ok_pressed() {
  114. if (is_new_script_created) {
  115. _create_new();
  116. } else {
  117. _load_exist();
  118. }
  119. is_new_script_created = true;
  120. _update_dialog();
  121. }
  122. void ScriptCreateDialog::_create_new() {
  123. String cname_param;
  124. if (has_named_classes) {
  125. cname_param = class_name->get_text();
  126. } else {
  127. cname_param = ProjectSettings::get_singleton()->localize_path(file_path->get_text()).get_file().get_basename();
  128. }
  129. Ref<Script> scr;
  130. if (script_template != "") {
  131. scr = ResourceLoader::load(script_template);
  132. if (scr.is_null()) {
  133. alert->get_ok()->set_text(TTR("OK"));
  134. alert->set_text(vformat(TTR("Error loading template '%s'"), script_template));
  135. alert->popup_centered();
  136. return;
  137. }
  138. scr = scr->duplicate();
  139. ScriptServer::get_language(language_menu->get_selected())->make_template(cname_param, parent_name->get_text(), scr);
  140. } else {
  141. scr = ScriptServer::get_language(language_menu->get_selected())->get_template(cname_param, parent_name->get_text());
  142. }
  143. if (has_named_classes) {
  144. String cname = class_name->get_text();
  145. if (cname.length())
  146. scr->set_name(cname);
  147. }
  148. if (!is_built_in) {
  149. String lpath = ProjectSettings::get_singleton()->localize_path(file_path->get_text());
  150. scr->set_path(lpath);
  151. Error err = ResourceSaver::save(lpath, scr, ResourceSaver::FLAG_CHANGE_PATH);
  152. if (err != OK) {
  153. alert->set_text(TTR("Error - Could not create script in filesystem."));
  154. alert->popup_centered();
  155. return;
  156. }
  157. }
  158. hide();
  159. emit_signal("script_created", scr);
  160. }
  161. void ScriptCreateDialog::_load_exist() {
  162. String path = file_path->get_text();
  163. RES p_script = ResourceLoader::load(path, "Script");
  164. if (p_script.is_null()) {
  165. alert->get_ok()->set_text(TTR("OK"));
  166. alert->set_text(vformat(TTR("Error loading script from %s"), path));
  167. alert->popup_centered();
  168. return;
  169. }
  170. hide();
  171. emit_signal("script_created", p_script.get_ref_ptr());
  172. }
  173. void ScriptCreateDialog::_lang_changed(int l) {
  174. l = language_menu->get_selected();
  175. ScriptLanguage *language = ScriptServer::get_language(l);
  176. if (language->has_named_classes()) {
  177. has_named_classes = true;
  178. } else {
  179. has_named_classes = false;
  180. }
  181. if (language->supports_builtin_mode()) {
  182. supports_built_in = true;
  183. } else {
  184. supports_built_in = false;
  185. is_built_in = false;
  186. }
  187. if (ScriptServer::get_language(l)->can_inherit_from_file()) {
  188. can_inherit_from_file = true;
  189. } else {
  190. can_inherit_from_file = false;
  191. }
  192. String selected_ext = "." + language->get_extension();
  193. String path = file_path->get_text();
  194. String extension = "";
  195. if (path != "") {
  196. if (path.find(".") >= 0) {
  197. extension = path.get_extension();
  198. }
  199. if (extension.length() == 0) {
  200. // add extension if none
  201. path += selected_ext;
  202. _path_changed(path);
  203. } else {
  204. // change extension by selected language
  205. List<String> extensions;
  206. // get all possible extensions for script
  207. for (int l = 0; l < language_menu->get_item_count(); l++) {
  208. ScriptServer::get_language(l)->get_recognized_extensions(&extensions);
  209. }
  210. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  211. if (E->get().nocasecmp_to(extension) == 0) {
  212. path = path.get_basename() + selected_ext;
  213. _path_changed(path);
  214. break;
  215. }
  216. }
  217. }
  218. } else {
  219. path = "class" + selected_ext;
  220. _path_changed(path);
  221. }
  222. file_path->set_text(path);
  223. bool use_templates = language->is_using_templates();
  224. template_menu->set_disabled(!use_templates);
  225. template_menu->clear();
  226. if (use_templates) {
  227. template_list = EditorSettings::get_singleton()->get_script_templates(language->get_extension());
  228. String last_lang = EditorSettings::get_singleton()->get_project_metadata("script_setup", "last_selected_language", "");
  229. String last_template = EditorSettings::get_singleton()->get_project_metadata("script_setup", "last_selected_template", "");
  230. template_menu->add_item(TTR("Default"));
  231. for (int i = 0; i < template_list.size(); i++) {
  232. String s = template_list[i].capitalize();
  233. template_menu->add_item(s);
  234. if (language_menu->get_item_text(language_menu->get_selected()) == last_lang && last_template == s) {
  235. template_menu->select(i + 1);
  236. }
  237. }
  238. } else {
  239. template_menu->add_item(TTR("N/A"));
  240. script_template = "";
  241. }
  242. _template_changed(template_menu->get_selected());
  243. EditorSettings::get_singleton()->set_project_metadata("script_setup", "last_selected_language", language_menu->get_item_text(language_menu->get_selected()));
  244. _update_dialog();
  245. }
  246. void ScriptCreateDialog::_built_in_pressed() {
  247. if (internal->is_pressed()) {
  248. is_built_in = true;
  249. } else {
  250. is_built_in = false;
  251. }
  252. _update_dialog();
  253. }
  254. void ScriptCreateDialog::_browse_path(bool browse_parent) {
  255. is_browsing_parent = browse_parent;
  256. file_browse->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  257. file_browse->set_disable_overwrite_warning(true);
  258. file_browse->clear_filters();
  259. List<String> extensions;
  260. int lang = language_menu->get_selected();
  261. ScriptServer::get_language(lang)->get_recognized_extensions(&extensions);
  262. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  263. file_browse->add_filter("*." + E->get());
  264. }
  265. file_browse->set_current_path(file_path->get_text());
  266. file_browse->popup_centered_ratio();
  267. }
  268. void ScriptCreateDialog::_file_selected(const String &p_file) {
  269. String p = ProjectSettings::get_singleton()->localize_path(p_file);
  270. if (is_browsing_parent) {
  271. parent_name->set_text("\"" + p + "\"");
  272. _class_name_changed("\"" + p + "\"");
  273. } else {
  274. file_path->set_text(p);
  275. _path_changed(p);
  276. }
  277. }
  278. void ScriptCreateDialog::_path_changed(const String &p_path) {
  279. is_path_valid = false;
  280. is_new_script_created = true;
  281. String p = p_path;
  282. if (p == "") {
  283. _msg_path_valid(false, TTR("Path is empty"));
  284. _update_dialog();
  285. return;
  286. }
  287. p = ProjectSettings::get_singleton()->localize_path(p);
  288. if (!p.begins_with("res://")) {
  289. _msg_path_valid(false, TTR("Path is not local"));
  290. _update_dialog();
  291. return;
  292. }
  293. if (p.find("/") || p.find("\\")) {
  294. DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  295. if (d->change_dir(p.get_base_dir()) != OK) {
  296. _msg_path_valid(false, TTR("Invalid base path"));
  297. memdelete(d);
  298. _update_dialog();
  299. return;
  300. }
  301. memdelete(d);
  302. }
  303. /* Does file already exist */
  304. DirAccess *f = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  305. if (f->dir_exists(p)) {
  306. is_new_script_created = false;
  307. is_path_valid = false;
  308. _msg_path_valid(false, TTR("Directory of the same name exists"));
  309. } else if (f->file_exists(p)) {
  310. is_new_script_created = false;
  311. is_path_valid = true;
  312. _msg_path_valid(true, TTR("File exists, will be reused"));
  313. } else {
  314. path_error_label->set_text("");
  315. }
  316. memdelete(f);
  317. _update_dialog();
  318. /* Check file extension */
  319. String extension = p.get_extension();
  320. List<String> extensions;
  321. // get all possible extensions for script
  322. for (int l = 0; l < language_menu->get_item_count(); l++) {
  323. ScriptServer::get_language(l)->get_recognized_extensions(&extensions);
  324. }
  325. bool found = false;
  326. bool match = false;
  327. int index = 0;
  328. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  329. if (E->get().nocasecmp_to(extension) == 0) {
  330. //FIXME (?) - changing language this way doesn't update controls, needs rework
  331. //language_menu->select(index); // change Language option by extension
  332. found = true;
  333. if (E->get() == ScriptServer::get_language(language_menu->get_selected())->get_extension()) {
  334. match = true;
  335. }
  336. break;
  337. }
  338. index++;
  339. }
  340. if (!found) {
  341. _msg_path_valid(false, TTR("Invalid extension"));
  342. _update_dialog();
  343. return;
  344. }
  345. if (!match) {
  346. _msg_path_valid(false, TTR("Wrong extension chosen"));
  347. _update_dialog();
  348. return;
  349. }
  350. /* All checks passed */
  351. is_path_valid = true;
  352. _update_dialog();
  353. }
  354. void ScriptCreateDialog::_msg_script_valid(bool valid, const String &p_msg) {
  355. error_label->set_text(TTR(p_msg));
  356. if (valid) {
  357. error_label->add_color_override("font_color", get_color("success_color", "Editor"));
  358. } else {
  359. error_label->add_color_override("font_color", get_color("error_color", "Editor"));
  360. }
  361. }
  362. void ScriptCreateDialog::_msg_path_valid(bool valid, const String &p_msg) {
  363. path_error_label->set_text(TTR(p_msg));
  364. if (valid) {
  365. path_error_label->add_color_override("font_color", get_color("success_color", "Editor"));
  366. } else {
  367. path_error_label->add_color_override("font_color", get_color("error_color", "Editor"));
  368. }
  369. }
  370. void ScriptCreateDialog::_update_dialog() {
  371. bool script_ok = true;
  372. /* "Add Script Dialog" gui logic and script checks */
  373. // Is Script Valid (order from top to bottom)
  374. get_ok()->set_disabled(true);
  375. if (!is_built_in) {
  376. if (!is_path_valid) {
  377. _msg_script_valid(false, TTR("Invalid Path"));
  378. script_ok = false;
  379. }
  380. }
  381. if (has_named_classes && (!is_class_name_valid)) {
  382. _msg_script_valid(false, TTR("Invalid class name"));
  383. script_ok = false;
  384. }
  385. if (!is_parent_name_valid) {
  386. _msg_script_valid(false, TTR("Invalid inherited parent name or path"));
  387. script_ok = false;
  388. }
  389. if (script_ok) {
  390. _msg_script_valid(true, TTR("Script valid"));
  391. get_ok()->set_disabled(false);
  392. }
  393. /* Does script have named classes */
  394. if (has_named_classes) {
  395. if (is_new_script_created) {
  396. class_name->set_editable(true);
  397. class_name->set_placeholder(TTR("Allowed: a-z, A-Z, 0-9 and _"));
  398. class_name->set_placeholder_alpha(0.3);
  399. } else {
  400. class_name->set_editable(false);
  401. }
  402. } else {
  403. class_name->set_editable(false);
  404. class_name->set_placeholder(TTR("N/A"));
  405. class_name->set_placeholder_alpha(1);
  406. }
  407. /* Can script inherit from a file */
  408. if (can_inherit_from_file) {
  409. parent_browse_button->set_disabled(false);
  410. } else {
  411. parent_browse_button->set_disabled(true);
  412. }
  413. /* Is script Built-in */
  414. if (is_built_in) {
  415. file_path->set_editable(false);
  416. path_button->set_disabled(true);
  417. re_check_path = true;
  418. } else {
  419. file_path->set_editable(true);
  420. path_button->set_disabled(false);
  421. if (re_check_path) {
  422. re_check_path = false;
  423. _path_changed(file_path->get_text());
  424. }
  425. }
  426. if (!supports_built_in)
  427. internal->set_pressed(false);
  428. /* Is Script created or loaded from existing file */
  429. if (is_new_script_created) {
  430. // New Script Created
  431. get_ok()->set_text(TTR("Create"));
  432. parent_name->set_editable(true);
  433. parent_browse_button->set_disabled(false);
  434. internal->set_disabled(!supports_built_in);
  435. if (is_built_in) {
  436. _msg_path_valid(true, TTR("Built-in script (into scene file)"));
  437. } else {
  438. if (script_ok) {
  439. _msg_path_valid(true, TTR("Create new script file"));
  440. }
  441. }
  442. } else {
  443. // Script Loaded
  444. get_ok()->set_text(TTR("Load"));
  445. parent_name->set_editable(false);
  446. parent_browse_button->set_disabled(true);
  447. internal->set_disabled(true);
  448. if (script_ok) {
  449. _msg_path_valid(true, TTR("Load existing script file"));
  450. }
  451. }
  452. }
  453. void ScriptCreateDialog::_bind_methods() {
  454. ClassDB::bind_method("_class_name_changed", &ScriptCreateDialog::_class_name_changed);
  455. ClassDB::bind_method("_parent_name_changed", &ScriptCreateDialog::_parent_name_changed);
  456. ClassDB::bind_method("_lang_changed", &ScriptCreateDialog::_lang_changed);
  457. ClassDB::bind_method("_built_in_pressed", &ScriptCreateDialog::_built_in_pressed);
  458. ClassDB::bind_method("_browse_path", &ScriptCreateDialog::_browse_path);
  459. ClassDB::bind_method("_file_selected", &ScriptCreateDialog::_file_selected);
  460. ClassDB::bind_method("_path_changed", &ScriptCreateDialog::_path_changed);
  461. ClassDB::bind_method("_template_changed", &ScriptCreateDialog::_template_changed);
  462. ADD_SIGNAL(MethodInfo("script_created", PropertyInfo(Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, "Script")));
  463. }
  464. ScriptCreateDialog::ScriptCreateDialog() {
  465. /* DIALOG */
  466. /* Main Controls */
  467. GridContainer *gc = memnew(GridContainer);
  468. gc->set_columns(2);
  469. /* Error Messages Field */
  470. VBoxContainer *vb = memnew(VBoxContainer);
  471. HBoxContainer *hb = memnew(HBoxContainer);
  472. Label *l = memnew(Label);
  473. l->set_text(" - ");
  474. hb->add_child(l);
  475. error_label = memnew(Label);
  476. error_label->set_text(TTR("Error!"));
  477. error_label->set_align(Label::ALIGN_LEFT);
  478. hb->add_child(error_label);
  479. vb->add_child(hb);
  480. hb = memnew(HBoxContainer);
  481. l = memnew(Label);
  482. l->set_text(" - ");
  483. hb->add_child(l);
  484. path_error_label = memnew(Label);
  485. path_error_label->set_text(TTR("Error!"));
  486. path_error_label->set_align(Label::ALIGN_LEFT);
  487. hb->add_child(path_error_label);
  488. vb->add_child(hb);
  489. PanelContainer *pc = memnew(PanelContainer);
  490. pc->set_h_size_flags(Control::SIZE_FILL);
  491. pc->add_style_override("panel", EditorNode::get_singleton()->get_gui_base()->get_stylebox("bg", "Tree"));
  492. pc->add_child(vb);
  493. /* Margins */
  494. Control *empty_h = memnew(Control);
  495. empty_h->set_name("empty_h"); //duplicate() doesn't like nodes without a name
  496. empty_h->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  497. empty_h->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  498. empty_h->set_custom_minimum_size(Size2(0, 10 * EDSCALE));
  499. Control *empty_v = memnew(Control);
  500. empty_v->set_name("empty_v");
  501. empty_v->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  502. empty_v->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  503. empty_v->set_custom_minimum_size(Size2(10, 0 * EDSCALE));
  504. vb = memnew(VBoxContainer);
  505. vb->add_child(empty_h->duplicate());
  506. vb->add_child(gc);
  507. vb->add_child(empty_h->duplicate());
  508. vb->add_child(pc);
  509. vb->add_child(empty_h->duplicate());
  510. hb = memnew(HBoxContainer);
  511. hb->add_child(empty_v->duplicate());
  512. hb->add_child(vb);
  513. hb->add_child(empty_v->duplicate());
  514. memdelete(empty_h);
  515. memdelete(empty_v);
  516. add_child(hb);
  517. /* Language */
  518. language_menu = memnew(OptionButton);
  519. language_menu->set_custom_minimum_size(Size2(250, 0) * EDSCALE);
  520. language_menu->set_h_size_flags(SIZE_EXPAND_FILL);
  521. l = memnew(Label);
  522. l->set_text(TTR("Language"));
  523. l->set_align(Label::ALIGN_RIGHT);
  524. gc->add_child(l);
  525. gc->add_child(language_menu);
  526. int default_lang = 0;
  527. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  528. String lang = ScriptServer::get_language(i)->get_name();
  529. language_menu->add_item(lang);
  530. if (lang == "GDScript") {
  531. default_lang = i;
  532. }
  533. }
  534. String last_selected_language = EditorSettings::get_singleton()->get_project_metadata("script_setup", "last_selected_language", "");
  535. if (last_selected_language != "") {
  536. for (int i = 0; i < language_menu->get_item_count(); i++) {
  537. if (language_menu->get_item_text(i) == last_selected_language) {
  538. language_menu->select(i);
  539. current_language = i;
  540. break;
  541. }
  542. }
  543. } else {
  544. language_menu->select(default_lang);
  545. current_language = default_lang;
  546. }
  547. language_menu->connect("item_selected", this, "_lang_changed");
  548. /* Inherits */
  549. hb = memnew(HBoxContainer);
  550. hb->set_h_size_flags(SIZE_EXPAND_FILL);
  551. parent_name = memnew(LineEdit);
  552. parent_name->connect("text_changed", this, "_parent_name_changed");
  553. parent_name->set_h_size_flags(SIZE_EXPAND_FILL);
  554. hb->add_child(parent_name);
  555. parent_browse_button = memnew(Button);
  556. parent_browse_button->set_flat(true);
  557. parent_browse_button->connect("pressed", this, "_browse_path", varray(true));
  558. hb->add_child(parent_browse_button);
  559. l = memnew(Label);
  560. l->set_text(TTR("Inherits"));
  561. l->set_align(Label::ALIGN_RIGHT);
  562. gc->add_child(l);
  563. gc->add_child(hb);
  564. is_browsing_parent = false;
  565. /* Class Name */
  566. class_name = memnew(LineEdit);
  567. class_name->connect("text_changed", this, "_class_name_changed");
  568. class_name->set_h_size_flags(SIZE_EXPAND_FILL);
  569. l = memnew(Label);
  570. l->set_text(TTR("Class Name"));
  571. l->set_align(Label::ALIGN_RIGHT);
  572. gc->add_child(l);
  573. gc->add_child(class_name);
  574. /* Templates */
  575. template_menu = memnew(OptionButton);
  576. l = memnew(Label);
  577. l->set_text(TTR("Template"));
  578. l->set_align(Label::ALIGN_RIGHT);
  579. gc->add_child(l);
  580. gc->add_child(template_menu);
  581. template_menu->connect("item_selected", this, "_template_changed");
  582. /* Built-in Script */
  583. internal = memnew(CheckButton);
  584. internal->connect("pressed", this, "_built_in_pressed");
  585. hb = memnew(HBoxContainer);
  586. Control *empty = memnew(Control);
  587. hb->add_child(internal);
  588. hb->add_child(empty);
  589. l = memnew(Label);
  590. l->set_text(TTR("Built-in Script"));
  591. l->set_align(Label::ALIGN_RIGHT);
  592. gc->add_child(l);
  593. gc->add_child(hb);
  594. /* Path */
  595. hb = memnew(HBoxContainer);
  596. file_path = memnew(LineEdit);
  597. file_path->connect("text_changed", this, "_path_changed");
  598. file_path->set_h_size_flags(SIZE_EXPAND_FILL);
  599. hb->add_child(file_path);
  600. path_button = memnew(Button);
  601. path_button->set_flat(true);
  602. path_button->connect("pressed", this, "_browse_path", varray(false));
  603. hb->add_child(path_button);
  604. l = memnew(Label);
  605. l->set_text(TTR("Path"));
  606. l->set_align(Label::ALIGN_RIGHT);
  607. gc->add_child(l);
  608. gc->add_child(hb);
  609. /* Dialog Setup */
  610. file_browse = memnew(EditorFileDialog);
  611. file_browse->connect("file_selected", this, "_file_selected");
  612. add_child(file_browse);
  613. get_ok()->set_text(TTR("Create"));
  614. alert = memnew(AcceptDialog);
  615. alert->set_as_minsize();
  616. alert->get_label()->set_autowrap(true);
  617. alert->get_label()->set_align(Label::ALIGN_CENTER);
  618. alert->get_label()->set_valign(Label::VALIGN_CENTER);
  619. alert->get_label()->set_custom_minimum_size(Size2(325, 60) * EDSCALE);
  620. add_child(alert);
  621. set_as_minsize();
  622. set_hide_on_ok(false);
  623. set_title(TTR("Attach Node Script"));
  624. is_parent_name_valid = false;
  625. is_class_name_valid = false;
  626. is_path_valid = false;
  627. has_named_classes = false;
  628. supports_built_in = false;
  629. can_inherit_from_file = false;
  630. is_built_in = false;
  631. is_new_script_created = true;
  632. }