Skip to content

Commit 88ac1a3

Browse files
committed
add saveas and open single object
1 parent d4b805b commit 88ac1a3

File tree

4 files changed

+91
-6
lines changed

4 files changed

+91
-6
lines changed

AvaGui/ViewModels/MainWindowViewModel.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public class MainWindowViewModel : ViewModelBase
5252

5353
public ReactiveCommand<Unit, Unit> LoadPalette { get; }
5454

55-
5655
public ReactiveCommand<Unit, Unit> OpenSettingsFolder { get; }
56+
public ReactiveCommand<Unit, Task> OpenSingleObject { get; }
5757

5858
public const string GithubApplicationName = "ObjectEditor";
5959
public const string GithubIssuePage = "https://github.com/OpenLoco/ObjectEditor/issues";
@@ -120,6 +120,7 @@ public MainWindowViewModel()
120120

121121
//LoadPalette = ReactiveCommand.Create(LoadPaletteFunc);
122122
OpenSettingsFolder = ReactiveCommand.Create(PlatformSpecific.FolderOpenInDesktop);
123+
OpenSingleObject = ReactiveCommand.Create(LoadSingleObjectToIndex);
123124

124125
#region Version
125126

@@ -142,6 +143,33 @@ public MainWindowViewModel()
142143
#endregion
143144
}
144145

146+
public async Task LoadSingleObjectToIndex()
147+
{
148+
var openFile = await PlatformSpecific.OpenFilePicker();
149+
if (openFile == null)
150+
{
151+
return;
152+
}
153+
154+
var path = openFile.SingleOrDefault()?.Path.AbsolutePath;
155+
if (path == null)
156+
{
157+
return;
158+
}
159+
160+
//logger?.Info($"Opening {path}");
161+
if (Model.TryGetObject(path, out UiLocoFile? uiLocoFile, true))
162+
{
163+
Model.Logger.Warning($"Successfully loaded {path}");
164+
ObjectEditorViewModel.CurrentlySelectedObject = new FileSystemItem(path, uiLocoFile.DatFileInfo.S5Header.Name, uiLocoFile.DatFileInfo.S5Header.SourceGame);
165+
ObjectEditorViewModel.CurrentObject = uiLocoFile;
166+
}
167+
else
168+
{
169+
Model.Logger.Warning($"Unable to load {path}");
170+
}
171+
}
172+
145173
static Version GetCurrentAppVersion(Assembly assembly)
146174
{
147175
// grab current appl version from assembly

AvaGui/ViewModels/ObjectEditorViewModel.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
using OpenLoco.ObjectEditor.Headers;
99
using System.Reactive;
1010
using OpenLoco.ObjectEditor.Logging;
11+
using System.Linq;
12+
using System.Threading.Tasks;
1113

1214
namespace AvaGui.ViewModels
1315
{
1416
public class ObjectEditorViewModel : ReactiveObject
1517
{
1618
public ReactiveCommand<Unit, Unit> ReloadObjectCommand { get; init; }
1719
public ReactiveCommand<Unit, Unit> SaveObjectCommand { get; init; }
20+
public ReactiveCommand<Unit, Task> SaveAsObjectCommand { get; init; }
1821

1922
[Reactive]
2023
public StringTableViewModel? StringTableViewModel { get; set; }
@@ -33,7 +36,7 @@ public UiLocoFile? CurrentObject
3336
set
3437
{
3538
Model.ObjectCache[CurrentlySelectedObject.Path] = value;
36-
this.RaiseAndSetIfChanged(ref _currentObject, value);
39+
_ = this.RaiseAndSetIfChanged(ref _currentObject, value);
3740
}
3841
}
3942

@@ -132,8 +135,8 @@ public void SaveCurrentObject()
132135
return;
133136
}
134137

135-
Logger?.Info($"[DISABLED] Saving {CurrentObject.DatFileInfo.S5Header.Name} to {CurrentlySelectedObject.Path}");
136-
//SawyerStreamWriter.Save(CurrentlySelectedObject.Path, CurrentObject.DatFileInfo.S5Header.Name, CurrentObject.LocoObject);
138+
Logger?.Info($"Saving {CurrentObject.DatFileInfo.S5Header.Name} to {CurrentlySelectedObject.Path}");
139+
SawyerStreamWriter.Save(CurrentlySelectedObject.Path, CurrentObject.DatFileInfo.S5Header.Name, CurrentObject.LocoObject);
137140
}
138141

139142
public void ReloadCurrentObject()
@@ -145,6 +148,24 @@ public void ReloadCurrentObject()
145148
}
146149
}
147150

