From eb0ddc786451a51fa1edb4e7dbac32db4b5fb3b5 Mon Sep 17 00:00:00 2001 From: KillzXGaming Date: Mon, 17 Feb 2020 19:50:18 -0500 Subject: [PATCH] Some color selector improvements --- .../Forms/Color/VertexColorBox.cs | 37 ++++++--- .../Forms/Color/VertexColorTopBottomBox.cs | 78 ++++++++++++++++--- 2 files changed, 94 insertions(+), 21 deletions(-) diff --git a/Switch_Toolbox_Library/Forms/Color/VertexColorBox.cs b/Switch_Toolbox_Library/Forms/Color/VertexColorBox.cs index 613a5ff5..fc184dd3 100644 --- a/Switch_Toolbox_Library/Forms/Color/VertexColorBox.cs +++ b/Switch_Toolbox_Library/Forms/Color/VertexColorBox.cs @@ -391,30 +391,45 @@ namespace Toolbox.Library.Forms if (mouseLoc != Point.Empty) { if (AllHit.IsHit(mouseLoc)) - DrawSelectionOutline(pe, AllHit); + DrawSelectionOutline(pe, AllHit, AllColor); if (TopLeftHit.IsHit(mouseLoc)) - DrawSelectionOutline(pe, TopLeftHit); + DrawSelectionOutline(pe, TopLeftHit, TopLeftColor); if (TopRightHit.IsHit(mouseLoc)) - DrawSelectionOutline(pe, TopRightHit); + DrawSelectionOutline(pe, TopRightHit, TopRightColor); if (BottomLeftHit.IsHit(mouseLoc)) - DrawSelectionOutline(pe, BottomLeftHit); + DrawSelectionOutline(pe, BottomLeftHit, BottomLeftColor); if (BottomRightHit.IsHit(mouseLoc)) - DrawSelectionOutline(pe, BottomRightHit); + DrawSelectionOutline(pe, BottomRightHit, BottomRightColor); if (TopHit.IsHit(mouseLoc)) - DrawSelectionOutline(pe, TopHit); + DrawSelectionOutline(pe, TopHit, ColorBlend(TopRightColor, TopLeftColor)); if (BottomHit.IsHit(mouseLoc)) - DrawSelectionOutline(pe, BottomHit); + DrawSelectionOutline(pe, BottomHit, ColorBlend(BottomRightColor, BottomLeftColor)); if (RightHit.IsHit(mouseLoc)) - DrawSelectionOutline(pe, RightHit); + DrawSelectionOutline(pe, RightHit, ColorBlend(BottomRightColor, TopRightColor)); if (LeftHit.IsHit(mouseLoc)) - DrawSelectionOutline(pe, LeftHit); + DrawSelectionOutline(pe, LeftHit, ColorBlend(BottomLeftColor, TopLeftColor)); } base.OnPaint(pe); } - private void DrawSelectionOutline(PaintEventArgs pe, Rectangle rect) { + private static Color ColorBlend(Color A, Color B) + { + Color[] colors = new Color[2] + { A, B }; + + return Color.FromArgb( + (int)colors.Average(a => a.A), + (int)colors.Average(a => a.R), + (int)colors.Average(a => a.G), + (int)colors.Average(a => a.B)); + } + + private void DrawSelectionOutline(PaintEventArgs pe, Rectangle rect, Color color) { //Select entire regions + + var colorAmount = color.GrayScale(true).Inverse(); + int lineThickness = 2; Rectangle selection = rect; if (rect == TopHit || rect == BottomHit) { @@ -428,7 +443,7 @@ namespace Toolbox.Library.Forms lineThickness = 2; } - pe.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black), lineThickness), new Rectangle( + pe.Graphics.DrawRectangle(new Pen(new SolidBrush(colorAmount), lineThickness), new Rectangle( selection.X + (lineThickness / 2), selection.Y + (lineThickness / 2), selection.Width - lineThickness, selection.Height - lineThickness)); } diff --git a/Switch_Toolbox_Library/Forms/Color/VertexColorTopBottomBox.cs b/Switch_Toolbox_Library/Forms/Color/VertexColorTopBottomBox.cs index b3c08abc..231fdc5d 100644 --- a/Switch_Toolbox_Library/Forms/Color/VertexColorTopBottomBox.cs +++ b/Switch_Toolbox_Library/Forms/Color/VertexColorTopBottomBox.cs @@ -14,6 +14,7 @@ namespace Toolbox.Library.Forms { public EventHandler OnColorChanged; + private Point mouseLoc = Point.Empty; private Color TColor; private Color BColor; @@ -65,6 +66,8 @@ namespace Toolbox.Library.Forms this.BackgroundImage = Properties.Resources.CheckerBackground; this.BackColor = Color.Transparent; this.MouseClick += OnMouseClick; + this.MouseMove += OnMouseMove; + this.MouseLeave += OnMouseLeave; this.SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | @@ -72,8 +75,23 @@ namespace Toolbox.Library.Forms true); } + private void OnMouseMove(object sender, MouseEventArgs e) { + if (dialogActive) return; + + mouseLoc = e.Location; + Invalidate(); + } + + private void OnMouseLeave(object sender, EventArgs e) { + if (dialogActive) return; + + mouseLoc = Point.Empty; + Invalidate(); + } + public void DisposeControl() { + mouseLoc = Point.Empty; if (colorDlg != null) { colorDlg.Close(); @@ -102,7 +120,9 @@ namespace Toolbox.Library.Forms colorDlg.Show(); colorDlg.FormClosed += delegate { + mouseLoc = Point.Empty; dialogActive = false; + this.Invalidate(); }; colorDlg.ColorChanged += delegate { @@ -137,7 +157,9 @@ namespace Toolbox.Library.Forms colorDlg = new STColorDialog(color); colorDlg.FormClosed += delegate { + mouseLoc = Point.Empty; dialogActive = false; + this.Invalidate(); }; colorDlg.Show(); colorDlg.ColorChanged += delegate @@ -153,6 +175,8 @@ namespace Toolbox.Library.Forms private Rectangle BottomHit; private Rectangle AllHit; + private bool DisplayHitboxes = true; + protected override void OnPaint(PaintEventArgs pe) { pe.Graphics.InterpolationMode = InterpolationMode.Bilinear; @@ -163,16 +187,14 @@ namespace Toolbox.Library.Forms int halfWidth = r.Width / 2; int halfHeight = r.Height / 2; - int LeftX = 10; - int RightX = r.Width - 30; - int topY = 10; - int BottomY = r.Height - 25; - var linearGradientBrush = new LinearGradientBrush(r, TopColor, BottomColor, LinearGradientMode.Vertical); pe.Graphics.FillRectangle(linearGradientBrush, r); + int topY = 10; + int BottomY = r.Height - 25; + var font = new Font(this.Font, FontStyle.Bold); using (Brush br = new SolidBrush(AllColor.GrayScale(true).Inverse())) @@ -184,14 +206,50 @@ namespace Toolbox.Library.Forms using (Brush br = new SolidBrush(BottomColor.GrayScale(true).Inverse())) pe.Graphics.DrawString("B", font, br, new Point(halfWidth - 7, BottomY)); - // pe.Graphics.FillRectangle(linearGradientBrush, ClientRectangle); + topY = 0; + BottomY = r.Height - 32; - const int hitSize = 40; - TopHit = new Rectangle(halfWidth - 10, topY, hitSize, hitSize); - BottomHit = new Rectangle(halfWidth - 10, BottomY, hitSize, hitSize); - AllHit = new Rectangle(halfWidth - 10, halfHeight - 10, hitSize, hitSize); + const int hitSize = 32; + TopHit = new Rectangle(r.X, topY, r.Width, hitSize); + BottomHit = new Rectangle(r.X, BottomY, r.Width, hitSize); + AllHit = new Rectangle(r.X, halfHeight - 16, r.Width, hitSize); + + if (mouseLoc != Point.Empty) + { + if (AllHit.IsHit(mouseLoc)) + DrawSelectionOutline(pe, AllHit, AllColor); + if (TopHit.IsHit(mouseLoc)) + DrawSelectionOutline(pe, TopHit, TopColor); + if (BottomHit.IsHit(mouseLoc)) + DrawSelectionOutline(pe, BottomHit, BottomColor); + } + + // pe.Graphics.FillRectangle(linearGradientBrush, ClientRectangle); base.OnPaint(pe); } + + private void DrawSelectionOutline(PaintEventArgs pe, Rectangle rect, Color color) + { + //Select entire regions + + var colorAmount = color.GrayScale(true).Inverse(); + + int lineThickness = 2; + Rectangle selection = rect; + if (rect == TopHit || rect == BottomHit) + { + selection = new Rectangle(rect.X, rect.Y, pe.ClipRectangle.Width, rect.Height); + } + if (rect == AllHit) + { + selection = ClientRectangle; + lineThickness = 2; + } + + pe.Graphics.DrawRectangle(new Pen(new SolidBrush(colorAmount), lineThickness), new Rectangle( + selection.X + (lineThickness / 2), selection.Y + (lineThickness / 2), + selection.Width - lineThickness, selection.Height - lineThickness)); + } } }