Tanoda
GamePadInputModule.cs
Go to the documentation of this file.
1
3
5{
6 [AddComponentMenu("Event/Extensions/GamePad Input Module")]
7 public class GamePadInputModule : BaseInputModule
8 {
9 private float m_PrevActionTime;
10 Vector2 m_LastMoveVector;
11 int m_ConsecutiveMoveCount = 0;
12
14 {}
15
16 [SerializeField]
17 private string m_HorizontalAxis = "Horizontal";
18
22 [SerializeField]
23 private string m_VerticalAxis = "Vertical";
24
28 [SerializeField]
29 private string m_SubmitButton = "Submit";
30
34 [SerializeField]
35 private string m_CancelButton = "Cancel";
36
37 [SerializeField]
38 private float m_InputActionsPerSecond = 10;
39
40 [SerializeField]
41 private float m_RepeatDelay = 0.1f;
42
44 {
45 get { return m_InputActionsPerSecond; }
46 set { m_InputActionsPerSecond = value; }
47 }
48
49 public float repeatDelay
50 {
51 get { return m_RepeatDelay; }
52 set { m_RepeatDelay = value; }
53 }
54
58 public string horizontalAxis
59 {
60 get { return m_HorizontalAxis; }
61 set { m_HorizontalAxis = value; }
62 }
63
67 public string verticalAxis
68 {
69 get { return m_VerticalAxis; }
70 set { m_VerticalAxis = value; }
71 }
72
73 public string submitButton
74 {
75 get { return m_SubmitButton; }
76 set { m_SubmitButton = value; }
77 }
78
79 public string cancelButton
80 {
81 get { return m_CancelButton; }
82 set { m_CancelButton = value; }
83 }
84
85 public override bool ShouldActivateModule()
86 {
87 if (!base.ShouldActivateModule())
88 return false;
89
90 var shouldActivate = true;
91 shouldActivate |= Input.GetButtonDown(m_SubmitButton);
92 shouldActivate |= Input.GetButtonDown(m_CancelButton);
93 shouldActivate |= !Mathf.Approximately(Input.GetAxisRaw(m_HorizontalAxis), 0.0f);
94 shouldActivate |= !Mathf.Approximately(Input.GetAxisRaw(m_VerticalAxis), 0.0f);
95 return shouldActivate;
96 }
97
98 public override void ActivateModule()
99 {
100 StandaloneInputModule StandAloneSystem = GetComponent<StandaloneInputModule>();
101
102 if (StandAloneSystem && StandAloneSystem.enabled)
103 {
104 Debug.LogError("StandAloneInputSystem should not be used with the GamePadInputModule, " +
105 "please remove it from the Event System in this scene or disable it when this module is in use");
106 }
107
108 base.ActivateModule();
109
110 var toSelect = eventSystem.currentSelectedGameObject;
111 if (toSelect == null)
112 toSelect = eventSystem.firstSelectedGameObject;
113
114 eventSystem.SetSelectedGameObject(toSelect, GetBaseEventData());
115 }
116
117 public override void DeactivateModule()
118 {
119 base.DeactivateModule();
120 }
121
122 public override void Process()
123 {
124 bool usedEvent = SendUpdateEventToSelectedObject();
125
126 if (eventSystem.sendNavigationEvents)
127 {
128 if (!usedEvent)
129 usedEvent |= SendMoveEventToSelectedObject();
130
131 if (!usedEvent)
133 }
134 }
135
140 {
141 if (eventSystem.currentSelectedGameObject == null)
142 return false;
143
144 var data = GetBaseEventData();
145 if (Input.GetButtonDown(m_SubmitButton))
146 ExecuteEvents.Execute(eventSystem.currentSelectedGameObject, data, ExecuteEvents.submitHandler);
147
148 if (Input.GetButtonDown(m_CancelButton))
149 ExecuteEvents.Execute(eventSystem.currentSelectedGameObject, data, ExecuteEvents.cancelHandler);
150 return data.used;
151 }
152
153 private Vector2 GetRawMoveVector()
154 {
155 Vector2 move = Vector2.zero;
156 move.x = Input.GetAxisRaw(m_HorizontalAxis);
157 move.y = Input.GetAxisRaw(m_VerticalAxis);
158
159 if (Input.GetButtonDown(m_HorizontalAxis))
160 {
161 if (move.x < 0)
162 move.x = -1f;
163 if (move.x > 0)
164 move.x = 1f;
165 }
166 if (Input.GetButtonDown(m_VerticalAxis))
167 {
168 if (move.y < 0)
169 move.y = -1f;
170 if (move.y > 0)
171 move.y = 1f;
172 }
173 return move;
174 }
175
180 {
181 float time = Time.unscaledTime;
182
183 Vector2 movement = GetRawMoveVector();
184 if (Mathf.Approximately(movement.x, 0f) && Mathf.Approximately(movement.y, 0f))
185 {
186 m_ConsecutiveMoveCount = 0;
187 return false;
188 }
189
190 // If user pressed key again, always allow event
191 bool allow = Input.GetButtonDown(m_HorizontalAxis) || Input.GetButtonDown(m_VerticalAxis);
192 bool similarDir = (Vector2.Dot(movement, m_LastMoveVector) > 0);
193 if (!allow)
194 {
195 // Otherwise, user held down key or axis.
196 // If direction didn't change at least 90 degrees, wait for delay before allowing consecutive event.
197 if (similarDir && m_ConsecutiveMoveCount == 1)
198 allow = (time > m_PrevActionTime + m_RepeatDelay);
199 // If direction changed at least 90 degree, or we already had the delay, repeat at repeat rate.
200 else
201 allow = (time > m_PrevActionTime + 1f / m_InputActionsPerSecond);
202 }
203 if (!allow)
204 return false;
205
206 var axisEventData = GetAxisEventData(movement.x, movement.y, 0.6f);
207 ExecuteEvents.Execute(eventSystem.currentSelectedGameObject, axisEventData, ExecuteEvents.moveHandler);
208 if (!similarDir)
209 m_ConsecutiveMoveCount = 0;
210 m_ConsecutiveMoveCount++;
211 m_PrevActionTime = time;
212 m_LastMoveVector = movement;
213 return axisEventData.used;
214 }
215
217 {
218 if (eventSystem.currentSelectedGameObject == null)
219 return false;
220
221 var data = GetBaseEventData();
222 ExecuteEvents.Execute(eventSystem.currentSelectedGameObject, data, ExecuteEvents.updateSelectedHandler);
223 return data.used;
224 }
225 }
226}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
string verticalAxis
Name of the vertical axis for movement (if axis events are used).
bool SendSubmitEventToSelectedObject()
Process submit keys.
string horizontalAxis
Name of the horizontal axis for movement (if axis events are used).