ColorPicker.cs 6.8 KB

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