Skip to content

Commit 3aed913

Browse files
authored
Image importing refactor (#87)
* move images out of viewmodel * fix many errors in image importing * working image importing * cleanup
1 parent 7df9a01 commit 3aed913

File tree

13 files changed

+224
-234
lines changed

13 files changed

+224
-234
lines changed

AvaGui/Assets/palette.png

2.61 KB
Loading

AvaGui/Models/G1ImageConversion.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Avalonia.Media.Imaging;
2+
using SkiaSharp;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
6+
namespace AvaGui.Models
7+
{
8+
public static class G1ImageConversion
9+
{
10+
public static IEnumerable<Bitmap> CreateAvaloniaImages(IEnumerable<SKBitmap> skBitmaps)
11+
{
12+
foreach (var bmp in skBitmaps)
13+
{
14+
yield return CreateAvaloniaImage(bmp);
15+
}
16+
}
17+
18+
public static Bitmap CreateAvaloniaImage(SKBitmap skBitmap)
19+
{
20+
using (var stream = new MemoryStream())
21+
{
22+
_ = skBitmap.Encode(stream, SKEncodedImageFormat.Png, 100);
23+
stream.Position = 0;
24+
return new Bitmap(stream);
25+
}
26+
}
27+
}
28+
}

AvaGui/Models/ObjectEditorModel.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using OpenLoco.Dat.Types;
1010
using OpenLoco.Definitions.DTO;
1111
using OpenLoco.Definitions.Web;
12+
using SkiaSharp;
1213
using System;
1314
using System.Collections.Generic;
1415
using System.Collections.ObjectModel;
@@ -167,6 +168,7 @@ public bool TryLoadObject(FileSystemItem filesystemItem, out UiLocoFile? uiLocoF
167168
ILocoObject? locoObject = null;
168169
MetadataModel? metadata = null;
169170
uiLocoFile = null;
171+
List<SKBitmap> images = [];
170172

171173
try
172174
{
@@ -203,6 +205,11 @@ public bool TryLoadObject(FileSystemItem filesystemItem, out UiLocoFile? uiLocoF
203205
Logger.Debug($"Found object {filesystemItem.Name} with unique id {uniqueObjectId} in cache - reusing it");
204206
}
205207

208+
if (locoObj.OriginalBytes == null)
209+
{
210+
Logger.Error($"Received no data for {filesystemItem.Name}");
211+
return false;
212+
}
206213
var obj = SawyerStreamReader.LoadFullObjectFromStream(Convert.FromBase64String(locoObj.OriginalBytes), $"{filesystemItem.Filename}-{filesystemItem.Name}", true, Logger);
207214
if (obj == null)
208215
{
@@ -224,6 +231,11 @@ public bool TryLoadObject(FileSystemItem filesystemItem, out UiLocoFile? uiLocoF
224231
Availability = locoObj.Availability,
225232
Licence = locoObj.Licence,
226233
};
234+
235+
foreach (var i in locoObject?.G1Elements)
236+
{
237+
images.Add(PaletteMap.ConvertG1IndexedToRgb32Bitmap(i));
238+
}
227239
}
228240
else
229241
{
@@ -234,6 +246,11 @@ public bool TryLoadObject(FileSystemItem filesystemItem, out UiLocoFile? uiLocoF
234246
fileInfo = obj.Value.DatFileInfo;
235247
locoObject = obj.Value.LocoObject;
236248
metadata = null; // todo: look this up from internet anyways
249+
250+
foreach (var i in locoObject?.G1Elements)
251+
{
252+
images.Add(PaletteMap.ConvertG1IndexedToRgb32Bitmap(i));
253+
}
237254
}
238255
}
239256
}
@@ -251,7 +268,7 @@ public bool TryLoadObject(FileSystemItem filesystemItem, out UiLocoFile? uiLocoF
251268
return false;
252269
}
253270

254-
uiLocoFile = new UiLocoFile() { DatFileInfo = fileInfo, LocoObject = locoObject, Metadata = metadata };
271+
uiLocoFile = new UiLocoFile() { DatFileInfo = fileInfo, LocoObject = locoObject, Metadata = metadata, Images = images };
255272
return true;
256273
}
257274

AvaGui/Models/UiLocoFile.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
using OpenLoco.Common;
21
using OpenLoco.Dat.Types;
2+
using SkiaSharp;
3+
using System.Collections.Generic;
34
using System.ComponentModel;
45

56
namespace AvaGui.Models
@@ -9,6 +10,7 @@ public class UiLocoFile : IUiObject
910
{
1011
public required DatFileInfo DatFileInfo { get; set; }
1112
public ILocoObject? LocoObject { get; set; }
13+
public IList<SKBitmap> Images { get; set; } = [];
1214
public MetadataModel? Metadata { get; set; }
1315
}
1416
}

