script_create_dialog.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*************************************************************************/
  2. /* script_create_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "global_config.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.get_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. String path_chars = "\"res://";
  53. bool is_val_path = ScriptServer::get_language(language_menu->get_selected())->can_inherit_from_file();
  54. for (int i = 0; i < p_string.length(); i++) {
  55. if (i == 0) {
  56. if (p_string[0] >= '0' && p_string[0] <= '9')
  57. return false; // no start with number plz
  58. }
  59. if (i == p_string.length() - 1 && is_val_path)
  60. return p_string[i] == '\"';
  61. if (is_val_path && i < path_chars.length()) {
  62. if (p_string[i] != path_chars[i])
  63. is_val_path = false;
  64. else
  65. continue;
  66. }
  67. 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] == '_' || (is_val_path && (p_string[i] == '/' || p_string[i] == '.'));
  68. if (!valid_char)
  69. return false;
  70. }
  71. return true;
  72. }
  73. void ScriptCreateDialog::_class_name_changed(const String &p_name) {
  74. if (!_validate(parent_name->get_text())) {
  75. error_label->set_text(TTR("Invalid parent class name or path"));
  76. error_label->add_color_override("font_color", Color(1, 0.4, 0.0, 0.8));
  77. } else if (class_name->is_editable()) {
  78. if (class_name->get_text() == "") {
  79. error_label->set_text(TTR("Valid chars:") + " a-z A-Z 0-9 _");
  80. error_label->add_color_override("font_color", Color(1, 1, 1, 0.6));
  81. } else if (!_validate(class_name->get_text())) {
  82. error_label->set_text(TTR("Invalid class name"));
  83. error_label->add_color_override("font_color", Color(1, 0.2, 0.2, 0.8));
  84. } else {
  85. error_label->set_text(TTR("Valid name"));
  86. error_label->add_color_override("font_color", Color(0, 1.0, 0.8, 0.8));
  87. }
  88. } else {
  89. error_label->set_text(TTR("N/A"));
  90. error_label->add_color_override("font_color", Color(0, 1.0, 0.8, 0.8));
  91. }
  92. }
  93. void ScriptCreateDialog::ok_pressed() {
  94. if (create_new) {
  95. _create_new();
  96. } else {
  97. _load_exist();
  98. }
  99. create_new = true;
  100. _update_controls();
  101. }
  102. void ScriptCreateDialog::_create_new() {
  103. if (class_name->is_editable() && !_validate(class_name->get_text())) {
  104. alert->set_text(TTR("Class name is invalid!"));
  105. alert->popup_centered_minsize();
  106. return;
  107. }
  108. if (!_validate(parent_name->get_text())) {
  109. alert->set_text(TTR("Parent class name is invalid!"));
  110. alert->popup_centered_minsize();
  111. return;
  112. }
  113. String cname;
  114. if (class_name->is_editable())
  115. cname = class_name->get_text();
  116. Ref<Script> scr = ScriptServer::get_language(language_menu->get_selected())->get_template(cname, parent_name->get_text());
  117. String selected_language = language_menu->get_item_text(language_menu->get_selected());
  118. editor_settings->set_project_metadata("script_setup", "last_selected_language", selected_language);
  119. if (cname != "")
  120. scr->set_name(cname);
  121. if (!internal->is_pressed()) {
  122. String lpath = GlobalConfig::get_singleton()->localize_path(file_path->get_text());
  123. scr->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. if (ScriptServer::get_language(l)->can_inherit_from_file()) {
  159. parent_browse_button->show();
  160. } else {
  161. parent_browse_button->hide();
  162. }
  163. String selected_ext = "." + ScriptServer::get_language(l)->get_extension();
  164. String path = file_path->get_text();
  165. String extension = "";
  166. if (path.find(".") >= 0) {
  167. extension = path.get_extension();
  168. }
  169. if (extension.length() == 0) {
  170. // add extension if none
  171. path += selected_ext;
  172. _path_changed(path);
  173. } else {
  174. // change extension by selected language
  175. List<String> extensions;
  176. // get all possible extensions for script
  177. for (int l = 0; l < language_menu->get_item_count(); l++) {
  178. ScriptServer::get_language(l)->get_recognized_extensions(&extensions);
  179. }
  180. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  181. if (E->get().nocasecmp_to(extension) == 0) {
  182. path = path.get_basename() + selected_ext;
  183. _path_changed(path);
  184. break;
  185. }
  186. }
  187. }
  188. file_path->set_text(path);
  189. _class_name_changed(class_name->get_text());
  190. }
  191. void ScriptCreateDialog::_built_in_pressed() {
  192. if (internal->is_pressed()) {
  193. path_vb->hide();
  194. } else {
  195. path_vb->show();
  196. }
  197. }
  198. void ScriptCreateDialog::_browse_path(bool browse_parent) {
  199. is_browsing_parent = browse_parent;
  200. file_browse->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  201. file_browse->set_disable_overwrite_warning(true);
  202. file_browse->clear_filters();
  203. List<String> extensions;
  204. // get all possible extensions for script
  205. for (int l = 0; l < language_menu->get_item_count(); l++) {
  206. ScriptServer::get_language(l)->get_recognized_extensions(&extensions);
  207. }
  208. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  209. file_browse->add_filter("*." + E->get());
  210. }
  211. file_browse->set_current_path(file_path->get_text());
  212. file_browse->popup_centered_ratio();
  213. }
  214. void ScriptCreateDialog::_file_selected(const String &p_file) {
  215. String p = GlobalConfig::get_singleton()->localize_path(p_file);
  216. if (is_browsing_parent) {
  217. parent_name->set_text("\"" + p + "\"");
  218. _class_name_changed("\"" + p + "\"");
  219. } else {
  220. file_path->set_text(p);
  221. _path_changed(p);
  222. }
  223. }
  224. void ScriptCreateDialog::_path_changed(const String &p_path) {
  225. path_valid = false;
  226. String p = p_path;
  227. if (p == "") {
  228. path_error_label->set_text(TTR("Path is empty"));
  229. path_error_label->add_color_override("font_color", Color(1, 0.4, 0.0, 0.8));
  230. return;
  231. }
  232. p = GlobalConfig::get_singleton()->localize_path(p);
  233. if (!p.begins_with("res://")) {
  234. path_error_label->set_text(TTR("Path is not local"));
  235. path_error_label->add_color_override("font_color", Color(1, 0.4, 0.0, 0.8));
  236. return;
  237. }
  238. if (p.find("/") || p.find("\\")) {
  239. DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  240. if (d->change_dir(p.get_base_dir()) != OK) {
  241. path_error_label->set_text(TTR("Invalid base path"));
  242. path_error_label->add_color_override("font_color", Color(1, 0.4, 0.0, 0.8));
  243. memdelete(d);
  244. return;
  245. }
  246. memdelete(d);
  247. }
  248. FileAccess *f = FileAccess::create(FileAccess::ACCESS_RESOURCES);
  249. create_new = !f->file_exists(p);
  250. memdelete(f);
  251. String extension = p.get_extension();
  252. List<String> extensions;
  253. // get all possible extensions for script
  254. for (int l = 0; l < language_menu->get_item_count(); l++) {
  255. ScriptServer::get_language(l)->get_recognized_extensions(&extensions);
  256. }
  257. bool found = false;
  258. int index = 0;
  259. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  260. if (E->get().nocasecmp_to(extension) == 0) {
  261. language_menu->select(index); // change Language option by extension
  262. found = true;
  263. break;
  264. }
  265. index++;
  266. }
  267. if (!found) {
  268. path_error_label->set_text(TTR("Invalid extension"));
  269. path_error_label->add_color_override("font_color", Color(1, 0.4, 0.0, 0.8));
  270. return;
  271. }
  272. _update_controls();
  273. path_error_label->add_color_override("font_color", Color(0, 1.0, 0.8, 0.8));
  274. path_valid = true;
  275. }
  276. void ScriptCreateDialog::_update_controls() {
  277. if (create_new) {
  278. path_error_label->set_text(TTR("Create new script"));
  279. get_ok()->set_text(TTR("Create"));
  280. } else {
  281. path_error_label->set_text(TTR("Load existing script"));
  282. get_ok()->set_text(TTR("Load"));
  283. }
  284. parent_name->set_editable(create_new);
  285. internal->set_disabled(!create_new);
  286. }
  287. void ScriptCreateDialog::_bind_methods() {
  288. ClassDB::bind_method("_class_name_changed", &ScriptCreateDialog::_class_name_changed);
  289. ClassDB::bind_method("_lang_changed", &ScriptCreateDialog::_lang_changed);
  290. ClassDB::bind_method("_built_in_pressed", &ScriptCreateDialog::_built_in_pressed);
  291. ClassDB::bind_method("_browse_path", &ScriptCreateDialog::_browse_path);
  292. ClassDB::bind_method("_file_selected", &ScriptCreateDialog::_file_selected);
  293. ClassDB::bind_method("_path_changed", &ScriptCreateDialog::_path_changed);
  294. ADD_SIGNAL(MethodInfo("script_created", PropertyInfo(Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, "Script")));
  295. }
  296. ScriptCreateDialog::ScriptCreateDialog() {
  297. /* SNAP DIALOG */
  298. VBoxContainer *vb = memnew(VBoxContainer);
  299. add_child(vb);
  300. //set_child_rect(vb);
  301. class_name = memnew(LineEdit);
  302. VBoxContainer *vb2 = memnew(VBoxContainer);
  303. vb2->add_child(class_name);
  304. class_name->connect("text_changed", this, "_class_name_changed");
  305. error_label = memnew(Label);
  306. error_label->set_text("valid chars: a-z A-Z 0-9 _");
  307. error_label->set_align(Label::ALIGN_CENTER);
  308. vb2->add_child(error_label);
  309. vb->add_margin_child(TTR("Class Name:"), vb2);
  310. HBoxContainer *hb1 = memnew(HBoxContainer);
  311. parent_name = memnew(LineEdit);
  312. parent_name->connect("text_changed", this, "_class_name_changed");
  313. parent_name->set_h_size_flags(SIZE_EXPAND_FILL);
  314. hb1->add_child(parent_name);
  315. parent_browse_button = memnew(Button);
  316. parent_browse_button->set_text(" .. ");
  317. parent_browse_button->connect("pressed", this, "_browse_path", varray(true));
  318. hb1->add_child(parent_browse_button);
  319. parent_browse_button->hide();
  320. vb->add_margin_child(TTR("Inherits:"), hb1);
  321. is_browsing_parent = false;
  322. language_menu = memnew(OptionButton);
  323. vb->add_margin_child(TTR("Language"), language_menu);
  324. int default_lang = 0;
  325. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  326. String lang = ScriptServer::get_language(i)->get_name();
  327. language_menu->add_item(lang);
  328. if (lang == "GDScript") {
  329. default_lang = i;
  330. }
  331. }
  332. editor_settings = EditorSettings::get_singleton();
  333. String last_selected_language = editor_settings->get_project_metadata("script_setup", "last_selected_language", "");
  334. if (last_selected_language != "") {
  335. for (int i = 0; i < language_menu->get_item_count(); i++) {
  336. if (language_menu->get_item_text(i) == last_selected_language) {
  337. language_menu->select(i);
  338. break;
  339. }
  340. }
  341. } else {
  342. language_menu->select(default_lang);
  343. }
  344. language_menu->connect("item_selected", this, "_lang_changed");
  345. //parent_name->set_text();
  346. vb2 = memnew(VBoxContainer);
  347. path_vb = memnew(VBoxContainer);
  348. vb2->add_child(path_vb);
  349. HBoxContainer *hbc = memnew(HBoxContainer);
  350. file_path = memnew(LineEdit);
  351. file_path->connect("text_changed", this, "_path_changed");
  352. hbc->add_child(file_path);
  353. file_path->set_h_size_flags(SIZE_EXPAND_FILL);
  354. Button *b = memnew(Button);
  355. b->set_text(" .. ");
  356. b->connect("pressed", this, "_browse_path", varray(false));
  357. hbc->add_child(b);
  358. path_vb->add_child(hbc);
  359. path_error_label = memnew(Label);
  360. path_vb->add_child(path_error_label);
  361. path_error_label->set_text(TTR("Error!"));
  362. path_error_label->set_align(Label::ALIGN_CENTER);
  363. internal = memnew(CheckButton);
  364. internal->set_text(TTR("Built-In Script"));
  365. vb2->add_child(internal);
  366. internal->connect("pressed", this, "_built_in_pressed");
  367. vb->add_margin_child(TTR("Path:"), vb2);
  368. set_size(Size2(200, 150));
  369. set_hide_on_ok(false);
  370. set_title(TTR("Attach Node Script"));
  371. file_browse = memnew(EditorFileDialog);
  372. file_browse->connect("file_selected", this, "_file_selected");
  373. add_child(file_browse);
  374. get_ok()->set_text(TTR("Create"));
  375. alert = memnew(AcceptDialog);
  376. add_child(alert);
  377. _lang_changed(0);
  378. create_new = true;
  379. }