-
Notifications
You must be signed in to change notification settings - Fork 673
/
ColorPicker.cs
249 lines (213 loc) · 5.79 KB
/
ColorPicker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
using System;
using NStack;
namespace Terminal.Gui {
/// <summary>
/// The <see cref="ColorPicker"/> <see cref="View"/> Color picker.
/// </summary>
public class ColorPicker : View {
/// <summary>
/// Number of colors on a line.
/// </summary>
private static readonly int colorsPerLine = 8;
/// <summary>
/// Number of color lines.
/// </summary>
private static readonly int lineCount = 2;
/// <summary>
/// Horizontal zoom.
/// </summary>
private static readonly int horizontalZoom = 4;
/// <summary>
/// Vertical zoom.
/// </summary>
private static readonly int verticalZoom = 2;
// Cursor runes.
private static readonly Rune [] cursorRunes = new Rune []
{
0x250C, 0x2500, 0x2500, 0x2510,
0x2514, 0x2500, 0x2500, 0x2518
};
/// <summary>
/// Cursor for the selected color.
/// </summary>
public Point Cursor {
get {
return new Point (selectColorIndex % colorsPerLine, selectColorIndex / colorsPerLine);
}
set {
var colorIndex = value.Y * colorsPerLine + value.X;
SelectedColor = (Color)colorIndex;
}
}
/// <summary>
/// Fired when a color is picked.
/// </summary>
public event Action ColorChanged;
private int selectColorIndex = (int)Color.Black;
/// <summary>
/// Selected color.
/// </summary>
public Color SelectedColor {
get {
return (Color)selectColorIndex;
}
set {
selectColorIndex = (int)value;
ColorChanged?.Invoke ();
SetNeedsDisplay ();
}
}
/// <summary>
/// Initializes a new instance of <see cref="ColorPicker"/>.
/// </summary>
public ColorPicker () : base ("Color Picker")
{
Initialize ();
}
/// <summary>
/// Initializes a new instance of <see cref="ColorPicker"/>.
/// </summary>
/// <param name="title">Title.</param>
public ColorPicker (ustring title) : base (title)
{
Initialize ();
}
/// <summary>
/// Initializes a new instance of <see cref="ColorPicker"/>.
/// </summary>
/// <param name="point">Location point.</param>
/// <param name="title">Title.</param>
public ColorPicker (Point point, ustring title) : this (point.X, point.Y, title)
{
}
/// <summary>
/// Initializes a new instance of <see cref="ColorPicker"/>.
/// </summary>
/// <param name="x">X location.</param>
/// <param name="y">Y location.</param>
/// <param name="title">Title</param>
public ColorPicker (int x, int y, ustring title) : base (x, y, title)
{
Initialize ();
}
private void Initialize()
{
CanFocus = true;
Width = colorsPerLine * horizontalZoom;
Height = lineCount * verticalZoom + 1;
AddCommands ();
AddKeyBindings ();
}
/// <summary>
/// Add the commands.
/// </summary>
private void AddCommands ()
{
AddCommand (Command.Left, () => MoveLeft ());
AddCommand (Command.Right, () => MoveRight ());
AddCommand (Command.LineUp, () => MoveUp ());
AddCommand (Command.LineDown, () => MoveDown ());
}
/// <summary>
/// Add the KeyBindinds.
/// </summary>
private void AddKeyBindings ()
{
AddKeyBinding (Key.CursorLeft, Command.Left);
AddKeyBinding (Key.CursorRight, Command.Right);
AddKeyBinding (Key.CursorUp, Command.LineUp);
AddKeyBinding (Key.CursorDown, Command.LineDown);
}
///<inheritdoc/>
public override void Redraw (Rect bounds)
{
base.Redraw (bounds);
Driver.SetAttribute (HasFocus ? ColorScheme.Focus : GetNormalColor ());
var colorIndex = 0;
for (var y = 0; y < (Height.Anchor (0) - 1) / verticalZoom; y++) {
for (var x = 0; x < Width.Anchor (0) / horizontalZoom; x++) {
var foregroundColorIndex = y == 0 ? colorIndex + colorsPerLine : colorIndex - colorsPerLine;
Driver.SetAttribute (Driver.MakeAttribute ((Color)foregroundColorIndex, (Color)colorIndex));
var selected = x == Cursor.X && y == Cursor.Y;
DrawColorBox (x, y, selected);
colorIndex++;
}
}
}
/// <summary>
/// Draw a box for one color.
/// </summary>
/// <param name="x">X location.</param>
/// <param name="y">Y location</param>
/// <param name="selected"></param>
private void DrawColorBox (int x, int y, bool selected)
{
var index = 0;
for (var zommedY = 0; zommedY < verticalZoom; zommedY++) {
for (var zommedX = 0; zommedX < horizontalZoom; zommedX++) {
Move (x * horizontalZoom + zommedX, y * verticalZoom + zommedY + 1);
if (selected) {
var character = cursorRunes [index];
Driver.AddRune (character);
} else {
Driver.AddRune (' ');
}
index++;
}
}
}
/// <summary>
/// Moves the selected item index to the previous column.
/// </summary>
/// <returns></returns>
public virtual bool MoveLeft ()
{
if (Cursor.X > 0) SelectedColor--;
return true;
}
/// <summary>
/// Moves the selected item index to the next column.
/// </summary>
/// <returns></returns>
public virtual bool MoveRight ()
{
if (Cursor.X < colorsPerLine - 1) SelectedColor++;
return true;
}
/// <summary>
/// Moves the selected item index to the previous row.
/// </summary>
/// <returns></returns>
public virtual bool MoveUp ()
{
if (Cursor.Y > 0) SelectedColor -= colorsPerLine;
return true;
}
/// <summary>
/// Moves the selected item index to the next row.
/// </summary>
/// <returns></returns>
public virtual bool MoveDown ()
{
if (Cursor.Y < lineCount - 1) SelectedColor += colorsPerLine;
return true;
}
///<inheritdoc/>
public override bool ProcessKey (KeyEvent kb)
{
var result = InvokeKeybindings (kb);
if (result != null)
return (bool)result;
return false;
}
///<inheritdoc/>
public override bool MouseEvent (MouseEvent me)
{
if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) || !CanFocus)
return false;
SetFocus ();
Cursor = new Point (me.X / horizontalZoom, (me.Y - 1) / verticalZoom);
return true;
}
}
}