ColorPicker.16.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. private bool MoveDown (ICommandContext commandContext)
  62. {
  63. if (commandContext is not CommandContext<KeyBinding> ctx)
  64. {
  65. return false;
  66. }
  67. if (RaiseSelecting (ctx) == true)
  68. {
  69. return true;
  70. }
  71. if (Cursor.Y < _rows - 1)
  72. {
  73. SelectedColor += _cols;
  74. }
  75. return true;
  76. }
  77. /// <summary>Moves the selected item index to the previous column.</summary>
  78. /// <returns></returns>
  79. private bool MoveLeft (ICommandContext commandContext)
  80. {
  81. if (commandContext is not CommandContext<KeyBinding> ctx)
  82. {
  83. return false;
  84. }
  85. if (RaiseSelecting (ctx) == true)
  86. {
  87. return true;
  88. }
  89. if (Cursor.X > 0)
  90. {
  91. SelectedColor--;
  92. }
  93. return true;
  94. }
  95. /// <summary>Moves the selected item index to the next column.</summary>
  96. /// <returns></returns>
  97. private bool MoveRight (ICommandContext commandContext)
  98. {
  99. if (commandContext is not CommandContext<KeyBinding> ctx)
  100. {
  101. return false;
  102. }
  103. if (RaiseSelecting (ctx) == true)
  104. {
  105. return true;
  106. }
  107. if (Cursor.X < _cols - 1)
  108. {
  109. SelectedColor++;
  110. }
  111. return true;
  112. }
  113. /// <summary>Moves the selected item index to the previous row.</summary>
  114. /// <returns></returns>
  115. private bool MoveUp (ICommandContext commandContext)
  116. {
  117. if (commandContext is not CommandContext<KeyBinding> ctx)
  118. {
  119. return false;
  120. }
  121. if (RaiseSelecting (ctx) == true)
  122. {
  123. return true;
  124. }
  125. if (Cursor.Y > 0)
  126. {
  127. SelectedColor -= _cols;
  128. }
  129. return true;
  130. }
  131. ///<inheritdoc/>
  132. protected override bool OnDrawingContent ()
  133. {
  134. SetAttribute (HasFocus ? ColorScheme.Focus : GetNormalColor ());
  135. var colorIndex = 0;
  136. for (var y = 0; y < Math.Max (2, Viewport.Height / BoxHeight); y++)
  137. {
  138. for (var x = 0; x < Math.Max (8, Viewport.Width / BoxWidth); x++)
  139. {
  140. int foregroundColorIndex = y == 0 ? colorIndex + _cols : colorIndex - _cols;
  141. if (foregroundColorIndex > 15 || colorIndex > 15)
  142. {
  143. continue;
  144. }
  145. SetAttribute (new ((ColorName16)foregroundColorIndex, (ColorName16)colorIndex));
  146. bool selected = x == Cursor.X && y == Cursor.Y;
  147. DrawColorBox (x, y, selected);
  148. colorIndex++;
  149. }
  150. }
  151. return true;
  152. }
  153. /// <summary>Selected color.</summary>
  154. public ColorName16 SelectedColor
  155. {
  156. get => (ColorName16)_selectColorIndex;
  157. set
  158. {
  159. if (value == (ColorName16)_selectColorIndex)
  160. {
  161. return;
  162. }
  163. _selectColorIndex = (int)value;
  164. ColorChanged?.Invoke (
  165. this,
  166. new (value)
  167. );
  168. SetNeedsDraw ();
  169. }
  170. }
  171. /// <summary>Add the commands.</summary>
  172. private void AddCommands ()
  173. {
  174. AddCommand (Command.Left, (ctx) => MoveLeft (ctx));
  175. AddCommand (Command.Right, (ctx) => MoveRight (ctx));
  176. AddCommand (Command.Up, (ctx) => MoveUp (ctx));
  177. AddCommand (Command.Down, (ctx) => MoveDown (ctx));
  178. AddCommand (Command.Select, (ctx) =>
  179. {
  180. bool set = false;
  181. if (ctx is CommandContext<MouseEventArgs> { Binding: { } } mouseCommandContext)
  182. {
  183. Cursor = new (mouseCommandContext.Binding.Position.X / _boxWidth, mouseCommandContext.Binding.Position.Y / _boxHeight);
  184. set = true;
  185. }
  186. return RaiseAccepting (ctx) == true || set;
  187. });
  188. }
  189. /// <summary>Add the KeyBindings.</summary>
  190. private void AddKeyBindings ()
  191. {
  192. KeyBindings.Add (Key.CursorLeft, Command.Left);
  193. KeyBindings.Add (Key.CursorRight, Command.Right);
  194. KeyBindings.Add (Key.CursorUp, Command.Up);
  195. KeyBindings.Add (Key.CursorDown, Command.Down);
  196. }
  197. // TODO: Decouple Cursor from SelectedColor so that mouse press-and-hold can show the color under the cursor.
  198. /// <summary>Draw a box for one color.</summary>
  199. /// <param name="x">X location.</param>
  200. /// <param name="y">Y location</param>
  201. /// <param name="selected"></param>
  202. private void DrawColorBox (int x, int y, bool selected)
  203. {
  204. var index = 0;
  205. for (var zoomedY = 0; zoomedY < BoxHeight; zoomedY++)
  206. {
  207. for (var zoomedX = 0; zoomedX < BoxWidth; zoomedX++)
  208. {
  209. AddRune (x * BoxWidth + zoomedX, y * BoxHeight + zoomedY, (Rune)' ');
  210. index++;
  211. }
  212. }
  213. if (selected)
  214. {
  215. DrawFocusRect (new (x * BoxWidth, y * BoxHeight, BoxWidth, BoxHeight));
  216. }
  217. }
  218. private void DrawFocusRect (Rectangle rect)
  219. {
  220. var lc = new LineCanvas ();
  221. if (rect.Width == 1)
  222. {
  223. lc.AddLine (rect.Location, rect.Height, Orientation.Vertical, LineStyle.Dotted);
  224. }
  225. else if (rect.Height == 1)
  226. {
  227. lc.AddLine (rect.Location, rect.Width, Orientation.Horizontal, LineStyle.Dotted);
  228. }
  229. else
  230. {
  231. lc.AddLine (rect.Location, rect.Width, Orientation.Horizontal, LineStyle.Dotted);
  232. lc.AddLine (
  233. rect.Location with { Y = rect.Location.Y + rect.Height - 1 },
  234. rect.Width,
  235. Orientation.Horizontal,
  236. LineStyle.Dotted
  237. );
  238. lc.AddLine (rect.Location, rect.Height, Orientation.Vertical, LineStyle.Dotted);
  239. lc.AddLine (
  240. rect.Location with { X = rect.Location.X + rect.Width - 1 },
  241. rect.Height,
  242. Orientation.Vertical,
  243. LineStyle.Dotted
  244. );
  245. }
  246. foreach (KeyValuePair<Point, Rune> p in lc.GetMap ())
  247. {
  248. AddRune (p.Key.X, p.Key.Y, p.Value);
  249. }
  250. }
  251. private void SetInitialProperties ()
  252. {
  253. HighlightStyle = HighlightStyle.PressedOutside | HighlightStyle.Pressed;
  254. CanFocus = true;
  255. AddCommands ();
  256. AddKeyBindings ();
  257. Width = Dim.Auto (minimumContentDim: _boxWidth * _cols);
  258. Height = Dim.Auto (minimumContentDim: _boxHeight * _rows);
  259. SetContentSize (new (_boxWidth * _cols, _boxHeight * _rows));
  260. }
  261. }