summaryrefslogtreecommitdiff
path: root/Thronefall_v1.0/Rewired/Rewired.Demos.GamepadTemplateUI/GamepadTemplateUI.cs
blob: e56617ca6dc5691934a01d417d5d00514e7283d3 (plain)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
using System.Collections.Generic;
using UnityEngine;

namespace Rewired.Demos.GamepadTemplateUI;

public class GamepadTemplateUI : MonoBehaviour
{
	private class Stick
	{
		private RectTransform _transform;

		private Vector2 _origPosition;

		private int _xAxisElementId = -1;

		private int _yAxisElementId = -1;

		public Vector2 position
		{
			get
			{
				if (!(_transform != null))
				{
					return Vector2.zero;
				}
				return _transform.anchoredPosition - _origPosition;
			}
			set
			{
				if (!(_transform == null))
				{
					_transform.anchoredPosition = _origPosition + value;
				}
			}
		}

		public Stick(RectTransform transform, int xAxisElementId, int yAxisElementId)
		{
			if (!(transform == null))
			{
				_transform = transform;
				_origPosition = _transform.anchoredPosition;
				_xAxisElementId = xAxisElementId;
				_yAxisElementId = yAxisElementId;
			}
		}

		public void Reset()
		{
			if (!(_transform == null))
			{
				_transform.anchoredPosition = _origPosition;
			}
		}

		public bool ContainsElement(int elementId)
		{
			if (_transform == null)
			{
				return false;
			}
			if (elementId != _xAxisElementId)
			{
				return elementId == _yAxisElementId;
			}
			return true;
		}

		public void SetAxisPosition(int elementId, float value)
		{
			if (!(_transform == null))
			{
				Vector2 vector = position;
				if (elementId == _xAxisElementId)
				{
					vector.x = value;
				}
				else if (elementId == _yAxisElementId)
				{
					vector.y = value;
				}
				position = vector;
			}
		}
	}

	private class UIElement
	{
		public int id;

		public ControllerUIElement element;

		public UIElement(int id, ControllerUIElement element)
		{
			this.id = id;
			this.element = element;
		}
	}

	private const float stickRadius = 20f;

	public int playerId;

	[SerializeField]
	private RectTransform leftStick;

	[SerializeField]
	private RectTransform rightStick;

	[SerializeField]
	private ControllerUIElement leftStickX;

	[SerializeField]
	private ControllerUIElement leftStickY;

	[SerializeField]
	private ControllerUIElement leftStickButton;

	[SerializeField]
	private ControllerUIElement rightStickX;

	[SerializeField]
	private ControllerUIElement rightStickY;

	[SerializeField]
	private ControllerUIElement rightStickButton;

	[SerializeField]
	private ControllerUIElement actionBottomRow1;

	[SerializeField]
	private ControllerUIElement actionBottomRow2;

	[SerializeField]
	private ControllerUIElement actionBottomRow3;

	[SerializeField]
	private ControllerUIElement actionTopRow1;

	[SerializeField]
	private ControllerUIElement actionTopRow2;

	[SerializeField]
	private ControllerUIElement actionTopRow3;

	[SerializeField]
	private ControllerUIElement leftShoulder;

	[SerializeField]
	private ControllerUIElement leftTrigger;

	[SerializeField]
	private ControllerUIElement rightShoulder;

	[SerializeField]
	private ControllerUIElement rightTrigger;

	[SerializeField]
	private ControllerUIElement center1;

	[SerializeField]
	private ControllerUIElement center2;

	[SerializeField]
	private ControllerUIElement center3;

	[SerializeField]
	private ControllerUIElement dPadUp;

	[SerializeField]
	private ControllerUIElement dPadRight;

	[SerializeField]
	private ControllerUIElement dPadDown;

	[SerializeField]
	private ControllerUIElement dPadLeft;

	private UIElement[] _uiElementsArray;

	private Dictionary<int, ControllerUIElement> _uiElements = new Dictionary<int, ControllerUIElement>();

	private IList<ControllerTemplateElementTarget> _tempTargetList = new List<ControllerTemplateElementTarget>(2);

	private Stick[] _sticks;

	private Player player => ReInput.players.GetPlayer(playerId);

