ColorPicker.16.cs 7.8 KB

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