script_create_dialog.cpp 25 KB

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