Skip to content

Commit fbba851

Browse files
committed
Core add SubRegionRect
1 parent 190082d commit fbba851

7 files changed

Lines changed: 241 additions & 20 deletions

File tree

DolphinDynamicInputTexture/Data/DynamicInputPack.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,9 @@ private InputRegion ReadV2InputRegion(JsonReader reader, EmulatedDevice emulated
606606
case "sub_entries":
607607
while (reader.Read() && reader.TokenType != JsonToken.EndArray)
608608
{
609-
Region.SubEntries.Add(ReadV2InputRegion(reader, emulatedDevice, texture));
609+
InputRegion subentry = ReadV2InputRegion(reader, emulatedDevice, texture);
610+
subentry.RegionRect = new InputSubRegionRect(subentry.RegionRect);
611+
Region.SubEntries.Add(subentry);
610612
}
611613
break;
612614
}

DolphinDynamicInputTexture/Data/DynamicInputTexture.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,8 @@ public ObservableCollection<InputRegion> Regions
9595
{
9696
foreach (InputRegion Region in value)
9797
{
98+
Region.OwnedRegion = null;
9899
Region.RegionRect.OwnedTexture = this;
99-
foreach (InputRegion SubEntrie in Region.SubEntries)
100-
{
101-
SubEntrie.RegionRect.OwnedTexture = this;
102-
}
103100
}
104101
}
105102
_regions = value;
@@ -118,10 +115,11 @@ private void OnCollectionOfRegionsChanged(object sender, NotifyCollectionChanged
118115
{
119116
foreach (InputRegion Region in e.NewItems)
120117
{
118+
Region.OwnedRegion = null;
121119
Region.RegionRect.OwnedTexture = this;
122-
foreach (InputRegion SubEntrie in Region.SubEntries)
120+
foreach (InputRegion subentry in Region.SubEntries)
123121
{
124-
SubEntrie.RegionRect.OwnedTexture = this;
122+
subentry.RegionRect.OwnedTexture = this;
125123
}
126124
}
127125
}

DolphinDynamicInputTexture/Data/InputRegion.cs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class InputRegion : Other.PropertyChangedBase, ITagable, ICloneable, IEqu
1414
/// </summary>
1515
public EmulatedDevice Device
1616
{
17-
get => _emulated_device ??= new EmulatedDevice();
17+
get => OwnedRegion == null ? _emulated_device ??= new EmulatedDevice() : OwnedRegion.Device;
1818
set
1919
{
2020
_emulated_device = value;
@@ -89,7 +89,15 @@ public IRectRegion RegionRect
8989
{
9090
IRectRegion clonerect = (IRectRegion)value.Clone();
9191
clonerect.OwnedTexture = _region_rect.OwnedTexture;
92+
if (OwnedRegion != null && clonerect is ISubRectRegion)
93+
{
94+
((ISubRectRegion)clonerect).MainRegion = OwnedRegion.RegionRect;
95+
}
9296
_region_rect = clonerect;
97+
foreach (InputRegion Region in SubEntries)
98+
{
99+
Region.OwnedRegion = this;
100+
}
93101
OnPropertyChanged(nameof(RegionRect));
94102
}
95103
}
@@ -109,9 +117,12 @@ public ObservableCollection<InputRegion> SubEntries
109117
}
110118
if (value.Count > 0)
111119
{
120+
if (BindType == BindTypeProperties.single)
121+
BindType = BindTypeProperties.multi;
122+
112123
foreach (InputRegion Region in value)
113124
{
114-
Region.RegionRect.OwnedTexture = RegionRect.OwnedTexture;
125+
Region.OwnedRegion = this;
115126
}
116127
}
117128
_sub_entries = value;
@@ -121,16 +132,46 @@ public ObservableCollection<InputRegion> SubEntries
121132
}
122133
private ObservableCollection<InputRegion> _sub_entries;
123134

