Skip to content

Commit

Permalink
Replay reimplementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaifm committed Nov 9, 2022
1 parent 22e2fa2 commit 5700a99
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 59 deletions.
85 changes: 79 additions & 6 deletions ElegantRecorder/AutomationEngine.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;

namespace ElegantRecorder
{
Expand Down Expand Up @@ -61,6 +61,41 @@ public virtual bool FillClipboardAction(ref UIAction uiAction, ref string status
return true;
}

public virtual void ReplayAction(UIAction action, ref string status)
{
bool result = true;

if (action.EventType == "click")
{
result = ReplayClickAction(action, ref status);
App.PlayAction();
}
else if (action.EventType == "mousemove")
{
result = ReplayMouseMoveAction(action, ref status);
App.PlayAction();
}
else if (action.EventType == "mousewheel")
{
result = ReplayMouseWheelAction(action, ref status);
App.PlayAction();
}
else if (action.EventType == "keypress")
{
result = ReplayKeypressAction(action, ref status);
App.PlayAction();
}
else if (action.EventType == "clipboard")
{
result = ReplayClipboardAction(action, ref status);
App.PlayAction();
}
else if (action.EventType == "mousepath")
{
result = ReplayMousePathAction(action, ref status);
}
}

public abstract bool ReplayClickAction(UIAction action, ref string status);

public virtual bool ReplayMouseMoveAction(UIAction action, ref string status)
Expand All @@ -69,15 +104,53 @@ public virtual bool ReplayMouseMoveAction(UIAction action, ref string status)
return true;
}

public virtual bool ReplayMousePathAction(UIAction action, string playbackSpeed, ref string status)
Timer mousePathTimer = null;
int currentMousePathStep = -1;
UIAction mousePathAction = null;

public virtual bool ReplayMousePathAction(UIAction action, ref string status)
{
mousePathAction = action;

currentMousePathStep = -1;

mousePathTimer = new Timer();
mousePathTimer.Tick += MousePathTimer_Tick;
PlayMousePathStep();

return false;
}

private void PlayMousePathStep()
{
foreach(var m in action.MoveData)
mousePathTimer.Stop();

currentMousePathStep++;

if (currentMousePathStep <= mousePathAction.MoveData.Length - 1)
{
if (mousePathAction.MoveData[currentMousePathStep].T > 0)
{
mousePathTimer.Interval = ElegantOptions.GetPlaybackSpeed(App.ElegantOptions.PlaybackSpeed, mousePathAction.MoveData[currentMousePathStep].T);
mousePathTimer.Start();
}
else
{
MousePathTimer_Tick(null, new EventArgs());
}
}
else
{
App.WinAPI.MouseMove(m.X, m.Y, UIntPtr.Zero);
Thread.Sleep((int)ElegantOptions.GetPlaybackSpeedDuration(playbackSpeed, m.T));
App.PlayAction();
}
}

return true;
private void MousePathTimer_Tick(object? sender, EventArgs e)
{
var m = mousePathAction.MoveData[currentMousePathStep];
App.WinAPI.MouseMove(m.X, m.Y, UIntPtr.Zero);

PlayMousePathStep();
}

public virtual bool ReplayMouseWheelAction(UIAction action, ref string status)
Expand Down
12 changes: 6 additions & 6 deletions ElegantRecorder/ElegantOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,23 @@ public void Save(string FilePath)
File.WriteAllText(FilePath, JsonSerializer.Serialize(this));
}

public static double GetPlaybackSpeedDuration(string playbackSpeed, double initialDuration)
public static int GetPlaybackSpeed(string playbackSpeed, double? initialDuration)
{
switch(playbackSpeed)
{
case "Fastest":
return 0;
case "Fast":
return 0.5 * initialDuration;
return (int)(0.5 * initialDuration);
case "Normal":
return initialDuration;
return (int)(initialDuration);
case "Slow":
return 1.5 * initialDuration;
return (int)(1.5 * initialDuration);
case "Slowest":
return 2.0 * initialDuration;
return (int)(2.0 * initialDuration);
}

return initialDuration;
return (int)initialDuration;
}
}
}
61 changes: 14 additions & 47 deletions ElegantRecorder/ElegantRecorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,93 +332,60 @@ private void buttonReplay_Click(object sender, EventArgs e)
ClearStatus();

Replay();

SetStatus(status);
}

private int currentActionIndex = 0;
private int currentActionIndex = -1;
private Timer replayTimer = null;
private Recording ReplayRec = null;
private bool replayInterrupted = false;

void Replay()
public void Replay()
{
ReplayRec = new Recording(this, CurrentRecordingName);
ReplayRec.Load();

if (currentActionIndex >= ReplayRec.UIActions.Length - 1)
currentActionIndex = 0;
currentActionIndex = -1;

if (ReplayRec.UIActions.Length > 0)
{
replayTimer = new Timer();
replayTimer.Tick += ReplayTimer_Tick;
PlayAction();
}
replayTimer = new Timer();
replayTimer.Tick += ReplayTimer_Tick;
PlayAction();
}

private void PlayAction()
public void PlayAction()
{
replayTimer.Stop();

if (replayInterrupted)
{
replayTimer.Stop();
SetStatus("Replay interrupted");
return;
}

var action = ReplayRec.UIActions[currentActionIndex];

if (action.EventType == "click")
{
AutomationEngine.ReplayClickAction(action, ref status);
}
else if (action.EventType == "mousemove")
{
AutomationEngine.ReplayMouseMoveAction(action, ref status);
}
else if (action.EventType == "mousewheel")
{
AutomationEngine.ReplayMouseWheelAction(action, ref status);
}
else if (action.EventType == "keypress")
{
AutomationEngine.ReplayKeypressAction(action, ref status);
}
else if (action.EventType == "clipboard")
{
AutomationEngine.ReplayClipboardAction(action, ref status);
}
else if (action.EventType == "mousepath")
{
AutomationEngine.ReplayMousePathAction(action, ReplayRec.PlaybackSpeed, ref status);
}
currentActionIndex++;

if (currentActionIndex < ReplayRec.UIActions.Length - 1)
if (currentActionIndex <= ReplayRec.UIActions.Length - 1)
{
currentActionIndex++;

if (ReplayRec.UIActions[currentActionIndex].elapsed != null && ReplayRec.UIActions[currentActionIndex].elapsed != 0)
{
replayTimer.Interval = (int)ElegantOptions.GetPlaybackSpeedDuration(ReplayRec.PlaybackSpeed, (double)ReplayRec.UIActions[currentActionIndex].elapsed);
replayTimer.Interval = ElegantOptions.GetPlaybackSpeed(ReplayRec.PlaybackSpeed, ReplayRec.UIActions[currentActionIndex].elapsed);
replayTimer.Start();
}
else
{
PlayAction();
ReplayTimer_Tick(this, new EventArgs());
}
}
else
{
replayTimer.Stop();
SetStatus("Replay finished");
ResetButtons();
}
}

private void ReplayTimer_Tick(object? sender, EventArgs e)
{
replayTimer.Stop();
PlayAction();
AutomationEngine.ReplayAction(ReplayRec.UIActions[currentActionIndex], ref status);
}

private void buttonSettings_Click(object sender, EventArgs e)
Expand Down

0 comments on commit 5700a99

Please sign in to comment.