script_create_dialog.cpp 29 KB

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