Repository navigation
Expand file tree
/
Copy pathControllerInput.cs
More file actions
76 lines (64 loc) · 2.55 KB
/
Copy pathControllerInput.cs
File metadata and controls
76 lines (64 loc) · 2.55 KB
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
using System;
namespace IngameScript.WolfeLabs.AnalogThrottleAPI
{
public class ControllerInput
{
public ControllerInput (string source, string axis, ushort rawValue) {
this.Source = source;
this.Axis = axis;
this.RawValue = rawValue;
this.AnalogValue = (float)rawValue / (float)ushort.MaxValue;
this.DigitalValue = rawValue > ushort.MaxValue / 2;
}
/// <summary>
/// The Controller name that triggered this event
/// </summary>
public string Source { get; private set; }
/// <summary>
/// The Controller axis that triggered this event
/// </summary>
public string Axis { get; private set; }
/// <summary>
/// The Raw value of this event
/// </summary>
public float RawValue { get; private set; }
/// <summary>
/// The Analog value of this event
/// </summary>
public float AnalogValue { get; private set; }
/// <summary>
/// The Digital value of this event
/// </summary>
public bool DigitalValue { get; private set; }
// This is the character separating each of the parts of each line
private static readonly char separator = ':';
/// <summary>
/// Parses a string containing the data from controller input into a script-friendly object
/// </summary>
public static ControllerInput FromString (string source)
{
// Parses the string
string[] args = source.Trim().Split(ControllerInput.separator);
// Makes sure we have the right amount of arguments
if (3 != args.Length)
throw new Exception("Invalid number of arguments!");
// Extracts individual values
string controller = args[0].Trim();
string axis = args[1].Trim();
ushort value = ushort.Parse(args[2].Trim());
// Creates the input data and applies the actual values
return new ControllerInput(controller, axis, value);
}
/// <summary>
/// Converts a controller input object into a storage/transmission-friendly string
/// </summary>
public override string ToString ()
{
return String.Join(ControllerInput.separator.ToString(), new string[] {
this.Source,
this.Axis,
this.RawValue.ToString(),
});
}
}
}