diff --git a/ElegantRecorder/AutomationEngine.cs b/ElegantRecorder/AutomationEngine.cs index 1d5ddaf..56b9327 100644 --- a/ElegantRecorder/AutomationEngine.cs +++ b/ElegantRecorder/AutomationEngine.cs @@ -1,6 +1,6 @@ using System; using System.Collections.Generic; -using System.Threading; +using System.Windows.Forms; namespace ElegantRecorder { @@ -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) @@ -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) diff --git a/ElegantRecorder/ElegantOptions.cs b/ElegantRecorder/ElegantOptions.cs index b7881cf..438ba22 100644 --- a/ElegantRecorder/ElegantOptions.cs +++ b/ElegantRecorder/ElegantOptions.cs @@ -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; } } } diff --git a/ElegantRecorder/ElegantRecorder.cs b/ElegantRecorder/ElegantRecorder.cs index b852089..407d442 100644 --- a/ElegantRecorder/ElegantRecorder.cs +++ b/ElegantRecorder/ElegantRecorder.cs @@ -332,84 +332,52 @@ 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(); } @@ -417,8 +385,7 @@ private void PlayAction() 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)