Skip to content

Commit

Permalink
Implement own timer
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorGrycuk committed Nov 20, 2020
1 parent 7fad824 commit 042802a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
50 changes: 30 additions & 20 deletions streamdeck-magnifier/streamdeck-magnifier/Magnifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
using System;
using System.Threading.Tasks;
using System.Drawing;
using System.Timers;

namespace Magnifier
{
[PluginActionId("victorgrycuk.streamdeck.magnifier")]
public class Magnifier : PluginBase
{
private readonly MagnifierSettings settings;
private bool isRunning;
private readonly Timer Timer;
private bool isFixed;
private Point mouseLocation;
private DateTime dateTime;
Expand All @@ -21,16 +22,40 @@ public Magnifier(SDConnection connection, InitialPayload payload) : base(connect
settings = payload.Settings == null || payload.Settings.Count == 0
? MagnifierSettings.CreateDefaultSettings()
: payload.Settings.ToObject<MagnifierSettings>();

Timer = new Timer(10);
Timer.Elapsed += new ElapsedEventHandler(UpdateKey);
Timer.Enabled = false;
Timer.Start();
}

private void UpdateKey(object sender, ElapsedEventArgs e)
{
if (Timer.Enabled)
{
var location = isFixed ? mouseLocation : ScreenHelper.GetMouseLocation();
var img = ImageHelper.CopyFromScreen(settings.ZoomLevel, location);
img = ImageHelper.ResizeImage(img, 144, 144);

if (settings.UseCrosshair)
{
ImageHelper.DrawCrosshair(img);
}

Connection.SetImageAsync(img);
}
}

public override void Dispose()
{
Timer.Stop();
Timer.Dispose();
Logger.Instance.LogMessage(TracingLevel.INFO, $"Destructor called");
}

public override void KeyPressed(KeyPayload payload)
{
isRunning = !isRunning;
Timer.Enabled = !Timer.Enabled;
dateTime = DateTime.Now;
}

Expand All @@ -39,27 +64,12 @@ public override void KeyReleased(KeyPayload payload)
if ((DateTime.Now - dateTime).TotalSeconds > 2)
{
mouseLocation = ScreenHelper.GetMouseLocation();
isFixed = true;
isRunning = true;
isFixed = !isFixed;
Timer.Enabled = true;
}
}

public override void OnTick()
{
if (isRunning)
{
var location = isFixed ? mouseLocation : ScreenHelper.GetMouseLocation();
var img = ImageHelper.CopyFromScreen(settings.ZoomLevel, location);
img = ImageHelper.ResizeImage(img, 144, 144);

if (settings.UseCrosshair)
{
ImageHelper.DrawCrosshair(img);
}

Connection.SetImageAsync(img);
}
}
public override void OnTick() { }

public override void ReceivedSettings(ReceivedSettingsPayload payload)
{
Expand Down
2 changes: 1 addition & 1 deletion streamdeck-magnifier/streamdeck-magnifier/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"Description": "Converts a Stream Deck key into a screen magnifier, with up to 32x zoom.\nIncludes an option to move the mouse pixel by pixel for better precision.",
"Icon": "Images/magnifierIcon",
"URL": "https://github.com/VictorGrycuk/streamdeck-magnifier",
"Version": "1.2",
"Version": "1.2.1",
"CodePath": "streamdeck-magnifier",
"Category": "Magnifier",
"CategoryIcon": "Images/categoryIcon",
Expand Down

0 comments on commit 042802a

Please sign in to comment.