script_create_dialog.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 "editor_file_system.h"
  32. #include "globals.h"
  33. #include "io/resource_saver.h"
  34. #include "os/file_access.h"
  35. #include "script_language.h"
  36. void ScriptCreateDialog::config(const String &p_base_name, const String &p_base_path) {
  37. class_name->set_text("");
  38. parent_name->set_text(p_base_name);
  39. if (p_base_path != "") {
  40. initial_bp = p_base_path.basename();
  41. file_path->set_text(initial_bp + "." + ScriptServer::get_language(language_menu->get_selected())->get_extension());
  42. } else {
  43. initial_bp = "";
  44. file_path->set_text("");
  45. }
  46. _class_name_changed("");
  47. _path_changed(file_path->get_text());
  48. }
  49. bool ScriptCreateDialog::_validate(const String &p_string) {
  50. if (p_string.length() == 0)
  51. return false;
  52. for (int i = 0; i < p_string.length(); i++) {
  53. if (i == 0) {
  54. if (p_string[0] >= '0' && p_string[0] <= '9')
  55. return false; // no start with number plz
  56. }
  57. 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] == '_';
  58. if (!valid_char)
  59. return false;
  60. }
  61. return true;
  62. }
  63. void ScriptCreateDialog::_class_name_changed(const String &p_name) {
  64. if (!_validate(parent_name->get_text())) {
  65. error_label->set_text(TTR("Invalid parent class name"));
  66. error_label->add_color_override("font_color", Color(1, 0.4, 0.0, 0.8));
  67. } else if (class_name->is_editable()) {
  68. if (class_name->get_text() == "") {
  69. error_label->set_text(TTR("Valid chars:") + " a-z A-Z 0-9 _");
  70. error_label->add_color_override("font_color", Color(1, 1, 1, 0.6));
  71. } else if (!_validate(class_name->get_text())) {
  72. error_label->set_text(TTR("Invalid class name"));
  73. error_label->add_color_override("font_color", Color(1, 0.2, 0.2, 0.8));
  74. } else {
  75. error_label->set_text(TTR("Valid name"));
  76. error_label->add_color_override("font_color", Color(0, 1.0, 0.8, 0.8));
  77. }
  78. } else {
  79. error_label->set_text(TTR("N/A"));
  80. error_label->add_color_override("font_color", Color(0, 1.0, 0.8, 0.8));
  81. }
  82. }
  83. void ScriptCreateDialog::ok_pressed() {
  84. if (create_new) {
  85. _create_new();
  86. } else {
  87. _load_exist();
  88. }
  89. create_new = true;
  90. _update_controls();
  91. }
  92. void ScriptCreateDialog::_create_new() {
  93. if (class_name->is_editable() && !_validate(class_name->get_text())) {
  94. alert->set_text(TTR("Class name is invalid!"));
  95. alert->popup_centered_minsize();
  96. return;
  97. }
  98. if (!_validate(parent_name->get_text())) {
  99. alert->set_text(TTR("Parent class name is invalid!"));
  100. alert->popup_centered_minsize();
  101. return;
  102. }
  103. String cname;
  104. if (class_name->is_editable())
  105. cname = class_name->get_text();
  106. String text;
  107. if (template_select == 0) {
  108. text = ScriptServer::get_language(language_menu->get_selected())->get_template(cname, parent_name->get_text());
  109. } else if (template_select == 1) {
  110. text = ScriptServer::get_language(language_menu->get_selected())->get_empty_template(cname, parent_name->get_text());
  111. } else if (template_select == 2) {
  112. text = ScriptServer::get_language(language_menu->get_selected())->get_nocomment_template(cname, parent_name->get_text());
  113. } else {
  114. text = ScriptServer::get_language(language_menu->get_selected())->get_template(cname, parent_name->get_text());
  115. }
  116. Script *script = ScriptServer::get_language(language_menu->get_selected())->create_script();
  117. script->set_source_code(text);
  118. if (cname != "")
  119. script->set_name(cname);
  120. Ref<Script> scr(script);
  121. if (!internal->is_pressed()) {
  122. String lpath = Globals::get_singleton()->localize_path(file_path->get_text());
  123. script->set_path(lpath);
  124. if (!path_valid) {
  125. alert->set_text(TTR("Invalid path!"));
  126. alert->popup_centered_minsize();
  127. return;
  128. }
  129. Error err = ResourceSaver::save(lpath, scr, ResourceSaver::FLAG_CHANGE_PATH);
  130. if (err != OK) {
  131. alert->set_text(TTR("Could not create script in filesystem."));
  132. alert->popup_centered_minsize();
  133. return;
  134. }
  135. }
  136. hide();
  137. emit_signal("script_created", scr);
  138. }
  139. void ScriptCreateDialog::_load_exist() {
  140. String path = file_path->get_text();
  141. RES p_script = ResourceLoader::load(path, "Script");
  142. if (p_script.is_null()) {
  143. alert->get_ok()->set_text(TTR("Ugh"));
  144. alert->set_text(vformat(TTR("Error loading script from %s"), path));
  145. alert->popup_centered_minsize();
  146. return;
  147. }
  148. hide();
  149. emit_signal("script_created", p_script.get_ref_ptr());
  150. }
  151. void ScriptCreateDialog::_lang_changed(int l) {
  152. l = language_menu->get_selected();
  153. if (ScriptServer::get_language(l)->has_named_classes()) {
  154. class_name->set_editable(true);
  155. } else {
  156. class_name->set_editable(false);
  157. }
  158. String selected_ext = "." + ScriptServer::get_language(l)->get_extension();
  159. String path = file_path->get_text();
  160. String extension = "";
  161. if (path.find(".") >= 0) {
  162. extension = path.extension();
  163. }
  164. if (extension.length() == 0) {
  165. // add extension if none
  166. path += selected_ext;
  167. _path_changed(path);
  168. } else {
  169. // change extension by selected language
  170. List<String> extensions;
  171. // get all possible extensions for script
  172. for (int l = 0; l < language_menu->get_item_count(); l++) {
  173. ScriptServer::get_language(l)->get_recognized_extensions(&extensions);
  174. }
  175. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  176. if (E->get().nocasecmp_to(extension) == 0) {
  177. path = path.basename() + selected_ext;
  178. _path_changed(path);
  179. break;
  180. }
  181. }
  182. }
  183. file_path->set_text(path);
  184. _class_name_changed(class_name->get_text());
  185. }
  186. void ScriptCreateDialog::_template_changed(int p_template) {
  187. template_select = p_template;
  188. }
  189. void ScriptCreateDialog::_built_in_pressed() {
  190. if (internal->is_pressed()) {
  191. path_vb->hide();
  192. } else {
  193. path_vb->show();
  194. }
  195. }
  196. void ScriptCreateDialog::_browse_path() {
  197. file_browse->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  198. file_browse->set_disable_overwrite_warning(true);
  199. file_browse->clear_filters();
  200. List<String> extensions;
  201. int lang = language_menu->get_selected();
  202. ScriptServer::get_language(lang)->get_recognized_extensions(&extensions);
  203. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  204. file_browse->add_filter("*." + E->get());
  205. }
  206. file_browse->set_current_path(file_path->get_text());
  207. file_browse->popup_centered_ratio();
  208. }
  209. void ScriptCreateDialog::_file_selected(const String &p_file) {
  210. String p = Globals::get_singleton()->localize_path(p_file);
  211. file_path->set_text(p);
  212. _path_changed(p);
  213. }
  214. void ScriptCreateDialog::_path_changed(const String &p_path) {
  215. path_valid = false;
  216. String p = p_path;
  217. if (p == "") {
  218. path_error_label->set_text(TTR("Path is empty"));
  219. path_error_label->add_color_override("font_color", Color(1, 0.4, 0.0, 0.8));
  220. return;
  221. }
  222. p = Globals::get_singleton()->localize_path(p);
  223. if (!p.begins_with("res://")) {
  224. path_error_label->set_text(TTR("Path is not local"));
  225. path_error_label->add_color_override("font_color", Color(1, 0.4, 0.0, 0.8));
  226. return;
  227. }
  228. if (p.find("/") || p.find("\\")) {
  229. DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  230. if (d->change_dir(p.get_base_dir()) != OK) {
  231. path_error_label->set_text(TTR("Invalid base path"));
  232. path_error_label->add_color_override("font_color", Color(1, 0.4, 0.0, 0.8));
  233. memdelete(d);
  234. return;
  235. }
  236. memdelete(d);
  237. }
  238. FileAccess *f = FileAccess::create(FileAccess::ACCESS_RESOURCES);
  239. create_new = !f->file_exists(p);
  240. memdelete(f);
  241. String extension = p.extension();
  242. List<String> extensions;
  243. // get all possible extensions for script
  244. for (int l = 0; l < language_menu->get_item_count(); l++) {
  245. ScriptServer::get_language(l)->get_recognized_extensions(&extensions);
  246. }
  247. bool found = false;
  248. int index = 0;
  249. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  250. if (E->get().nocasecmp_to(extension) == 0) {
  251. language_menu->select(index); // change Language option by extension
  252. found = true;
  253. break;
  254. }
  255. index++;
  256. }
  257. if (!found) {
  258. path_error_label->set_text(TTR("Invalid extension"));
  259. path_error_label->add_color_override("font_color", Color(1, 0.4, 0.0, 0.8));
  260. return;
  261. }
  262. _update_controls();
  263. path_error_label->add_color_override("font_color", Color(0, 1.0, 0.8, 0.8));
  264. path_valid = true;
  265. }
  266. void ScriptCreateDialog::_update_controls() {
  267. if (create_new) {
  268. path_error_label->set_text(TTR("Create new script"));
  269. get_ok()->set_text(TTR("Create"));
  270. } else {
  271. path_error_label->set_text(TTR("Load existing script"));
  272. get_ok()->set_text(TTR("Load"));
  273. }
  274. parent_name->set_editable(create_new);
  275. internal->set_disabled(!create_new);
  276. }
  277. void ScriptCreateDialog::_bind_methods() {
  278. ObjectTypeDB::bind_method("_class_name_changed", &ScriptCreateDialog::_class_name_changed);
  279. ObjectTypeDB::bind_method("_lang_changed", &ScriptCreateDialog::_lang_changed);
  280. ObjectTypeDB::bind_method("_template_changed", &ScriptCreateDialog::_template_changed);
  281. ObjectTypeDB::bind_method("_built_in_pressed", &ScriptCreateDialog::_built_in_pressed);
  282. ObjectTypeDB::bind_method("_browse_path", &ScriptCreateDialog::_browse_path);
  283. ObjectTypeDB::bind_method("_file_selected", &ScriptCreateDialog::_file_selected);
  284. ObjectTypeDB::bind_method("_path_changed", &ScriptCreateDialog::_path_changed);
  285. ADD_SIGNAL(MethodInfo("script_created", PropertyInfo(Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, "Script")));
  286. }
  287. ScriptCreateDialog::ScriptCreateDialog() {
  288. /* SNAP DIALOG */
  289. VBoxContainer *vb = memnew(VBoxContainer);
  290. add_child(vb);
  291. set_child_rect(vb);
  292. class_name = memnew(LineEdit);
  293. VBoxContainer *vb2 = memnew(VBoxContainer);
  294. vb2->add_child(class_name);
  295. class_name->connect("text_changed", this, "_class_name_changed");
  296. error_label = memnew(Label);
  297. error_label->set_text("valid chars: a-z A-Z 0-9 _");
  298. error_label->set_align(Label::ALIGN_CENTER);
  299. vb2->add_child(error_label);
  300. vb->add_margin_child(TTR("Class Name:"), vb2);
  301. parent_name = memnew(LineEdit);
  302. vb->add_margin_child(TTR("Inherits:"), parent_name);
  303. parent_name->connect("text_changed", this, "_class_name_changed");
  304. language_menu = memnew(OptionButton);
  305. vb->add_margin_child(TTR("Language"), language_menu);
  306. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  307. language_menu->add_item(ScriptServer::get_language(i)->get_name());
  308. }
  309. language_menu->select(0);
  310. language_menu->connect("item_selected", this, "_lang_changed");
  311. template_menu = memnew(OptionButton);
  312. vb->add_margin_child(TTR("Template"), template_menu);
  313. template_menu->add_item(TTR("Default"));
  314. template_menu->add_item(TTR("Empty GD File"));
  315. template_menu->add_item(TTR("No Comment GD File"));
  316. template_menu->select(0);
  317. template_menu->connect("item_selected", this, "_template_changed");
  318. //parent_name->set_text();
  319. vb2 = memnew(VBoxContainer);
  320. path_vb = memnew(VBoxContainer);
  321. vb2->add_child(path_vb);
  322. HBoxContainer *hbc = memnew(HBoxContainer);
  323. file_path = memnew(LineEdit);
  324. file_path->connect("text_changed", this, "_path_changed");
  325. hbc->add_child(file_path);
  326. file_path->set_h_size_flags(SIZE_EXPAND_FILL);
  327. Button *b = memnew(Button);
  328. b->set_text(" .. ");
  329. b->connect("pressed", this, "_browse_path");
  330. hbc->add_child(b);
  331. path_vb->add_child(hbc);
  332. path_error_label = memnew(Label);
  333. path_vb->add_child(path_error_label);
  334. path_error_label->set_text(TTR("Error!"));
  335. path_error_label->set_align(Label::ALIGN_CENTER);
  336. internal = memnew(CheckButton);
  337. internal->set_text(TTR("Built-In Script"));
  338. vb2->add_child(internal);
  339. internal->connect("pressed", this, "_built_in_pressed");
  340. vb->add_margin_child(TTR("Path:"), vb2);
  341. set_size(Size2(200, 150));
  342. set_hide_on_ok(false);
  343. set_title(TTR("Attach Node Script"));
  344. file_browse = memnew(EditorFileDialog);
  345. file_browse->connect("file_selected", this, "_file_selected");
  346. add_child(file_browse);
  347. get_ok()->set_text(TTR("Create"));
  348. alert = memnew(AcceptDialog);
  349. add_child(alert);
  350. _lang_changed(0);
  351. create_new = true;
  352. }