ToggleSwitchRenderer.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. using System.Drawing;
  2. namespace Optimizer
  3. {
  4. internal class ToggleSwitchRenderer : ToggleSwitchRendererBase
  5. {
  6. public Color BackColor { get; set; }
  7. public Color LeftSideColor { get; set; }
  8. public Color LeftSideColorHovered { get; set; }
  9. public Color LeftSideColorPressed { get; set; }
  10. public Color RightSideColor { get; set; }
  11. public Color RightSideColorHovered { get; set; }
  12. public Color RightSideColorPressed { get; set; }
  13. public Color BorderColor { get; set; }
  14. public Color ButtonColor { get; set; }
  15. public Color ButtonColorHovered { get; set; }
  16. public Color ButtonColorPressed { get; set; }
  17. public ToggleSwitchRenderer()
  18. {
  19. switch (Options.CurrentOptions.Color)
  20. {
  21. case Theme.Caramel:
  22. LeftSideColor = Color.DarkOrange;
  23. LeftSideColorHovered = Color.DarkOrange;
  24. LeftSideColorPressed = Color.DarkOrange;
  25. break;
  26. case Theme.Ocean:
  27. LeftSideColor = Color.DodgerBlue;
  28. LeftSideColorHovered = Color.DodgerBlue;
  29. LeftSideColorPressed = Color.DodgerBlue;
  30. break;
  31. case Theme.Lime:
  32. LeftSideColor = Color.LimeGreen;
  33. LeftSideColorHovered = Color.LimeGreen;
  34. LeftSideColorPressed = Color.LimeGreen;
  35. break;
  36. case Theme.Magma:
  37. LeftSideColor = Color.Tomato;
  38. LeftSideColorHovered = Color.Tomato;
  39. LeftSideColorPressed = Color.Tomato;
  40. break;
  41. case Theme.Minimal:
  42. LeftSideColor = Color.Gray;
  43. LeftSideColorHovered = Color.Gray;
  44. LeftSideColorPressed = Color.Gray;
  45. break;
  46. case Theme.Zerg:
  47. LeftSideColor = Color.MediumOrchid;
  48. LeftSideColorHovered = Color.MediumOrchid;
  49. LeftSideColorPressed = Color.MediumOrchid;
  50. break;
  51. }
  52. BorderColor = Color.FromArgb(20, 20, 20);
  53. BackColor = Color.White;
  54. RightSideColor = Color.FromArgb(40, 40, 40);
  55. RightSideColorHovered = Color.FromArgb(40, 40, 40);
  56. RightSideColorPressed = Color.FromArgb(40, 40, 40);
  57. ButtonColor = Color.White;
  58. ButtonColorHovered = Color.WhiteSmoke;
  59. ButtonColorPressed = Color.WhiteSmoke;
  60. }
  61. public override void RenderBorder(Graphics g, Rectangle borderRectangle)
  62. {
  63. Color borderColor = !ToggleSwitch.Enabled && ToggleSwitch.GrayWhenDisabled ? BorderColor.ToGrayScale() : BorderColor;
  64. g.SetClip(borderRectangle);
  65. using (Pen borderPen = new Pen(borderColor))
  66. {
  67. g.DrawRectangle(borderPen, borderRectangle.X, borderRectangle.Y, borderRectangle.Width - 1, borderRectangle.Height - 1);
  68. }
  69. g.ResetClip();
  70. }
  71. public override void RenderLeftToggleField(Graphics g, Rectangle leftRectangle, int totalToggleFieldWidth)
  72. {
  73. Rectangle adjustedLeftRect = new Rectangle(leftRectangle.X + 2, 2, leftRectangle.Width - 2, leftRectangle.Height - 4);
  74. if (adjustedLeftRect.Width > 0)
  75. {
  76. Color leftColor = LeftSideColor;
  77. if (ToggleSwitch.IsLeftSidePressed)
  78. leftColor = LeftSideColorPressed;
  79. else if (ToggleSwitch.IsLeftSideHovered)
  80. leftColor = LeftSideColorHovered;
  81. if (!ToggleSwitch.Enabled && ToggleSwitch.GrayWhenDisabled)
  82. leftColor = leftColor.ToGrayScale();
  83. g.SetClip(adjustedLeftRect);
  84. using (Brush leftBrush = new SolidBrush(leftColor))
  85. {
  86. g.FillRectangle(leftBrush, adjustedLeftRect);
  87. }
  88. if (ToggleSwitch.OnSideImage != null || !string.IsNullOrEmpty(ToggleSwitch.OnText))
  89. {
  90. RectangleF fullRectangle = new RectangleF(leftRectangle.X + 2 - (totalToggleFieldWidth - leftRectangle.Width), 2, totalToggleFieldWidth - 2, ToggleSwitch.Height - 4);
  91. g.IntersectClip(fullRectangle);
  92. if (ToggleSwitch.OnSideImage != null)
  93. {
  94. Size imageSize = ToggleSwitch.OnSideImage.Size;
  95. Rectangle imageRectangle;
  96. int imageXPos = (int)fullRectangle.X;
  97. if (ToggleSwitch.OnSideScaleImageToFit)
  98. {
  99. Size canvasSize = new Size((int)fullRectangle.Width, (int)fullRectangle.Height);
  100. Size resizedImageSize = ImageHelper.RescaleImageToFit(imageSize, canvasSize);
  101. if (ToggleSwitch.OnSideAlignment == ToggleSwitchAlignment.Center)
  102. {
  103. imageXPos = (int)((float)fullRectangle.X + (((float)fullRectangle.Width - (float)resizedImageSize.Width) / 2));
  104. }
  105. else if (ToggleSwitch.OnSideAlignment == ToggleSwitchAlignment.Near)
  106. {
  107. imageXPos = (int)((float)fullRectangle.X + (float)fullRectangle.Width - (float)resizedImageSize.Width);
  108. }
  109. imageRectangle = new Rectangle(imageXPos, (int)((float)fullRectangle.Y + (((float)fullRectangle.Height - (float)resizedImageSize.Height) / 2)), resizedImageSize.Width, resizedImageSize.Height);
  110. if (!ToggleSwitch.Enabled && ToggleSwitch.GrayWhenDisabled)
  111. g.DrawImage(ToggleSwitch.OnSideImage, imageRectangle, 0, 0, ToggleSwitch.OnSideImage.Width, ToggleSwitch.OnSideImage.Height, GraphicsUnit.Pixel, ImageHelper.GetGrayscaleAttributes());
  112. else
  113. g.DrawImage(ToggleSwitch.OnSideImage, imageRectangle);
  114. }
  115. else
  116. {
  117. if (ToggleSwitch.OnSideAlignment == ToggleSwitchAlignment.Center)
  118. {
  119. imageXPos = (int)((float)fullRectangle.X + (((float)fullRectangle.Width - (float)imageSize.Width) / 2));
  120. }
  121. else if (ToggleSwitch.OnSideAlignment == ToggleSwitchAlignment.Near)
  122. {
  123. imageXPos = (int)((float)fullRectangle.X + (float)fullRectangle.Width - (float)imageSize.Width);
  124. }
  125. imageRectangle = new Rectangle(imageXPos, (int)((float)fullRectangle.Y + (((float)fullRectangle.Height - (float)imageSize.Height) / 2)), imageSize.Width, imageSize.Height);
  126. if (!ToggleSwitch.Enabled && ToggleSwitch.GrayWhenDisabled)
  127. g.DrawImage(ToggleSwitch.OnSideImage, imageRectangle, 0, 0, ToggleSwitch.OnSideImage.Width, ToggleSwitch.OnSideImage.Height, GraphicsUnit.Pixel, ImageHelper.GetGrayscaleAttributes());
  128. else
  129. g.DrawImageUnscaled(ToggleSwitch.OnSideImage, imageRectangle);
  130. }
  131. }
  132. else if (!string.IsNullOrEmpty(ToggleSwitch.OnText))
  133. {
  134. SizeF textSize = g.MeasureString(ToggleSwitch.OnText, ToggleSwitch.OnFont);
  135. float textXPos = fullRectangle.X;
  136. if (ToggleSwitch.OnSideAlignment == ToggleSwitchAlignment.Center)
  137. {
  138. textXPos = (float)fullRectangle.X + (((float)fullRectangle.Width - (float)textSize.Width) / 2);
  139. }
  140. else if (ToggleSwitch.OnSideAlignment == ToggleSwitchAlignment.Near)
  141. {
  142. textXPos = (float)fullRectangle.X + (float)fullRectangle.Width - (float)textSize.Width;
  143. }
  144. RectangleF textRectangle = new RectangleF(textXPos, (float)fullRectangle.Y + (((float)fullRectangle.Height - (float)textSize.Height) / 2), textSize.Width, textSize.Height);
  145. Color textForeColor = ToggleSwitch.OnForeColor;
  146. if (!ToggleSwitch.Enabled && ToggleSwitch.GrayWhenDisabled)
  147. textForeColor = textForeColor.ToGrayScale();
  148. using (Brush textBrush = new SolidBrush(textForeColor))
  149. {
  150. g.DrawString(ToggleSwitch.OnText, ToggleSwitch.OnFont, textBrush, textRectangle);
  151. }
  152. }
  153. }
  154. g.ResetClip();
  155. }
  156. }
  157. public override void RenderRightToggleField(Graphics g, Rectangle rightRectangle, int totalToggleFieldWidth)
  158. {
  159. Rectangle adjustedRightRect = new Rectangle(rightRectangle.X, 2, rightRectangle.Width - 2, rightRectangle.Height - 4);
  160. if (adjustedRightRect.Width > 0)
  161. {
  162. Color rightColor = RightSideColor;
  163. if (ToggleSwitch.IsRightSidePressed)
  164. rightColor = RightSideColorPressed;
  165. else if (ToggleSwitch.IsRightSideHovered)
  166. rightColor = RightSideColorHovered;
  167. if (!ToggleSwitch.Enabled && ToggleSwitch.GrayWhenDisabled)
  168. rightColor = rightColor.ToGrayScale();
  169. g.SetClip(adjustedRightRect);
  170. using (Brush rightBrush = new SolidBrush(rightColor))
  171. {
  172. g.FillRectangle(rightBrush, adjustedRightRect);
  173. }
  174. if (ToggleSwitch.OffSideImage != null || !string.IsNullOrEmpty(ToggleSwitch.OnText))
  175. {
  176. RectangleF fullRectangle = new RectangleF(rightRectangle.X, 2, totalToggleFieldWidth - 2, ToggleSwitch.Height - 4);
  177. g.IntersectClip(fullRectangle);
  178. if (ToggleSwitch.OffSideImage != null)
  179. {
  180. Size imageSize = ToggleSwitch.OffSideImage.Size;
  181. Rectangle imageRectangle;
  182. int imageXPos = (int)fullRectangle.X;
  183. if (ToggleSwitch.OffSideScaleImageToFit)
  184. {
  185. Size canvasSize = new Size((int)fullRectangle.Width, (int)fullRectangle.Height);
  186. Size resizedImageSize = ImageHelper.RescaleImageToFit(imageSize, canvasSize);
  187. if (ToggleSwitch.OffSideAlignment == ToggleSwitchAlignment.Center)
  188. {
  189. imageXPos = (int)((float)fullRectangle.X + (((float)fullRectangle.Width - (float)resizedImageSize.Width) / 2));
  190. }
  191. else if (ToggleSwitch.OffSideAlignment == ToggleSwitchAlignment.Far)
  192. {
  193. imageXPos = (int)((float)fullRectangle.X + (float)fullRectangle.Width - (float)resizedImageSize.Width);
  194. }
  195. imageRectangle = new Rectangle(imageXPos, (int)((float)fullRectangle.Y + (((float)fullRectangle.Height - (float)resizedImageSize.Height) / 2)), resizedImageSize.Width, resizedImageSize.Height);
  196. if (!ToggleSwitch.Enabled && ToggleSwitch.GrayWhenDisabled)
  197. g.DrawImage(ToggleSwitch.OnSideImage, imageRectangle, 0, 0, ToggleSwitch.OnSideImage.Width, ToggleSwitch.OnSideImage.Height, GraphicsUnit.Pixel, ImageHelper.GetGrayscaleAttributes());
  198. else
  199. g.DrawImage(ToggleSwitch.OnSideImage, imageRectangle);
  200. }
  201. else
  202. {
  203. if (ToggleSwitch.OffSideAlignment == ToggleSwitchAlignment.Center)
  204. {
  205. imageXPos = (int)((float)fullRectangle.X + (((float)fullRectangle.Width - (float)imageSize.Width) / 2));
  206. }
  207. else if (ToggleSwitch.OffSideAlignment == ToggleSwitchAlignment.Far)
  208. {
  209. imageXPos = (int)((float)fullRectangle.X + (float)fullRectangle.Width - (float)imageSize.Width);
  210. }
  211. imageRectangle = new Rectangle(imageXPos, (int)((float)fullRectangle.Y + (((float)fullRectangle.Height - (float)imageSize.Height) / 2)), imageSize.Width, imageSize.Height);
  212. if (!ToggleSwitch.Enabled && ToggleSwitch.GrayWhenDisabled)
  213. g.DrawImage(ToggleSwitch.OnSideImage, imageRectangle, 0, 0, ToggleSwitch.OnSideImage.Width, ToggleSwitch.OnSideImage.Height, GraphicsUnit.Pixel, ImageHelper.GetGrayscaleAttributes());
  214. else
  215. g.DrawImageUnscaled(ToggleSwitch.OffSideImage, imageRectangle);
  216. }
  217. }
  218. else if (!string.IsNullOrEmpty(ToggleSwitch.OnText))
  219. {
  220. SizeF textSize = g.MeasureString(ToggleSwitch.OnText, ToggleSwitch.OffFont);
  221. float textXPos = fullRectangle.X;
  222. if (ToggleSwitch.OffSideAlignment == ToggleSwitchAlignment.Center)
  223. {
  224. textXPos = (float)fullRectangle.X + (((float)fullRectangle.Width - (float)textSize.Width) / 2);
  225. }
  226. else if (ToggleSwitch.OffSideAlignment == ToggleSwitchAlignment.Far)
  227. {
  228. textXPos = (float)fullRectangle.X + (float)fullRectangle.Width - (float)textSize.Width;
  229. }
  230. RectangleF textRectangle = new RectangleF(textXPos, (float)fullRectangle.Y + (((float)fullRectangle.Height - (float)textSize.Height) / 2), textSize.Width, textSize.Height);
  231. Color textForeColor = ToggleSwitch.OffForeColor;
  232. if (!ToggleSwitch.Enabled && ToggleSwitch.GrayWhenDisabled)
  233. textForeColor = textForeColor.ToGrayScale();
  234. using (Brush textBrush = new SolidBrush(textForeColor))
  235. {
  236. g.DrawString(ToggleSwitch.OnText, ToggleSwitch.OffFont, textBrush, textRectangle);
  237. }
  238. }
  239. }
  240. }
  241. }
  242. public override void RenderButton(Graphics g, Rectangle buttonRectangle)
  243. {
  244. Color buttonColor = ButtonColor;
  245. if (ToggleSwitch.IsButtonPressed)
  246. {
  247. buttonColor = ButtonColorPressed;
  248. }
  249. else if (ToggleSwitch.IsButtonHovered)
  250. {
  251. buttonColor = ButtonColorHovered;
  252. }
  253. if (!ToggleSwitch.Enabled && ToggleSwitch.GrayWhenDisabled)
  254. buttonColor = buttonColor.ToGrayScale();
  255. g.SetClip(buttonRectangle);
  256. using (Brush backBrush = new SolidBrush(buttonColor))
  257. {
  258. g.FillRectangle(backBrush, buttonRectangle);
  259. }
  260. g.ResetClip();
  261. }
  262. public override int GetButtonWidth()
  263. {
  264. return (int)((double)ToggleSwitch.Height / 3 * 2);
  265. }
  266. public override Rectangle GetButtonRectangle()
  267. {
  268. int buttonWidth = GetButtonWidth();
  269. return GetButtonRectangle(buttonWidth);
  270. }
  271. public override Rectangle GetButtonRectangle(int buttonWidth)
  272. {
  273. Rectangle buttonRect = new Rectangle(ToggleSwitch.ButtonValue, 0, buttonWidth, ToggleSwitch.Height);
  274. return buttonRect;
  275. }
  276. }
  277. }