Skip to content

Commit c275b7c

Browse files
committed
cleanup object-main-folder viewmodel interaction
1 parent 9b0f599 commit c275b7c

File tree

4 files changed

+40
-48
lines changed

4 files changed

+40
-48
lines changed

AvaGui/Models/UiLocoFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace AvaGui.Models
66
[TypeConverter(typeof(ExpandableObjectConverter))]
77
public class UiLocoFile : IUiObject
88
{
9-
public DatFileInfo? DatFileInfo { get; set; }
9+
public required DatFileInfo DatFileInfo { get; set; }
1010
public ILocoObject? LocoObject { get; set; }
1111
}
1212
}

AvaGui/ViewModels/DatTypes/ObjectEditorViewModel.cs

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using ReactiveUI;
22
using AvaGui.Models;
33
using OpenLoco.ObjectEditor.DatFileParsing;
4-
using System;
54
using ReactiveUI.Fody.Helpers;
65
using System.Reactive;
76
using OpenLoco.ObjectEditor.Logging;
@@ -22,60 +21,59 @@ public class ObjectEditorViewModel : ReactiveObject, ILocoFileViewModel
2221
[Reactive]
2322
public IExtraContentViewModel? ExtraContentViewModel { get; set; }
2423

25-
ObjectEditorModel Model { get; }
24+
ObjectEditorModel Model { get; init; }
2625

2726
[Reactive]
28-
public UiLocoFile? CurrentObject { private set; get; }
27+
public UiLocoFile? CurrentObject { get; private set; }
2928

3029
[Reactive]
31-
public FileSystemItemBase? CurrentFile { get; set; }
30+
public FileSystemItemBase CurrentFile { get; init; }
3231

33-
public ObjectEditorViewModel(ObjectEditorModel model)
32+
ILogger? Logger => Model.Logger;
33+
34+
public ObjectEditorViewModel(FileSystemItemBase currentFile, ObjectEditorModel model)
3435
{
36+
CurrentFile = currentFile;
3537
Model = model;
36-
_ = this.WhenAnyValue(o => o.CurrentFile)
37-
.Subscribe(_ => SelectedObjectChanged());
3838

39-
ReloadObjectCommand = ReactiveCommand.Create(ReloadCurrentObject);
39+
LoadObject();
40+
41+
ReloadObjectCommand = ReactiveCommand.Create(LoadObject);
4042
SaveObjectCommand = ReactiveCommand.Create(SaveCurrentObject);
4143
SaveAsObjectCommand = ReactiveCommand.Create(SaveAsCurrentObject);
4244
}
4345

44-
public void SelectedObjectChanged()
46+
public void LoadObject()
4547
{
4648
// this stops any currently-playing sounds
4749
if (ExtraContentViewModel is SoundViewModel svm)
4850
{
4951
svm.Dispose();
5052
}
5153

52-
ReloadCurrentObject();
53-
54-
if (CurrentObject?.LocoObject != null)
55-
{
56-
StringTableViewModel = new(CurrentObject.LocoObject.StringTable);
57-
ExtraContentViewModel = CurrentObject.LocoObject.Object is SoundObject
58-
? new SoundViewModel(CurrentObject.LocoObject)
59-
: new ImageTableViewModel(CurrentObject.LocoObject, Model.PaletteMap);
60-
}
61-
else
62-
{
63-
StringTableViewModel = null;
64-
ExtraContentViewModel = null;
65-
}
66-
}
67-
68-
public void ReloadCurrentObject()
69-
{
7054
if (CurrentFile == null)
7155
{
7256
return;
7357
}
7458

75-
Logger?.Info($"Loading {CurrentObject?.DatFileInfo.S5Header.Name} from {CurrentFile.Path}");
59+
Logger?.Info($"Loading {CurrentFile.Name} from {CurrentFile.Path}");
60+
7661
if (Model.TryLoadObject(CurrentFile.Path, out var newObj))
7762
{
7863
CurrentObject = newObj;
64+
65+
if (CurrentObject?.LocoObject != null)
66+
{
67+
StringTableViewModel = new(CurrentObject.LocoObject.StringTable);
68+
ExtraContentViewModel = CurrentObject.LocoObject.Object is SoundObject
69+
? new SoundViewModel(CurrentObject.LocoObject)
70+
: new ImageTableViewModel(CurrentObject.LocoObject, Model.PaletteMap);
71+
}
72+
else
73+
{
74+
StringTableViewModel = null;
75+
ExtraContentViewModel = null;
76+
}
7977
}
8078
else
8179
{
@@ -113,7 +111,5 @@ public void SaveAsCurrentObject()
113111
Logger?.Info($"Saving {CurrentObject.DatFileInfo.S5Header.Name} to {saveFile.Path.AbsolutePath}");
114112
SawyerStreamWriter.Save(saveFile.Path.AbsolutePath, CurrentObject.DatFileInfo.S5Header.Name, CurrentObject.LocoObject);
115113
}
116-
117-
ILogger? Logger => Model.Logger;
118114
}
119115
}

