summaryrefslogtreecommitdiff
path: root/Thronefall_v1.0/Rewired/Rewired.Demos/SimpleControlRemapping.cs
blob: 386ebe75c93dde7e2bf9fb0f91a7b06e21803182 (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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Rewired.Demos;

[AddComponentMenu("")]
public class SimpleControlRemapping : MonoBehaviour
{
	private class Row
	{
		public InputAction action;

		public AxisRange actionRange;

		public Button button;

		public Text text;
	}

	private const string category = "Default";

	private const string layout = "Default";

	private const string uiCategory = "UI";

	private InputMapper inputMapper = new InputMapper();

	public GameObject buttonPrefab;

	public GameObject textPrefab;

	public RectTransform fieldGroupTransform;

	public RectTransform actionGroupTransform;

	public Text controllerNameUIText;

	public Text statusUIText;

	private ControllerType selectedControllerType;

	private int selectedControllerId;

	private List<Row> rows = new List<Row>();

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

	private ControllerMap controllerMap
	{
		get
		{
			if (controller == null)
			{
				return null;
			}
			return player.controllers.maps.GetMap(controller.type, controller.id, "Default", "Default");
		}
	}

	private Controller controller => player.controllers.GetController(selectedControllerType, selectedControllerId);

	private void OnEnable()
	{
		if (ReInput.isReady)
		{
			inputMapper.options.timeout = 5f;
			inputMapper.options.ignoreMouseXAxis = true;
			inputMapper.options.ignoreMouseYAxis = true;
			ReInput.ControllerConnectedEvent += OnControllerChanged;
			ReInput.ControllerDisconnectedEvent += OnControllerChanged;
			inputMapper.InputMappedEvent += OnInputMapped;
			inputMapper.StoppedEvent += OnStopped;
			InitializeUI();
		}
	}

	private void OnDisable()
	{
		inputMapper.Stop();
		inputMapper.RemoveAllEventListeners();
		ReInput.ControllerConnectedEvent -= OnControllerChanged;
		ReInput.ControllerDisconnectedEvent -= OnControllerChanged;
	}

	private void RedrawUI()
	{
		if (controller == null)
		{
			ClearUI();
			return;
		}
		controllerNameUIText.text = controller.name;
		for (int i = 0; i < rows.Count; i++)
		{
			Row row = rows[i];
			InputAction action = rows[i].action;
			string text = string.Empty;
			int actionElementMapId = -1;
			foreach (ActionElementMap item in controllerMap.ElementMapsWithAction(action.id))
			{
				if (item.ShowInField(row.actionRange))
				{
					text = item.elementIdentifierName;
					actionElementMapId = item.id;
					break;
				}
			}
			row.text.text = text;
			row.button.onClick.RemoveAllListeners();
			int index = i;
			row.button.onClick.AddListener(delegate
			{
				OnInputFieldClicked(index, actionElementMapId);
			});
		}
	}

	private void ClearUI()
	{
		if (selectedControllerType == ControllerType.Joystick)
		{
			controllerNameUIText.text = "No joysticks attached";
		}
		else
		{
			controllerNameUIText.text = string.Empty;
		}
		for (int i = 0; i < rows.Count; i++)
		{
			rows[i].text.text = string.Empty;
		}
	}

	private void InitializeUI()
	{
		foreach (Transform item in actionGroupTransform)
		{
			Object.Destroy(item.gameObject);
		}
		foreach (Transform item2 in fieldGroupTransform)
		{
			Object.Destroy(item2.gameObject);
		}
		foreach (InputAction item3 in ReInput.mapping.ActionsInCategory("Default"))
		{
			if (item3.type == InputActionType.Axis)
			{
				CreateUIRow(item3, AxisRange.Full, item3.descriptiveName);
				CreateUIRow(item3, AxisRange.Positive, (!string.IsNullOrEmpty(item3.positiveDescriptiveName)) ? item3.positiveDescriptiveName : (item3.descriptiveName + " +"));
				CreateUIRow(item3, AxisRange.Negative, (!string.IsNullOrEmpty(item3.negativeDescriptiveName)) ? item3.negativeDescriptiveName : (item3.descriptiveName + " -"));
			}
			else if (item3.type == InputActionType.Button)
			{
				CreateUIRow(item3, AxisRange.Positive, item3.descriptiveName);
			}
		}
		RedrawUI();
	}

	private void CreateUIRow(InputAction action, AxisRange actionRange, string label)
	{
		GameObject obj = Object.Instantiate(textPrefab);
		obj.transform.SetParent(actionGroupTransform);
		obj.transform.SetAsLastSibling();
		obj.GetComponent<Text>().text = label;
		GameObject gameObject = Object.Instantiate(buttonPrefab);
		gameObject.transform.SetParent(fieldGroupTransform);
		gameObject.transform.SetAsLastSibling();
		rows.Add(new Row
		{
			action = action,
			actionRange = actionRange,
			button = gameObject.GetComponent<Button>(),
			text = gameObject.GetComponentInChildren<Text>()
		});
	}

	private void SetSelectedController(ControllerType controllerType)
	{
		bool flag = false;
		if (controllerType != selectedControllerType)
		{
			selectedControllerType = controllerType;
			flag = true;
		}
		int num = selectedControllerId;
		if (selectedControllerType == ControllerType.Joystick)
		{
			if (player.controllers.joystickCount > 0)
			{
				selectedControllerId = player.controllers.Joysticks[0].id;
			}
			else
			{
				selectedControllerId = -1;
			}
		}
		else
		{
			selectedControllerId = 0;
		}
		if (selectedControllerId != num)
		{
			flag = true;
		}
		if (flag)
		{
			inputMapper.Stop();
			RedrawUI();
		}
	}

	public void OnControllerSelected(int controllerType)
	{
		SetSelectedController((ControllerType)controllerType);
	}

	private void OnInputFieldClicked(int index, int actionElementMapToReplaceId)
	{
		if (index >= 0 && index < rows.Count && controller != null)
		{
			StartCoroutine(StartListeningDelayed(index, actionElementMapToReplaceId));
		}
	}

	private IEnumerator StartListeningDelayed(int index, int actionElementMapToReplaceId)
	{
		yield return new WaitForSeconds(0.1f);
		inputMapper.Start(new InputMapper.Context
		{
			actionId = rows[index].action.id,
			controllerMap = controllerMap,
			actionRange = rows[index].actionRange,
			actionElementMapToReplace = controllerMap.GetElementMap(actionElementMapToReplaceId)
		});
		player.controllers.maps.SetMapsEnabled(state: false, "UI");
		statusUIText.text = "Listening...";
	}

	private void OnControllerChanged(ControllerStatusChangedEventArgs args)
	{
		SetSelectedController(selectedControllerType);
	}

	private void OnInputMapped(InputMapper.InputMappedEventData data)
	{
		RedrawUI();
	}

	private void OnStopped(InputMapper.StoppedEventData data)
	{
		statusUIText.text = string.Empty;
		player.controllers.maps.SetMapsEnabled(state: true, "UI");
	}
}