script_create_dialog.cpp 24 KB

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