|
5 | 5 | using Definitions.ObjectModels.Types; |
6 | 6 | using PropertyModels.ComponentModel.DataAnnotations; |
7 | 7 | using PropertyModels.Extensions; |
| 8 | +using ReactiveUI; |
8 | 9 | using System.ComponentModel; |
9 | 10 | using System.ComponentModel.DataAnnotations; |
10 | 11 | using System.Linq; |
11 | 12 |
|
12 | 13 | namespace Gui.ViewModels.LocoTypes.Objects.Building; |
13 | 14 |
|
14 | | -public class BuildingViewModel(BuildingObject model) |
15 | | - : LocoObjectViewModel<BuildingObject>(model) |
| 15 | +public class BuildingViewModel : LocoObjectViewModel<BuildingObject> |
16 | 16 | { |
| 17 | + public BuildingViewModel(BuildingObject model) : base(model) |
| 18 | + { |
| 19 | + ProducedCargo = new(model.ProducedCargo); |
| 20 | + RequiredCargo = new(model.RequiredCargo); |
| 21 | + ProducedQuantity = new(model.ProducedQuantity); |
| 22 | + |
| 23 | + BuildingVariations = new(model.BuildingComponents.BuildingVariations.Select(x => x.ToBindingList()).ToBindingList()); |
| 24 | + BuildingHeights = model.BuildingComponents.BuildingHeights.ToBindingList(); |
| 25 | + BuildingAnimations = model.BuildingComponents.BuildingAnimations.ToBindingList(); |
| 26 | + |
| 27 | + ElevatorSequence1 = model.ElevatorHeightSequences.Count > 0 ? new(model.ElevatorHeightSequences[0]) : null; |
| 28 | + ElevatorSequence2 = model.ElevatorHeightSequences.Count > 1 ? new(model.ElevatorHeightSequences[1]) : null; |
| 29 | + ElevatorSequence3 = model.ElevatorHeightSequences.Count > 2 ? new(model.ElevatorHeightSequences[2]) : null; |
| 30 | + ElevatorSequence4 = model.ElevatorHeightSequences.Count > 3 ? new(model.ElevatorHeightSequences[3]) : null; |
| 31 | + |
| 32 | + // Subscribe to BuildingVariations changes (including nested lists) |
| 33 | + BuildingVariations.ListChanged += OnBuildingComponentChanged; |
| 34 | + foreach (var variation in BuildingVariations) |
| 35 | + { |
| 36 | + variation.ListChanged += OnBuildingComponentChanged; |
| 37 | + } |
| 38 | + |
| 39 | + // Subscribe to BuildingHeights changes |
| 40 | + BuildingHeights.ListChanged += OnBuildingComponentChanged; |
| 41 | + |
| 42 | + // Subscribe to BuildingAnimations changes |
| 43 | + BuildingAnimations.ListChanged += OnBuildingComponentChanged; |
| 44 | + } |
| 45 | + public override void CopyBackToModel() |
| 46 | + { |
| 47 | + Model.BuildingComponents.BuildingVariations = [.. BuildingVariations.Select(x => x.ToList())]; |
| 48 | + Model.BuildingComponents.BuildingHeights = [.. BuildingHeights]; |
| 49 | + Model.BuildingComponents.BuildingAnimations = [.. BuildingAnimations]; |
| 50 | + } |
| 51 | + |
| 52 | + void OnBuildingComponentChanged(object? sender, ListChangedEventArgs e) |
| 53 | + { |
| 54 | + // When a new nested list is added to BuildingVariations, subscribe to it |
| 55 | + if (sender == BuildingVariations && e.ListChangedType == ListChangedType.ItemAdded) |
| 56 | + { |
| 57 | + BuildingVariations[e.NewIndex].ListChanged += OnBuildingComponentChanged; |
| 58 | + } |
| 59 | + |
| 60 | + // 'live' updates are not needed |
| 61 | + //CopyBackToModel(); |
| 62 | + |
| 63 | + MessageBus.Current.SendMessage(new BuildingComponents() |
| 64 | + { |
| 65 | + BuildingAnimations = [.. BuildingAnimations], |
| 66 | + BuildingHeights = [.. BuildingHeights], |
| 67 | + BuildingVariations = [.. BuildingVariations.Select(x => x.ToList())] |
| 68 | + }); |
| 69 | + } |
| 70 | + |
17 | 71 | [EnumProhibitValues<BuildingObjectFlags>(BuildingObjectFlags.None)] |
18 | 72 | public BuildingObjectFlags Flags |
19 | 73 | { |
@@ -87,33 +141,40 @@ public uint16_t SellCostFactor |
87 | 141 | set => Model.SellCostFactor = value; |
88 | 142 | } |
89 | 143 |
|
90 | | - [Category("Production"), Length(0, BuildingObjectLoader.Constants.MaxProducedCargoType)] public BindingList<ObjectModelHeader> ProducedCargo { get; set; } = new(model.ProducedCargo); |
91 | | - [Category("Production"), Length(0, BuildingObjectLoader.Constants.MaxProducedCargoType)] public BindingList<ObjectModelHeader> RequiredCargo { get; set; } = new(model.RequiredCargo); |
92 | | - [Category("Production"), Length(1, BuildingObjectLoader.Constants.MaxProducedCargoType)] public BindingList<uint8_t> ProducedQuantity { get; set; } = new(model.ProducedQuantity); |
| 144 | + [Category("Production"), Length(0, BuildingObjectLoader.Constants.MaxProducedCargoType)] public BindingList<ObjectModelHeader> ProducedCargo { get; set; } |
| 145 | + [Category("Production"), Length(0, BuildingObjectLoader.Constants.MaxProducedCargoType)] public BindingList<ObjectModelHeader> RequiredCargo { get; set; } |
| 146 | + [Category("Production"), Length(1, BuildingObjectLoader.Constants.MaxProducedCargoType)] public BindingList<uint8_t> ProducedQuantity { get; set; } |
| 147 | + |
| 148 | + //[Category("Building")] |
| 149 | + //public BuildingComponents BuildingComponents |
| 150 | + //{ |
| 151 | + // get => Model.BuildingComponents; |
| 152 | + // set => Model.BuildingComponents = value; |
| 153 | + //} |
93 | 154 |
|
94 | 155 | [Category("Building"), Length(1, BuildingObjectLoader.Constants.BuildingVariationCount)] |
95 | | - public BindingList<BindingList<uint8_t>> BuildingVariations { get; init; } = new(model.BuildingComponents.BuildingVariations.Select(x => x.ToBindingList()).ToBindingList()); |
| 156 | + public BindingList<BindingList<uint8_t>> BuildingVariations { get; init; } |
96 | 157 |
|
97 | 158 | [Category("Building"), Length(1, BuildingObjectLoader.Constants.BuildingHeightCount)] |
98 | | - public BindingList<uint8_t> BuildingHeights { get; init; } = model.BuildingComponents.BuildingHeights.ToBindingList(); |
| 159 | + public BindingList<uint8_t> BuildingHeights { get; init; } |
99 | 160 |
|
100 | 161 | [Category("Building"), Length(1, BuildingObjectLoader.Constants.BuildingAnimationCount)] |
101 | | - public BindingList<BuildingPartAnimation> BuildingAnimations { get; init; } = model.BuildingComponents.BuildingAnimations.ToBindingList(); |
| 162 | + public BindingList<BuildingPartAnimation> BuildingAnimations { get; init; } |
102 | 163 |
|
103 | 164 | // note: these height sequences are massive. BLDCTY28 has 2 sequences, 512 in length and 1024 in length. Avalonia PropertyGrid takes 30+ seconds to render this. todo: don't use property grid in future |
104 | 165 | //[Reactive, Category("Building"), Length(1, BuildingObject.MaxElevatorHeightSequences), Browsable(false)] public BindingList<BindingList<uint8_t>> ElevatorHeightSequences { get; set; } // NumElevatorSequences |
105 | 166 |
|
106 | 167 | [Category("Elevator"), Browsable(false)] |
107 | | - public BindingList<uint8_t>? ElevatorSequence1 { get; init; } = model.ElevatorHeightSequences.Count > 0 ? new(model.ElevatorHeightSequences[0]) : null; |
| 168 | + public BindingList<uint8_t>? ElevatorSequence1 { get; init; } |
108 | 169 |
|
109 | 170 | [Category("Elevator"), Browsable(false)] |
110 | | - public BindingList<uint8_t>? ElevatorSequence2 { get; init; } = model.ElevatorHeightSequences.Count > 1 ? new(model.ElevatorHeightSequences[1]) : null; |
| 171 | + public BindingList<uint8_t>? ElevatorSequence2 { get; init; } |
111 | 172 |
|
112 | 173 | [Category("Elevator"), Browsable(false)] |
113 | | - public BindingList<uint8_t>? ElevatorSequence3 { get; init; } = model.ElevatorHeightSequences.Count > 2 ? new(model.ElevatorHeightSequences[2]) : null; |
| 174 | + public BindingList<uint8_t>? ElevatorSequence3 { get; init; } |
114 | 175 |
|
115 | 176 | [Category("Elevator"), Browsable(false)] |
116 | | - public BindingList<uint8_t>? ElevatorSequence4 { get; init; } = model.ElevatorHeightSequences.Count > 3 ? new(model.ElevatorHeightSequences[3]) : null; |
| 177 | + public BindingList<uint8_t>? ElevatorSequence4 { get; init; } |
117 | 178 |
|
118 | 179 | [Category("<unknown>")] |
119 | 180 | public uint8_t var_A6 |
|
0 commit comments