SwatchControl.cs 8.3 KB

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