ColorPicker.cs 7.7 KB

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