ColorPicker.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. // MouseEvent += ColorPicker_MouseEvent;
  37. MouseClick += ColorPicker_MouseClick;
  38. }
  39. // TODO: Decouple Cursor from SelectedColor so that mouse press-and-hold can show the color under the cursor.
  40. //private void ColorPicker_MouseEvent (object sender, MouseEventEventArgs me)
  41. //{
  42. // if (me.MouseEvent.X > Bounds.Width || me.MouseEvent.Y > Bounds.Height)
  43. // {
  44. // me.Handled = true;
  45. // return;
  46. // }
  47. // me.Handled = true;
  48. // return;
  49. //}
  50. private void ColorPicker_MouseClick (object sender, MouseEventEventArgs me)
  51. {
  52. if (CanFocus)
  53. {
  54. Cursor = new Point (me.MouseEvent.X / _boxWidth, me.MouseEvent.Y / _boxHeight);
  55. SetFocus ();
  56. me.Handled = true;
  57. }
  58. }
  59. /// <summary>Height of a color box</summary>
  60. public int BoxHeight
  61. {
  62. get => _boxHeight;
  63. set
  64. {
  65. if (_boxHeight != value)
  66. {
  67. _boxHeight = value;
  68. SetNeedsLayout ();
  69. }
  70. }
  71. }
  72. /// <summary>Width of a color box</summary>
  73. public int BoxWidth
  74. {
  75. get => _boxWidth;
  76. set
  77. {
  78. if (_boxWidth != value)
  79. {
  80. _boxWidth = value;
  81. SetNeedsLayout ();
  82. }
  83. }
  84. }
  85. /// <summary>Cursor for the selected color.</summary>
  86. public Point Cursor
  87. {
  88. get => new (_selectColorIndex % _cols, _selectColorIndex / _cols);
  89. set
  90. {
  91. int colorIndex = value.Y * _cols + value.X;
  92. SelectedColor = (ColorName)colorIndex;
  93. }
  94. }
  95. /// <summary>Selected color.</summary>
  96. public ColorName SelectedColor
  97. {
  98. get => (ColorName)_selectColorIndex;
  99. set
  100. {
  101. var prev = (ColorName)_selectColorIndex;
  102. _selectColorIndex = (int)value;
  103. ColorChanged?.Invoke (
  104. this,
  105. new ColorEventArgs { PreviousColor = new Color (prev), Color = new Color (value) }
  106. );
  107. SetNeedsDisplay ();
  108. }
  109. }
  110. /// <summary>Fired when a color is picked.</summary>
  111. public event EventHandler<ColorEventArgs> ColorChanged;
  112. /// <summary>Moves the selected item index to the previous column.</summary>
  113. /// <returns></returns>
  114. public virtual bool MoveLeft ()
  115. {
  116. if (Cursor.X > 0)
  117. {
  118. SelectedColor--;
  119. }
  120. return true;
  121. }
  122. /// <summary>Moves the selected item index to the next column.</summary>
  123. /// <returns></returns>
  124. public virtual bool MoveRight ()
  125. {
  126. if (Cursor.X < _cols - 1)
  127. {
  128. SelectedColor++;
  129. }
  130. return true;
  131. }
  132. /// <summary>Moves the selected item index to the previous row.</summary>
  133. /// <returns></returns>
  134. public virtual bool MoveUp ()
  135. {
  136. if (Cursor.Y > 0)
  137. {
  138. SelectedColor -= _cols;
  139. }
  140. return true;
  141. }
  142. /// <summary>Moves the selected item index to the next row.</summary>
  143. /// <returns></returns>
  144. public virtual bool MoveDown ()
  145. {
  146. if (Cursor.Y < _rows - 1)
  147. {
  148. SelectedColor += _cols;
  149. }
  150. return true;
  151. }
  152. ///<inheritdoc/>
  153. public override void OnDrawContent (Rectangle contentArea)
  154. {
  155. base.OnDrawContent (contentArea);
  156. Driver.SetAttribute (HasFocus ? ColorScheme.Focus : GetNormalColor ());
  157. var colorIndex = 0;
  158. for (var y = 0; y < Bounds.Height / BoxHeight; y++)
  159. {
  160. for (var x = 0; x < Bounds.Width / BoxWidth; x++)
  161. {
  162. int foregroundColorIndex = y == 0 ? colorIndex + _cols : colorIndex - _cols;
  163. Driver.SetAttribute (new Attribute ((ColorName)foregroundColorIndex, (ColorName)colorIndex));
  164. bool selected = x == Cursor.X && y == Cursor.Y;
  165. DrawColorBox (x, y, selected);
  166. colorIndex++;
  167. }
  168. }
  169. }
  170. ///<inheritdoc/>
  171. public override bool OnEnter (View view)
  172. {
  173. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  174. return base.OnEnter (view);
  175. }
  176. /// <summary>Add the commands.</summary>
  177. private void AddCommands ()
  178. {
  179. AddCommand (Command.Left, () => MoveLeft ());
  180. AddCommand (Command.Right, () => MoveRight ());
  181. AddCommand (Command.LineUp, () => MoveUp ());
  182. AddCommand (Command.LineDown, () => MoveDown ());
  183. }
  184. /// <summary>Add the KeyBindinds.</summary>
  185. private void AddKeyBindings ()
  186. {
  187. KeyBindings.Add (Key.CursorLeft, Command.Left);
  188. KeyBindings.Add (Key.CursorRight, Command.Right);
  189. KeyBindings.Add (Key.CursorUp, Command.LineUp);
  190. KeyBindings.Add (Key.CursorDown, Command.LineDown);
  191. }
  192. /// <summary>Draw a box for one color.</summary>
  193. /// <param name="x">X location.</param>
  194. /// <param name="y">Y location</param>
  195. /// <param name="selected"></param>
  196. private void DrawColorBox (int x, int y, bool selected)
  197. {
  198. var index = 0;
  199. for (var zoomedY = 0; zoomedY < BoxHeight; zoomedY++)
  200. {
  201. for (var zoomedX = 0; zoomedX < BoxWidth; zoomedX++)
  202. {
  203. Move (x * BoxWidth + zoomedX, y * BoxHeight + zoomedY);
  204. Driver.AddRune ((Rune)' ');
  205. index++;
  206. }
  207. }
  208. if (selected)
  209. {
  210. DrawFocusRect (new (x * BoxWidth, y * BoxHeight, BoxWidth, BoxHeight));
  211. }
  212. }
  213. private void DrawFocusRect (Rectangle rect)
  214. {
  215. var lc = new LineCanvas ();
  216. if (rect.Width == 1)
  217. {
  218. lc.AddLine (rect.Location, rect.Height, Orientation.Vertical, LineStyle.Dotted);
  219. }
  220. else if (rect.Height == 1)
  221. {
  222. lc.AddLine (rect.Location, rect.Width, Orientation.Horizontal, LineStyle.Dotted);
  223. }
  224. else
  225. {
  226. lc.AddLine (rect.Location, rect.Width, Orientation.Horizontal, LineStyle.Dotted);
  227. lc.AddLine (
  228. rect.Location with { Y = rect.Location.Y + rect.Height - 1 },
  229. rect.Width,
  230. Orientation.Horizontal,
  231. LineStyle.Dotted
  232. );
  233. lc.AddLine (rect.Location, rect.Height, Orientation.Vertical, LineStyle.Dotted);
  234. lc.AddLine (
  235. rect.Location with { X = rect.Location.X + rect.Width - 1 },
  236. rect.Height,
  237. Orientation.Vertical,
  238. LineStyle.Dotted
  239. );
  240. }
  241. foreach (KeyValuePair<Point, Rune> p in lc.GetMap ())
  242. {
  243. AddRune (p.Key.X, p.Key.Y, p.Value);
  244. }
  245. }
  246. }