script_create_dialog.cpp 25 KB

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