151+
public async Task SaveAsCurrentObject()
152+
{
153+
var saveFile = await PlatformSpecific.SaveFilePicker();
154+
if (saveFile == null)
155+
{
156+
return;
157+
}
158+
159+
if (CurrentObject?.LocoObject == null)
160+
{
161+
Logger?.Error("Cannot save an object with a null loco object - the file would be empty!");
162+
return;
163+
}
164+
165+
Logger?.Info($"Saving {CurrentObject.DatFileInfo.S5Header.Name} to {saveFile.Path.AbsolutePath}");
166+
SawyerStreamWriter.Save(saveFile.Path.AbsolutePath, CurrentObject.DatFileInfo.S5Header.Name, CurrentObject.LocoObject);
167+
}
168+
148169
ILogger? Logger => Model.Logger;
149170

150171
public ObjectEditorViewModel(ObjectEditorModel model)
@@ -163,6 +184,7 @@ public ObjectEditorViewModel(ObjectEditorModel model)
163184

164185
ReloadObjectCommand = ReactiveCommand.Create(ReloadCurrentObject);
165186
SaveObjectCommand = ReactiveCommand.Create(SaveCurrentObject);
187+
SaveAsObjectCommand = ReactiveCommand.Create(SaveAsCurrentObject);
166188

167189
//_ = this.WhenAnyValue(o => o.CurrentObject)
168190
// .Subscribe(_ => this.RaisePropertyChanged(nameof(StringTableViewModel))); // done in SelectedObjectChanged()

AvaGui/ViewModels/PlatformSpecific.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,45 @@ public static async Task<IReadOnlyList<IStorageFolder>> OpenFolderPicker()
6363
throw new ArgumentNullException("ApplicationLifetime|StorageProvider", "Missing StorageProvider instance.");
6464
}
6565

66-
var folders = await provider.OpenFolderPickerAsync(new FolderPickerOpenOptions()
66+
return await provider.OpenFolderPickerAsync(new FolderPickerOpenOptions()
6767
{
6868
Title = "Select a folder containing objects",
6969
AllowMultiple = false
7070
});
71-
return folders;
71+
}
72+
73+
public static async Task<IReadOnlyList<IStorageFile>> OpenFilePicker()
74+
{
75+
// See IoCFileOps project for an example of how to accomplish this.
76+
if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop
77+
|| desktop.MainWindow?.StorageProvider is not { } provider)
78+
{
79+
throw new ArgumentNullException("ApplicationLifetime|StorageProvider", "Missing StorageProvider instance.");
80+
}
81+
82+
return await provider.OpenFilePickerAsync(new FilePickerOpenOptions()
83+
{
84+
Title = "Select a Locomotion object file",
85+
AllowMultiple = false,
86+
FileTypeFilter = [new("Locomotion DAT Files") { Patterns = ["*.dat", "*.DAT"] }]
87+
});
88+
}
89+
90+
public static async Task<IStorageFile?> SaveFilePicker()
91+
{
92+
// See IoCFileOps project for an example of how to accomplish this.
93+
if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop
94+
|| desktop.MainWindow?.StorageProvider is not { } provider)
95+
{
96+
throw new ArgumentNullException("ApplicationLifetime|StorageProvider", "Missing StorageProvider instance.");
97+
}
98+
99+
return await provider.SaveFilePickerAsync(new FilePickerSaveOptions()
100+
{
101+
ShowOverwritePrompt = true,
102+
DefaultExtension = "dat",
103+
Title = "Select a new file name",
104+
});
72105
}
73106
}
74107
}

AvaGui/Views/MainWindow.axaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<StackPanel Orientation="Horizontal">
6363
<Button Content="⭮ Reload" Command="{Binding ReloadObjectCommand}"/>
6464
<Button Content="💾 Save" Command="{Binding SaveObjectCommand}"/>
65+
<Button Content="💾 Save As" Command="{Binding SaveAsObjectCommand}"/>
6566
</StackPanel>
6667
<TabControl>
6768
<TabItem Header="Headers">
@@ -141,6 +142,7 @@
141142
<MenuItem Header="_File">
142143
<!--<MenuItem Header="Load Palette" Command="{Binding LoadPalette}" />-->
143144
<MenuItem Header="⚙ Open settings folder" Command="{Binding OpenSettingsFolder}" />
145+
<MenuItem Header="🚉 Open single object" Command="{Binding OpenSingleObject}" />
144146
</MenuItem>
145147
<MenuItem Header="_ObjData" ItemsSource="{Binding ObjDataItems}" Classes="SubItems" >
146148
<MenuItem.Styles>

0 commit comments

Comments
 (0)