ColorPicker.cs 6.9 KB

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