2626
2727namespace OpenLoco . Gui . ViewModels
2828{
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-
5529 public record SpriteOffset (
5630 [ property: JsonPropertyName ( "path" ) ] string Path ,
5731 [ property: JsonPropertyName ( "x" ) ] int16_t X ,
@@ -66,10 +40,13 @@ public class ImageTableViewModel : ReactiveObject, IExtraContentViewModel
6640 readonly IImageTableNameProvider NameProvider ;
6741 readonly ILogger Logger ;
6842
69- public ColourSwatches [ ] ColourSwatchesArr { get ; } = ( ColourSwatches [ ] ) Enum . GetValues ( typeof ( ColourSwatches ) ) ;
43+ public ColourRemapSwatch [ ] ColourSwatchesArr { get ; } = Enum . GetValues < ColourRemapSwatch > ( ) ;
7044
7145 [ 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 ;
7350
7451 readonly DispatcherTimer animationTimer ;
7552 int currentFrameIndex ;
@@ -100,9 +77,6 @@ public Avalonia.Size SelectedBitmapPreviewBorder
10077 [ Reactive ]
10178 public ICommand CropAllImagesCommand { get ; set ; }
10279
103- [ Reactive ]
104- public int Zoom { get ; set ; } = 1 ;
105-
10680 // where the actual image data is stored
10781 [ Reactive ]
10882 public IList < Image < Rgba32 > > Images { get ; set ; }
@@ -118,7 +92,7 @@ public Avalonia.Size SelectedBitmapPreviewBorder
11892 public SelectionModel < Bitmap > SelectionModel { get ; set ; }
11993
12094 public UIG1Element32 ? SelectedG1Element
121- => SelectedImageIndex == - 1 || G1Provider . G1Elements . Count == 0
95+ => SelectedImageIndex == - 1 || G1Provider . G1Elements . Count == 0 || SelectedImageIndex >= G1Provider . G1Elements . Count
12296 ? null
12397 : new UIG1Element32 ( SelectedImageIndex , GetImageName ( NameProvider , SelectedImageIndex ) , G1Provider . G1Elements [ SelectedImageIndex ] ) ;
12498
@@ -143,10 +117,12 @@ public ImageTableViewModel(IHasG1Elements g1ElementProvider, IImageTableNameProv
143117 . Subscribe ( _ => this . RaisePropertyChanged ( nameof ( Images ) ) ) ;
144118 _ = this . WhenAnyValue ( o => o . PaletteMap )
145119 . 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 ( ) ) ;
148124 _ = this . WhenAnyValue ( o => o . Images )
149- . Subscribe ( _ => Bitmaps = new ObservableCollection < Bitmap ? > ( G1ImageConversion . CreateAvaloniaImages ( Images ) ) ) ;
125+ . Subscribe ( _ => Bitmaps = [ .. G1ImageConversion . CreateAvaloniaImages ( Images ) ] ) ;
150126 _ = this . WhenAnyValue ( o => o . SelectedImageIndex )
151127 . Subscribe ( _ => this . RaisePropertyChanged ( nameof ( SelectedG1Element ) ) ) ;
152128 _ = this . WhenAnyValue ( o => o . SelectedG1Element )
@@ -323,7 +299,7 @@ void LoadSprite(string filename, uint imageOffset, short xOffset, short yOffset,
323299
324300 var newElement = new G1Element32 ( imageOffset , ( int16_t ) img . Width , ( int16_t ) img . Height , xOffset , yOffset , flags , zoomOffset )
325301 {
326- ImageData = PaletteMap . ConvertRgba32ImageToG1Data ( img , flags )
302+ ImageData = PaletteMap . ConvertRgba32ImageToG1Data ( img , flags , SelectedPrimarySwatch , SelectedSecondarySwatch )
327303 } ;
328304
329305 G1Provider . G1Elements . Add ( newElement ) ;
@@ -332,6 +308,34 @@ void LoadSprite(string filename, uint imageOffset, short xOffset, short yOffset,
332308 }
333309 }
334310
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+
335339 // todo: second half should be in model
336340 public async Task ExportImages ( )
337341 {
@@ -458,7 +462,7 @@ void UpdateImage(Image<Rgba32> img, int index, short? xOffset, short? yOffset)
458462 Width = ( int16_t ) img . Width ,
459463 Height = ( int16_t ) img . Height ,
460464 Flags = currG1 . Flags ,
461- ImageData = PaletteMap . ConvertRgba32ImageToG1Data ( img , currG1 . Flags ) ,
465+ ImageData = PaletteMap . ConvertRgba32ImageToG1Data ( img , currG1 . Flags , SelectedPrimarySwatch , SelectedSecondarySwatch ) ,
462466 XOffset = xOffset ?? currG1 . XOffset ,
463467 YOffset = yOffset ?? currG1 . YOffset ,
464468 } ;
0 commit comments