| 1 | /**************************************************************************/ |
| 2 | /* editor_settings.h */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 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 | |
| 31 | #ifndef EDITOR_SETTINGS_H |
| 32 | #define EDITOR_SETTINGS_H |
| 33 | |
| 34 | #include "core/input/shortcut.h" |
| 35 | #include "core/io/config_file.h" |
| 36 | #include "core/io/resource.h" |
| 37 | #include "core/os/thread_safe.h" |
| 38 | #include "core/templates/rb_set.h" |
| 39 | |
| 40 | class EditorPlugin; |
| 41 | |
| 42 | class EditorSettings : public Resource { |
| 43 | GDCLASS(EditorSettings, Resource); |
| 44 | |
| 45 | _THREAD_SAFE_CLASS_ |
| 46 | |
| 47 | public: |
| 48 | struct Plugin { |
| 49 | EditorPlugin *instance = nullptr; |
| 50 | String path; |
| 51 | String name; |
| 52 | String author; |
| 53 | String version; |
| 54 | String description; |
| 55 | bool installs = false; |
| 56 | String script; |
| 57 | Vector<String> install_files; |
| 58 | }; |
| 59 | |
| 60 | private: |
| 61 | struct VariantContainer { |
| 62 | int order = 0; |
| 63 | Variant variant; |
| 64 | Variant initial; |
| 65 | bool has_default_value = false; |
| 66 | bool hide_from_editor = false; |
| 67 | bool save = false; |
| 68 | bool restart_if_changed = false; |
| 69 | |
| 70 | VariantContainer() {} |
| 71 | |
| 72 | VariantContainer(const Variant &p_variant, int p_order) : |
| 73 | order(p_order), |
| 74 | variant(p_variant) { |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | static Ref<EditorSettings> singleton; |
| 79 | |
| 80 | HashSet<String> changed_settings; |
| 81 | |
| 82 | HashMap<String, PropertyInfo> hints; |
| 83 | HashMap<String, VariantContainer> props; |
| 84 | int last_order; |
| 85 | |
| 86 | Ref<Resource> clipboard; |
| 87 | mutable HashMap<String, Ref<Shortcut>> shortcuts; |
| 88 | HashMap<String, List<Ref<InputEvent>>> builtin_action_overrides; |
| 89 | |
| 90 | Vector<String> favorites; |
| 91 | Vector<String> recent_dirs; |
| 92 | |
| 93 | bool save_changed_setting = true; |
| 94 | bool optimize_save = true; //do not save stuff that came from config but was not set from engine |
| 95 | |
| 96 | bool _set(const StringName &p_name, const Variant &p_value); |
| 97 | bool _set_only(const StringName &p_name, const Variant &p_value); |
| 98 | bool _get(const StringName &p_name, Variant &r_ret) const; |
| 99 | void _initial_set(const StringName &p_name, const Variant &p_value); |
| 100 | void _get_property_list(List<PropertyInfo> *p_list) const; |
| 101 | void _add_property_info_bind(const Dictionary &p_info); |
| 102 | bool _property_can_revert(const StringName &p_name) const; |
| 103 | bool _property_get_revert(const StringName &p_name, Variant &r_property) const; |
| 104 | |
| 105 | void _load_defaults(Ref<ConfigFile> = Ref<ConfigFile>()); |
| 106 | void _load_godot2_text_editor_theme(); |
| 107 | bool _save_text_editor_theme(String p_file); |
| 108 | bool _is_default_text_editor_theme(String p_theme_name); |
| 109 | |
| 110 | protected: |
| 111 | static void _bind_methods(); |
| 112 | |
| 113 | public: |
| 114 | enum { |
| 115 | NOTIFICATION_EDITOR_SETTINGS_CHANGED = 10000 |
| 116 | }; |
| 117 | |
| 118 | static EditorSettings *get_singleton(); |
| 119 | |
| 120 | static void create(); |
| 121 | void setup_language(); |
| 122 | void setup_network(); |
| 123 | static void save(); |
| 124 | static void destroy(); |
| 125 | void set_optimize_save(bool p_optimize); |
| 126 | |
| 127 | bool has_default_value(const String &p_setting) const; |
| 128 | void set_setting(const String &p_setting, const Variant &p_value); |
| 129 | Variant get_setting(const String &p_setting) const; |
| 130 | bool has_setting(const String &p_setting) const; |
| 131 | void erase(const String &p_setting); |
| 132 | void raise_order(const String &p_setting); |
| 133 | void set_initial_value(const StringName &p_setting, const Variant &p_value, bool p_update_current = false); |
| 134 | void set_restart_if_changed(const StringName &p_setting, bool p_restart); |
| 135 | void set_manually(const StringName &p_setting, const Variant &p_value, bool p_emit_signal = false) { |
| 136 | if (p_emit_signal) { |
| 137 | _set(p_setting, p_value); |
| 138 | } else { |
| 139 | _set_only(p_setting, p_value); |
| 140 | } |
| 141 | } |
| 142 | void add_property_hint(const PropertyInfo &p_hint); |
| 143 | PackedStringArray get_changed_settings() const; |
| 144 | bool check_changed_settings_in_group(const String &p_setting_prefix) const; |
| 145 | void mark_setting_changed(const String &p_setting); |
| 146 | |
| 147 | void set_resource_clipboard(const Ref<Resource> &p_resource) { clipboard = p_resource; } |
| 148 | Ref<Resource> get_resource_clipboard() const { return clipboard; } |
| 149 | |
| 150 | void set_project_metadata(const String &p_section, const String &p_key, Variant p_data); |
| 151 | Variant get_project_metadata(const String &p_section, const String &p_key, Variant p_default) const; |
| 152 | |
| 153 | void set_favorites(const Vector<String> &p_favorites); |
| 154 | Vector<String> get_favorites() const; |
| 155 | void set_recent_dirs(const Vector<String> &p_recent_dirs); |
| 156 | Vector<String> get_recent_dirs() const; |
| 157 | void load_favorites_and_recent_dirs(); |
| 158 | |
| 159 | bool is_dark_theme(); |
| 160 | |
| 161 | void list_text_editor_themes(); |
| 162 | void load_text_editor_theme(); |
| 163 | bool import_text_editor_theme(String p_file); |
| 164 | bool save_text_editor_theme(); |
| 165 | bool save_text_editor_theme_as(String p_file); |
| 166 | bool is_default_text_editor_theme(); |
| 167 | |
| 168 | Vector<String> get_script_templates(const String &p_extension, const String &p_custom_path = String()); |
| 169 | String get_editor_layouts_config() const; |
| 170 | float get_auto_display_scale() const; |
| 171 | |
| 172 | void _add_shortcut_default(const String &p_name, const Ref<Shortcut> &p_shortcut); |
| 173 | void add_shortcut(const String &p_name, const Ref<Shortcut> &p_shortcut); |
| 174 | bool is_shortcut(const String &p_name, const Ref<InputEvent> &p_event) const; |
| 175 | Ref<Shortcut> get_shortcut(const String &p_name) const; |
| 176 | void get_shortcut_list(List<String> *r_shortcuts); |
| 177 | |
| 178 | void set_builtin_action_override(const String &p_name, const TypedArray<InputEvent> &p_events); |
| 179 | const Array get_builtin_action_overrides(const String &p_name) const; |
| 180 | |
| 181 | void notify_changes(); |
| 182 | |
| 183 | EditorSettings(); |
| 184 | ~EditorSettings(); |
| 185 | }; |
| 186 | |
| 187 | //not a macro any longer |
| 188 | |
| 189 | #define EDITOR_DEF(m_var, m_val) _EDITOR_DEF(m_var, Variant(m_val)) |
| 190 | #define EDITOR_DEF_RST(m_var, m_val) _EDITOR_DEF(m_var, Variant(m_val), true) |
| 191 | Variant _EDITOR_DEF(const String &p_setting, const Variant &p_default, bool p_restart_if_changed = false); |
| 192 | |
| 193 | #define EDITOR_GET(m_var) _EDITOR_GET(m_var) |
| 194 | Variant _EDITOR_GET(const String &p_setting); |
| 195 | |
| 196 | #define ED_IS_SHORTCUT(p_name, p_ev) (EditorSettings::get_singleton()->is_shortcut(p_name, p_ev)) |
| 197 | Ref<Shortcut> ED_SHORTCUT(const String &p_path, const String &p_name, Key p_keycode = Key::NONE, bool p_physical = false); |
| 198 | Ref<Shortcut> ED_SHORTCUT_ARRAY(const String &p_path, const String &p_name, const PackedInt32Array &p_keycodes, bool p_physical = false); |
| 199 | void ED_SHORTCUT_OVERRIDE(const String &p_path, const String &p_feature, Key p_keycode = Key::NONE, bool p_physical = false); |
| 200 | void ED_SHORTCUT_OVERRIDE_ARRAY(const String &p_path, const String &p_feature, const PackedInt32Array &p_keycodes, bool p_physical = false); |
| 201 | Ref<Shortcut> ED_GET_SHORTCUT(const String &p_path); |
| 202 | |
| 203 | #endif // EDITOR_SETTINGS_H |
| 204 | |