script_create_dialog.cpp 29 KB

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