-
Notifications
You must be signed in to change notification settings - Fork 85
/
ViewWindow.xaml.cs
executable file
·350 lines (295 loc) · 14.2 KB
/
ViewWindow.xaml.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.ComponentModel;
using System.Windows.Shapes;
using NintendoSpy.Readers;
namespace NintendoSpy
{
public partial class ViewWindow : Window, INotifyPropertyChanged
{
Skin _skin;
IControllerReader _reader;
Keybindings _keybindings;
BlinkReductionFilter _blinkFilter = new BlinkReductionFilter ();
List <Tuple <Skin.Button,Image>> _buttonsWithImages = new List <Tuple <Skin.Button,Image>> ();
List <Tuple <Skin.RangeButton,Image>> _rangeButtonsWithImages = new List <Tuple <Skin.RangeButton,Image>> ();
List <Tuple <Skin.AnalogStick,Image>> _sticksWithImages = new List <Tuple <Skin.AnalogStick,Image>> ();
// The triggers images are embedded inside of a Grid element so that we can properly mask leftwards and upwards
// without the image aligning to the top left of its element.
List <Tuple <Skin.AnalogTrigger,Grid>> _triggersWithGridImages = new List <Tuple <Skin.AnalogTrigger,Grid>> ();
/// Expose the enabled status of the low-pass filter for data binding.
public bool ButtonBlinkReductionEnabled {
get { return _blinkFilter.ButtonEnabled; }
set {
_blinkFilter.ButtonEnabled = value;
OnPropertyChanged ("ButtonBlinkReductionEnabled");
}
}
public bool MassBlinkReductionEnabled
{
get { return _blinkFilter.MassEnabled; }
set {
_blinkFilter.MassEnabled = value;
OnPropertyChanged("MassBlinkReductionEnabled");
}
}
public bool AnalogBlinkReductionEnabled
{
get { return _blinkFilter.AnalogEnabled; }
set {
_blinkFilter.AnalogEnabled = value;
OnPropertyChanged("AnalogBlinkReductionEnabled");
}
}
public bool AllBlinkReductionEnabled
{
get { return ButtonBlinkReductionEnabled && AnalogBlinkReductionEnabled && MassBlinkReductionEnabled; }
set {
ButtonBlinkReductionEnabled = AnalogBlinkReductionEnabled = MassBlinkReductionEnabled = value;
OnPropertyChanged("AllBlinkReductionEnabled");
}
}
public ViewWindow (Skin skin, Skin.Background skinBackground, IControllerReader reader)
{
InitializeComponent ();
DataContext = this;
_skin = skin;
_reader = reader;
ControllerGrid.Width = skinBackground.Width;
ControllerGrid.Height = skinBackground.Height;
var brush = new SolidColorBrush(skinBackground.Color);
ControllerGrid.Background = brush;
if (skinBackground.Image != null)
{
var img = new Image();
img.VerticalAlignment = VerticalAlignment.Top;
img.HorizontalAlignment = HorizontalAlignment.Left;
img.Source = skinBackground.Image;
img.Stretch = Stretch.Uniform;
img.Margin = new Thickness(0, 0, 0, 0);
img.Width = skinBackground.Image.PixelWidth;
img.Height = skinBackground.Image.PixelHeight;
ControllerGrid.Children.Add(img);
}
foreach (var detail in _skin.Details)
{
if (bgIsActive(skinBackground.Name, detail.Config.TargetBackgrounds, detail.Config.IgnoreBackgrounds))
{
var image = getImageForElement(detail.Config);
ControllerGrid.Children.Add(image);
}
}
foreach (var trigger in _skin.AnalogTriggers) {
if (bgIsActive(skinBackground.Name, trigger.Config.TargetBackgrounds, trigger.Config.IgnoreBackgrounds))
{
var grid = getGridForAnalogTrigger(trigger);
_triggersWithGridImages.Add(new Tuple<Skin.AnalogTrigger, Grid>(trigger, grid));
ControllerGrid.Children.Add(grid);
}
}
foreach (var button in _skin.Buttons) {
if (bgIsActive(skinBackground.Name, button.Config.TargetBackgrounds, button.Config.IgnoreBackgrounds))
{
var image = getImageForElement(button.Config);
_buttonsWithImages.Add(new Tuple<Skin.Button, Image>(button, image));
image.Visibility = Visibility.Hidden;
ControllerGrid.Children.Add(image);
}
}
foreach (var button in _skin.RangeButtons) {
if (bgIsActive(skinBackground.Name, button.Config.TargetBackgrounds, button.Config.IgnoreBackgrounds))
{
var image = getImageForElement(button.Config);
_rangeButtonsWithImages.Add(new Tuple<Skin.RangeButton, Image>(button, image));
image.Visibility = Visibility.Hidden;
ControllerGrid.Children.Add(image);
}
}
foreach (var stick in _skin.AnalogSticks) {
if (bgIsActive(skinBackground.Name, stick.Config.TargetBackgrounds, stick.Config.IgnoreBackgrounds))
{
var image = getImageForElement(stick.Config);
_sticksWithImages.Add(new Tuple<Skin.AnalogStick, Image>(stick, image));
ControllerGrid.Children.Add(image);
}
}
_reader.ControllerStateChanged += reader_ControllerStateChanged;
_reader.ControllerDisconnected += reader_ControllerDisconnected;
try {
_keybindings = new Keybindings (Keybindings.XML_FILE_PATH, _reader);
} catch (ConfigParseException) {
MessageBox.Show ("Error parsing keybindings.xml. Not binding any keys to gamepad inputs");
}
MassBlinkReductionEnabled = Properties.Settings.Default.MassFilter;
AnalogBlinkReductionEnabled = Properties.Settings.Default.AnalogFilter;
ButtonBlinkReductionEnabled = Properties.Settings.Default.ButtonFilter;
Topmost = Properties.Settings.Default.TopMost;
}
static bool bgIsActive(string bgName, List<string> eligableBgs, List<string> ignoreBgs)
{
if (ignoreBgs.Contains(bgName))
{
return false;
}
return eligableBgs.Count == 0 || eligableBgs.Contains(bgName);
}
static Image getImageForElement (Skin.ElementConfig config)
{
var img = new Image ();
img.VerticalAlignment = VerticalAlignment.Top;
img.HorizontalAlignment = HorizontalAlignment.Left;
img.Source = config.Image;
img.Stretch = Stretch.Fill;
img.Margin = new Thickness (config.X, config.Y, 0, 0);
img.Width = config.Width;
img.Height = config.Height;
return img;
}
static Grid getGridForAnalogTrigger (Skin.AnalogTrigger trigger)
{
var img = new Image ();
img.VerticalAlignment = VerticalAlignment.Top;
img.HorizontalAlignment =
trigger.Direction == Skin.AnalogTrigger.DirectionValue.Left
? HorizontalAlignment.Right
: HorizontalAlignment.Left;
img.VerticalAlignment =
trigger.Direction == Skin.AnalogTrigger.DirectionValue.Up
? VerticalAlignment.Bottom
: VerticalAlignment.Top;
img.Source = trigger.Config.Image;
img.Stretch = Stretch.Fill;
img.Margin = new Thickness (0, 0, 0, 0);
img.Width = trigger.Config.Width;
img.Height = trigger.Config.Height;
var grid = new Grid ();
grid.HorizontalAlignment = HorizontalAlignment.Left;
grid.VerticalAlignment = VerticalAlignment.Top;
grid.Margin = new Thickness (trigger.Config.X, trigger.Config.Y, 0, 0);
grid.Width = trigger.Config.Width;
grid.Height = trigger.Config.Height;
grid.Children.Add (img);
return grid;
}
void AlwaysOnTop_Click (object sender, RoutedEventArgs e) {
this.Topmost = !this.Topmost;
Properties.Settings.Default.TopMost = Topmost;
}
void AllBlinkReductionEnabled_Click(object sender, RoutedEventArgs e)
{
if (this.ButtonBlinkReductionEnabled && this.AnalogBlinkReductionEnabled && this.MassBlinkReductionEnabled)
{
this.AllBlinkReductionEnabled = false;
}
else
{
this.AllBlinkReductionEnabled = true;
}
Properties.Settings.Default.ButtonFilter = ButtonBlinkReductionEnabled;
Properties.Settings.Default.AnalogFilter = AnalogBlinkReductionEnabled;
Properties.Settings.Default.MassFilter = MassBlinkReductionEnabled;
}
void ButtonBlinkReductionEnabled_Click (object sender, RoutedEventArgs e) {
this.ButtonBlinkReductionEnabled = !this.ButtonBlinkReductionEnabled;
Properties.Settings.Default.ButtonFilter = ButtonBlinkReductionEnabled;
}
void AnalogBlinkReductionEnabled_Click(object sender, RoutedEventArgs e)
{
this.AnalogBlinkReductionEnabled = !this.AnalogBlinkReductionEnabled;
Properties.Settings.Default.AnalogFilter = AnalogBlinkReductionEnabled;
}
void MassBlinkReductionEnabled_Click(object sender, RoutedEventArgs e)
{
this.MassBlinkReductionEnabled = !this.MassBlinkReductionEnabled;
Properties.Settings.Default.MassFilter = MassBlinkReductionEnabled;
}
void reader_ControllerDisconnected (object sender, EventArgs e)
{
Close ();
}
void Window_Closing (object sender, System.ComponentModel.CancelEventArgs e)
{
Properties.Settings.Default.Save();
if (_keybindings != null) {
_keybindings.Finish ();
}
_reader.Finish ();
}
void reader_ControllerStateChanged (IControllerReader reader, ControllerState newState)
{
newState = _blinkFilter.Process (newState);
foreach (var button in _buttonsWithImages)
{
if (!newState.Buttons.ContainsKey (button.Item1.Name)) continue;
button.Item2.Visibility = newState.Buttons [button.Item1.Name] ? Visibility.Visible : Visibility.Hidden ;
}
foreach (var button in _rangeButtonsWithImages)
{
if (!newState.Analogs.ContainsKey (button.Item1.Name)) continue;
var value = newState.Analogs [button.Item1.Name];
var visible = button.Item1.From <= value && value <= button.Item1.To;
button.Item2.Visibility = visible ? Visibility.Visible : Visibility.Hidden ;
}
foreach (var stick in _sticksWithImages)
{
var skin = stick.Item1;
var image = stick.Item2;
float xrange = (skin.XReverse ? -1 : 1) * skin.XRange;
float yrange = (skin.YReverse ? 1 : -1) * skin.YRange;
var x = newState.Analogs.ContainsKey (skin.XName)
? skin.Config.X + xrange * newState.Analogs [skin.XName]
: skin.Config.X ;
var y = newState.Analogs.ContainsKey (skin.YName)
? skin.Config.Y + yrange * newState.Analogs [skin.YName]
: skin.Config.Y ;
image.Margin = new Thickness (x,y,0,0);
}
foreach (var trigger in _triggersWithGridImages)
{
var skin = trigger.Item1;
var grid = trigger.Item2;
if (!newState.Analogs.ContainsKey (skin.Name)) continue;
var val = newState.Analogs [skin.Name];
if (skin.UseNegative) val *= -1;
if (skin.IsReversed) val = 1 - val;
if (val < 0) val = 0;
switch (skin.Direction)
{
case Skin.AnalogTrigger.DirectionValue.Right:
grid.Width = skin.Config.Width * val;
break;
case Skin.AnalogTrigger.DirectionValue.Left:
var width = skin.Config.Width * val;
var offx = skin.Config.Width - width;
grid.Margin = new Thickness (skin.Config.X + offx, skin.Config.Y, 0, 0);
grid.Width = width;
break;
case Skin.AnalogTrigger.DirectionValue.Down:
grid.Height = skin.Config.Height * val;
break;
case Skin.AnalogTrigger.DirectionValue.Up:
var height = skin.Config.Height * val;
var offy = skin.Config.Height - height;
grid.Margin = new Thickness (skin.Config.X, skin.Config.Y + offy, 0, 0);
grid.Height = height;
break;
}
}
}
// INotifyPropertyChanged interface implementation
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged (string propertyName) {
if (PropertyChanged != null) PropertyChanged (this, new PropertyChangedEventArgs(propertyName));
}
}
}