AvaGui/ViewModels/DatTypes/DatObjectEditorViewModel.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ public DatObjectEditorViewModel(FileSystemItem currentFile, ObjectEditorModel mo
7979

8080
_ = this.WhenAnyValue(o => o.CurrentlySelectedHexAnnotation)
8181
.Subscribe(_ => UpdateHexDumpView());
82-
83-
_ = this.WhenAnyValue(o => o.CurrentFrame)
84-
.Subscribe(_ => this.RaisePropertyChanged(nameof(CurrentAnimationFrame)));
8582
}
8683

8784
public void UpdateHexDumpView()
@@ -120,18 +117,16 @@ public void LoadObject()
120117
StringTableViewModel = new(CurrentObject.LocoObject.StringTable);
121118
ExtraContentViewModel = CurrentObject.LocoObject.Object is SoundObject
122119
? new SoundViewModel(CurrentObject.LocoObject)
123-
: new ImageTableViewModel(CurrentObject.LocoObject, Model.PaletteMap);
120+
: new ImageTableViewModel(CurrentObject.LocoObject, Model.PaletteMap, CurrentObject.Images);
124121

125-
var (treeView, annotationIdentifiers) = AnnotateFile(cf.Filename, false, null);
122+
var (treeView, annotationIdentifiers) = AnnotateFile(Path.Combine(Model.Settings.ObjDataDirectory, cf.Filename), false, null);
126123
CurrentHexAnnotations = new(treeView);
127124
DATDumpAnnotationIdentifiers = annotationIdentifiers;
128-
CurrentAnimations = [new AnimationSequence("Normal", 0, 63)];
129125
}
130126
else
131127
{
132128
StringTableViewModel = null;
133129
ExtraContentViewModel = null;
134-
CurrentAnimations = [];
135130
}
136131
}
137132
else

AvaGui/ViewModels/MainWindowViewModel.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
using OpenLoco.Dat;
77
using ReactiveUI;
88
using ReactiveUI.Fody.Helpers;
9-
using SixLabors.ImageSharp;
10-
using SixLabors.ImageSharp.PixelFormats;
9+
using SkiaSharp;
1110
using System;
1211
using System.Collections.ObjectModel;
1312
using System.IO;
@@ -62,7 +61,7 @@ public class MainWindowViewModel : ViewModelBase
6261
public MainWindowViewModel()
6362
{
6463
var paletteUri = new Uri("avares://ObjectEditor/Assets/palette.png");
65-
var palette = Image.Load<Rgb24>(AssetLoader.Open(paletteUri));
64+
var palette = SKBitmap.Decode(AssetLoader.Open(paletteUri));
6665

6766
Model = new()
6867
{
@@ -93,14 +92,25 @@ public MainWindowViewModel()
9392
.Subscribe(_ => this.RaisePropertyChanged(nameof(WindowTitle)));
9493

9594
var assembly = Assembly.GetExecutingAssembly();
95+
9696
ApplicationVersion = GetCurrentAppVersion(assembly);
9797

98-
// check for new version
99-
var latestVersion = GetLatestAppVersion();
100-
if (latestVersion > ApplicationVersion)
98+
#if !DEBUG
99+
100+
try
101101
{
102-
LatestVersionText = $"newer version exists: {latestVersion}";
102+
var latestVersion = GetLatestAppVersion();
103+
if (latestVersion > ApplicationVersion)
104+
{
105+
LatestVersionText = $"newer version exists: {latestVersion}";
106+
}
103107
}
108+
catch (Exception ex)
109+
{
110+
Model.Logger.Error(ex);
111+
}
112+
#endif
113+
104114
#endregion
105115
}
106116

@@ -170,7 +180,7 @@ Version GetLatestAppVersion()
170180
}
171181

172182
#pragma warning disable CA2201 // Do not raise reserved exception types
173-
throw new Exception("Unable to get latest version");
183+
throw new Exception($"Unable to get latest version. Error={response.StatusCode}");
174184
#pragma warning restore CA2201 // Do not raise reserved exception types
175185
}
176186

0 commit comments

Comments
 (0)