script_create_dialog.cpp 28 KB

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