Skip to content

Commit 2fc886d

Browse files
committed
remove copybacktomodel for buildingobject
1 parent b740efe commit 2fc886d

3 files changed

Lines changed: 63 additions & 13 deletions

File tree

Gui/ViewModels/LocoTypes/ObjectEditorViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ void SaveCore(string filename, SaveParameters saveParameters)
485485
StringTableViewModel?.WriteTableBackToObject();
486486

487487
// VM should auto-copy back now for everything but BuildingObject
488-
CurrentObjectViewModel.CopyBackToModel();
488+
//CurrentObjectViewModel.CopyBackToModel();
489489

490490
// this is hacky but it should work
491491
if (ExtraContentViewModel is AudioViewModel avm && CurrentObject.LocoObject.Object is SoundObject)

Gui/ViewModels/LocoTypes/Objects/Building/BuildingComponentsViewModel.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ void UpdateBuildingComponents(BuildingComponents buildingComponents)
7878

7979
protected void RecomputeBuildingVariationViewModels(List<List<uint8_t>> buildingVariations, List<byte> buildingHeights)
8080
{
81+
if (!buildingVariations.Any() || !buildingHeights.Any())
82+
{
83+
// todo: log error
84+
return;
85+
}
86+
87+
8188
var layers = ImageTable.Groups.ConvertAll(x => x.GraphicsElements);
8289

8390
BuildingVariationViewModels.Clear();

Gui/ViewModels/LocoTypes/Objects/Building/BuildingViewModel.cs

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,78 @@ public BuildingViewModel(BuildingObject model) : base(model)
2929
ElevatorSequence3 = model.ElevatorHeightSequences.Count > 2 ? new(model.ElevatorHeightSequences[2]) : null;
3030
ElevatorSequence4 = model.ElevatorHeightSequences.Count > 3 ? new(model.ElevatorHeightSequences[3]) : null;
3131

32+
#region Building Components Synchronization
33+
3234
// Subscribe to BuildingVariations changes (including nested lists)
33-
BuildingVariations.ListChanged += OnBuildingComponentChanged;
35+
BuildingVariations.ListChanged += OnBuildingVariationsChanged;
3436
foreach (var variation in BuildingVariations)
3537
{
36-
variation.ListChanged += OnBuildingComponentChanged;
38+
variation.ListChanged += OnBuildingVariationItemChanged;
3739
}
3840

3941
// Subscribe to BuildingHeights changes
40-
BuildingHeights.ListChanged += OnBuildingComponentChanged;
42+
BuildingHeights.ListChanged += OnBuildingHeightsChanged;
4143

4244
// Subscribe to BuildingAnimations changes
43-
BuildingAnimations.ListChanged += OnBuildingComponentChanged;
45+
BuildingAnimations.ListChanged += OnBuildingAnimationsChanged;
46+
47+
#endregion
48+
}
49+
50+
void OnBuildingVariationsChanged(object? sender, ListChangedEventArgs e)
51+
{
52+
// Only handle when the outer list itself changes, not when inner list items change
53+
if (e.ListChangedType == ListChangedType.ItemAdded && e.NewIndex >= 0 && e.NewIndex < BuildingVariations.Count)
54+
{
55+
BuildingVariations[e.NewIndex].ListChanged += OnBuildingVariationItemChanged;
56+
SynchronizeBuildingComponentsToModel();
57+
NotifyBuildingChanged();
58+
}
59+
else if (e.ListChangedType == ListChangedType.ItemDeleted)
60+
{
61+
SynchronizeBuildingComponentsToModel();
62+
NotifyBuildingChanged();
63+
}
64+
else if (e.ListChangedType == ListChangedType.Reset)
65+
{
66+
// Re-subscribe to all items after reset
67+
foreach (var variation in BuildingVariations)
68+
{
69+
variation.ListChanged += OnBuildingVariationItemChanged;
70+
}
71+
SynchronizeBuildingComponentsToModel();
72+
NotifyBuildingChanged();
73+
}
74+
// Ignore ItemChanged events as they're handled by OnBuildingVariationItemChanged
4475
}
4576

46-
public override void CopyBackToModel()
77+
void OnBuildingVariationItemChanged(object? sender, ListChangedEventArgs e)
78+
{
79+
SynchronizeBuildingComponentsToModel();
80+
NotifyBuildingChanged();
81+
}
82+
83+
void OnBuildingHeightsChanged(object? sender, ListChangedEventArgs e)
84+
{
85+
SynchronizeBuildingComponentsToModel();
86+
NotifyBuildingChanged();
87+
}
88+
89+
void OnBuildingAnimationsChanged(object? sender, ListChangedEventArgs e)
90+
{
91+
SynchronizeBuildingComponentsToModel();
92+
NotifyBuildingChanged();
93+
}
94+
95+
void SynchronizeBuildingComponentsToModel()
4796
{
4897
Model.BuildingComponents.BuildingVariations = [.. BuildingVariations.Select(x => x.ToList())];
4998
Model.BuildingComponents.BuildingHeights = [.. BuildingHeights];
5099
Model.BuildingComponents.BuildingAnimations = [.. BuildingAnimations];
51100
}
52101

53-
void OnBuildingComponentChanged(object? sender, ListChangedEventArgs e)
102+
void NotifyBuildingChanged()
54103
{
55-
// When a new nested list is added to BuildingVariations, subscribe to it
56-
if (sender == BuildingVariations && e.ListChangedType == ListChangedType.ItemAdded)
57-
{
58-
BuildingVariations[e.NewIndex].ListChanged += OnBuildingComponentChanged;
59-
}
60-
61104
MessageBus.Current.SendMessage(new BuildingComponents()
62105
{
63106
BuildingAnimations = [.. BuildingAnimations],

0 commit comments

Comments
 (0)