-
Notifications
You must be signed in to change notification settings - Fork 0
/
Immersifier.cs
249 lines (223 loc) · 6.17 KB
/
Immersifier.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace ImmersiveGaming
{
class GameImmersifier
{
internal GameInfo info;
internal Win32Form form;
static List<BlackoutForm> blackoutForms = new List<BlackoutForm>();
public GameImmersifier(Win32Form form, GameInfo info)
{
this.form = form;
this.info = info;
}
Rectangle Rect
{
get
{
return info.monitors.Select(a => (Rectangle)a.Bounds).Aggregate(Rectangle.Union);
}
}
private void RemoveBorders(Win32Form form)
{
form.WindowStyle = User32Types.WindowStyles.MinimizeBox | User32Types.WindowStyles.Popup | User32Types.WindowStyles.Tiled | User32Types.WindowStyles.Visible;
form.WindowExStyle = User32Types.WindowExStyles.Left;
}
public void Activate()
{
if (info.hideMouse)
{
MouseHider.HideCursors();
}
switch (info.alwaysOnTop)
{
case CheckState.Checked:
form.TopMost = true;
break;
case CheckState.Indeterminate:
break;
case CheckState.Unchecked:
form.TopMost = false;
break;
}
if (info.monitors.Any())
{
if (info.blackoutUnused)
{
Blackout(Rect);
}
RemoveBorders(form);
form.Rect = Rect;
}
}
public void Deactivate()
{
if (info.hideMouse)
{
MouseHider.ShowCursors();
}
if (info.monitors.Any())
{
if (info.blackoutUnused)
{
Whitein();
}
}
}
static public void Blackout(Rectangle rect)
{
while (blackoutForms.Count > 0)
{
blackoutForms[0].Close();
blackoutForms.RemoveAt(0);
}
var bounds = Screen.AllScreens.Where(a => !a.Bounds.IntersectsWith(rect)).ToArray();
blackoutForms = bounds.Select(a => { var tmp = new BlackoutForm(a); tmp.Show(); return tmp; }).ToList();
}
static public void Whitein()
{
while (blackoutForms.Count > 0)
{
blackoutForms[0].Close();
blackoutForms.RemoveAt(0);
}
}
}
[Serializable]
public class AllGamesInfo
{
public List<GameInfo> games = new List<GameInfo>();
public AllGamesInfo()
{
}
public void Add(GameInfo info)
{
games.Add(info);
}
public void Remove(IEnumerable<int> values)
{
int[] vals = values.ToArray().OrderBy(a => a).Reverse().ToArray();
foreach (var i in vals)
{
games.RemoveAt(i);
}
}
public void Merge(AllGamesInfo other)
{
Merge(other, (a, b) => new[] { a });
}
public void Merge(AllGamesInfo other, Func<GameInfo, GameInfo, GameInfo[]> handler)
{
if (other != null)
{
foreach (var game in other.games)
{
if (games.Contains(game, new Comparer<GameInfo>((a,b)=>a.IsSimilar(b))))
{
games.Add(game);
}
}
}
}
public GameInfo Match(Win32Form form)
{
foreach (var game in games)
{
if (game.IsMatch(form))
{
return game;
}
}
return null;
}
public static AllGamesInfo Load(string filename)
{
if (File.Exists(filename))
{
using (var fr = File.OpenRead(filename))
{
return (AllGamesInfo)new XmlSerializer(typeof(AllGamesInfo)).Deserialize(fr);
}
}
return null;
}
public void Save(string filename)
{
using (var wr = File.Create(filename))
{
new XmlSerializer(typeof(AllGamesInfo)).Serialize(wr, this);
}
}
}
class Immersifier
{
GameImmersifier current = null;
AllGamesInfo agi = new AllGamesInfo();
public List<GameInfo> Games
{
get
{
return agi.games;
}
}
void SetGame(Win32Form form, GameInfo info)
{
if (current != null)
{
if (info != current.info)
{
current.Deactivate();
}
else
{
return;
}
}
if (info == null)
{
current = null;
}
else
{
current = new GameImmersifier(form, info);
current.Activate();
}
}
public void Add(GameInfo info)
{
agi.Add(info);
}
public void Remove(IEnumerable<int> values)
{
agi.Remove(values);
}
public void Update(Win32Form form)
{
if (form != null)
{
if (current == null || (current != null && current.form != form))
{
SetGame(form, agi.Match(form));
}
}
}
public void Load(string filename)
{
agi = AllGamesInfo.Load(filename) ?? agi;
}
public void Merge(string filename)
{
agi.Merge(AllGamesInfo.Load(filename));
}
public void Save(string filename)
{
agi.Save(filename);
}
}
}