ColorPicker.cs 8.2 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. HighlightStyle = Gui.HighlightStyle.PressedOutside | Gui.HighlightStyle.Pressed;
  27. CanFocus = true;
  28. AddCommands ();
  29. AddKeyBindings ();
  30. Width = Dim.Auto (minimumContentDim: _boxWidth * _cols);
  31. Height = Dim.Auto (minimumContentDim: _boxHeight * _rows);
  32. SetContentSize (new (_boxWidth * _cols, _boxHeight * _rows));
  33. MouseClick += ColorPicker_MouseClick;
  34. }
  35. // TODO: Decouple Cursor from SelectedColor so that mouse press-and-hold can show the color under the cursor.
  36. private void ColorPicker_MouseClick (object sender, MouseEventEventArgs me)
  37. {
  38. // if (CanFocus)
  39. {
  40. Cursor = new Point (me.MouseEvent.Position.X / _boxWidth, me.MouseEvent.Position.Y / _boxHeight);
  41. SetFocus ();
  42. me.Handled = true;
  43. }
  44. }
  45. /// <summary>Height of a color box</summary>
  46. public int BoxHeight
  47. {
  48. get => _boxHeight;
  49. set
  50. {
  51. if (_boxHeight != value)
  52. {
  53. _boxHeight = value;
  54. Width = Dim.Auto (minimumContentDim: _boxWidth * _cols);
  55. Height = Dim.Auto (minimumContentDim: _boxHeight * _rows);
  56. SetContentSize (new (_boxWidth * _cols, _boxHeight * _rows));
  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. Width = Dim.Auto (minimumContentDim: _boxWidth * _cols);
  71. Height = Dim.Auto (minimumContentDim: _boxHeight * _rows);
  72. SetContentSize (new (_boxWidth * _cols, _boxHeight * _rows));
  73. SetNeedsLayout ();
  74. }
  75. }
  76. }
  77. /// <summary>Cursor for the selected color.</summary>
  78. public Point Cursor
  79. {
  80. get => new (_selectColorIndex % _cols, _selectColorIndex / _cols);
  81. set
  82. {
  83. int colorIndex = value.Y * _cols + value.X;
  84. SelectedColor = (ColorName)colorIndex;
  85. }
  86. }
  87. /// <summary>Selected color.</summary>
  88. public ColorName SelectedColor
  89. {
  90. get => (ColorName)_selectColorIndex;
  91. set
  92. {
  93. if (value == (ColorName)_selectColorIndex)
  94. {
  95. return;
  96. }
  97. var prev = (ColorName)_selectColorIndex;
  98. _selectColorIndex = (int)value;
  99. ColorChanged?.Invoke (
  100. this,
  101. new ColorEventArgs { PreviousColor = new Color (prev), Color = new Color (value) }
  102. );
  103. SetNeedsDisplay ();
  104. }
  105. }
  106. /// <summary>Fired when a color is picked.</summary>
  107. public event EventHandler<ColorEventArgs> ColorChanged;
  108. /// <summary>Moves the selected item index to the previous column.</summary>
  109. /// <returns></returns>
  110. public virtual bool MoveLeft ()
  111. {
  112. if (Cursor.X > 0)
  113. {
  114. SelectedColor--;
  115. }
  116. return true;
  117. }
  118. /// <summary>Moves the selected item index to the next column.</summary>
  119. /// <returns></returns>
  120. public virtual bool MoveRight ()
  121. {
  122. if (Cursor.X < _cols - 1)
  123. {
  124. SelectedColor++;
  125. }
  126. return true;
  127. }
  128. /// <summary>Moves the selected item index to the previous row.</summary>
  129. /// <returns></returns>
  130. public virtual bool MoveUp ()
  131. {
  132. if (Cursor.Y > 0)
  133. {
  134. SelectedColor -= _cols;
  135. }
  136. return true;
  137. }
  138. /// <summary>Moves the selected item index to the next row.</summary>
  139. /// <returns></returns>
  140. public virtual bool MoveDown ()
  141. {
  142. if (Cursor.Y < _rows - 1)
  143. {
  144. SelectedColor += _cols;
  145. }
  146. return true;
  147. }
  148. ///<inheritdoc/>
  149. public override void OnDrawContent (Rectangle viewport)
  150. {
  151. base.OnDrawContent (viewport);
  152. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : GetNormalColor ());
  153. var colorIndex = 0;
  154. for (var y = 0; y < Math.Max (2, viewport.Height / BoxHeight); y++)
  155. {
  156. for (var x = 0; x < Math.Max (8, viewport.Width / BoxWidth); x++)
  157. {
  158. int foregroundColorIndex = y == 0 ? colorIndex + _cols : colorIndex - _cols;
  159. Driver.SetAttribute (new Attribute ((ColorName)foregroundColorIndex, (ColorName)colorIndex));
  160. bool selected = x == Cursor.X && y == Cursor.Y;
  161. DrawColorBox (x, y, selected);
  162. colorIndex++;
  163. }
  164. }
  165. }
  166. /// <summary>Add the commands.</summary>
  167. private void AddCommands ()
  168. {
  169. AddCommand (Command.Left, () => MoveLeft ());
  170. AddCommand (Command.Right, () => MoveRight ());
  171. AddCommand (Command.LineUp, () => MoveUp ());
  172. AddCommand (Command.LineDown, () => MoveDown ());
  173. }
  174. /// <summary>Add the KeyBindinds.</summary>
  175. private void AddKeyBindings ()
  176. {
  177. KeyBindings.Add (Key.CursorLeft, Command.Left);
  178. KeyBindings.Add (Key.CursorRight, Command.Right);
  179. KeyBindings.Add (Key.CursorUp, Command.LineUp);
  180. KeyBindings.Add (Key.CursorDown, Command.LineDown);
  181. }
  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. }