Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Commit

Permalink
Fix warnings and make app only render when visible and non-minimized
Browse files Browse the repository at this point in the history
  • Loading branch information
CptWesley committed Oct 13, 2021
1 parent 84e9cbd commit 6f7c93a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 28 deletions.
37 changes: 27 additions & 10 deletions src/NewWorldMinimap/MapControl.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,48 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewWorldMinimap
{
/// <summary>
/// Custom picture box for rendering as a circle.
/// </summary>
/// <seealso cref="PictureBox" />
public class MapControl : PictureBox
{
/// <summary>
/// Gets or sets a value indicating whether the image should be rendered as an ellipse.
/// </summary>
public bool IsCircular { get; set; }

/// <inheritdoc/>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (var path = new GraphicsPath())

if (e is null)
{
return;
}

if (this.Parent is MapForm mapParent)
{
using var path = new GraphicsPath();

if (IsCircular)
path.AddEllipse(0, 0, (this.Parent as MapForm).DisplayRectangle.Width, (this.Parent as MapForm).DisplayRectangle.Height - 1);
{
path.AddEllipse(0, 0, mapParent.DisplayRectangle.Width, mapParent.DisplayRectangle.Height - 1);
}
else
{
path.AddRectangle(new Rectangle(0, 0, Parent.Width, Parent.Height));
}

Region = new Region(path);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawRectangle(new Pen(new SolidBrush(this.BackColor), 1), 0, 0, this.Width, this.Height);
using Brush brush = new SolidBrush(this.BackColor);
using Pen pen = new Pen(brush, 1);
e.Graphics.DrawRectangle(pen, 0, 0, this.Width, this.Height);
}
}
}
Expand Down
40 changes: 22 additions & 18 deletions src/NewWorldMinimap/MapForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ private void SetupHotkeys()

private void ToggleInteractivity()
{
if (this.Opacity == 0)
if (!this.Visible)
{
return;
}

overlayMode = !overlayMode;
SafeInvoke(() =>
Expand Down Expand Up @@ -136,12 +138,11 @@ private void ToggleInteractivity()
this.TransparencyKey = BackColor;
}
});

}

private void ToggleVisibility()
{
SafeInvoke(() => this.Opacity = this.Opacity == 1.0 ? 0 : 1.0);
SafeInvoke(() => this.Visible = !this.Visible);
}

private void BuildMenu()
Expand Down Expand Up @@ -226,9 +227,9 @@ private void ToggleShape(object sender, EventArgs e)
{
picture.IsCircular = false;
}

this.TransparencyKey = System.Drawing.Color.Empty;
this.TransparencyKey = BackColor;

});
}

Expand Down Expand Up @@ -282,27 +283,30 @@ private void UpdateLoop()
{
sw.Restart();

if (pd.TryGetPosition(ScreenGrabber.TakeScreenshot(currentScreen), out Vector2 pos, this.debugEnabled, out Image<Rgba32> debugImage))
if (Visible && WindowState != FormWindowState.Minimized)
{
Vector2 posDifference = pos - lastPos;
if (pd.TryGetPosition(ScreenGrabber.TakeScreenshot(currentScreen), out Vector2 pos, this.debugEnabled, out Image<Rgba32> debugImage))
{
Vector2 posDifference = pos - lastPos;

if (posDifference != Vector2.Zero)
{
rotationAngle = Math.Atan2(posDifference.X, posDifference.Y);
}

if (posDifference != Vector2.Zero)
Redraw(pos, rotationAngle);

lastPos = pos;
}
else
{
rotationAngle = Math.Atan2(posDifference.X, posDifference.Y);
Console.WriteLine($"{i}: Failure");
}

Redraw(pos, rotationAngle);
DrawDebugImage(debugImage);

lastPos = pos;
i++;
}
else
{
Console.WriteLine($"{i}: Failure");
}

DrawDebugImage(debugImage);

i++;

sw.Stop();
long elapsed = sw.ElapsedMilliseconds;
Expand Down

0 comments on commit 6f7c93a

Please sign in to comment.