少年修仙传客户端代码仓库
hch
2025-03-19 6770e1eb64c4282def45adb824b14b2a407fdd30
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System.Collections.Generic;
using UnityEngine;
using System;
 
namespace CnControls
{
    /// <summary>
    /// Common input manager class
    /// Can be used instead of Input logic, as it replicates the standard behaviour but adds additional logic
    /// </summary>
    public class CnInputManager
    {
        private static CnInputManager _instance;
 
        public static CnInputManager Instance
        {
            get { return _instance ?? (_instance = new CnInputManager()); }
        }
 
        private CnInputManager()
        { }
 
        public event Action FingerTouchedEvent;
        public event Action FingerLeftedEvent;
        public event Action FingerMovedEvent;
 
        /// <summary>
        /// Dictionary of virtual axis
        /// Every axis can be mapped to a number of actual virtual axis, 
        /// as with standard Unity Input system where you can create different buttons for, say, "Horizontal" axis
        /// </summary>
        private Dictionary<string, List<VirtualAxis>> _virtualAxisDictionary =
            new Dictionary<string, List<VirtualAxis>>();
 
        public void HandleTouched()
        {
            if (FingerTouchedEvent != null)
            {
                FingerTouchedEvent();
            }
        }
 
        public void HandleTouchLefted()
        {
            if (FingerLeftedEvent != null)
            {
                FingerLeftedEvent();
            }
        }
 
        public void HandleTouchMoved()
        {
            if (FingerMovedEvent != null)
            {
                FingerMovedEvent();
            }
        }
 
        /// <summary>
        /// Additional logic for touch retreival
        /// It's possible to add some reflection-based emulated touches
        /// </summary>
        public static int TouchCount
        {
            get
            {
                return Input.touchCount;
            }
        }
 
        /// <summary>
        /// Additional logic for touch retreival
        /// It's possible to add some reflection-based emulated touches
        /// </summary>
        public static Touch GetTouch(int touchIndex)
        {
            return Input.GetTouch(touchIndex);
        }
 
        /// <summary>
        /// GetAxis method for getting current values for any desired axis
        /// </summary>
        /// <param name="axisName">The name of the axis to get value from</param>
        /// <returns>
        /// Current value of FIRST NON ZERO axis that are registered for that name
        /// ZERO if non if the virtual axis are being tweaked
        /// </returns>
        public static float GetAxis(string axisName)
        {
            return GetAxis(axisName, false);
        }
 
        /// <summary>
        /// "Copy" of the Input.GetAxisRaw method
        /// </summary>
        /// <param name="axisName">The name of the axis to get value from</param>
        /// <returns>
        /// Current value of FIRST NON ZERO axis that are registered for that name
        /// ZERO if non if the virtual axis are being tweaked
        /// </returns>
        public static float GetAxisRaw(string axisName)
        {
            return GetAxis(axisName, true);
        }
 
        /// <summary>
        /// Common private method for getting the axis values
        /// </summary>
        /// <param name="axisName">The name of the axis to get value from</param>
        /// <param name="isRaw">Whether the method sould return the raw value of the axis</param>
        /// <returns></returns>
        private static float GetAxis(string axisName, bool isRaw)
        {
            // If we have the axis registered as virtual, we call the retreival logic
            if (AxisExists(axisName))
            {
                return GetVirtualAxisValue(Instance._virtualAxisDictionary[axisName], axisName, isRaw);
            }
 
            // If we don't have the desired virtual axis registered, we just fallback to the default Unity Input behaviour
            return isRaw ? Input.GetAxisRaw(axisName) : Input.GetAxis(axisName);
        }
 
        /// <summary>
        /// Check whether the specified axis exists
        /// </summary>
        /// <param name="axisName">Name of the axis to check</param>
        /// <returns>Does this axis exist?</returns>
        public static bool AxisExists(string axisName)
        {
            return Instance._virtualAxisDictionary.ContainsKey(axisName);
        }
 
        /// <summary>
        /// Registers the provided virtual axis
        /// </summary>
        /// <param name="virtualAxis">Virtual axis to register</param>
        public static void RegisterVirtualAxis(VirtualAxis virtualAxis)
        {
            // If it's the first such virtual axis, create a new list for that axis name
            if (!Instance._virtualAxisDictionary.ContainsKey(virtualAxis.Name))
            {
                Instance._virtualAxisDictionary[virtualAxis.Name] = new List<VirtualAxis>();
            }
 
            Instance._virtualAxisDictionary[virtualAxis.Name].Add(virtualAxis);
        }
 
        /// <summary>
        /// Unregisters the provided virtual axis
        /// </summary>
        /// <param name="virtualAxis">Virtual axis to unregister</param>
        public static void UnregisterVirtualAxis(VirtualAxis virtualAxis)
        {
            // If it's the first such virtual axis, create a new list for that axis name
            if (Instance._virtualAxisDictionary.ContainsKey(virtualAxis.Name))
            {
                if (!Instance._virtualAxisDictionary[virtualAxis.Name].Remove(virtualAxis))
                {
                    DebugEx.LogError("Requested axis " + virtualAxis.Name + " exists, but there's no such virtual axis that you're trying to unregister");
                }
            }
            else
            {
                DebugEx.LogError("Trying to unregister an axis " + virtualAxis.Name + " that was never registered");
            }
        }
 
        /// <summary>
        /// Private method that get's the value of the first non-zero virtual axis, registered with the specified name
        /// </summary>
        /// <param name="virtualAxisList">List of virtual axis to search through</param>
        /// <param name="axisName">Name of the axis (for the standard Input behaviour)</param>
        /// <param name="isRaw">Whether the method should return the Raw value of the axis</param>
        /// <returns></returns>
        private static float GetVirtualAxisValue(List<VirtualAxis> virtualAxisList, string axisName, bool isRaw)
        {
            // The method is really straightforward here
            // First, we check the standard Input.GetAxis method
            // If it's not zero, we return the value
            // If it IS zero, we return first non-zero value of any of the passed virtual axis
            // Or zero if all of them are zero
 
            float axisValue = isRaw ? Input.GetAxisRaw(axisName) : Input.GetAxis(axisName);
            if (!Mathf.Approximately(axisValue, 0f))
            {
                return axisValue;
            }
 
            for (int i = 0; i < virtualAxisList.Count; i++)
            {
                var currentAxisValue = virtualAxisList[i].Value;
                if (!Mathf.Approximately(currentAxisValue, 0f))
                {
                    return currentAxisValue;
                }
            }
 
            return 0f;
        }
    }
}