script_create_dialog.cpp 22 KB

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