@@ -94,26 +94,24 @@ public override void Render(DrawingContext context)
94
94
95
95
// Color table.
96
96
{
97
+ // Colors
97
98
for ( int i = 0 ; i < 6 ; i ++ )
98
99
{
99
100
for ( int j = 0 ; j < 8 ; j ++ )
100
- {
101
- var idx = i * 8 + j ;
102
- var x = j * 32.0 ;
103
- var y = i * 32.0 ;
104
- context . FillRectangle ( new SolidColorBrush ( COLOR_TABLE [ i , j ] ) , new Rect ( x , y , 32 , 32 ) ) ;
105
-
106
- if ( idx == _hightlightedTableElement )
107
- context . DrawRectangle ( new Pen ( Brushes . White , 2 ) , new Rect ( x + 2 , y + 2 , 28 , 28 ) ) ;
108
- }
101
+ context . FillRectangle ( new SolidColorBrush ( COLOR_TABLE [ i , j ] ) , new Rect ( j * 32 , i * 32 , 32 , 32 ) ) ;
109
102
}
110
103
104
+ // Borders
111
105
var border = this . FindResource ( "Brush.Border0" ) as IBrush ;
112
106
var pen = new Pen ( border , 0.4 ) ;
113
107
for ( int i = 1 ; i < 6 ; i ++ )
114
108
context . DrawLine ( pen , new Point ( 0 , i * 32 ) , new Point ( 256 , i * 32 ) ) ;
115
109
for ( int j = 1 ; j < 8 ; j ++ )
116
110
context . DrawLine ( pen , new Point ( j * 32 , 0 ) , new Point ( j * 32 , 192 ) ) ;
111
+
112
+ // Selected
113
+ if ( _hightlightedTableRect is { } rect )
114
+ context . DrawRectangle ( new Pen ( Brushes . White , 2 ) , rect ) ;
117
115
}
118
116
119
117
// Palette picker
@@ -142,6 +140,12 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
142
140
}
143
141
}
144
142
143
+ protected override void OnDataContextChanged ( EventArgs e )
144
+ {
145
+ base . OnDataContextChanged ( e ) ;
146
+ _hightlightedTableRect = null ;
147
+ }
148
+
145
149
protected override Size MeasureOverride ( Size availableSize )
146
150
{
147
151
return new Size ( 256 , 256 ) ;
@@ -154,12 +158,12 @@ protected override void OnPointerPressed(PointerPressedEventArgs e)
154
158
var p = e . GetPosition ( this ) ;
155
159
if ( _colorTableRect . Contains ( p ) )
156
160
{
157
- int col = ( int ) Math . Floor ( p . X / 32.0 ) ;
158
- int row = ( int ) Math . Floor ( p . Y / 32.0 ) ;
159
- int idx = row * 8 + col ;
160
- if ( _hightlightedTableElement != idx )
161
+ var col = ( int ) Math . Floor ( p . X / 32.0 ) ;
162
+ var row = ( int ) Math . Floor ( p . Y / 32.0 ) ;
163
+ var rect = new Rect ( col * 32 + 2 , row * 32 + 2 , 28 , 28 ) ;
164
+ if ( ! rect . Equals ( _hightlightedTableRect ) )
161
165
{
162
- _hightlightedTableElement = idx ;
166
+ _hightlightedTableRect = rect ;
163
167
SetCurrentValue ( ValueProperty , COLOR_TABLE [ row , col ] . ToUInt32 ( ) ) ;
164
168
}
165
169
@@ -168,32 +172,32 @@ protected override void OnPointerPressed(PointerPressedEventArgs e)
168
172
169
173
if ( _darkestRect . Rect . Contains ( p ) )
170
174
{
171
- _hightlightedTableElement = - 1 ;
175
+ _hightlightedTableRect = null ;
172
176
SetCurrentValue ( ValueProperty , _darkestColor . ToUInt32 ( ) ) ;
173
177
}
174
178
else if ( _darkerRect . Contains ( p ) )
175
179
{
176
- _hightlightedTableElement = - 1 ;
180
+ _hightlightedTableRect = null ;
177
181
SetCurrentValue ( ValueProperty , _darkerColor . ToUInt32 ( ) ) ;
178
182
}
179
183
else if ( _darkRect . Contains ( p ) )
180
184
{
181
- _hightlightedTableElement = - 1 ;
185
+ _hightlightedTableRect = null ;
182
186
SetCurrentValue ( ValueProperty , _darkColor . ToUInt32 ( ) ) ;
183
187
}
184
188
else if ( _lightRect . Contains ( p ) )
185
189
{
186
- _hightlightedTableElement = - 1 ;
190
+ _hightlightedTableRect = null ;
187
191
SetCurrentValue ( ValueProperty , _lightColor . ToUInt32 ( ) ) ;
188
192
}
189
193
else if ( _lighterRect . Contains ( p ) )
190
194
{
191
- _hightlightedTableElement = - 1 ;
195
+ _hightlightedTableRect = null ;
192
196
SetCurrentValue ( ValueProperty , _lighterColor . ToUInt32 ( ) ) ;
193
197
}
194
198
else if ( _lightestRect . Rect . Contains ( p ) )
195
199
{
196
- _hightlightedTableElement = - 1 ;
200
+ _hightlightedTableRect = null ;
197
201
SetCurrentValue ( ValueProperty , _lightestColor . ToUInt32 ( ) ) ;
198
202
}
199
203
}
@@ -230,8 +234,7 @@ private Color GetNextColor(HsvColor c, double step)
230
234
private Rect _lightRect = new Rect ( 160 , 200 , 32 , 32 ) ;
231
235
private Rect _lighterRect = new Rect ( 192 , 200 , 32 , 32 ) ;
232
236
private RoundedRect _lightestRect = new RoundedRect ( new Rect ( 224 , 200 , 32 , 32 ) , new CornerRadius ( 0 , 4 , 4 , 0 ) ) ;
233
-
234
- private int _hightlightedTableElement = - 1 ;
237
+ private Rect ? _hightlightedTableRect = null ;
235
238
236
239
private Color _darkestColor ;
237
240
private Color _darkerColor ;
0 commit comments