summaryrefslogtreecommitdiff
path: root/Thronefall_v1.0/Rewired/Rewired.Demos/FallbackJoystickIdentificationDemo.cs
blob: 3213b71f1ee4364f245fe8b898526ef6ac88f53a (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
using System.Collections.Generic;
using UnityEngine;

namespace Rewired.Demos;

[AddComponentMenu("")]
public class FallbackJoystickIdentificationDemo : MonoBehaviour
{
	private const float windowWidth = 250f;

	private const float windowHeight = 250f;

	private const float inputDelay = 1f;

	private bool identifyRequired;

	private Queue<Joystick> joysticksToIdentify;

	private float nextInputAllowedTime;

	private GUIStyle style;

	private void Awake()
	{
		if (ReInput.unityJoystickIdentificationRequired)
		{
			ReInput.ControllerConnectedEvent += JoystickConnected;
			ReInput.ControllerDisconnectedEvent += JoystickDisconnected;
			IdentifyAllJoysticks();
		}
	}

	private void JoystickConnected(ControllerStatusChangedEventArgs args)
	{
		IdentifyAllJoysticks();
	}

	private void JoystickDisconnected(ControllerStatusChangedEventArgs args)
	{
		IdentifyAllJoysticks();
	}

	public void IdentifyAllJoysticks()
	{
		Reset();
		if (ReInput.controllers.joystickCount != 0)
		{
			Joystick[] joysticks = ReInput.controllers.GetJoysticks();
			if (joysticks != null)
			{
				identifyRequired = true;
				joysticksToIdentify = new Queue<Joystick>(joysticks);
				SetInputDelay();
			}
		}
	}

	private void SetInputDelay()
	{
		nextInputAllowedTime = Time.time + 1f;
	}

	private void OnGUI()
	{
		if (!identifyRequired)
		{
			return;
		}
		if (joysticksToIdentify == null || joysticksToIdentify.Count == 0)
		{
			Reset();
			return;
		}
		Rect screenRect = new Rect((float)Screen.width * 0.5f - 125f, (float)Screen.height * 0.5f - 125f, 250f, 250f);
		GUILayout.Window(0, screenRect, DrawDialogWindow, "Joystick Identification Required");
		GUI.FocusWindow(0);
		if (!(Time.time < nextInputAllowedTime) && ReInput.controllers.SetUnityJoystickIdFromAnyButtonOrAxisPress(joysticksToIdentify.Peek().id, 0.8f, positiveAxesOnly: false))
		{
			joysticksToIdentify.Dequeue();
			SetInputDelay();
			if (joysticksToIdentify.Count == 0)
			{
				Reset();
			}
		}
	}

	private void DrawDialogWindow(int windowId)
	{
		if (identifyRequired)
		{
			if (style == null)
			{
				style = new GUIStyle(GUI.skin.label);
				style.wordWrap = true;
			}
			GUILayout.Space(15f);
			GUILayout.Label("A joystick has been attached or removed. You will need to identify each joystick by pressing a button on the controller listed below:", style);
			Joystick joystick = joysticksToIdentify.Peek();
			GUILayout.Label("Press any button on \"" + joystick.name + "\" now.", style);
			GUILayout.FlexibleSpace();
			if (GUILayout.Button("Skip"))
			{
				joysticksToIdentify.Dequeue();
			}
		}
	}

	private void Reset()
	{
		joysticksToIdentify = null;
		identifyRequired = false;
	}
}