ColorPicker.16.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. namespace Terminal.Gui;
  2. /// <summary>The <see cref="ColorPicker16"/> <see cref="View"/> Color picker.</summary>
  3. public class ColorPicker16 : View
  4. {
  5. /// <summary>Initializes a new instance of <see cref="ColorPicker16"/>.</summary>
  6. public ColorPicker16 () { SetInitialProperties (); }
  7. /// <summary>Columns of color boxes</summary>
  8. private readonly int _cols = 8;
  9. /// <summary>Rows of color boxes</summary>
  10. private readonly int _rows = 2;
  11. private int _boxHeight = 2;
  12. private int _boxWidth = 4;
  13. private int _selectColorIndex = (int)Color.Black;
  14. /// <summary>Height of a color box</summary>
  15. public int BoxHeight
  16. {
  17. get => _boxHeight;
  18. set
  19. {
  20. if (_boxHeight != value)
  21. {
  22. _boxHeight = value;
  23. Width = Dim.Auto (minimumContentDim: _boxWidth * _cols);
  24. Height = Dim.Auto (minimumContentDim: _boxHeight * _rows);
  25. SetContentSize (new (_boxWidth * _cols, _boxHeight * _rows));
  26. SetLayoutNeeded ();
  27. }
  28. }
  29. }
  30. /// <summary>Width of a color box</summary>
  31. public int BoxWidth
  32. {
  33. get => _boxWidth;
  34. set
  35. {
  36. if (_boxWidth != value)
  37. {
  38. _boxWidth = value;
  39. Width = Dim.Auto (minimumContentDim: _boxWidth * _cols);
  40. Height = Dim.Auto (minimumContentDim: _boxHeight * _rows);
  41. SetContentSize (new (_boxWidth * _cols, _boxHeight * _rows));
  42. SetLayoutNeeded ();
  43. }
  44. }
  45. }
  46. /// <summary>Fired when a color is picked.</summary>
  47. [CanBeNull]
  48. public event EventHandler<ColorEventArgs> ColorChanged;
  49. /// <summary>Cursor for the selected color.</summary>
  50. public Point Cursor
  51. {
  52. get => new (_selectColorIndex % _cols, _selectColorIndex / _cols);
  53. set
  54. {
  55. int colorIndex = value.Y * _cols + value.X;
  56. SelectedColor = (ColorName16)colorIndex;
  57. }
  58. }
  59. /// <summary>Moves the selected item index to the next row.</summary>
  60. /// <returns></returns>
  61. private bool MoveDown (CommandContext ctx)
  62. {
  63. if (RaiseSelecting (ctx) == true)
  64. {
  65. return true;
  66. }
  67. if (Cursor.Y < _rows - 1)
  68. {
  69. SelectedColor += _cols;
  70. }
  71. return true;
  72. }
  73. /// <summary>Moves the selected item index to the previous column.</summary>
  74. /// <returns></returns>
  75. private bool MoveLeft (CommandContext ctx)
  76. {
  77. if (RaiseSelecting (ctx) == true)
  78. {
  79. return true;
  80. }
  81. if (Cursor.X > 0)
  82. {
  83. SelectedColor--;
  84. }
  85. return true;
  86. }
  87. /// <summary>Moves the selected item index to the next column.</summary>
  88. /// <returns></returns>
  89. private bool MoveRight (CommandContext ctx)
  90. {
  91. if (RaiseSelecting (ctx) == true)
  92. {
  93. return true;
  94. }
  95. if (Cursor.X < _cols - 1)
  96. {
  97. SelectedColor++;
  98. }
  99. return true;
  100. }
  101. /// <summary>Moves the selected item index to the previous row.</summary>
  102. /// <returns></returns>
  103. private bool MoveUp (CommandContext ctx)
  104. {
  105. if (RaiseSelecting (ctx) == true)
  106. {
  107. return true;
  108. }
  109. if (Cursor.Y > 0)
  110. {
  111. SelectedColor -= _cols;
  112. }
  113. return true;
  114. }
  115. ///<inheritdoc/>
  116. public override void OnDrawContent (Rectangle viewport)
  117. {
  118. base.OnDrawContent (viewport);
  119. Driver?.SetAttribute (HasFocus ? ColorScheme.Focus : GetNormalColor ());
  120. var colorIndex = 0;
  121. for (var y = 0; y < Math.Max (2, viewport.Height / BoxHeight); y++)
  122. {
  123. for (var x = 0; x < Math.Max (8, viewport.Width / BoxWidth); x++)
  124. {
  125. int foregroundColorIndex = y == 0 ? colorIndex + _cols : colorIndex - _cols;
  126. if (foregroundColorIndex > 15 || colorIndex > 15)
  127. {
  128. continue;
  129. }
  130. Driver?.SetAttribute (new ((ColorName16)foregroundColorIndex, (ColorName16)colorIndex));
  131. bool selected = x == Cursor.X && y == Cursor.Y;
  132. DrawColorBox (x, y, selected);
  133. colorIndex++;
  134. }
  135. }
  136. }
  137. /// <summary>Selected color.</summary>
  138. public ColorName16 SelectedColor
  139. {
  140. get => (ColorName16)_selectColorIndex;
  141. set
  142. {
  143. if (value == (ColorName16)_selectColorIndex)
  144. {
  145. return;
  146. }
  147. _selectColorIndex = (int)value;
  148. ColorChanged?.Invoke (
  149. this,
  150. new (value)
  151. );
  152. SetNeedsDisplay ();
  153. }
  154. }
  155. /// <summary>Add the commands.</summary>
  156. private void AddCommands ()
  157. {
  158. AddCommand (Command.Left, (ctx) => MoveLeft (ctx));
  159. AddCommand (Command.Right, (ctx) => MoveRight (ctx));
  160. AddCommand (Command.Up, (ctx) => MoveUp (ctx));
  161. AddCommand (Command.Down, (ctx) => MoveDown (ctx));
  162. AddCommand (Command.Select, (ctx) =>
  163. {
  164. bool set = false;
  165. if (ctx.Data is MouseEventArgs me)
  166. {
  167. Cursor = new (me.Position.X / _boxWidth, me.Position.Y / _boxHeight);
  168. set = true;
  169. }
  170. return RaiseAccepting (ctx) == true || set;
  171. });
  172. }
  173. /// <summary>Add the KeyBindings.</summary>
  174. private void AddKeyBindings ()
  175. {
  176. KeyBindings.Add (Key.CursorLeft, Command.Left);
  177. KeyBindings.Add (Key.CursorRight, Command.Right);
  178. KeyBindings.Add (Key.CursorUp, Command.Up);
  179. KeyBindings.Add (Key.CursorDown, Command.Down);
  180. }
  181. // TODO: Decouple Cursor from SelectedColor so that mouse press-and-hold can show the color under the cursor.
  182. /// <summary>Draw a box for one color.</summary>
  183. /// <param name="x">X location.</param>
  184. /// <param name="y">Y location</param>
  185. /// <param name="selected"></param>
  186. private void DrawColorBox (int x, int y, bool selected)
  187. {
  188. var index = 0;
  189. for (var zoomedY = 0; zoomedY < BoxHeight; zoomedY++)
  190. {
  191. for (var zoomedX = 0; zoomedX < BoxWidth; zoomedX++)
  192. {
  193. Move (x * BoxWidth + zoomedX, y * BoxHeight + zoomedY);
  194. Driver?.AddRune ((Rune)' ');
  195. index++;
  196. }
  197. }
  198. if (selected)
  199. {
  200. DrawFocusRect (new (x * BoxWidth, y * BoxHeight, BoxWidth, BoxHeight));
  201. }
  202. }
  203. private void DrawFocusRect (Rectangle rect)
  204. {
  205. var lc = new LineCanvas ();
  206. if (rect.Width == 1)
  207. {
  208. lc.AddLine (rect.Location, rect.Height, Orientation.Vertical, LineStyle.Dotted);
  209. }
  210. else if (rect.Height == 1)
  211. {
  212. lc.AddLine (rect.Location, rect.Width, Orientation.Horizontal, LineStyle.Dotted);
  213. }
  214. else
  215. {
  216. lc.AddLine (rect.Location, rect.Width, Orientation.Horizontal, LineStyle.Dotted);
  217. lc.AddLine (
  218. rect.Location with { Y = rect.Location.Y + rect.Height - 1 },
  219. rect.Width,
  220. Orientation.Horizontal,
  221. LineStyle.Dotted
  222. );
  223. lc.AddLine (rect.Location, rect.Height, Orientation.Vertical, LineStyle.Dotted);
  224. lc.AddLine (
  225. rect.Location with { X = rect.Location.X + rect.Width - 1 },
  226. rect.Height,
  227. Orientation.Vertical,
  228. LineStyle.Dotted
  229. );
  230. }
  231. foreach (KeyValuePair<Point, Rune> p in lc.GetMap ())
  232. {
  233. AddRune (p.Key.X, p.Key.Y, p.Value);
  234. }
  235. }
  236. private void SetInitialProperties ()
  237. {
  238. HighlightStyle = HighlightStyle.PressedOutside | HighlightStyle.Pressed;
  239. CanFocus = true;
  240. AddCommands ();
  241. AddKeyBindings ();
  242. Width = Dim.Auto (minimumContentDim: _boxWidth * _cols);
  243. Height = Dim.Auto (minimumContentDim: _boxHeight * _rows);
  244. SetContentSize (new (_boxWidth * _cols, _boxHeight * _rows));
  245. }
  246. }