summaryrefslogtreecommitdiff
path: root/Thronefall_v1.0/Rewired/Rewired.Demos/CustomControllerDemo.cs
blob: 2c67e86a059118199635bbb4052900b6c33c0383 (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
using System;
using UnityEngine;

namespace Rewired.Demos;

[AddComponentMenu("")]
public class CustomControllerDemo : MonoBehaviour
{
	public int playerId;

	public string controllerTag;

	public bool useUpdateCallbacks;

	private int buttonCount;

	private int axisCount;

	private float[] axisValues;

	private bool[] buttonValues;

	private TouchJoystickExample[] joysticks;

	private TouchButtonExample[] buttons;

	private CustomController controller;

	[NonSerialized]
	private bool initialized;

	private void Awake()
	{
		ScreenOrientation screenOrientation = ScreenOrientation.LandscapeLeft;
		if (SystemInfo.deviceType == DeviceType.Handheld && Screen.orientation != screenOrientation)
		{
			Screen.orientation = screenOrientation;
		}
		Initialize();
	}

	private void Initialize()
	{
		ReInput.InputSourceUpdateEvent += OnInputSourceUpdate;
		joysticks = GetComponentsInChildren<TouchJoystickExample>();
		buttons = GetComponentsInChildren<TouchButtonExample>();
		axisCount = joysticks.Length * 2;
		buttonCount = buttons.Length;
		axisValues = new float[axisCount];
		buttonValues = new bool[buttonCount];
		Player player = ReInput.players.GetPlayer(playerId);
		controller = player.controllers.GetControllerWithTag<CustomController>(controllerTag);
		if (controller == null)
		{
			Debug.LogError("A matching controller was not found for tag \"" + controllerTag + "\"");
		}
		if (controller.buttonCount != buttonValues.Length || controller.axisCount != axisValues.Length)
		{
			Debug.LogError("Controller has wrong number of elements!");
		}
		if (useUpdateCallbacks && controller != null)
		{
			controller.SetAxisUpdateCallback(GetAxisValueCallback);
			controller.SetButtonUpdateCallback(GetButtonValueCallback);
		}
		initialized = true;
	}

	private void Update()
	{
		if (ReInput.isReady && !initialized)
		{
			Initialize();
		}
	}

	private void OnInputSourceUpdate()
	{
		GetSourceAxisValues();
		GetSourceButtonValues();
		if (!useUpdateCallbacks)
		{
			SetControllerAxisValues();
			SetControllerButtonValues();
		}
	}

	private void GetSourceAxisValues()
	{
		for (int i = 0; i < axisValues.Length; i++)
		{
			if (i % 2 != 0)
			{
				axisValues[i] = joysticks[i / 2].position.y;
			}
			else
			{
				axisValues[i] = joysticks[i / 2].position.x;
			}
		}
	}

	private void GetSourceButtonValues()
	{
		for (int i = 0; i < buttonValues.Length; i++)
		{
			buttonValues[i] = buttons[i].isPressed;
		}
	}

	private void SetControllerAxisValues()
	{
		for (int i = 0; i < axisValues.Length; i++)
		{
			controller.SetAxisValue(i, axisValues[i]);
		}
	}

	private void SetControllerButtonValues()
	{
		for (int i = 0; i < buttonValues.Length; i++)
		{
			controller.SetButtonValue(i, buttonValues[i]);
		}
	}

	private float GetAxisValueCallback(int index)
	{
		if (index >= axisValues.Length)
		{
			return 0f;
		}
		return axisValues[index];
	}

	private bool GetButtonValueCallback(int index)
	{
		if (index >= buttonValues.Length)
		{
			return false;
		}
		return buttonValues[index];
	}
}