SwatchControl.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using PaintDotNet.SystemLayer;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Windows.Forms;
  8. using System.Windows.Forms.VisualStyles;
  9. namespace PaintDotNet
  10. {
  11. public sealed class SwatchControl
  12. : Control
  13. {
  14. private List<ColorBgra> colors = new List<ColorBgra>();
  15. private const int defaultUnscaledSwatchSize = 12;
  16. private int unscaledSwatchSize = defaultUnscaledSwatchSize;
  17. private bool mouseDown = false;
  18. private int mouseDownIndex = -1;
  19. private bool blinkHighlight = false;
  20. private const int blinkInterval = 500;
  21. private System.Windows.Forms.Timer blinkHighlightTimer;
  22. [Browsable(false)]
  23. public bool BlinkHighlight
  24. {
  25. get
  26. {
  27. return this.blinkHighlight;
  28. }
  29. set
  30. {
  31. this.blinkHighlight = value;
  32. this.blinkHighlightTimer.Enabled = value;
  33. Invalidate();
  34. }
  35. }
  36. public event EventHandler ColorsChanged;
  37. private void OnColorsChanged()
  38. {
  39. if (ColorsChanged != null)
  40. {
  41. ColorsChanged(this, EventArgs.Empty);
  42. }
  43. }
  44. [Browsable(false)]
  45. public ColorBgra[] Colors
  46. {
  47. get
  48. {
  49. return this.colors.ToArray();
  50. }
  51. set
  52. {
  53. this.colors = new List<ColorBgra>(value);
  54. this.mouseDown = false;
  55. Invalidate();
  56. OnColorsChanged();
  57. }
  58. }
  59. [DefaultValue(defaultUnscaledSwatchSize)]
  60. [Browsable(true)]
  61. public int UnscaledSwatchSize
  62. {
  63. get
  64. {
  65. return this.unscaledSwatchSize;
  66. }
  67. set
  68. {
  69. this.unscaledSwatchSize = value;
  70. this.mouseDown = false;
  71. Invalidate();
  72. }
  73. }
  74. public event EventHandler<EventArgs<Pair<int, MouseButtons>>> ColorClicked;
  75. private void OnColorClicked(int index, MouseButtons buttons)
  76. {
  77. if (ColorClicked != null)
  78. {
  79. ColorClicked(this, new EventArgs<Pair<int, MouseButtons>>(Pair.Create(index, buttons)));
  80. }
  81. }
  82. public SwatchControl()
  83. {
  84. InitializeComponent();
  85. }
  86. private void InitializeComponent()
  87. {
  88. this.blinkHighlightTimer = new Timer();
  89. this.blinkHighlightTimer.Tick += new EventHandler(BlinkHighlightTimer_Tick);
  90. this.blinkHighlightTimer.Enabled = false;
  91. this.blinkHighlightTimer.Interval = blinkInterval;
  92. this.DoubleBuffered = true;
  93. this.ResizeRedraw = true;
  94. }
  95. private void BlinkHighlightTimer_Tick(object sender, EventArgs e)
  96. {
  97. Invalidate();
  98. }
  99. protected override void Dispose(bool disposing)
  100. {
  101. if (disposing)
  102. {
  103. if (this.blinkHighlightTimer != null)
  104. {
  105. this.blinkHighlightTimer.Dispose();
  106. this.blinkHighlightTimer = null;
  107. }
  108. }
  109. base.Dispose(disposing);
  110. }
  111. private int MouseXYToColorIndex(int x, int y)
  112. {
  113. if (x < 0 || y < 0 || x >= ClientSize.Width || y >= ClientSize.Height)
  114. {
  115. return -1;
  116. }
  117. int scaledSwatchSize = UI.ScaleWidth(this.unscaledSwatchSize);
  118. int swatchColumns = this.ClientSize.Width / scaledSwatchSize;
  119. int row = y / scaledSwatchSize;
  120. int col = x / scaledSwatchSize;
  121. int index = col + (row * swatchColumns);
  122. // Make sure they aren't on the last item of a row that actually got clipped off
  123. if (col == swatchColumns)
  124. {
  125. index = -1;
  126. }
  127. return index;
  128. }
  129. protected override void OnMouseLeave(EventArgs e)
  130. {
  131. this.mouseDown = false;
  132. Invalidate();
  133. base.OnMouseLeave(e);
  134. }
  135. protected override void OnMouseDown(MouseEventArgs e)
  136. {
  137. this.mouseDown = true;
  138. this.mouseDownIndex = MouseXYToColorIndex(e.X, e.Y);
  139. Invalidate();
  140. base.OnMouseDown(e);
  141. }
  142. protected override void OnMouseUp(MouseEventArgs e)
  143. {
  144. int colorIndex = MouseXYToColorIndex(e.X, e.Y);
  145. if (colorIndex == this.mouseDownIndex &&
  146. colorIndex >= 0 &&
  147. colorIndex < this.colors.Count)
  148. {
  149. OnColorClicked(colorIndex, e.Button);
  150. }
  151. this.mouseDown = false;
  152. Invalidate();
  153. base.OnMouseUp(e);
  154. }
  155. protected override void OnMouseMove(MouseEventArgs e)
  156. {
  157. Invalidate();
  158. base.OnMouseMove(e);
  159. }
  160. protected override void OnPaint(PaintEventArgs e)
  161. {
  162. e.Graphics.CompositingMode = CompositingMode.SourceOver;
  163. int scaledSwatchSize = UI.ScaleWidth(this.unscaledSwatchSize);
  164. int swatchColumns = this.ClientSize.Width / scaledSwatchSize;
  165. Point mousePt = Control.MousePosition;
  166. mousePt = PointToClient(mousePt);
  167. int activeIndex = MouseXYToColorIndex(mousePt.X, mousePt.Y);
  168. for (int i = 0; i < this.colors.Count; ++i)
  169. {
  170. ColorBgra c = this.colors[i];
  171. int swatchX = i % swatchColumns;
  172. int swatchY = i / swatchColumns;
  173. Rectangle swatchRect = new Rectangle(
  174. swatchX * scaledSwatchSize,
  175. swatchY * scaledSwatchSize,
  176. scaledSwatchSize,
  177. scaledSwatchSize);
  178. PushButtonState state;
  179. if (this.mouseDown)
  180. {
  181. if (i == this.mouseDownIndex)
  182. {
  183. state = PushButtonState.Pressed;
  184. }
  185. else
  186. {
  187. state = PushButtonState.Normal;
  188. }
  189. }
  190. else if (i == activeIndex)
  191. {
  192. state = PushButtonState.Hot;
  193. }
  194. else
  195. {
  196. state = PushButtonState.Normal;
  197. }
  198. bool drawOutline;
  199. switch (state)
  200. {
  201. case PushButtonState.Hot:
  202. drawOutline = true;
  203. break;
  204. case PushButtonState.Pressed:
  205. drawOutline = false;
  206. break;
  207. case PushButtonState.Default:
  208. case PushButtonState.Disabled:
  209. case PushButtonState.Normal:
  210. drawOutline = false;
  211. break;
  212. default:
  213. throw new InvalidEnumArgumentException();
  214. }
  215. Utility.DrawColorRectangle(e.Graphics, swatchRect, c.ToColor(), drawOutline);
  216. }
  217. if (this.blinkHighlight)
  218. {
  219. int period = (Math.Abs(Environment.TickCount) / blinkInterval) % 2;
  220. Color color;
  221. switch (period)
  222. {
  223. case 0:
  224. color = SystemColors.Window;
  225. break;
  226. case 1:
  227. color = SystemColors.Highlight;
  228. break;
  229. default:
  230. throw new InvalidOperationException();
  231. }
  232. using (Pen pen = new Pen(color))
  233. {
  234. e.Graphics.DrawRectangle(pen, new Rectangle(0, 0, Width - 1, Height - 1));
  235. }
  236. }
  237. base.OnPaint(e);
  238. }
  239. }
  240. }