-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathControllerWizard.cs
206 lines (179 loc) · 7.49 KB
/
ControllerWizard.cs
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
using System;
using SharpDX.DirectInput;
using GTA;
using System.Collections.Generic;
namespace Benjamin94.Input
{
/// <summary>
/// This class is useful for letting the user configure their controller
/// </summary>
class ControllerWizard
{
private readonly DeviceButton[] dpads = new DeviceButton[] { DeviceButton.DPadUp, DeviceButton.DPadDown, DeviceButton.DPadLeft, DeviceButton.DPadRight };
private Joystick stick;
public ControllerWizard(Joystick stick)
{
this.stick = stick;
}
/// <summary>
/// Starts a wizard in which the user gets asked for all buttons one by one
/// </summary>
/// <param name="iniFile">The target INI file</param>
/// <returns></returns>
public bool StartConfiguration(string iniFile)
{
DirectInputManager input = new DirectInputManager(stick);
ScriptSettings data = ScriptSettings.Load(iniFile);
string guid = stick.Information.ProductGuid.ToString();
DpadType dpadType = DetermineDpadType(input);
if (dpadType == (DpadType)3)
{
return false;
}
if (dpadType == DpadType.Unknown)
{
UI.Notify("Unknown Dpad type, controller configuration stopped.");
return false;
}
data.SetValue(guid, DirectInputManager.DpadTypeKey, dpadType.ToString());
while (input.GetDpadValue() != -1)
{
UI.ShowSubtitle("Please let go the Dpad button.");
Script.Wait(100);
}
Script.Wait(1000);
UI.ShowSubtitle("Determined Dpad type: " + dpadType, 2500);
Script.Wait(2500);
foreach (DeviceButton btn in Enum.GetValues(typeof(DeviceButton)))
{
if (Array.Exists(dpads, item => { return item == btn ; }) && dpadType == DpadType.DigitalDpad)
{
bool result = ConfigureDigitalDpadButton(btn, data, input, guid);
if (!result)
{
return false;
}
}
else
{
bool result = Configure(btn, data, input, guid);
if (!result)
{
return false;
}
}
UI.Notify(GetBtnText(btn) + " button configured.");
}
data.Save();
return true;
}
/// <summary>
/// Small wizard to determine what kind of Dpad the controller belonging to the InputManager has
/// </summary>
/// <param name="input">InputManager which to determine the Dpad of</param>
/// <returns>DpadType enum</returns>
private DpadType DetermineDpadType(DirectInputManager input)
{
while (input.GetPressedButton() == -1 && input.GetDpadValue() == -1)
{
if (Game.IsKeyPressed(System.Windows.Forms.Keys.Escape)) return (DpadType)3;
UI.ShowSubtitle("Press and hold at least one Dpad button for 1 second. Press the Esc key to cancel.", 120);
Script.Wait(100);
}
UI.ShowSubtitle("Now keep holding that Dpad button.");
Script.Wait(1000);
int button = input.GetPressedButton();
int digitalDpadvalue = input.GetDpadValue();
if (digitalDpadvalue != -1)
{
return DpadType.DigitalDpad;
}
else if (button != -1)
{
return DpadType.ButtonsDpad;
}
return DpadType.Unknown;
}
/// <summary>
/// Helper method to configure a single DeviceButton and add it the configuration
/// </summary>
/// <param name="btn">The target DeviceButton</param>
/// <param name="data">The ScriptSettings object which it needs to be saved too</param>
/// <param name="input">InputManager object to handle input</param>
/// <param name="guid">The GUID of the controller</param>
private bool Configure(DeviceButton btn, ScriptSettings data, DirectInputManager input, string guid)
{
while (input.GetPressedButton() == -1)
{
if (Game.IsKeyPressed(System.Windows.Forms.Keys.Escape)) return false;
UI.ShowSubtitle("Press and hold the " + GetBtnText(btn) + " button on the controller for 1 second. Press the Esc key to cancel.", 120);
Script.Wait(100);
}
int button = input.GetPressedButton();
UI.ShowSubtitle("Please hold the " + GetBtnText(btn) + " button to confirm it.");
Script.Wait(1000);
if (button != input.GetPressedButton())
{
UI.ShowSubtitle("Now hold the " + GetBtnText(btn) + " button to confirm.");
Script.Wait(1000);
Configure(btn, data, input, guid);
}
else
{
data.SetValue(guid, btn.ToString(), button);
while (input.GetPressedButton() != -1)
{
UI.ShowSubtitle("Now let go the button to configure the next one.");
Script.Wait(100);
}
Script.Wait(1000);
}
return true;
}
/// <summary>
/// Helper method to configure a single DeviceButton and add it the configuration
/// </summary>
/// <param name="btn">The target DeviceButton</param>
/// <param name="data">The ScriptSettings object which it needs to be saved too</param>
/// <param name="input">InputManager object to handle input</param>
/// <param name="guid">The GUID of the controller</param>
private bool ConfigureDigitalDpadButton(DeviceButton btn, ScriptSettings data, DirectInputManager input, string guid)
{
while (input.GetDpadValue() == -1)
{
if (Game.IsKeyPressed(System.Windows.Forms.Keys.Escape)) return false;
UI.ShowSubtitle("Press and hold the " + GetBtnText(btn) + " button on the controller for 1 second. Press the Esc key to cancel.", 120);
Script.Wait(100);
}
int dpadValue = input.GetDpadValue();
UI.ShowSubtitle("Please hold the " + GetBtnText(btn) + " button to confirm it.");
Script.Wait(1000);
if (dpadValue == -1)
{
UI.ShowSubtitle("Now hold the " + GetBtnText(btn) + " button to confirm.");
Script.Wait(1000);
ConfigureDigitalDpadButton(btn, data, input, guid);
}
else
{
data.SetValue(guid, btn.ToString(), dpadValue);
while (input.GetDpadValue() != -1)
{
UI.ShowSubtitle("Now let go the button to configure the next one.");
Script.Wait(100);
}
Script.Wait(1000);
}
return true;
}
/// <summary>
/// Helper method which shows the DeviceButon in green text
/// </summary>
/// <param name="btn">The specific DeviceButton</param>
/// <returns>"~g~'Start'~w~" for example</returns>
private string GetBtnText(DeviceButton btn)
{
return "~g~'" + btn + "'~w~";
}
}
}