-
Notifications
You must be signed in to change notification settings - Fork 7
/
ControllerGroup.cs
141 lines (113 loc) · 4.61 KB
/
ControllerGroup.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TTMulti
{
/// <summary>
/// A group of controllers that contains one or more pairs of left and right controllers.
/// </summary>
sealed class ControllerGroup : IDisposable
{
/// <summary>
/// The Toontown window of a controller in this group was activated
/// </summary>
public event EventHandler ControllerWindowActivated;
/// <summary>
/// The Toontown window of a controller in this group was deactivated
/// </summary>
public event EventHandler ControllerWindowDeactivated;
/// <summary>
/// The Toontown window of a controller in this group was closed
/// </summary>
public event EventHandler ControllerWindowHandleChanged;
/// <summary>
/// A controller in this group should be activated (due to a mouse click)
/// </summary>
public event EventHandler ControllerShouldActivate;
/// <summary>
/// A mouse event was captured from a controller in this group
/// </summary>
internal event Forms.OverlayMouseEventHandler MouseEvent;
/// <summary>
/// A pair of controllers was added or removed
/// </summary>
public event EventHandler PairAddedRemoved;
internal List<ControllerPair> ControllerPairs { get; } = new List<ControllerPair>();
internal IEnumerable<ToontownController> AllControllers { get => ControllerPairs.SelectMany(p => p.AllControllers); }
internal IEnumerable<ToontownController> LeftControllers { get => ControllerPairs.Select(p => p.LeftController); }
internal IEnumerable<ToontownController> RightControllers { get => ControllerPairs.Select(p => p.RightController); }
internal int GroupNumber { get; }
public ControllerGroup(int groupNumber)
{
GroupNumber = groupNumber;
AddPair();
}
~ControllerGroup()
{
Dispose();
}
/// <summary>
/// Add a new ControllerPair at the end of the list
/// </summary>
/// <returns></returns>
public ControllerPair AddPair()
{
var pair = new ControllerPair(GroupNumber, ControllerPairs.Count + 1);
pair.LeftController.WindowActivated += Controller_WindowActivated;
pair.RightController.WindowActivated += Controller_WindowActivated;
pair.LeftController.WindowDeactivated += Controller_WindowDeactivated;
pair.RightController.WindowDeactivated += Controller_WindowDeactivated;
pair.LeftController.WindowHandleChanged += Controller_WindowHandleChanged;
pair.RightController.WindowHandleChanged += Controller_WindowHandleChanged;
pair.LeftController.ShouldActivate += Controller_ShouldActivate;
pair.RightController.ShouldActivate += Controller_ShouldActivate;
pair.LeftController.MouseEvent += Controller_MouseEvent;
pair.RightController.MouseEvent += Controller_MouseEvent;
ControllerPairs.Add(pair);
PairAddedRemoved?.Invoke(this, EventArgs.Empty);
return pair;
}
/// <summary>
/// Remove the last ControllerPair from the list
/// </summary>
public void RemoveLastPair()
{
if (ControllerPairs.Count > 1)
{
ControllerPair pair = ControllerPairs.Last();
pair.Shutdown();
ControllerPairs.Remove(pair);
PairAddedRemoved?.Invoke(this, EventArgs.Empty);
}
}
private void Controller_WindowHandleChanged(object sender, EventArgs e)
{
ControllerWindowHandleChanged?.Invoke(sender, e);
}
private void Controller_WindowDeactivated(object sender, EventArgs e)
{
ControllerWindowDeactivated?.Invoke(sender, e);
}
private void Controller_WindowActivated(object sender, EventArgs e)
{
ControllerWindowActivated?.Invoke(sender, e);
}
private void Controller_ShouldActivate(object sender, EventArgs e)
{
ControllerShouldActivate?.Invoke(sender, e);
}
private void Controller_MouseEvent(object sender, System.Windows.Forms.Message m)
{
MouseEvent?.Invoke(sender, m);
}
public void Dispose()
{
foreach (var pair in ControllerPairs)
{
pair.Shutdown();
}
GC.SuppressFinalize(this);
}
}
}