ColorPicker.cs 6.9 KB

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