	private void Awake()
	{
		_uiElementsArray = new UIElement[23]
		{
			new UIElement(0, leftStickX),
			new UIElement(1, leftStickY),
			new UIElement(17, leftStickButton),
			new UIElement(2, rightStickX),
			new UIElement(3, rightStickY),
			new UIElement(18, rightStickButton),
			new UIElement(4, actionBottomRow1),
			new UIElement(5, actionBottomRow2),
			new UIElement(6, actionBottomRow3),
			new UIElement(7, actionTopRow1),
			new UIElement(8, actionTopRow2),
			new UIElement(9, actionTopRow3),
			new UIElement(14, center1),
			new UIElement(15, center2),
			new UIElement(16, center3),
			new UIElement(19, dPadUp),
			new UIElement(20, dPadRight),
			new UIElement(21, dPadDown),
			new UIElement(22, dPadLeft),
			new UIElement(10, leftShoulder),
			new UIElement(11, leftTrigger),
			new UIElement(12, rightShoulder),
			new UIElement(13, rightTrigger)
		};
		for (int i = 0; i < _uiElementsArray.Length; i++)
		{
			_uiElements.Add(_uiElementsArray[i].id, _uiElementsArray[i].element);
		}
		_sticks = new Stick[2]
		{
			new Stick(leftStick, 0, 1),
			new Stick(rightStick, 2, 3)
		};
		ReInput.ControllerConnectedEvent += OnControllerConnected;
		ReInput.ControllerDisconnectedEvent += OnControllerDisconnected;
	}

	private void Start()
	{
		if (ReInput.isReady)
		{
			DrawLabels();
		}
	}

	private void OnDestroy()
	{
		ReInput.ControllerConnectedEvent -= OnControllerConnected;
		ReInput.ControllerDisconnectedEvent -= OnControllerDisconnected;
	}

	private void Update()
	{
		if (ReInput.isReady)
		{
			DrawActiveElements();
		}
	}

	private void DrawActiveElements()
	{
		for (int i = 0; i < _uiElementsArray.Length; i++)
		{
			_uiElementsArray[i].element.Deactivate();
		}
		for (int j = 0; j < _sticks.Length; j++)
		{
			_sticks[j].Reset();
		}
		IList<InputAction> actions = ReInput.mapping.Actions;
		for (int k = 0; k < actions.Count; k++)
		{
			ActivateElements(player, actions[k].id);
		}
	}

	private void ActivateElements(Player player, int actionId)
	{
		float axis = player.GetAxis(actionId);
		if (axis == 0f)
		{
			return;
		}
		IList<InputActionSourceData> currentInputSources = player.GetCurrentInputSources(actionId);
		for (int i = 0; i < currentInputSources.Count; i++)
		{
			InputActionSourceData inputActionSourceData = currentInputSources[i];
			IGamepadTemplate template = inputActionSourceData.controller.GetTemplate<IGamepadTemplate>();
			if (template == null)
			{
				continue;
			}
			template.GetElementTargets(inputActionSourceData.actionElementMap, _tempTargetList);
			for (int j = 0; j < _tempTargetList.Count; j++)
			{
				ControllerTemplateElementTarget controllerTemplateElementTarget = _tempTargetList[j];
				int id = controllerTemplateElementTarget.element.id;
				ControllerUIElement controllerUIElement = _uiElements[id];
				if (controllerTemplateElementTarget.elementType == ControllerTemplateElementType.Axis)
				{
					controllerUIElement.Activate(axis);
				}
				else if (controllerTemplateElementTarget.elementType == ControllerTemplateElementType.Button && (player.GetButton(actionId) || player.GetNegativeButton(actionId)))
				{
					controllerUIElement.Activate(1f);
				}
				GetStick(id)?.SetAxisPosition(id, axis * 20f);
			}
		}
	}

	private void DrawLabels()
	{
		for (int i = 0; i < _uiElementsArray.Length; i++)
		{
			_uiElementsArray[i].element.ClearLabels();
		}
		IList<InputAction> actions = ReInput.mapping.Actions;
		for (int j = 0; j < actions.Count; j++)
		{
			DrawLabels(player, actions[j]);
		}
	}