135+
136+
public InputRegion OwnedRegion
137+
{
138+
get => _main_region;
139+
internal set
140+
{
141+
_main_region = value;
142+
if (value != null)
143+
{
144+
RegionRect.OwnedTexture = _main_region.RegionRect.OwnedTexture;
145+
if (RegionRect is ISubRectRegion)
146+
{
147+
((ISubRectRegion)RegionRect).MainRegion = OwnedRegion.RegionRect;
148+
}
149+
}
150+
OnPropertyChanged(nameof(OwnedRegion));
151+
}
152+
}
153+
private InputRegion _main_region;
154+
124155
/// <summary>
125156
/// connects all sub regions with this regions.
126157
/// </summary>
127158
private void OnCollectionOfSubEntriesChanged(object sender, NotifyCollectionChangedEventArgs e)
128159
{
160+
161+
if (e.OldItems != null)
162+
{
163+
if (SubEntries.Count == 0)
164+
BindType = BindTypeProperties.single;
165+
}
166+
129167
if (e.NewItems != null)
130168
{
169+
if (BindType == BindTypeProperties.single)
170+
BindType = BindTypeProperties.multi;
171+
131172
foreach (InputRegion Region in e.NewItems)
132173
{
133-
Region.RegionRect.OwnedTexture = RegionRect.OwnedTexture;
174+
Region.OwnedRegion = this;
134175
}
135176
}
136177
}
@@ -158,5 +199,6 @@ public bool Equals([AllowNull] InputRegion other)
158199
}
159200
return false;
160201
}
202+
161203
}
162204
}
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
using DolphinDynamicInputTexture.Interfaces;
2+
using System.ComponentModel;
3+
using System.Diagnostics.CodeAnalysis;
4+
5+
namespace DolphinDynamicInputTexture.Data
6+
{
7+
public class InputSubRegionRect : InputRegionRect, ISubRectRegion
8+
{
9+
IRectRegion ISubRectRegion.MainRegion
10+
{
11+
get => _main_region;
12+
set
13+
{
14+
if (MainRegion != null)
15+
{
16+
MainRegion.PropertyChanged -= OnMainRegionChanged;
17+
_main_region = value;
18+
UpdateXScale();
19+
UpdateYScale();
20+
}
21+
else
22+
{
23+
base.X = X - value.X;
24+
base.Y = Y - value.Y;
25+
LastMainRegionWidth = value.Width;
26+
LastMainRegionHeight = value.Height;
27+
_main_region = value;
28+
}
29+
MainRegion.PropertyChanged += OnMainRegionChanged;
30+
OnPropertyChanged(nameof(MainRegion));
31+
}
32+
}
33+
34+
public IRectRegion MainRegion => _main_region;
35+
private IRectRegion _main_region;
36+
37+
public new double X
38+
{
39+
get => MainRegion != null ? MainRegion.X + base.X : base.X;
40+
set
41+
{
42+
if (MainRegion != null)
43+
{
44+
base.X = value - MainRegion.X;
45+
46+
if (RightX > MainRegion.RightX)
47+
base.X -= RightX - MainRegion.RightX;
48+
}
49+
else
50+
{
51+
base.X = value;
52+
}
53+
}
54+
}
55+
56+
public new double Y
57+
{
58+
get => MainRegion != null ? MainRegion.Y + base.Y : base.Y;
59+
set
60+
{
61+
if (MainRegion != null)
62+
{
63+
base.Y = value - MainRegion.Y;
64+
65+
if (BottomY > MainRegion.BottomY)
66+
base.Y -= BottomY - MainRegion.BottomY;
67+
}
68+
else
69+
{
70+
base.Y = value;
71+
}
72+
}
73+
}
74+
75+
public new double Width
76+
{
77+
get => base.Width;
78+
set
79+
{
80+
base.Width = value;
81+
if (MainRegion != null)
82+
{
83+
if (RightX > MainRegion.RightX)
84+
base.Width -= RightX - MainRegion.RightX;
85+
}
86+
}
87+
}
88+
89+
public new double Height
90+
{
91+
get => base.Height;
92+
set
93+
{
94+
base.Height = value;
95+
if (MainRegion != null)
96+
{
97+
if (BottomY > MainRegion.BottomY)
98+
base.Height -= BottomY - MainRegion.BottomY;
99+
}
100+
}
101+
}
102+
103+
public new double BottomY => Y + Height;
104+
105+
public new double RightX => X + Width;
106+
107+
public new bool Equals([AllowNull] IRectRegion other)
108+
{
109+
return other != null && Equals(other.X, other.Y, other.Width, other.Height);
110+
}
111+
112+
public new bool Equals(double x, double y, double width, double height)
113+
{
114+
return x == X & y == Y & width == Width & height == Height;
115+
}
116+
117+
private double LastMainRegionWidth;
118+
private double LastMainRegionHeight;
119+
120+
private void UpdateXScale()
121+
{
122+
if (LastMainRegionWidth != MainRegion.Width)
123+
{
124+
int i = InputRegionRect.DecimalPlaces;
125+
InputRegionRect.DecimalPlaces = 3;
126+
base.X *= MainRegion.Width / LastMainRegionWidth;
127+
Width *= MainRegion.Width / LastMainRegionWidth;
128+
LastMainRegionWidth = MainRegion.Width;
129+
InputRegionRect.DecimalPlaces = i;
130+
}
131+
}
132+
133+
private void UpdateYScale()
134+
{
135+
if (LastMainRegionHeight != MainRegion.Height)
136+
{
137+
int i = InputRegionRect.DecimalPlaces;
138+
InputRegionRect.DecimalPlaces = 3;
139+
base.Y *= MainRegion.Height / LastMainRegionHeight;
140+
Height *= MainRegion.Height / LastMainRegionHeight;
141+
LastMainRegionHeight = MainRegion.Height;
142+
InputRegionRect.DecimalPlaces = i;
143+
}
144+
}
145+
146+
public InputSubRegionRect() { }
147+
148+
public InputSubRegionRect(double x, double y, double width, double height) :base(x,y, width, height) { }
149+
150+
public InputSubRegionRect(IRectRegion Rect) : this(Rect.X, Rect.Y, Rect.Width, Rect.Height){}
151+
152+
private void OnMainRegionChanged(object sender, PropertyChangedEventArgs e)
153+
{
154+
switch (e.PropertyName)
155+
{
156+
case nameof(Width):
157+
UpdateXScale();
158+
break;
159+
case nameof(Height):
160+
UpdateYScale();
161+
break;
162+
default:
163+
OnPropertyChanged(e.PropertyName);
164+
break;
165+
}
166+
}
167+
}
168+
}

