ColorPicker.cs 6.3 KB

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