@@ -21,7 +21,9 @@ namespace ZXBasicStudio.DocumentEditors.ZXGraphics
2121 public partial class ColorPickerControl : UserControl
2222 {
2323 private GraphicsModes graphicMode = GraphicsModes . Monochrome ;
24-
24+ private Action < string , int > callBackCommand = null ;
25+ private PaletteColor [ ] palette = null ;
26+ private int selectedIndexColor = 0 ;
2527
2628 /// <summary>
2729 /// Constructor
@@ -32,19 +34,23 @@ public ColorPickerControl()
3234 }
3335
3436
35- public bool Inicialize ( GraphicsModes graphicMode , PaletteColor [ ] palette , int primaryColor , int secondaryColor , bool bright , bool flash )
37+ public bool Inicialize ( GraphicsModes graphicMode , PaletteColor [ ] palette , int selectedIndexColor , Action < string , int > callBackCommand )
3638 {
3739 this . graphicMode = graphicMode ;
40+ this . palette = palette ;
41+ this . callBackCommand = callBackCommand ;
42+ this . selectedIndexColor = selectedIndexColor ;
3843
39- grdMonochrome . IsVisible = true ;
40- grdZXSpectrum . IsVisible = true ;
41- grdNext . IsVisible = true ;
44+ grdMonochrome . IsVisible = false ;
45+ grdZXSpectrum . IsVisible = false ;
46+ grdNext . IsVisible = false ;
4247 switch ( graphicMode )
4348 {
4449 case GraphicsModes . Monochrome :
4550 grdMonochrome . IsVisible = true ;
4651 break ;
4752 case GraphicsModes . ZXSpectrum :
53+ DrawZXSpectrum ( ) ;
4854 grdZXSpectrum . IsVisible = true ;
4955 break ;
5056 case GraphicsModes . Next :
@@ -55,5 +61,76 @@ public bool Inicialize(GraphicsModes graphicMode, PaletteColor[] palette, int pr
5561
5662 return true ;
5763 }
64+
65+ /// <summary>
66+ /// Draw colors for ZXSpectrum palette
67+ /// </summary>
68+ private void DrawZXSpectrum ( )
69+ {
70+ cnvZXSpectrum . Children . Clear ( ) ;
71+ // Draw all colors
72+ int index = 0 ;
73+ for ( int y = 0 ; y < 2 ; y ++ )
74+ {
75+ for ( int x = 0 ; x < 8 ; x ++ )
76+ {
77+ var p = palette [ index ] ;
78+ var b = new SolidColorBrush ( new Color ( 255 , p . Red , p . Green , p . Blue ) ) ;
79+ DrawRectangle ( cnvZXSpectrum , x * 20 , y * 20 , 18 , 18 , Brushes . White , b , index ) ;
80+ index ++ ;
81+
82+ }
83+ }
84+
85+ // Draw selected color (ink)
86+ {
87+ int y = selectedIndexColor / 8 ;
88+ int x = selectedIndexColor % 8 ;
89+ DrawRectangle ( cnvZXSpectrum , ( x * 20 ) - 2 , ( y * 20 ) - 2 , 22 , 22 , Brushes . Red , null , - 1 ) ;
90+ }
91+ }
92+
93+
94+ private void DrawRectangle ( Canvas canvas , int x , int y , int w , int h , IBrush ? borderBrush , IBrush ? colorBrush , int tag )
95+ {
96+ var r = new Rectangle ( ) ;
97+ r . Width = w ;
98+ r . Height = h ;
99+ r . Tag = tag . ToString ( ) ;
100+ if ( borderBrush != null )
101+ {
102+ r . Stroke = borderBrush ;
103+ r . StrokeThickness = 1 ;
104+ }
105+ if ( colorBrush != null )
106+ {
107+ r . Fill = colorBrush ;
108+ }
109+ canvas . Children . Add ( r ) ;
110+ Canvas . SetTop ( r , y ) ;
111+ Canvas . SetLeft ( r , x ) ;
112+
113+ r . Tapped += R_Tapped ;
114+ }
115+
116+
117+ private void R_Tapped ( object ? sender , TappedEventArgs e )
118+ {
119+ if ( sender == null )
120+ {
121+ return ;
122+ }
123+
124+ Rectangle r = ( Rectangle ) sender ;
125+
126+ int color = r . Tag . ToInteger ( ) ;
127+ if ( color >= 0 )
128+ {
129+ selectedIndexColor = color ;
130+ this . IsVisible = false ;
131+
132+ callBackCommand ? . Invoke ( "SELECT" , selectedIndexColor ) ;
133+ }
134+ }
58135 }
59136}
0 commit comments