-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDynamicInputPackViewModel.cs
More file actions
342 lines (295 loc) · 12.7 KB
/
Copy pathDynamicInputPackViewModel.cs
File metadata and controls
342 lines (295 loc) · 12.7 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
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
using DolphinDynamicInputTexture.Data;
using DolphinDynamicInputTexture.Properties;
using DolphinDynamicInputTextureCreator.Models;
using DolphinDynamicInputTextureCreator.Other;
using DolphinDynamicInputTextureCreator.ViewModels.Commands;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Input;
namespace DolphinDynamicInputTextureCreator.ViewModels
{
public class DynamicInputPackViewModel : DynamicInputPack
{
#region View Model Properties
/// <summary>
/// The textures defined for dynamic input modification in this pack
/// </summary>
public new UICollection<DynamicInputTexture> Textures
{
get => _textures ??= Textures = new UICollection<DynamicInputTexture>(base.Textures);
set
{
base.Textures = _textures = value;
Textures.SelectedChanged = CheckTextureSelection;
Textures.Select(Selection.First);
OnPropertyChanged(nameof(Textures));
}
}
private UICollection<DynamicInputTexture> _textures;
/// <summary>
/// The host devices mapped in this pack
/// </summary>
public new UICollection<HostDevice> HostDevices
{
get => _host_devices ??= HostDevices = new UICollection<HostDevice>(base.HostDevices);
set
{
base.HostDevices = _host_devices = value;
HostDevices.Select(Selection.First);
OnPropertyChanged(nameof(HostDevices));
}
}
private UICollection<HostDevice> _host_devices;
/// <summary>
/// The emulated devices mapped in this pack
/// </summary>
public UICollection<EmulatedDevice> EmulatedDevices
{
get => _emulated_devices ??= EmulatedDevices = new UICollection<EmulatedDevice>(base._emulated_devices);
set
{
base._emulated_devices = _emulated_devices = value;
EmulatedDevices.Select(Selection.First);
SelectedRegionBrush.SelectedEmulatedDevice = EmulatedDevices.Selected;
OnPropertyChanged(nameof(EmulatedDevices));
}
}
private new UICollection<EmulatedDevice> _emulated_devices;
/// <summary>
/// The Tags mapped in this pack
/// </summary>
public UICollection<Tag> Tags
{
get => _tags ??= Tags = new UICollection<Tag>(base._tags);
set
{
base._tags = _tags = value;
Tags.Select(Selection.First);
SelectedRegionBrush.SelectedTag = Tags.Selected;
OnPropertyChanged(nameof(Tags));
}
}
private new UICollection<Tag> _tags;
/// <summary>
/// The current emulated device / key "brush" chosen for writing regions to the texture
/// </summary>
private RegionBrush _selected_region_brush = new RegionBrush();
public RegionBrush SelectedRegionBrush
{
get => _selected_region_brush;
set
{
_selected_region_brush = value;
OnPropertyChanged(nameof(SelectedRegionBrush));
}
}
/// <summary>
/// The currently selected Region.
/// </summary>
[JsonIgnore]
public InputRegion SelectedRegion
{
get => _selected_region;
set
{
_selected_region = value;
if (SelectedRegion != null)
{
SelectedRegionBrush.SelectedEmulatedDevice = SelectedRegion.Device;
SelectedRegionBrush.UseKey = SelectedRegion.Key == null || SelectedRegion.Key.Name != "";
SelectedRegionBrush.UseTag = SelectedRegion.Tag == null || SelectedRegion.Tag.Name != "";
SelectedRegionBrush.SelectedEmulatedKey = SelectedRegionBrush.UseKey ? SelectedRegion.Key : EmulatedDevices.Selected.EmulatedKeys.Count > 0 ? EmulatedDevices.Selected.EmulatedKeys[0] : null;
SelectedRegionBrush.SelectedTag = SelectedRegionBrush.UseTag ? SelectedRegion.Tag : Tags.Count > 0 ? Tags[0] : null;
}
OnPropertyChanged(nameof(SelectedRegion));
OnPropertyChanged(nameof(IfCopyTypeOverwrite));
OnPropertyChanged(nameof(IsRegionSelected));
}
}
private InputRegion _selected_region;
/// <summary>
/// Options used to rescale textures during export.
/// </summary>
public ExportTextureScalingViewModel ExportTextureScaling
{
get
{
if (_export_texture_scaling == null)
{
_export_texture_scaling = new ExportTextureScalingViewModel();
if (Textures.ValidSelection)
{
ExportTextureScaling.SelectedScalingFactor = (int)Textures.Selected.ImageWidthScaling;
}
}
return _export_texture_scaling;
}
}
public ExportTextureScalingViewModel _export_texture_scaling;
#endregion
#region Commands
#region SelectedTexture
[JsonIgnore]
public ICommand DeleteRegionCommand => new RelayCommand<InputRegion>(Region => GetRegionList(Region).Remove(Region));
[JsonIgnore]
public ICommand AutoSubRegionCommand => new RelayCommand<InputRegion>(Region => AutoSubRegion(Region));
[JsonIgnore]
public ICommand ResetScaleFactorCommand => new RelayCommand((x) => SetInitialZoom(Textures.Selected));
[JsonIgnore]
public ICommand FillRegionCommand => new RelayCommand((x) => FillRegion(), (x) => CanFillRegion());
/// <summary>
/// adds a filled region.
/// </summary>
public void FillRegion()
{
InputRegion f = SelectedRegionBrush.GetNewRegion(0, 0, Textures.Selected.ImageWidth, Textures.Selected.ImageHeight, this);
if (Textures.Selected.Regions.Count == 0)
{
Textures.Selected.Regions.Add(f);
}
else
{
SelectedRegion.RegionRect = f.RegionRect;
((UIRegionRect)SelectedRegion.RegionRect).UpdateScale();
}
}
private bool CanFillRegion()
{
if (!Textures.ValidSelection || !SelectedRegionBrush.IsValid() || IsRegionSelected && SelectedRegion.OwnedRegion != null)
return false;
return Textures.Selected.Regions.Count == 0 || Textures.Selected.Regions.Count == 1 & IsRegionSelected && !SelectedRegion.RegionRect.Equals(new InputRegionRect(0, 0, SelectedRegion.RegionRect.OwnedTexture.ImageWidth, SelectedRegion.RegionRect.OwnedTexture.ImageHeight));
}
public void AutoSubRegion(InputRegion region)
{
if (region.SubEntries.Count != 0)
return;
foreach (var texture in Textures)
{
foreach (var dreg in texture.Regions)
{
if (dreg.SubEntries.Count > 0 && dreg.Device.Equals(region.Device) & dreg.Key.Equals(region.Key) & dreg.Tag.Equals(region.Tag))
{
foreach (var dsubreg in dreg.SubEntries)
{
InputRegion clonregion = (InputRegion)dsubreg.Clone();
clonregion.RegionRect = new UIRegionRect(clonregion.RegionRect) { Pack = this };
region.SubEntries.Add(clonregion);
}
return;
}
}
}
int d = InputRegionRect.DecimalPlaces;
InputRegionRect.DecimalPlaces = 3;
double height = region.RegionRect.Height / 3;
double width = region.RegionRect.Width / 3;
for (int i = 1; i < 8; i += 2)
{
int x = i / 3;
int y = i - 3 * x;
InputRegion subregion = new InputRegion() { Key = region.Key, Tag = region.Tag, CopyType = region.CopyType, RegionRect = new UIRegionRect(region.RegionRect.X + (width * x), region.RegionRect.Y + (height * y), width, height) { Pack = this } };
region.SubEntries.Add(subregion);
if (region.SubEntries.Count == 4)
subregion.SubIndex -= 1;
}
InputRegionRect.DecimalPlaces = d;
}
#endregion
#region SelectedRegion
[JsonIgnore]
public ICommand DeleteSelectedRegionCommand => new RelayCommand(x => GetRegionList(SelectedRegion).Remove(SelectedRegion), x => IsRegionSelected);
[JsonIgnore]
public ICommand UpdateSelectedRegionCommand => new RelayCommand(x => SelectedRegionBrush.UpdateRegion(SelectedRegion),x => IsRegionSelected & SelectedRegionBrush.IsValid());
[JsonIgnore]
public ICommand MoveUpSelectedRegionCommand => new RelayCommand(x => SelectedRegion.SubIndex += 1, x => IsRegionSelected && SelectedRegion.OwnedRegion != null && SelectedRegion.SubIndex < SelectedRegion.OwnedRegion.SubEntries.Count - 1);
[JsonIgnore]
public ICommand MoveDownSelectedRegionCommand => new RelayCommand(x => SelectedRegion.SubIndex -= 1, x => IsRegionSelected && SelectedRegion.OwnedRegion != null && SelectedRegion.SubIndex > 0);
public IList<InputRegion> GetRegionList(InputRegion region)
{
if (region.OwnedRegion != null)
{
if (region.OwnedRegion.OwnedRegion != null)
return GetRegionList(region.OwnedRegion);
return region.OwnedRegion.SubEntries;
}
return Textures.Selected.Regions;
}
[JsonIgnore]
public bool IfCopyTypeOverwrite
{
get => SelectedRegion == null || SelectedRegion.CopyType == default;
set
{
SelectedRegion.CopyType = value ? CopyTypeProperties.overwrite : CopyTypeProperties.overlay;
OnPropertyChanged(nameof(IfCopyTypeOverwrite));
}
}
[JsonIgnore]
public bool IsRegionSelected => SelectedRegion != null;
#endregion
#endregion
#region Checks
private void CheckTextureSelection(DynamicInputTexture Texture)
{
if (Texture != null && !File.Exists(Texture.TexturePath))
{
if (!Dialogs.ImageNotExistMessage(Texture, Path.GetFileName(Texture.TexturePath)))
Textures.Select(null);
}
if (Texture != null && File.Exists(Texture.TexturePath))
{
foreach (InputRegion Region in Texture.Regions)
{
if (Region.RegionRect is InputRegionRect)
{
if (Region.RegionRect is UIRegionRect)
break;
Region.RegionRect = new UIRegionRect(Region.RegionRect) { Pack = this };
foreach (InputRegion SubRegion in Region.SubEntries)
{
SubRegion.RegionRect = new UIRegionRect(SubRegion.RegionRect) { Pack = this };
}
}
}
SelectedRegion = null;
SetInitialZoom(Texture);
}
}
#endregion
#region Zoom
/// <summary>
/// The scale factor for how much to zoom the current texture and regions
/// </summary>
[JsonIgnore]
public double ScaleFactor
{
get => _scale_factor;
set
{
_scale_factor = Smooth(value, 2);
foreach (InputRegion region in Textures.Selected.Regions)
{
((UIRegionRect)region.RegionRect).UpdateScale();
}
OnPropertyChanged(nameof(ScaleFactor));
}
}
private double _scale_factor = 1;
private static double Smooth(double value, int accuracy)
{
double factor = ((int)value * 10).ToString().Length;
factor = Math.Pow(10, factor);
return Math.Round(value / factor, accuracy + 1) * factor;
}
public void SetInitialZoom(DynamicInputTexture Texture, double absolutescale = 600)
{
if (Texture.ImageHeight <= 0 || Texture.ImageWidth <= 0)
return;
absolutescale /= (Texture.ImageHeight + Texture.ImageWidth) / 2;
ScaleFactor = absolutescale;
}
#endregion
}
}