script_create_dialog.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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-2015 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.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("Invaild 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("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("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("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("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 (class_name->is_editable() && !_validate(class_name->get_text())) {
  84. alert->set_text("Class Name is Invalid!");
  85. alert->popup_centered_minsize();
  86. return;
  87. }
  88. if (!_validate(parent_name->get_text())) {
  89. alert->set_text("Parent Class Name is Invalid!");
  90. alert->popup_centered_minsize();
  91. return;
  92. }
  93. String cname;
  94. if (class_name->is_editable())
  95. cname=class_name->get_text();
  96. String text = ScriptServer::get_language( language_menu->get_selected() )->get_template(cname,parent_name->get_text());
  97. Script *script = ScriptServer::get_language( language_menu->get_selected() )->create_script();
  98. script->set_source_code(text);
  99. if (cname!="")
  100. script->set_name(cname);
  101. Ref<Script> scr(script);
  102. if (!internal->is_pressed()) {
  103. String lpath = Globals::get_singleton()->localize_path(file_path->get_text());
  104. script->set_path(lpath);
  105. if (!path_valid) {
  106. alert->set_text("Path is Invalid!");
  107. alert->popup_centered_minsize();
  108. return;
  109. }
  110. Error err = ResourceSaver::save(lpath,scr,ResourceSaver::FLAG_CHANGE_PATH);
  111. if (err!=OK) {
  112. alert->set_text("Could not create script in filesystem: "+String(""));
  113. alert->popup_centered_minsize();
  114. return;
  115. }
  116. scr->set_path(lpath);
  117. //EditorFileSystem::get_singleton()->update_file(lpath,scr->get_type());
  118. }
  119. hide();
  120. emit_signal("script_created",scr);
  121. }
  122. void ScriptCreateDialog::_lang_changed(int l) {
  123. l=language_menu->get_selected();
  124. if (ScriptServer::get_language( l )->has_named_classes()) {
  125. class_name->set_editable(true);
  126. } else {
  127. class_name->set_editable(false);
  128. }
  129. if (file_path->get_text().basename()==initial_bp) {
  130. file_path->set_text(initial_bp+"."+ScriptServer::get_language( l )->get_extension());
  131. _path_changed(file_path->get_text());
  132. }
  133. _class_name_changed(class_name->get_text());
  134. }
  135. void ScriptCreateDialog::_built_in_pressed() {
  136. if (internal->is_pressed()) {
  137. path_vb->hide();
  138. } else {
  139. path_vb->show();
  140. }
  141. }
  142. void ScriptCreateDialog::_browse_path() {
  143. file_browse->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  144. file_browse->clear_filters();
  145. List<String> extensions;
  146. int l=language_menu->get_selected();
  147. ScriptServer::get_language( l )->get_recognized_extensions(&extensions);
  148. for(List<String>::Element *E=extensions.front();E;E=E->next()) {
  149. file_browse->add_filter("*."+E->get());
  150. }
  151. file_browse->set_current_path(file_path->get_text());
  152. file_browse->popup_centered_ratio();
  153. }
  154. void ScriptCreateDialog::_file_selected(const String& p_file) {
  155. String p = Globals::get_singleton()->localize_path(p_file);
  156. file_path->set_text(p);
  157. _path_changed(p);
  158. }
  159. void ScriptCreateDialog::_path_changed(const String& p_path) {
  160. path_valid=false;
  161. String p =p_path;
  162. if (p=="") {
  163. path_error_label->set_text("Path is Empty");
  164. path_error_label->add_color_override("font_color",Color(1,0.4,0.0,0.8));
  165. return;
  166. }
  167. p = Globals::get_singleton()->localize_path(p);
  168. if (!p.begins_with("res://")) {
  169. path_error_label->set_text("Path is not local");
  170. path_error_label->add_color_override("font_color",Color(1,0.4,0.0,0.8));
  171. return;
  172. }
  173. if (p.find("/") || p.find("\\")) {
  174. DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  175. if (d->change_dir(p.get_base_dir())!=OK) {
  176. path_error_label->set_text("Base Path Invalid");
  177. path_error_label->add_color_override("font_color",Color(1,0.4,0.0,0.8));
  178. memdelete(d);
  179. return;
  180. }
  181. memdelete(d);
  182. }
  183. FileAccess *f = FileAccess::create(FileAccess::ACCESS_RESOURCES);
  184. if (f->file_exists(p)) {
  185. path_error_label->set_text("File Exists");
  186. path_error_label->add_color_override("font_color",Color(1,0.4,0.0,0.8));
  187. memdelete(f);
  188. return;
  189. }
  190. memdelete(f);
  191. String extension=p.extension();
  192. List<String> extensions;
  193. int l=language_menu->get_selected();
  194. ScriptServer::get_language( l )->get_recognized_extensions(&extensions);
  195. bool found=false;
  196. for(List<String>::Element *E=extensions.front();E;E=E->next()) {
  197. if (E->get().nocasecmp_to(extension)==0) {
  198. found=true;
  199. break;
  200. }
  201. }
  202. if (!found) {
  203. path_error_label->set_text("Invalid Extension");
  204. path_error_label->add_color_override("font_color",Color(1,0.4,0.0,0.8));
  205. return;
  206. }
  207. path_error_label->set_text("Path is Valid");
  208. path_error_label->add_color_override("font_color",Color(0,1.0,0.8,0.8));
  209. path_valid=true;
  210. }
  211. void ScriptCreateDialog::_bind_methods() {
  212. ObjectTypeDB::bind_method("_class_name_changed",&ScriptCreateDialog::_class_name_changed);
  213. ObjectTypeDB::bind_method("_lang_changed",&ScriptCreateDialog::_lang_changed);
  214. ObjectTypeDB::bind_method("_built_in_pressed",&ScriptCreateDialog::_built_in_pressed);
  215. ObjectTypeDB::bind_method("_browse_path",&ScriptCreateDialog::_browse_path);
  216. ObjectTypeDB::bind_method("_file_selected",&ScriptCreateDialog::_file_selected);
  217. ObjectTypeDB::bind_method("_path_changed",&ScriptCreateDialog::_path_changed);
  218. ADD_SIGNAL(MethodInfo("script_created",PropertyInfo(Variant::OBJECT,"script",PROPERTY_HINT_RESOURCE_TYPE,"Script")));
  219. }
  220. ScriptCreateDialog::ScriptCreateDialog() {
  221. /* SNAP DIALOG */
  222. VBoxContainer *vb = memnew( VBoxContainer );
  223. add_child(vb);
  224. set_child_rect(vb);
  225. class_name = memnew( LineEdit );
  226. VBoxContainer *vb2 = memnew( VBoxContainer );
  227. vb2->add_child(class_name);
  228. class_name->connect("text_changed", this,"_class_name_changed");
  229. error_label = memnew(Label);
  230. error_label->set_text("valid chars: a-z A-Z 0-9 _");
  231. error_label->set_align(Label::ALIGN_CENTER);
  232. vb2->add_child(error_label);
  233. vb->add_margin_child("Class Name:",vb2);
  234. parent_name = memnew( LineEdit );
  235. vb->add_margin_child("Inherits:",parent_name);
  236. parent_name->connect("text_changed", this,"_class_name_changed");
  237. language_menu = memnew( OptionButton );
  238. vb->add_margin_child("Language",language_menu);
  239. for(int i=0;i<ScriptServer::get_language_count();i++) {
  240. language_menu->add_item(ScriptServer::get_language(i)->get_name());
  241. }
  242. language_menu->select(0);
  243. language_menu->connect("item_selected",this,"_lang_changed");
  244. //parent_name->set_text();
  245. vb2 = memnew( VBoxContainer );
  246. path_vb = memnew( VBoxContainer );
  247. vb2->add_child(path_vb);
  248. HBoxContainer *hbc = memnew( HBoxContainer );
  249. file_path = memnew( LineEdit );
  250. file_path->connect("text_changed",this,"_path_changed");
  251. hbc->add_child(file_path);
  252. file_path->set_h_size_flags(SIZE_EXPAND_FILL);
  253. Button *b = memnew( Button );
  254. b->set_text(" .. ");
  255. b->connect("pressed",this,"_browse_path");
  256. hbc->add_child(b);
  257. path_vb->add_child(hbc);
  258. path_error_label = memnew( Label );
  259. path_vb->add_child( path_error_label );
  260. path_error_label->set_text("Error!");
  261. path_error_label->set_align(Label::ALIGN_CENTER);
  262. internal = memnew( CheckButton );
  263. internal->set_text("Built-In Script");
  264. vb2->add_child(internal);
  265. internal->connect("pressed",this,"_built_in_pressed");
  266. vb->add_margin_child("Path:",vb2);
  267. set_size(Size2(200,150));
  268. set_hide_on_ok(false);
  269. set_title("Create Script for Node");
  270. file_browse = memnew( EditorFileDialog );
  271. file_browse->connect("file_selected",this,"_file_selected");
  272. add_child(file_browse);
  273. get_ok()->set_text("Create");
  274. alert = memnew( AcceptDialog );
  275. add_child(alert);
  276. _lang_changed(0);
  277. }