script_create_dialog.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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_script_templates_dir().plus_file(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(".") != -1) {
  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_OPEN_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. String filename = p.get_file().get_basename();
  277. int select_start = p.find_last(filename);
  278. file_path->select(select_start, select_start + filename.length());
  279. file_path->set_cursor_position(select_start + filename.length());
  280. file_path->grab_focus();
  281. }
  282. }
  283. void ScriptCreateDialog::_path_changed(const String &p_path) {
  284. is_path_valid = false;
  285. is_new_script_created = true;
  286. String p = p_path;
  287. if (p == "") {
  288. _msg_path_valid(false, TTR("Path is empty"));
  289. _update_dialog();
  290. return;
  291. }
  292. p = ProjectSettings::get_singleton()->localize_path(p);
  293. if (!p.begins_with("res://")) {
  294. _msg_path_valid(false, TTR("Path is not local"));
  295. _update_dialog();
  296. return;
  297. }
  298. DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  299. if (d->change_dir(p.get_base_dir()) != OK) {
  300. _msg_path_valid(false, TTR("Invalid base path"));
  301. memdelete(d);
  302. _update_dialog();
  303. return;
  304. }
  305. memdelete(d);
  306. /* Does file already exist */
  307. DirAccess *f = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  308. if (f->dir_exists(p)) {
  309. is_new_script_created = false;
  310. is_path_valid = false;
  311. _msg_path_valid(false, TTR("Directory of the same name exists"));
  312. } else if (f->file_exists(p)) {
  313. is_new_script_created = false;
  314. is_path_valid = true;
  315. _msg_path_valid(true, TTR("File exists, will be reused"));
  316. } else {
  317. path_error_label->set_text("");
  318. }
  319. memdelete(f);
  320. _update_dialog();
  321. /* Check file extension */
  322. String extension = p.get_extension();
  323. List<String> extensions;
  324. // get all possible extensions for script
  325. for (int l = 0; l < language_menu->get_item_count(); l++) {
  326. ScriptServer::get_language(l)->get_recognized_extensions(&extensions);
  327. }
  328. bool found = false;
  329. bool match = false;
  330. int index = 0;
  331. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  332. if (E->get().nocasecmp_to(extension) == 0) {
  333. //FIXME (?) - changing language this way doesn't update controls, needs rework
  334. //language_menu->select(index); // change Language option by extension
  335. found = true;
  336. if (E->get() == ScriptServer::get_language(language_menu->get_selected())->get_extension()) {
  337. match = true;
  338. }
  339. break;
  340. }
  341. index++;
  342. }
  343. if (!found) {
  344. _msg_path_valid(false, TTR("Invalid extension"));
  345. _update_dialog();
  346. return;
  347. }
  348. if (!match) {
  349. _msg_path_valid(false, TTR("Wrong extension chosen"));
  350. _update_dialog();
  351. return;
  352. }
  353. /* All checks passed */
  354. is_path_valid = true;
  355. _update_dialog();
  356. }
  357. void ScriptCreateDialog::_path_entered(const String &p_path) {
  358. ok_pressed();
  359. }
  360. void ScriptCreateDialog::_msg_script_valid(bool valid, const String &p_msg) {
  361. error_label->set_text(TTR(p_msg));
  362. if (valid) {
  363. error_label->add_color_override("font_color", get_color("success_color", "Editor"));
  364. } else {
  365. error_label->add_color_override("font_color", get_color("error_color", "Editor"));
  366. }
  367. }
  368. void ScriptCreateDialog::_msg_path_valid(bool valid, const String &p_msg) {
  369. path_error_label->set_text(TTR(p_msg));
  370. if (valid) {
  371. path_error_label->add_color_override("font_color", get_color("success_color", "Editor"));
  372. } else {
  373. path_error_label->add_color_override("font_color", get_color("error_color", "Editor"));
  374. }
  375. }
  376. void ScriptCreateDialog::_update_dialog() {
  377. bool script_ok = true;
  378. /* "Add Script Dialog" gui logic and script checks */
  379. // Is Script Valid (order from top to bottom)
  380. get_ok()->set_disabled(true);
  381. if (!is_built_in) {
  382. if (!is_path_valid) {
  383. _msg_script_valid(false, TTR("Invalid Path"));
  384. script_ok = false;
  385. }
  386. }
  387. if (has_named_classes && (is_new_script_created && !is_class_name_valid)) {
  388. _msg_script_valid(false, TTR("Invalid class name"));
  389. script_ok = false;
  390. }
  391. if (!is_parent_name_valid) {
  392. _msg_script_valid(false, TTR("Invalid inherited parent name or path"));
  393. script_ok = false;
  394. }
  395. if (script_ok) {
  396. _msg_script_valid(true, TTR("Script valid"));
  397. get_ok()->set_disabled(false);
  398. }
  399. /* Does script have named classes */
  400. if (has_named_classes) {
  401. if (is_new_script_created) {
  402. class_name->set_editable(true);
  403. class_name->set_placeholder(TTR("Allowed: a-z, A-Z, 0-9 and _"));
  404. class_name->set_placeholder_alpha(0.3);
  405. } else {
  406. class_name->set_editable(false);
  407. }
  408. } else {
  409. class_name->set_editable(false);
  410. class_name->set_placeholder(TTR("N/A"));
  411. class_name->set_placeholder_alpha(1);
  412. }
  413. /* Can script inherit from a file */
  414. if (can_inherit_from_file) {
  415. parent_browse_button->set_disabled(false);
  416. } else {
  417. parent_browse_button->set_disabled(true);
  418. }
  419. /* Is script Built-in */
  420. if (is_built_in) {
  421. file_path->set_editable(false);
  422. path_button->set_disabled(true);
  423. re_check_path = true;
  424. } else {
  425. file_path->set_editable(true);
  426. path_button->set_disabled(false);
  427. if (re_check_path) {
  428. re_check_path = false;
  429. _path_changed(file_path->get_text());
  430. }
  431. }
  432. if (!supports_built_in)
  433. internal->set_pressed(false);
  434. /* Is Script created or loaded from existing file */
  435. if (is_new_script_created) {
  436. // New Script Created
  437. get_ok()->set_text(TTR("Create"));
  438. parent_name->set_editable(true);
  439. parent_browse_button->set_disabled(false);
  440. internal->set_disabled(!supports_built_in);
  441. if (is_built_in) {
  442. _msg_path_valid(true, TTR("Built-in script (into scene file)"));
  443. } else {
  444. if (script_ok) {
  445. _msg_path_valid(true, TTR("Create new script file"));
  446. }
  447. }
  448. } else {
  449. // Script Loaded
  450. get_ok()->set_text(TTR("Load"));
  451. parent_name->set_editable(false);
  452. parent_browse_button->set_disabled(true);
  453. internal->set_disabled(true);
  454. if (script_ok) {
  455. _msg_path_valid(true, TTR("Load existing script file"));
  456. }
  457. }
  458. }
  459. void ScriptCreateDialog::_bind_methods() {
  460. ClassDB::bind_method("_class_name_changed", &ScriptCreateDialog::_class_name_changed);
  461. ClassDB::bind_method("_parent_name_changed", &ScriptCreateDialog::_parent_name_changed);
  462. ClassDB::bind_method("_lang_changed", &ScriptCreateDialog::_lang_changed);
  463. ClassDB::bind_method("_built_in_pressed", &ScriptCreateDialog::_built_in_pressed);
  464. ClassDB::bind_method("_browse_path", &ScriptCreateDialog::_browse_path);
  465. ClassDB::bind_method("_file_selected", &ScriptCreateDialog::_file_selected);
  466. ClassDB::bind_method("_path_changed", &ScriptCreateDialog::_path_changed);
  467. ClassDB::bind_method("_path_entered", &ScriptCreateDialog::_path_entered);
  468. ClassDB::bind_method("_template_changed", &ScriptCreateDialog::_template_changed);
  469. ADD_SIGNAL(MethodInfo("script_created", PropertyInfo(Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, "Script")));
  470. }
  471. ScriptCreateDialog::ScriptCreateDialog() {
  472. /* DIALOG */
  473. /* Main Controls */
  474. GridContainer *gc = memnew(GridContainer);
  475. gc->set_columns(2);
  476. /* Error Messages Field */
  477. VBoxContainer *vb = memnew(VBoxContainer);
  478. HBoxContainer *hb = memnew(HBoxContainer);
  479. Label *l = memnew(Label);
  480. l->set_text(" - ");
  481. hb->add_child(l);
  482. error_label = memnew(Label);
  483. error_label->set_text(TTR("Error!"));
  484. error_label->set_align(Label::ALIGN_LEFT);
  485. hb->add_child(error_label);
  486. vb->add_child(hb);
  487. hb = memnew(HBoxContainer);
  488. l = memnew(Label);
  489. l->set_text(" - ");
  490. hb->add_child(l);
  491. path_error_label = memnew(Label);
  492. path_error_label->set_text(TTR("Error!"));
  493. path_error_label->set_align(Label::ALIGN_LEFT);
  494. hb->add_child(path_error_label);
  495. vb->add_child(hb);
  496. PanelContainer *pc = memnew(PanelContainer);
  497. pc->set_h_size_flags(Control::SIZE_FILL);
  498. pc->add_style_override("panel", EditorNode::get_singleton()->get_gui_base()->get_stylebox("bg", "Tree"));
  499. pc->add_child(vb);
  500. /* Margins */
  501. Control *empty_h = memnew(Control);
  502. empty_h->set_name("empty_h"); //duplicate() doesn't like nodes without a name
  503. empty_h->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  504. empty_h->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  505. empty_h->set_custom_minimum_size(Size2(0, 10 * EDSCALE));
  506. Control *empty_v = memnew(Control);
  507. empty_v->set_name("empty_v");
  508. empty_v->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  509. empty_v->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  510. empty_v->set_custom_minimum_size(Size2(10, 0 * EDSCALE));
  511. vb = memnew(VBoxContainer);
  512. vb->add_child(empty_h->duplicate());
  513. vb->add_child(gc);
  514. vb->add_child(empty_h->duplicate());
  515. vb->add_child(pc);
  516. vb->add_child(empty_h->duplicate());
  517. hb = memnew(HBoxContainer);
  518. hb->add_child(empty_v->duplicate());
  519. hb->add_child(vb);
  520. hb->add_child(empty_v->duplicate());
  521. memdelete(empty_h);
  522. memdelete(empty_v);
  523. add_child(hb);
  524. /* Language */
  525. language_menu = memnew(OptionButton);
  526. language_menu->set_custom_minimum_size(Size2(250, 0) * EDSCALE);
  527. language_menu->set_h_size_flags(SIZE_EXPAND_FILL);
  528. l = memnew(Label);
  529. l->set_text(TTR("Language"));
  530. l->set_align(Label::ALIGN_RIGHT);
  531. gc->add_child(l);
  532. gc->add_child(language_menu);
  533. int default_lang = 0;
  534. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  535. String lang = ScriptServer::get_language(i)->get_name();
  536. language_menu->add_item(lang);
  537. if (lang == "GDScript") {
  538. default_lang = i;
  539. }
  540. }
  541. String last_selected_language = EditorSettings::get_singleton()->get_project_metadata("script_setup", "last_selected_language", "");
  542. if (last_selected_language != "") {
  543. for (int i = 0; i < language_menu->get_item_count(); i++) {
  544. if (language_menu->get_item_text(i) == last_selected_language) {
  545. language_menu->select(i);
  546. current_language = i;
  547. break;
  548. }
  549. }
  550. } else {
  551. language_menu->select(default_lang);
  552. current_language = default_lang;
  553. }
  554. language_menu->connect("item_selected", this, "_lang_changed");
  555. /* Inherits */
  556. hb = memnew(HBoxContainer);
  557. hb->set_h_size_flags(SIZE_EXPAND_FILL);
  558. parent_name = memnew(LineEdit);
  559. parent_name->connect("text_changed", this, "_parent_name_changed");
  560. parent_name->set_h_size_flags(SIZE_EXPAND_FILL);
  561. hb->add_child(parent_name);
  562. parent_browse_button = memnew(Button);
  563. parent_browse_button->set_flat(true);
  564. parent_browse_button->connect("pressed", this, "_browse_path", varray(true));
  565. hb->add_child(parent_browse_button);
  566. l = memnew(Label);
  567. l->set_text(TTR("Inherits"));
  568. l->set_align(Label::ALIGN_RIGHT);
  569. gc->add_child(l);
  570. gc->add_child(hb);
  571. is_browsing_parent = false;
  572. /* Class Name */
  573. class_name = memnew(LineEdit);
  574. class_name->connect("text_changed", this, "_class_name_changed");
  575. class_name->set_h_size_flags(SIZE_EXPAND_FILL);
  576. l = memnew(Label);
  577. l->set_text(TTR("Class Name"));
  578. l->set_align(Label::ALIGN_RIGHT);
  579. gc->add_child(l);
  580. gc->add_child(class_name);
  581. /* Templates */
  582. template_menu = memnew(OptionButton);
  583. l = memnew(Label);
  584. l->set_text(TTR("Template"));
  585. l->set_align(Label::ALIGN_RIGHT);
  586. gc->add_child(l);
  587. gc->add_child(template_menu);
  588. template_menu->connect("item_selected", this, "_template_changed");
  589. /* Built-in Script */
  590. internal = memnew(CheckButton);
  591. internal->connect("pressed", this, "_built_in_pressed");
  592. hb = memnew(HBoxContainer);
  593. hb->add_child(internal);
  594. l = memnew(Label);
  595. l->set_text(TTR("Built-in Script"));
  596. l->set_align(Label::ALIGN_RIGHT);
  597. gc->add_child(l);
  598. gc->add_child(hb);
  599. /* Path */
  600. hb = memnew(HBoxContainer);
  601. file_path = memnew(LineEdit);
  602. file_path->connect("text_changed", this, "_path_changed");
  603. file_path->connect("text_entered", this, "_path_entered");
  604. file_path->set_h_size_flags(SIZE_EXPAND_FILL);
  605. hb->add_child(file_path);
  606. path_button = memnew(Button);
  607. path_button->set_flat(true);
  608. path_button->connect("pressed", this, "_browse_path", varray(false));
  609. hb->add_child(path_button);
  610. l = memnew(Label);
  611. l->set_text(TTR("Path"));
  612. l->set_align(Label::ALIGN_RIGHT);
  613. gc->add_child(l);
  614. gc->add_child(hb);
  615. /* Dialog Setup */
  616. file_browse = memnew(EditorFileDialog);
  617. file_browse->connect("file_selected", this, "_file_selected");
  618. add_child(file_browse);
  619. get_ok()->set_text(TTR("Create"));
  620. alert = memnew(AcceptDialog);
  621. alert->set_as_minsize();
  622. alert->get_label()->set_autowrap(true);
  623. alert->get_label()->set_align(Label::ALIGN_CENTER);
  624. alert->get_label()->set_valign(Label::VALIGN_CENTER);
  625. alert->get_label()->set_custom_minimum_size(Size2(325, 60) * EDSCALE);
  626. add_child(alert);
  627. set_as_minsize();
  628. set_hide_on_ok(false);
  629. set_title(TTR("Attach Node Script"));
  630. is_parent_name_valid = false;
  631. is_class_name_valid = false;
  632. is_path_valid = false;
  633. has_named_classes = false;
  634. supports_built_in = false;
  635. can_inherit_from_file = false;
  636. is_built_in = false;
  637. is_new_script_created = true;
  638. }