script_create_dialog.cpp 28 KB

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