DolphinDynamicInputTexture/Interfaces/IRegionRect.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using DolphinDynamicInputTexture.Data;
22
using System;
3+
using System.ComponentModel;
34

45
namespace DolphinDynamicInputTexture.Interfaces
56
{
6-
public interface IRectRegion : ICloneable, IEquatable<IRectRegion>
7+
public interface IRectRegion : ICloneable, IEquatable<IRectRegion>, INotifyPropertyChanged
78
{
89
DynamicInputTexture OwnedTexture { get; internal set; }
910
/// <summary>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using DolphinDynamicInputTexture.Data;
2+
using System;
3+
4+
namespace DolphinDynamicInputTexture.Interfaces
5+
{
6+
public interface ISubRectRegion : IRectRegion
7+
{
8+
IRectRegion MainRegion { get; internal set; }
9+
}
10+
}

Models/UIRegionRect.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace DolphinDynamicInputTextureCreator.Models
66
{
7-
public class UIRegionRect : InputRegionRect
7+
public class UIRegionRect : InputSubRegionRect
88
{
99
public DynamicInputPackViewModel Pack
1010
{
@@ -24,7 +24,6 @@ public DynamicInputPackViewModel Pack
2424
set
2525
{
2626
base.X = value;
27-
OnPropertyChanged(nameof(X));
2827
OnPropertyChanged(nameof(ScaledX));
2928
}
3029
}
@@ -35,28 +34,29 @@ public DynamicInputPackViewModel Pack
3534
set
3635
{
3736
base.Y = value;
38-
OnPropertyChanged(nameof(Y));
3937
OnPropertyChanged(nameof(ScaledY));
4038
}
4139
}
4240

41+
4342
public new double Height
4443
{
4544
get => base.Height;
4645
set
4746
{
4847
base.Height = value;
49-
OnPropertyChanged(nameof(Height));
48+
OnPropertyChanged(nameof(ScaledY));
5049
OnPropertyChanged(nameof(ScaledHeight));
5150
}
5251
}
52+
5353
public new double Width
5454
{
5555
get => base.Width;
5656
set
5757
{
5858
base.Width = value;
59-
OnPropertyChanged(nameof(Width));
59+
OnPropertyChanged(nameof(ScaledX));
6060
OnPropertyChanged(nameof(ScaledWidth));
6161
}
6262
}
@@ -78,14 +78,14 @@ public void UpdateScale()
7878

7979
public double ScaledX
8080
{
81-
get => X * Pack.ScaleFactor;
82-
set => X = value / Pack.ScaleFactor;
81+
get => (X - (MainRegion != null ? MainRegion.X: 0)) * Pack.ScaleFactor;
82+
set => X = value / Pack.ScaleFactor + (MainRegion != null ? MainRegion.X : 0);
8383
}
8484

8585
public double ScaledY
8686
{
87-
get => Y * Pack.ScaleFactor;
88-
set => Y = value / Pack.ScaleFactor;
87+
get => (Y - (MainRegion != null ? MainRegion.Y : 0)) * Pack.ScaleFactor;
88+
set => Y = value / Pack.ScaleFactor + (MainRegion != null ? MainRegion.Y : 0);
8989
}
9090

9191
public double ScaledWidth

0 commit comments

Comments
 (0)