26
26
27
27
namespace OpenLoco . Gui . ViewModels
28
28
{
29
- public enum ColourSwatches
30
- {
31
- Black ,
32
- Bronze ,
33
- Copper ,
34
- Yellow ,
35
- Rose ,
36
- GrassGreen ,
37
- AvocadoGreen ,
38
- Green ,
39
- Brass ,
40
- Lavender ,
41
- Blue ,
42
- SeaGreen ,
43
- Purple ,
44
- Red ,
45
- Orange ,
46
- Teal ,
47
- Brown ,
48
- Amber ,
49
- MiscGrey ,
50
- MiscYellow ,
51
- PrimaryRemap ,
52
- SecondaryRemap ,
53
- }
54
-
55
29
public record SpriteOffset (
56
30
[ property: JsonPropertyName ( "path" ) ] string Path ,
57
31
[ property: JsonPropertyName ( "x" ) ] int16_t X ,
@@ -66,10 +40,13 @@ public class ImageTableViewModel : ReactiveObject, IExtraContentViewModel
66
40
readonly IImageTableNameProvider NameProvider ;
67
41
readonly ILogger Logger ;
68
42
69
- public ColourSwatches [ ] ColourSwatchesArr { get ; } = ( ColourSwatches [ ] ) Enum . GetValues ( typeof ( ColourSwatches ) ) ;
43
+ public ColourRemapSwatch [ ] ColourSwatchesArr { get ; } = Enum . GetValues < ColourRemapSwatch > ( ) ;
70
44
71
45
[ Reactive ]
72
- public ColourSwatches SelectedColourSwatch { get ; set ; } = ColourSwatches . PrimaryRemap ;
46
+ public ColourRemapSwatch SelectedPrimarySwatch { get ; set ; } = ColourRemapSwatch . PrimaryRemap ;
47
+
48
+ [ Reactive ]
49
+ public ColourRemapSwatch SelectedSecondarySwatch { get ; set ; } = ColourRemapSwatch . SecondaryRemap ;
73
50
74
51
readonly DispatcherTimer animationTimer ;
75
52
int currentFrameIndex ;
@@ -100,9 +77,6 @@ public Avalonia.Size SelectedBitmapPreviewBorder
100
77
[ Reactive ]
101
78
public ICommand CropAllImagesCommand { get ; set ; }
102
79
103
- [ Reactive ]
104
- public int Zoom { get ; set ; } = 1 ;
105
-
106
80
// where the actual image data is stored
107
81
[ Reactive ]
108
82
public IList < Image < Rgba32 > > Images { get ; set ; }
@@ -118,7 +92,7 @@ public Avalonia.Size SelectedBitmapPreviewBorder
118
92
public SelectionModel < Bitmap > SelectionModel { get ; set ; }
119
93
120
94
public UIG1Element32 ? SelectedG1Element
121
- => SelectedImageIndex == - 1 || G1Provider . G1Elements . Count == 0
95
+ => SelectedImageIndex == - 1 || G1Provider . G1Elements . Count == 0 || SelectedImageIndex >= G1Provider . G1Elements . Count
122
96
? null
123
97
: new UIG1Element32 ( SelectedImageIndex , GetImageName ( NameProvider , SelectedImageIndex ) , G1Provider . G1Elements [ SelectedImageIndex ] ) ;
124
98
@@ -143,10 +117,12 @@ public ImageTableViewModel(IHasG1Elements g1ElementProvider, IImageTableNameProv
143
117
. Subscribe ( _ => this . RaisePropertyChanged ( nameof ( Images ) ) ) ;
144
118
_ = this . WhenAnyValue ( o => o . PaletteMap )
145
119
. Subscribe ( _ => this . RaisePropertyChanged ( nameof ( Images ) ) ) ;
146
- _ = this . WhenAnyValue ( o => o . Zoom )
147
- . Subscribe ( _ => this . RaisePropertyChanged ( nameof ( Images ) ) ) ;
120
+ _ = this . WhenAnyValue ( o => o . SelectedPrimarySwatch ) . Skip ( 1 )
121
+ . Subscribe ( _ => RecalcImages ( ) ) ;
122
+ _ = this . WhenAnyValue ( o => o . SelectedSecondarySwatch ) . Skip ( 1 )
123
+ . Subscribe ( _ => RecalcImages ( ) ) ;
148
124
_ = this . WhenAnyValue ( o => o . Images )
149
- . Subscribe ( _ => Bitmaps = new ObservableCollection < Bitmap ? > ( G1ImageConversion . CreateAvaloniaImages ( Images ) ) ) ;
125
+ . Subscribe ( _ => Bitmaps = [ .. G1ImageConversion . CreateAvaloniaImages ( Images ) ] ) ;
150
126
_ = this . WhenAnyValue ( o => o . SelectedImageIndex )
151
127
. Subscribe ( _ => this . RaisePropertyChanged ( nameof ( SelectedG1Element ) ) ) ;
152
128
_ = this . WhenAnyValue ( o => o . SelectedG1Element )
@@ -323,7 +299,7 @@ void LoadSprite(string filename, uint imageOffset, short xOffset, short yOffset,
323
299
324
300
var newElement = new G1Element32 ( imageOffset , ( int16_t ) img . Width , ( int16_t ) img . Height , xOffset , yOffset , flags , zoomOffset )
325
301
{
326
- ImageData = PaletteMap . ConvertRgba32ImageToG1Data ( img , flags )
302
+ ImageData = PaletteMap . ConvertRgba32ImageToG1Data ( img , flags , SelectedPrimarySwatch , SelectedSecondarySwatch )
327
303
} ;
328
304
329
305
G1Provider . G1Elements . Add ( newElement ) ;
@@ -332,6 +308,34 @@ void LoadSprite(string filename, uint imageOffset, short xOffset, short yOffset,
332
308
}
333
309
}
334
310
311
+ public void RecalcImages ( )
312
+ {
313
+ // unfortunately no way to "copy" the selection from old to new
314
+ SelectionModel . Clear ( ) ;
315
+
316
+ // clear existing images
317
+ Logger . Info ( "Clearing current G1Element32s and existing object images" ) ;
318
+ Images . Clear ( ) ;
319
+ Bitmaps . Clear ( ) ;
320
+
321
+ foreach ( var g1 in G1Provider . G1Elements )
322
+ {
323
+ if ( PaletteMap . TryConvertG1ToRgba32Bitmap ( g1 , SelectedPrimarySwatch , SelectedSecondarySwatch , out var img ) && img != null )
324
+ {
325
+ Images . Add ( img ) ;
326
+ Bitmaps . Add ( G1ImageConversion . CreateAvaloniaImage ( img ) ) ;
327
+ }
328
+ else
329
+ {
330
+ Logger . Error ( "Unable to convert G1 to image" ) ;
331
+ }
332
+ }
333
+
334
+ this . RaisePropertyChanged ( nameof ( Bitmaps ) ) ;
335
+ this . RaisePropertyChanged ( nameof ( Images ) ) ;
336
+ SelectedImageIndex = - 1 ;
337
+ }
338
+
335
339
// todo: second half should be in model
336
340
public async Task ExportImages ( )
337
341
{
@@ -458,7 +462,7 @@ void UpdateImage(Image<Rgba32> img, int index, short? xOffset, short? yOffset)
458
462
Width = ( int16_t ) img . Width ,
459
463
Height = ( int16_t ) img . Height ,
460
464
Flags = currG1 . Flags ,
461
- ImageData = PaletteMap . ConvertRgba32ImageToG1Data ( img , currG1 . Flags ) ,
465
+ ImageData = PaletteMap . ConvertRgba32ImageToG1Data ( img , currG1 . Flags , SelectedPrimarySwatch , SelectedSecondarySwatch ) ,
462
466
XOffset = xOffset ?? currG1 . XOffset ,
463
467
YOffset = yOffset ?? currG1 . YOffset ,
464
468
} ;
0 commit comments