AvaGui/ViewModels/FolderTreeViewModel.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,12 @@ public FolderTreeViewModel(ObjectEditorModel model)
2222
{
2323
Model = model;
2424
Progress = new();
25-
Progress.ProgressChanged += (a, b) =>
26-
{
27-
IndexingProgress = b;
28-
};
25+
Progress.ProgressChanged += (_, progress) => IndexingProgress = progress;
2926

3027
RecreateIndex = ReactiveCommand.Create(async () => await LoadObjDirectoryAsync(CurrentDirectory, false));
3128

3229
_ = this.WhenAnyValue(o => o.CurrentDirectory)
33-
.Subscribe(async _ => await LoadObjDirectoryAsync(CurrentDirectory, true));
30+
.Subscribe(async _ => await LoadObjDirectoryAsync(CurrentDirectory, false));
3431
_ = this.WhenAnyValue(o => o.DisplayVanillaOnly)
3532
.Subscribe(async _ => await LoadObjDirectoryAsync(CurrentDirectory, true));
3633
_ = this.WhenAnyValue(o => o.FilenameFilter)

AvaGui/ViewModels/MainWindowViewModel.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public class MainWindowViewModel : ViewModelBase
3737

3838
public FolderTreeViewModel FolderTreeViewModel { get; }
3939

40-
public ObjectEditorViewModel ObjectEditorViewModel { get; }
4140
public SCV5ViewModel SCV5ViewModel { get; }
4241

4342
[Reactive]
@@ -78,16 +77,9 @@ public MainWindowViewModel()
7877
};
7978

8079
FolderTreeViewModel = new FolderTreeViewModel(Model);
81-
ObjectEditorViewModel = new ObjectEditorViewModel(Model);
82-
83-
_ = ObjectEditorViewModel.WhenAnyValue(o => o.CurrentFile)
84-
.Subscribe(_ => CurrentEditorModel = ObjectEditorViewModel);
85-
86-
_ = this.WhenAnyValue(o => o.ObjectEditorViewModel)
87-
.Subscribe(_ => CurrentEditorModel = ObjectEditorViewModel);
8880

8981
_ = FolderTreeViewModel.WhenAnyValue(o => o.CurrentlySelectedObject)
90-
.Subscribe(o => ObjectEditorViewModel.CurrentFile = o);
82+
.Subscribe(SetObjectViewModel);
9183

9284
ObjDataItems = new ObservableCollection<MenuItemModel>(Model.Settings.ObjDataDirectories
9385
.Select(x => new MenuItemModel(
@@ -118,6 +110,14 @@ public MainWindowViewModel()
118110
#endregion
119111
}
120112

113+
void SetObjectViewModel(FileSystemItemBase? file)
114+
{
115+
if (file != null)
116+
{
117+
CurrentEditorModel = new ObjectEditorViewModel(file, Model);
118+
}
119+
}
120+
121121
public async Task LoadSingleObjectToIndex()
122122
{
123123
var openFile = await PlatformSpecific.OpenFilePicker();
@@ -136,7 +136,8 @@ public async Task LoadSingleObjectToIndex()
136136
if (Model.TryLoadObject(path, out var uiLocoFile))
137137
{
138138
Model.Logger.Warning($"Successfully loaded {path}");
139-
ObjectEditorViewModel.CurrentFile = new FileSystemItem(path, uiLocoFile!.DatFileInfo.S5Header.Name, uiLocoFile.DatFileInfo.S5Header.SourceGame);
139+
var file = new FileSystemItem(path, uiLocoFile!.DatFileInfo.S5Header.Name, uiLocoFile.DatFileInfo.S5Header.SourceGame);
140+
SetObjectViewModel(file);
140141
}
141142
else
142143
{
@@ -191,8 +192,6 @@ public async Task SelectNewFolder()
191192
if (Directory.Exists(dirPath) && !Model.Settings.ObjDataDirectories.Contains(dirPath))
192193
{
193194
FolderTreeViewModel.CurrentDirectory = dirPath; // this will cause the reindexing
194-
//var progress = new Progress<float>();
195-
//await Model.LoadObjDirectoryAsync(dirPath, progress, false);
196195
var menuItem = new MenuItemModel(
197196
dirPath,
198197
ReactiveCommand.Create(() => FolderTreeViewModel.CurrentDirectory = dirPath)

0 commit comments

Comments
 (0)