editor_settings.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /*************************************************************************/
  2. /* editor_settings.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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 "editor_settings.h"
  30. #include "os/os.h"
  31. #include "os/dir_access.h"
  32. #include "os/file_access.h"
  33. #include "io/object_format_xml.h"
  34. #include "version.h"
  35. #include "scene/main/scene_main_loop.h"
  36. #include "os/os.h"
  37. #include "scene/main/node.h"
  38. #include "io/resource_loader.h"
  39. #include "io/resource_saver.h"
  40. #include "scene/main/viewport.h"
  41. #include "io/config_file.h"
  42. #include "editor_node.h"
  43. #include "globals.h"
  44. Ref<EditorSettings> EditorSettings::singleton=NULL;
  45. EditorSettings *EditorSettings::get_singleton() {
  46. return singleton.ptr();
  47. }
  48. bool EditorSettings::_set(const StringName& p_name, const Variant& p_value) {
  49. _THREAD_SAFE_METHOD_
  50. if (p_value.get_type()==Variant::NIL)
  51. props.erase(p_name);
  52. else {
  53. if (props.has(p_name))
  54. props[p_name].variant=p_value;
  55. else
  56. props[p_name]=VariantContainer(p_value,last_order++);
  57. }
  58. emit_signal("settings_changed");
  59. return true;
  60. }
  61. bool EditorSettings::_get(const StringName& p_name,Variant &r_ret) const {
  62. _THREAD_SAFE_METHOD_
  63. const VariantContainer *v=props.getptr(p_name);
  64. if (!v)
  65. return false;
  66. r_ret = v->variant;
  67. return true;
  68. }
  69. struct _EVCSort {
  70. String name;
  71. Variant::Type type;
  72. int order;
  73. bool operator<(const _EVCSort& p_vcs) const{ return order< p_vcs.order; }
  74. };
  75. void EditorSettings::_get_property_list(List<PropertyInfo> *p_list) const {
  76. _THREAD_SAFE_METHOD_
  77. const String *k=NULL;
  78. Set<_EVCSort> vclist;
  79. while ((k=props.next(k))) {
  80. const VariantContainer *v=props.getptr(*k);
  81. if (v->hide_from_editor)
  82. continue;
  83. _EVCSort vc;
  84. vc.name=*k;
  85. vc.order=v->order;
  86. vc.type=v->variant.get_type();
  87. vclist.insert(vc);
  88. }
  89. for(Set<_EVCSort>::Element *E=vclist.front();E;E=E->next()) {
  90. int pinfo = PROPERTY_USAGE_STORAGE;
  91. if (!E->get().name.begins_with("_"))
  92. pinfo|=PROPERTY_USAGE_EDITOR;
  93. PropertyInfo pi(E->get().type, E->get().name);
  94. pi.usage=pinfo;
  95. if (hints.has(E->get().name))
  96. pi=hints[E->get().name];
  97. p_list->push_back( pi );
  98. }
  99. }
  100. bool EditorSettings::has(String p_var) const {
  101. _THREAD_SAFE_METHOD_
  102. return props.has(p_var);
  103. }
  104. void EditorSettings::erase(String p_var) {
  105. _THREAD_SAFE_METHOD_
  106. props.erase(p_var);
  107. }
  108. void EditorSettings::raise_order(const String& p_name) {
  109. _THREAD_SAFE_METHOD_
  110. ERR_FAIL_COND(!props.has(p_name));
  111. props[p_name].order=++last_order;
  112. }
  113. Variant _EDITOR_DEF( const String& p_var, const Variant& p_default) {
  114. if (EditorSettings::get_singleton()->has(p_var))
  115. return EditorSettings::get_singleton()->get(p_var);
  116. EditorSettings::get_singleton()->set(p_var,p_default);
  117. return p_default;
  118. }
  119. void EditorSettings::create() {
  120. if (singleton.ptr())
  121. return; //pointless
  122. DirAccess *dir=NULL;
  123. Object *object;
  124. Variant meta;
  125. String config_path;
  126. String config_dir;
  127. String config_file="editor_settings.xml";
  128. if (OS::get_singleton()->has_environment("APPDATA")) {
  129. // Most likely under windows, save here
  130. config_path=OS::get_singleton()->get_environment("APPDATA");
  131. config_dir=String(_MKSTR(VERSION_SHORT_NAME)).capitalize();
  132. } else if (OS::get_singleton()->has_environment("HOME")) {
  133. config_path=OS::get_singleton()->get_environment("HOME");
  134. config_dir="."+String(_MKSTR(VERSION_SHORT_NAME)).to_lower();
  135. }
  136. ObjectTypeDB::register_type<EditorSettings>(); //otherwise it can't be unserialized
  137. String config_file_path;
  138. if (config_path!=""){
  139. dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  140. if (dir->change_dir(config_path)!=OK) {
  141. ERR_PRINT("Cannot find path for config directory!");
  142. memdelete(dir);
  143. goto fail;
  144. }
  145. if (dir->change_dir(config_dir)!=OK) {
  146. dir->make_dir(config_dir);
  147. if (dir->change_dir(config_dir)!=OK) {
  148. ERR_PRINT("Cannot create config directory!");
  149. memdelete(dir);
  150. goto fail;
  151. }
  152. }
  153. if (dir->change_dir("templates")!=OK) {
  154. dir->make_dir("templates");
  155. } else {
  156. dir->change_dir("..");
  157. }
  158. if (dir->change_dir("tmp")!=OK) {
  159. dir->make_dir("tmp");
  160. } else {
  161. dir->change_dir("..");
  162. }
  163. if (dir->change_dir("plugins")!=OK) {
  164. dir->make_dir("plugins");
  165. } else {
  166. dir->change_dir("..");
  167. }
  168. // path at least is validated, so validate config file
  169. config_file_path = config_path+"/"+config_dir+"/"+config_file;
  170. if (!dir->file_exists(config_file)) {
  171. memdelete(dir);
  172. WARN_PRINT("Config file does not exist, creating.")
  173. goto fail;
  174. }
  175. memdelete(dir);
  176. singleton = ResourceLoader::load(config_file_path,"EditorSettings");
  177. if (singleton.is_null()) {
  178. WARN_PRINT("Could not open config file.");
  179. goto fail;
  180. }
  181. singleton->config_file_path=config_file_path;
  182. singleton->settings_path=config_path+"/"+config_dir;
  183. if (OS::get_singleton()->is_stdout_verbose()) {
  184. print_line("EditorSettings: Load OK!");
  185. }
  186. singleton->scan_plugins();
  187. return;
  188. }
  189. fail:
  190. singleton = Ref<EditorSettings>( memnew( EditorSettings ) );
  191. singleton->config_file_path=config_file_path;
  192. singleton->_load_defaults();
  193. singleton->scan_plugins();
  194. }
  195. String EditorSettings::get_settings_path() const {
  196. return settings_path;
  197. }
  198. Error EditorSettings::_load_plugin(const String& p_path, Plugin &plugin) {
  199. if (!FileAccess::exists(p_path))
  200. return ERR_FILE_NOT_FOUND;
  201. Ref<ConfigFile> cf = memnew(ConfigFile);
  202. Error err = cf->load(p_path);
  203. ERR_EXPLAIN("Error loading plugin description for: "+p_path);
  204. ERR_FAIL_COND_V(err!=OK,ERR_CANT_OPEN);
  205. plugin.instance=NULL;
  206. ERR_FAIL_COND_V(!cf->has_section_key("plugin","name"),ERR_INVALID_DATA);
  207. ERR_FAIL_COND_V(!cf->has_section_key("plugin","installs"),ERR_INVALID_DATA);
  208. ERR_FAIL_COND_V(!cf->has_section_key("plugin","author"),ERR_INVALID_DATA);
  209. ERR_FAIL_COND_V(!cf->has_section_key("plugin","version"),ERR_INVALID_DATA);
  210. ERR_FAIL_COND_V(!cf->has_section_key("plugin","script"),ERR_INVALID_DATA);
  211. plugin.name=cf->get_value("plugin","name");
  212. plugin.author=cf->get_value("plugin","author");
  213. plugin.version=cf->get_value("plugin","version");
  214. plugin.script=cf->get_value("plugin","script");
  215. if (cf->has_section_key("plugin","description"))
  216. plugin.description=cf->get_value("plugin","description");
  217. plugin.installs=cf->get_value("plugin","installs");
  218. if (cf->has_section_key("plugin","install_files"))
  219. plugin.install_files=cf->get_value("plugin","install_files");
  220. return OK;
  221. }
  222. void EditorSettings::scan_plugins() {
  223. Map<String,Plugin> new_plugins;
  224. new_plugins.clear();
  225. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  226. Error err = d->change_dir(get_settings_path().plus_file("plugins"));
  227. if (err!=OK) {
  228. memdelete(d);
  229. ERR_EXPLAIN("Plugin dir does not exist!")
  230. ERR_FAIL_COND(err!=OK);
  231. }
  232. d->list_dir_begin();
  233. String base = d->get_current_dir();
  234. //print_line("list diring on: "+base);
  235. while(true) {
  236. String p = d->get_next();
  237. if (p=="")
  238. break;
  239. if (!d->current_is_dir() || p.begins_with("."))
  240. continue;
  241. String cfpath=d->get_current_dir().plus_file(p+"/plugin.cfg");
  242. Plugin plugin;
  243. Error err = _load_plugin(cfpath,plugin);
  244. ERR_CONTINUE(err!=OK);
  245. if (plugins.has(p))
  246. plugin.instance=plugins[p].instance;
  247. new_plugins[p]=plugin;
  248. }
  249. plugins=new_plugins;
  250. memdelete(d);
  251. }
  252. void EditorSettings::save() {
  253. //_THREAD_SAFE_METHOD_
  254. if (!singleton.ptr())
  255. return;
  256. if (singleton->config_file_path=="") {
  257. ERR_PRINT("Cannot save EditorSettings config, no valid path");
  258. return;
  259. }
  260. Error err = ResourceSaver::save(singleton->config_file_path,singleton);
  261. if (err!=OK) {
  262. ERR_PRINT("Can't Save!");
  263. return;
  264. }
  265. if (OS::get_singleton()->is_stdout_verbose()) {
  266. print_line("EditorSettings Save OK!");
  267. }
  268. }
  269. void EditorSettings::destroy() {
  270. if (!singleton.ptr())
  271. return;
  272. save();
  273. singleton=Ref<EditorSettings>();
  274. }
  275. void EditorSettings::_load_defaults() {
  276. _THREAD_SAFE_METHOD_
  277. set("global/font","");
  278. hints["global/font"]=PropertyInfo(Variant::STRING,"global/font",PROPERTY_HINT_GLOBAL_FILE,"*.fnt");
  279. set("text_editor/background_color",Color::html("3b000000"));
  280. set("text_editor/text_color",Color::html("aaaaaa"));
  281. set("text_editor/text_selected_color",Color::html("000000"));
  282. set("text_editor/keyword_color",Color::html("ffffb3"));
  283. set("text_editor/base_type_color",Color::html("a4ffd4"));
  284. set("text_editor/engine_type_color",Color::html("83d3ff"));
  285. set("text_editor/comment_color",Color::html("983d1b"));
  286. set("text_editor/string_color",Color::html("ef6ebe"));
  287. set("text_editor/symbol_color",Color::html("badfff"));
  288. set("text_editor/selection_color",Color::html("7b5dbe"));
  289. set("text_editor/idle_parse_delay",2);
  290. set("text_editor/create_signal_callbacks",true);
  291. set("text_editor/autosave_interval_seconds",60);
  292. set("text_editor/font","");
  293. set("text_editor/auto_brace_complete", false);
  294. hints["text_editor/font"]=PropertyInfo(Variant::STRING,"text_editor/font",PROPERTY_HINT_GLOBAL_FILE,"*.fnt");
  295. set("3d_editor/default_fov",45.0);
  296. set("3d_editor/default_z_near",0.1);
  297. set("3d_editor/default_z_far",500.0);
  298. set("3d_editor/navigation_scheme",0);
  299. hints["3d_editor/navigation_scheme"]=PropertyInfo(Variant::INT,"3d_editor/navigation_scheme",PROPERTY_HINT_ENUM,"Godot,Maya,Modo");
  300. set("3d_editor/orbit_modifier",0);
  301. hints["3d_editor/orbit_modifier"]=PropertyInfo(Variant::INT,"3d_editor/orbit_modifier",PROPERTY_HINT_ENUM,"None,Shift,Alt,Meta,Ctrl");
  302. set("3d_editor/pan_modifier",1);
  303. hints["3d_editor/pan_modifier"]=PropertyInfo(Variant::INT,"3d_editor/pan_modifier",PROPERTY_HINT_ENUM,"None,Shift,Alt,Meta,Ctrl");
  304. set("3d_editor/zoom_modifier",4);
  305. hints["3d_editor/zoom_modifier"]=PropertyInfo(Variant::INT,"3d_editor/zoom_modifier",PROPERTY_HINT_ENUM,"None,Shift,Alt,Meta,Ctrl");
  306. set("2d_editor/bone_width",5);
  307. set("2d_editor/bone_color1",Color(1.0,1.0,1.0,0.9));
  308. set("2d_editor/bone_color2",Color(0.75,0.75,0.75,0.9));
  309. set("2d_editor/bone_selected_color",Color(0.9,0.45,0.45,0.9));
  310. set("2d_editor/bone_ik_color",Color(0.9,0.9,0.45,0.9));
  311. set("on_save/compress_binary_resources",true);
  312. set("on_save/save_modified_external_resources",true);
  313. set("on_save/save_paths_as_relative",false);
  314. set("on_save/save_paths_without_extension",false);
  315. set("text_editor/create_signal_callbacks",true);
  316. set("animation/autorename_animation_tracks",true);
  317. set("property_editor/texture_preview_width",48);
  318. set("help/doc_path","");
  319. set("import/ask_save_before_reimport",false);
  320. set("import/pvrtc_texture_tool","");
  321. #ifdef WINDOWS_ENABLED
  322. hints["import/pvrtc_texture_tool"]=PropertyInfo(Variant::STRING,"import/pvrtc_texture_tool",PROPERTY_HINT_GLOBAL_FILE,"*.exe");
  323. #else
  324. hints["import/pvrtc_texture_tool"]=PropertyInfo(Variant::STRING,"import/pvrtc_texture_tool",PROPERTY_HINT_GLOBAL_FILE,"");
  325. #endif
  326. set("PVRTC/fast_conversion",false);
  327. set("run/auto_save_before_running",true);
  328. set("resources/save_compressed_resources",true);
  329. set("resources/auto_reload_modified_images",true);
  330. }
  331. void EditorSettings::notify_changes() {
  332. _THREAD_SAFE_METHOD_
  333. SceneMainLoop *sml=NULL;
  334. if (OS::get_singleton()->get_main_loop())
  335. sml = OS::get_singleton()->get_main_loop()->cast_to<SceneMainLoop>();
  336. if (!sml) {
  337. print_line("not SML");
  338. return;
  339. }
  340. Node *root = sml->get_root()->get_child(0);
  341. if (!root) {
  342. return;
  343. }
  344. root->propagate_notification(NOTIFICATION_EDITOR_SETTINGS_CHANGED);
  345. }
  346. void EditorSettings::add_property_hint(const PropertyInfo& p_hint) {
  347. _THREAD_SAFE_METHOD_
  348. hints[p_hint.name]=p_hint;
  349. }
  350. bool EditorSettings::is_plugin_enabled(const String& p_plugin) {
  351. if (!has("_plugins/enabled"))
  352. return false;
  353. StringArray sa=get("_plugins/enabled");
  354. for(int i=0;i<sa.size();i++) {
  355. String plugin = sa[i];
  356. if (!plugins.has(plugin))
  357. continue;
  358. if (plugin==p_plugin)
  359. return true;
  360. }
  361. return false;
  362. }
  363. void EditorSettings::enable_plugins() {
  364. // editor plugins
  365. if (has("_plugins/enabled")) {
  366. StringArray sa=get("_plugins/enabled");
  367. for(int i=0;i<sa.size();i++) {
  368. String plugin = sa[i];
  369. if (!plugins.has(plugin))
  370. continue;
  371. if (plugins[plugin].installs)
  372. continue; //not configured here
  373. set_plugin_enabled(plugin,true);
  374. }
  375. }
  376. // installed plugins
  377. List<PropertyInfo> pi;
  378. Globals::get_singleton()->get_property_list(&pi);
  379. for (List<PropertyInfo>::Element *E=pi.front();E;E=E->next()) {
  380. String p = E->get().name;
  381. if (p.begins_with("plugins/")) {
  382. load_installed_plugin(p.replace_first("plugins/",""));
  383. }
  384. }
  385. }
  386. void EditorSettings::load_installed_plugin(const String& p_plugin) {
  387. ERR_FAIL_COND( !Globals::get_singleton()->has("plugins/"+p_plugin) );
  388. String path = Globals::get_singleton()->get("plugins/"+p_plugin);
  389. Plugin plugin;
  390. Error err = _load_plugin(path.plus_file("plugin.cfg"),plugin);
  391. if (err)
  392. return;
  393. print_line("installing plugin...");
  394. EditorPlugin *ep=_load_plugin_editor(path.plus_file(plugin.script));
  395. ERR_FAIL_COND(!ep);
  396. print_line("load!");
  397. EditorNode::add_editor_plugin(ep);
  398. }
  399. EditorPlugin *EditorSettings::_load_plugin_editor(const String& p_path) {
  400. Ref<Script> script = ResourceLoader::load(p_path);
  401. ERR_EXPLAIN("Invalid Script for plugin: "+p_path);
  402. ERR_FAIL_COND_V(script.is_null(),NULL);
  403. ERR_EXPLAIN("Script has errors: "+p_path);
  404. ERR_FAIL_COND_V(!script->can_instance(),NULL);
  405. ERR_EXPLAIN("Script does not inherit EditorPlugin: "+p_path);
  406. ERR_FAIL_COND_V(script->get_instance_base_type().operator String()!="EditorPlugin",NULL);
  407. EditorPlugin *ep = memnew( EditorPlugin );
  408. ep->set_script(script.get_ref_ptr());
  409. if (!ep->get_script_instance()) {
  410. memdelete(ep);
  411. ERR_EXPLAIN("Script could't load: "+p_path);
  412. ERR_FAIL_V(NULL);
  413. }
  414. return ep;
  415. }
  416. void EditorSettings::set_plugin_enabled(const String& p_plugin, bool p_enabled) {
  417. ERR_FAIL_COND(!plugins.has(p_plugin));
  418. if (p_enabled == (plugins[p_plugin].instance!=NULL)) //already enabled or disabled
  419. return;
  420. print_line("REQUEST "+p_plugin+" to "+itos(p_enabled));
  421. StringArray sa;
  422. if (has("_plugins/enabled"))
  423. sa=get("_plugins/enabled");
  424. int idx=-1;
  425. for(int i=0;i<sa.size();i++) {
  426. if (sa[i]==p_plugin) {
  427. idx=i;
  428. break;
  429. }
  430. }
  431. if (p_enabled) {
  432. String ppath = get_settings_path().plus_file("plugins/"+p_plugin+"/"+plugins[p_plugin].script);
  433. EditorPlugin *ep=_load_plugin_editor(ppath);
  434. if (!ep)
  435. return;
  436. plugins[p_plugin].instance=ep;
  437. EditorNode::add_editor_plugin(ep);
  438. if (idx==-1)
  439. sa.push_back(p_plugin);
  440. } else {
  441. print_line("DISABLING");
  442. EditorNode::remove_editor_plugin(plugins[p_plugin].instance);
  443. memdelete(plugins[p_plugin].instance);
  444. plugins[p_plugin].instance=NULL;
  445. if (idx!=-1)
  446. sa.remove(idx);
  447. }
  448. if (sa.size()==0)
  449. set("_plugins/enabled",Variant());
  450. else
  451. set("_plugins/enabled",sa);
  452. }
  453. void EditorSettings::_bind_methods() {
  454. ADD_SIGNAL(MethodInfo("settings_changed"));
  455. }
  456. EditorSettings::EditorSettings() {
  457. //singleton=this;
  458. last_order=0;
  459. _load_defaults();
  460. }
  461. EditorSettings::~EditorSettings() {
  462. // singleton=NULL;
  463. }