script_create_dialog.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "script_create_dialog.h"
  30. #include "script_language.h"
  31. #include "globals.h"
  32. #include "io/resource_saver.h"
  33. #include "os/file_access.h"
  34. #include "editor_file_system.h"
  35. void ScriptCreateDialog::config(const String& p_base_name,const String&p_base_path) {
  36. class_name->set_text("");
  37. parent_name->set_text(p_base_name);
  38. if (p_base_path!="") {
  39. initial_bp=p_base_path.get_basename();
  40. file_path->set_text(initial_bp+"."+ScriptServer::get_language( language_menu->get_selected() )->get_extension());
  41. } else {
  42. initial_bp="";
  43. file_path->set_text("");
  44. }
  45. _class_name_changed("");
  46. _path_changed(file_path->get_text());
  47. }
  48. bool ScriptCreateDialog::_validate(const String& p_string) {
  49. if (p_string.length()==0)
  50. return false;
  51. for(int i=0;i<p_string.length();i++) {
  52. if (i==0) {
  53. if (p_string[0]>='0' && p_string[0]<='9')
  54. return false; // no start with number plz
  55. }
  56. 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]=='_';
  57. if (!valid_char)
  58. return false;
  59. }
  60. return true;
  61. }
  62. void ScriptCreateDialog::_class_name_changed(const String& p_name) {
  63. if (!_validate(parent_name->get_text())) {
  64. error_label->set_text(TTR("Invalid parent class name"));
  65. error_label->add_color_override("font_color",Color(1,0.4,0.0,0.8));
  66. } else if (class_name->is_editable()) {
  67. if (class_name->get_text()=="") {
  68. error_label->set_text(TTR("Valid chars:")+" a-z A-Z 0-9 _");
  69. error_label->add_color_override("font_color",Color(1,1,1,0.6));
  70. } else if (!_validate(class_name->get_text())) {
  71. error_label->set_text(TTR("Invalid class name"));
  72. error_label->add_color_override("font_color",Color(1,0.2,0.2,0.8));
  73. } else {
  74. error_label->set_text(TTR("Valid name"));
  75. error_label->add_color_override("font_color",Color(0,1.0,0.8,0.8));
  76. }
  77. } else {
  78. error_label->set_text(TTR("N/A"));
  79. error_label->add_color_override("font_color",Color(0,1.0,0.8,0.8));
  80. }
  81. }
  82. void ScriptCreateDialog::ok_pressed() {
  83. if (create_new){
  84. _create_new();
  85. } else {
  86. _load_exist();
  87. }
  88. create_new=true;
  89. _update_controls();
  90. }
  91. void ScriptCreateDialog::_create_new() {
  92. if (class_name->is_editable() && !_validate(class_name->get_text())) {
  93. alert->set_text(TTR("Class name is invalid!"));
  94. alert->popup_centered_minsize();
  95. return;
  96. }
  97. if (!_validate(parent_name->get_text())) {
  98. alert->set_text(TTR("Parent class name is invalid!"));
  99. alert->popup_centered_minsize();
  100. return;
  101. }
  102. String cname;
  103. if (class_name->is_editable())
  104. cname=class_name->get_text();
  105. Ref<Script> scr = ScriptServer::get_language( language_menu->get_selected() )->get_template(cname,parent_name->get_text());
  106. String selected_language = language_menu->get_item_text(language_menu->get_selected());
  107. editor_settings->set_last_selected_language(selected_language);
  108. if (cname!="")
  109. scr->set_name(cname);
  110. if (!internal->is_pressed()) {
  111. String lpath = GlobalConfig::get_singleton()->localize_path(file_path->get_text());
  112. scr->set_path(lpath);
  113. if (!path_valid) {
  114. alert->set_text(TTR("Invalid path!"));
  115. alert->popup_centered_minsize();
  116. return;
  117. }
  118. Error err = ResourceSaver::save(lpath,scr,ResourceSaver::FLAG_CHANGE_PATH);
  119. if (err!=OK) {
  120. alert->set_text(TTR("Could not create script in filesystem."));
  121. alert->popup_centered_minsize();
  122. return;
  123. }
  124. }
  125. hide();
  126. emit_signal("script_created",scr);
  127. }
  128. void ScriptCreateDialog::_load_exist() {
  129. String path=file_path->get_text();
  130. RES p_script = ResourceLoader::load(path, "Script");
  131. if (p_script.is_null()) {
  132. alert->get_ok()->set_text(TTR("Ugh"));
  133. alert->set_text(vformat(TTR("Error loading script from %s"), path));
  134. alert->popup_centered_minsize();
  135. return;
  136. }
  137. hide();
  138. emit_signal("script_created",p_script.get_ref_ptr());
  139. }
  140. void ScriptCreateDialog::_lang_changed(int l) {
  141. l=language_menu->get_selected();
  142. if (ScriptServer::get_language( l )->has_named_classes()) {
  143. class_name->set_editable(true);
  144. } else {
  145. class_name->set_editable(false);
  146. }
  147. String selected_ext="."+ScriptServer::get_language( l )->get_extension();
  148. String path=file_path->get_text();
  149. String extension="";
  150. if (path.find(".")>=0) {
  151. extension=path.get_extension();
  152. }
  153. if (extension.length()==0) {
  154. // add extension if none
  155. path+=selected_ext;
  156. _path_changed(path);
  157. } else {
  158. // change extension by selected language
  159. List<String> extensions;
  160. // get all possible extensions for script
  161. for (int l=0;l<language_menu->get_item_count();l++) {
  162. ScriptServer::get_language( l )->get_recognized_extensions(&extensions);
  163. }
  164. for(List<String>::Element *E=extensions.front();E;E=E->next()) {
  165. if (E->get().nocasecmp_to(extension)==0) {
  166. path=path.get_basename()+selected_ext;
  167. _path_changed(path);
  168. break;
  169. }
  170. }
  171. }
  172. file_path->set_text(path);
  173. _class_name_changed(class_name->get_text());
  174. }
  175. void ScriptCreateDialog::_built_in_pressed() {
  176. if (internal->is_pressed()) {
  177. path_vb->hide();
  178. } else {
  179. path_vb->show();
  180. }
  181. }
  182. void ScriptCreateDialog::_browse_path() {
  183. file_browse->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  184. file_browse->set_disable_overwrite_warning(true);
  185. file_browse->clear_filters();
  186. List<String> extensions;
  187. // get all possible extensions for script
  188. for (int l=0;l<language_menu->get_item_count();l++) {
  189. ScriptServer::get_language( l )->get_recognized_extensions(&extensions);
  190. }
  191. for(List<String>::Element *E=extensions.front();E;E=E->next()) {
  192. file_browse->add_filter("*."+E->get());
  193. }
  194. file_browse->set_current_path(file_path->get_text());
  195. file_browse->popup_centered_ratio();
  196. }
  197. void ScriptCreateDialog::_file_selected(const String& p_file) {
  198. String p = GlobalConfig::get_singleton()->localize_path(p_file);
  199. file_path->set_text(p);
  200. _path_changed(p);
  201. }
  202. void ScriptCreateDialog::_path_changed(const String& p_path) {
  203. path_valid=false;
  204. String p =p_path;
  205. if (p=="") {
  206. path_error_label->set_text(TTR("Path is empty"));
  207. path_error_label->add_color_override("font_color",Color(1,0.4,0.0,0.8));
  208. return;
  209. }
  210. p = GlobalConfig::get_singleton()->localize_path(p);
  211. if (!p.begins_with("res://")) {
  212. path_error_label->set_text(TTR("Path is not local"));
  213. path_error_label->add_color_override("font_color",Color(1,0.4,0.0,0.8));
  214. return;
  215. }
  216. if (p.find("/") || p.find("\\")) {
  217. DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  218. if (d->change_dir(p.get_base_dir())!=OK) {
  219. path_error_label->set_text(TTR("Invalid base path"));
  220. path_error_label->add_color_override("font_color",Color(1,0.4,0.0,0.8));
  221. memdelete(d);
  222. return;
  223. }
  224. memdelete(d);
  225. }
  226. FileAccess *f = FileAccess::create(FileAccess::ACCESS_RESOURCES);
  227. create_new=!f->file_exists(p);
  228. memdelete(f);
  229. String extension=p.get_extension();
  230. List<String> extensions;
  231. // get all possible extensions for script
  232. for (int l=0;l<language_menu->get_item_count();l++) {
  233. ScriptServer::get_language( l )->get_recognized_extensions(&extensions);
  234. }
  235. bool found=false;
  236. int index=0;
  237. for(List<String>::Element *E=extensions.front();E;E=E->next()) {
  238. if (E->get().nocasecmp_to(extension)==0) {
  239. language_menu->select(index); // change Language option by extension
  240. found=true;
  241. break;
  242. }
  243. index++;
  244. }
  245. if (!found) {
  246. path_error_label->set_text(TTR("Invalid extension"));
  247. path_error_label->add_color_override("font_color",Color(1,0.4,0.0,0.8));
  248. return;
  249. }
  250. _update_controls();
  251. path_error_label->add_color_override("font_color",Color(0,1.0,0.8,0.8));
  252. path_valid=true;
  253. }
  254. void ScriptCreateDialog::_update_controls() {
  255. if (create_new) {
  256. path_error_label->set_text(TTR("Create new script"));
  257. get_ok()->set_text(TTR("Create"));
  258. } else {
  259. path_error_label->set_text(TTR("Load existing script"));
  260. get_ok()->set_text(TTR("Load"));
  261. }
  262. parent_name->set_editable(create_new);
  263. internal->set_disabled(!create_new);
  264. }
  265. void ScriptCreateDialog::_bind_methods() {
  266. ClassDB::bind_method("_class_name_changed",&ScriptCreateDialog::_class_name_changed);
  267. ClassDB::bind_method("_lang_changed",&ScriptCreateDialog::_lang_changed);
  268. ClassDB::bind_method("_built_in_pressed",&ScriptCreateDialog::_built_in_pressed);
  269. ClassDB::bind_method("_browse_path",&ScriptCreateDialog::_browse_path);
  270. ClassDB::bind_method("_file_selected",&ScriptCreateDialog::_file_selected);
  271. ClassDB::bind_method("_path_changed",&ScriptCreateDialog::_path_changed);
  272. ADD_SIGNAL(MethodInfo("script_created",PropertyInfo(Variant::OBJECT,"script",PROPERTY_HINT_RESOURCE_TYPE,"Script")));
  273. }
  274. ScriptCreateDialog::ScriptCreateDialog() {
  275. /* SNAP DIALOG */
  276. VBoxContainer *vb = memnew( VBoxContainer );
  277. add_child(vb);
  278. //set_child_rect(vb);
  279. class_name = memnew( LineEdit );
  280. VBoxContainer *vb2 = memnew( VBoxContainer );
  281. vb2->add_child(class_name);
  282. class_name->connect("text_changed", this,"_class_name_changed");
  283. error_label = memnew(Label);
  284. error_label->set_text("valid chars: a-z A-Z 0-9 _");
  285. error_label->set_align(Label::ALIGN_CENTER);
  286. vb2->add_child(error_label);
  287. vb->add_margin_child(TTR("Class Name:"),vb2);
  288. parent_name = memnew( LineEdit );
  289. vb->add_margin_child(TTR("Inherits:"),parent_name);
  290. parent_name->connect("text_changed", this,"_class_name_changed");
  291. language_menu = memnew( OptionButton );
  292. vb->add_margin_child(TTR("Language"),language_menu);
  293. for(int i=0;i<ScriptServer::get_language_count();i++) {
  294. language_menu->add_item(ScriptServer::get_language(i)->get_name());
  295. }
  296. editor_settings = EditorSettings::get_singleton();
  297. String last_selected_language = editor_settings->get_last_selected_language();
  298. if (last_selected_language != "")
  299. for (int i = 0; i < language_menu->get_item_count(); i++)
  300. if (language_menu->get_item_text(i) == last_selected_language)
  301. {
  302. language_menu->select(i);
  303. break;
  304. }
  305. else language_menu->select(0);
  306. language_menu->connect("item_selected",this,"_lang_changed");
  307. //parent_name->set_text();
  308. vb2 = memnew( VBoxContainer );
  309. path_vb = memnew( VBoxContainer );
  310. vb2->add_child(path_vb);
  311. HBoxContainer *hbc = memnew( HBoxContainer );
  312. file_path = memnew( LineEdit );
  313. file_path->connect("text_changed",this,"_path_changed");
  314. hbc->add_child(file_path);
  315. file_path->set_h_size_flags(SIZE_EXPAND_FILL);
  316. Button *b = memnew( Button );
  317. b->set_text(" .. ");
  318. b->connect("pressed",this,"_browse_path");
  319. hbc->add_child(b);
  320. path_vb->add_child(hbc);
  321. path_error_label = memnew( Label );
  322. path_vb->add_child( path_error_label );
  323. path_error_label->set_text(TTR("Error!"));
  324. path_error_label->set_align(Label::ALIGN_CENTER);
  325. internal = memnew( CheckButton );
  326. internal->set_text(TTR("Built-In Script"));
  327. vb2->add_child(internal);
  328. internal->connect("pressed",this,"_built_in_pressed");
  329. vb->add_margin_child(TTR("Path:"),vb2);
  330. set_size(Size2(200,150));
  331. set_hide_on_ok(false);
  332. set_title(TTR("Attach Node Script"));
  333. file_browse = memnew( EditorFileDialog );
  334. file_browse->connect("file_selected",this,"_file_selected");
  335. add_child(file_browse);
  336. get_ok()->set_text(TTR("Create"));
  337. alert = memnew( AcceptDialog );
  338. add_child(alert);
  339. _lang_changed(0);
  340. create_new=true;
  341. }