ColorPicker.cs 7.8 KB

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