ToggleSwitchRenderer.cs 16 KB

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