ColorPicker16.cs 7.8 KB

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