File size: 3,760 Bytes
18a519f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#if UNITY_EDITOR
using System;
using System.IO;

////TODO: event diagnostics should have a bool here

namespace UnityEngine.InputSystem.Editor
{
    /// <summary>
    /// Settings that are local to the current user and specific to the input system's use in the editor.
    /// </summary>
    /// <remarks>
    /// These settings are not stored in assets in the project along with the other input settings.
    /// Instead, they are serialized to JSON and stored in the project's Library/ folder.
    /// </remarks>
    internal static class InputEditorUserSettings
    {
        /// <summary>
        /// Where the settings are stored in the user's project.
        /// </summary>
        private const string kSavePath = "Library/InputUserSettings.json";

        public static bool simulateTouch
        {
            get => s_Settings.simulateTouch;
            set
            {
                if (s_Settings.simulateTouch == value)
                    return;
                s_Settings.simulateTouch = value;
                OnChange();
            }
        }

        /// <summary>
        /// If this is true, then if <see cref="InputSettings.supportedDevices"/> is not empty, do
        /// not use it to prevent native devices
        /// </summary>
        /// <remarks>
        /// This switch is useful to preserve use of devices in the editor regardless of whether they the
        /// kind of devices used by the game at runtime. For example, a game may support only gamepads but
        /// in the editor, the keyboard, mouse, and tablet should still be usable.
        ///
        /// This switch is enabled by default.
        /// </remarks>
        public static bool addDevicesNotSupportedByProject
        {
            get => s_Settings.addDevicesNotSupportedByProject;
            set
            {
                if (s_Settings.addDevicesNotSupportedByProject == value)
                    return;
                s_Settings.addDevicesNotSupportedByProject = value;
                OnChange();
            }
        }

        /// <summary>
        /// If this is true, then instead of having an explicit save button in the .inputactions asset editor,
        /// any changes will be saved automatically and immediately.
        /// </summary>
        /// <remarks>
        /// Disabled by default.
        /// </remarks>
        public static bool autoSaveInputActionAssets
        {
            get => s_Settings.autoSaveInputActionAssets;
            set
            {
                if (s_Settings.autoSaveInputActionAssets == value)
                    return;
                s_Settings.autoSaveInputActionAssets = value;
                OnChange();
            }
        }

        [Serializable]
        internal struct SerializedState
        {
            public bool addDevicesNotSupportedByProject;
            public bool autoSaveInputActionAssets;
            public bool simulateTouch;
        }

        internal static SerializedState s_Settings;

        private static void OnChange()
        {
            Save();
            InputSystem.s_Manager.ApplySettings();
        }

        internal static void Load()
        {
            s_Settings = new SerializedState();
            if (!File.Exists(kSavePath))
                return;

            try
            {
                var json = File.ReadAllText(kSavePath);
                s_Settings = JsonUtility.FromJson<SerializedState>(json);
            }
            catch
            {
                s_Settings = new SerializedState();
            }
        }

        internal static void Save()
        {
            var json = JsonUtility.ToJson(s_Settings, prettyPrint: true);
            File.WriteAllText(kSavePath, json);
        }
    }
}
#endif