script_create_dialog.cpp 23 KB

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