2
0
Эх сурвалжийг харах

Updated MoonToggle

A little update :)
vadis 2 жил өмнө
parent
commit
82cca4a2f9

+ 56 - 47
Optimizer/Controls/MoonToggle.cs

@@ -1,77 +1,86 @@
-using System.ComponentModel;
+using System;
 using System.Drawing;
 using System.Drawing.Drawing2D;
+using System.Drawing.Text;
 using System.Windows.Forms;
 
 namespace Optimizer
 {
     public sealed class MoonToggle : CheckBox
     {
-        bool solidStyle = true;
+        private readonly Timer AnimationTimer = new Timer();
+        private int CirclePos = 3;
+        private int Alpha = 0;
 
-        [Browsable(true)]
-        public override string Text
+        public MoonToggle()
         {
-            get { return base.Text; }
-            set { }
+            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true);
+            DoubleBuffered = true;
+            Height = 22; Width = 46;
+            AnimationTimer.Interval = 1;
+            AnimationTimer.Tick += new EventHandler(AnimationTick);
         }
 
-        [DefaultValue(true)]
-        public bool SolidStyle
+        protected override void OnHandleCreated(EventArgs e)
         {
-            get { return solidStyle; }
-            set
-            {
-                solidStyle = value;
-                this.Invalidate();
-            }
+            base.OnHandleCreated(e);
+            AnimationTimer.Start();
         }
 
-        public MoonToggle()
+        protected override void OnResize(EventArgs e)
         {
-            this.DoubleBuffered = true;
-            this.MinimumSize = new Size(46, 22);
-            this.ForeColor = Color.White;
+            Height = 22; Width = 44;
+            Invalidate();
         }
 
-        private GraphicsPath GetFigurePath()
+        protected override void OnPaint(PaintEventArgs pevent)
         {
-            int arcSize = this.Height - 1;
-            Rectangle leftArc = new Rectangle(0, 0, arcSize, arcSize);
-            Rectangle rightArc = new Rectangle(this.Width - arcSize - 2, 0, arcSize, arcSize);
+            pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
+            pevent.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
+            pevent.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
+            pevent.Graphics.InterpolationMode = InterpolationMode.High;
+            pevent.Graphics.Clear(Parent.BackColor);
 
-            GraphicsPath path = new GraphicsPath();
-            path.StartFigure();
-            path.AddArc(leftArc, 90, 180);
-            path.AddArc(rightArc, 270, 180);
-            path.CloseFigure();
+            GraphicsPath backRect = new GraphicsPath();
+            backRect.AddArc(new RectangleF(0.5f, 0.5f, Height - 1, Height - 1), 90, 180);
+            backRect.AddArc(new RectangleF(Width - Height + 0.5f, 0.5f, Height - 1, Height - 1), 270, 180);
+            backRect.CloseAllFigures();
 
-            return path;
+            pevent.Graphics.FillPath(new SolidBrush(Options.BackAccentColor), backRect);
+            pevent.Graphics.FillPath(new SolidBrush(Color.FromArgb(Alpha, Options.ForegroundColor.R, Options.ForegroundColor.G, Options.ForegroundColor.B)), backRect);
+            pevent.Graphics.FillEllipse(new SolidBrush(Color.White), new RectangleF(CirclePos, 3, 16, 16));
         }
 
-        protected override void OnPaint(PaintEventArgs pevent)
+        private void AnimationTick(object sender, EventArgs e)
         {
-            int toggleSize = this.Height - 5;
-            pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
-            pevent.Graphics.Clear(this.Parent.BackColor);
-
-            if (this.Checked) //ON
+            if (Checked)
             {
-                if (solidStyle)
-                    pevent.Graphics.FillPath(new SolidBrush(Options.ForegroundColor), GetFigurePath());
-                else pevent.Graphics.DrawPath(new Pen(Options.ForegroundColor, 2), GetFigurePath());
-                //Draw the toggle
-                pevent.Graphics.FillEllipse(new SolidBrush(Options.TextColor),
-                  new Rectangle(this.Width - this.Height + 1, 2, toggleSize, toggleSize));
+                if (CirclePos < 26)
+                {
+                    CirclePos += 2;
+                    Invalidate();
+                }
+
+                if (Alpha < 255)
+                {
+                    Alpha += 15;
+                    if (CirclePos > 26)
+                        Invalidate();
+                }
             }
-            else //OFF
+            else
             {
-                if (solidStyle)
-                    pevent.Graphics.FillPath(new SolidBrush(Options.BackAccentColor), GetFigurePath());
-                else pevent.Graphics.DrawPath(new Pen(Options.BackAccentColor, 2), GetFigurePath());
-                //Draw the toggle
-                pevent.Graphics.FillEllipse(new SolidBrush(Color.White),
-                  new Rectangle(2, 2, toggleSize, toggleSize));
+                if (CirclePos > 3)
+                {
+                    CirclePos -= 2;
+                    Invalidate();
+                }
+
+                if (Alpha > 0)
+                {
+                    Alpha -= 15;
+                    Invalidate();
+                }
             }
         }
     }