|
| 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 | +} |
0 commit comments