script_create_dialog.cpp 24 KB

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