ColorPicker.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. HighlightOnPress = true;
  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. ///<inheritdoc/>
  160. public override bool OnEnter (View view)
  161. {
  162. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  163. return base.OnEnter (view);
  164. }
  165. /// <summary>Add the commands.</summary>
  166. private void AddCommands ()
  167. {
  168. AddCommand (Command.Left, () => MoveLeft ());
  169. AddCommand (Command.Right, () => MoveRight ());
  170. AddCommand (Command.LineUp, () => MoveUp ());
  171. AddCommand (Command.LineDown, () => MoveDown ());
  172. }
  173. /// <summary>Add the KeyBindinds.</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.LineUp);
  179. KeyBindings.Add (Key.CursorDown, Command.LineDown);
  180. }
  181. /// <summary>Draw a box for one color.</summary>
  182. /// <param name="x">X location.</param>
  183. /// <param name="y">Y location</param>
  184. /// <param name="selected"></param>
  185. private void DrawColorBox (int x, int y, bool selected)
  186. {
  187. var index = 0;
  188. for (var zoomedY = 0; zoomedY < BoxHeight; zoomedY++)
  189. {
  190. for (var zoomedX = 0; zoomedX < BoxWidth; zoomedX++)
  191. {
  192. Move (x * BoxWidth + zoomedX, y * BoxHeight + zoomedY);
  193. Driver.AddRune ((Rune)' ');
  194. index++;
  195. }
  196. }
  197. if (selected)
  198. {
  199. DrawFocusRect (new (x * BoxWidth, y * BoxHeight, BoxWidth, BoxHeight));
  200. }
  201. }
  202. private void DrawFocusRect (Rectangle rect)
  203. {
  204. var lc = new LineCanvas ();
  205. if (rect.Width == 1)
  206. {
  207. lc.AddLine (rect.Location, rect.Height, Orientation.Vertical, LineStyle.Dotted);
  208. }
  209. else if (rect.Height == 1)
  210. {
  211. lc.AddLine (rect.Location, rect.Width, Orientation.Horizontal, LineStyle.Dotted);
  212. }
  213. else
  214. {
  215. lc.AddLine (rect.Location, rect.Width, Orientation.Horizontal, LineStyle.Dotted);
  216. lc.AddLine (
  217. rect.Location with { Y = rect.Location.Y + rect.Height - 1 },
  218. rect.Width,
  219. Orientation.Horizontal,
  220. LineStyle.Dotted
  221. );
  222. lc.AddLine (rect.Location, rect.Height, Orientation.Vertical, LineStyle.Dotted);
  223. lc.AddLine (
  224. rect.Location with { X = rect.Location.X + rect.Width - 1 },
  225. rect.Height,
  226. Orientation.Vertical,
  227. LineStyle.Dotted
  228. );
  229. }
  230. foreach (KeyValuePair<Point, Rune> p in lc.GetMap ())
  231. {
  232. AddRune (p.Key.X, p.Key.Y, p.Value);
  233. }
  234. }
  235. }