	private void DrawLabels(Player player, InputAction action)
	{
		Controller firstControllerWithTemplate = player.controllers.GetFirstControllerWithTemplate<IGamepadTemplate>();
		if (firstControllerWithTemplate == null)
		{
			return;
		}
		IGamepadTemplate template = firstControllerWithTemplate.GetTemplate<IGamepadTemplate>();
		ControllerMap map = player.controllers.maps.GetMap(firstControllerWithTemplate, "Default", "Default");
		if (map != null)
		{
			for (int i = 0; i < _uiElementsArray.Length; i++)
			{
				ControllerUIElement element = _uiElementsArray[i].element;
				int id = _uiElementsArray[i].id;
				IControllerTemplateElement element2 = template.GetElement(id);
				DrawLabel(element, action, map, template, element2);
			}
		}
	}

	private void DrawLabel(ControllerUIElement uiElement, InputAction action, ControllerMap controllerMap, IControllerTemplate template, IControllerTemplateElement element)
	{
		if (element.source == null)
		{
			return;
		}
		if (element.source.type == ControllerTemplateElementSourceType.Axis)
		{
			IControllerTemplateAxisSource controllerTemplateAxisSource = element.source as IControllerTemplateAxisSource;
			ActionElementMap firstElementMapWithElementTarget;
			if (controllerTemplateAxisSource.splitAxis)
			{
				firstElementMapWithElementTarget = controllerMap.GetFirstElementMapWithElementTarget(controllerTemplateAxisSource.positiveTarget, action.id, skipDisabledMaps: true);
				if (firstElementMapWithElementTarget != null)
				{
					uiElement.SetLabel(firstElementMapWithElementTarget.actionDescriptiveName, AxisRange.Positive);
				}
				firstElementMapWithElementTarget = controllerMap.GetFirstElementMapWithElementTarget(controllerTemplateAxisSource.negativeTarget, action.id, skipDisabledMaps: true);
				if (firstElementMapWithElementTarget != null)
				{
					uiElement.SetLabel(firstElementMapWithElementTarget.actionDescriptiveName, AxisRange.Negative);
				}
				return;
			}
			firstElementMapWithElementTarget = controllerMap.GetFirstElementMapWithElementTarget(controllerTemplateAxisSource.fullTarget, action.id, skipDisabledMaps: true);
			if (firstElementMapWithElementTarget != null)
			{
				uiElement.SetLabel(firstElementMapWithElementTarget.actionDescriptiveName, AxisRange.Full);
				return;
			}
			firstElementMapWithElementTarget = controllerMap.GetFirstElementMapWithElementTarget(new ControllerElementTarget(controllerTemplateAxisSource.fullTarget)
			{
				axisRange = AxisRange.Positive
			}, action.id, skipDisabledMaps: true);
			if (firstElementMapWithElementTarget != null)
			{
				uiElement.SetLabel(firstElementMapWithElementTarget.actionDescriptiveName, AxisRange.Positive);
			}
			firstElementMapWithElementTarget = controllerMap.GetFirstElementMapWithElementTarget(new ControllerElementTarget(controllerTemplateAxisSource.fullTarget)
			{
				axisRange = AxisRange.Negative
			}, action.id, skipDisabledMaps: true);
			if (firstElementMapWithElementTarget != null)
			{
				uiElement.SetLabel(firstElementMapWithElementTarget.actionDescriptiveName, AxisRange.Negative);
			}
		}
		else if (element.source.type == ControllerTemplateElementSourceType.Button)
		{
			IControllerTemplateButtonSource controllerTemplateButtonSource = element.source as IControllerTemplateButtonSource;
			ActionElementMap firstElementMapWithElementTarget = controllerMap.GetFirstElementMapWithElementTarget(controllerTemplateButtonSource.target, action.id, skipDisabledMaps: true);
			if (firstElementMapWithElementTarget != null)
			{
				uiElement.SetLabel(firstElementMapWithElementTarget.actionDescriptiveName, AxisRange.Full);
			}
		}
	}

	private Stick GetStick(int elementId)
	{
		for (int i = 0; i < _sticks.Length; i++)
		{
			if (_sticks[i].ContainsElement(elementId))
			{
				return _sticks[i];
			}
		}
		return null;
	}

	private void OnControllerConnected(ControllerStatusChangedEventArgs args)
	{
		DrawLabels();
	}

	private void OnControllerDisconnected(ControllerStatusChangedEventArgs args)
	{
		DrawLabels();
	}
}