From fee5437483ff1a071f87c6089b4d04561d0dfbfb Mon Sep 17 00:00:00 2001 From: xLinka Date: Fri, 13 Dec 2024 00:49:46 +0000 Subject: [PATCH 1/4] Create Local Stopwatch.cs Local Stopwatch Related to https://github.com/Yellow-Dog-Man/Resonite-Issues/issues/3337 --- .../ProtoFlux/Flow/Time/Local Stopwatch.cs | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 ProjectObsidian/ProtoFlux/Flow/Time/Local Stopwatch.cs diff --git a/ProjectObsidian/ProtoFlux/Flow/Time/Local Stopwatch.cs b/ProjectObsidian/ProtoFlux/Flow/Time/Local Stopwatch.cs new file mode 100644 index 0000000..ea4d03b --- /dev/null +++ b/ProjectObsidian/ProtoFlux/Flow/Time/Local Stopwatch.cs @@ -0,0 +1,101 @@ +using FrooxEngine.ProtoFlux; +using ProtoFlux.Core; +using ProtoFlux.Runtimes.Execution; + +namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Time +{ + [NodeCategory("Obsidian/Time")] + [NodeName("Local Stopwatch")] + public class LocalStopwatch : VoidNode + { + [ContinuouslyChanging] + public readonly ValueOutput ElapsedTime; + + [ContinuouslyChanging] + public readonly ValueOutput IsRunning; + + [PossibleContinuations(new string[] { "OnStart" })] + public readonly Operation Start; + + [PossibleContinuations(new string[] { "OnStop" })] + public readonly Operation Stop; + + [PossibleContinuations(new string[] { "OnReset" })] + public readonly Operation Reset; + + public Continuation OnStart; + public Continuation OnStop; + public Continuation OnReset; + + private double _startTime = -1.0; + private double _elapsedTime = 0.0; + private bool _isRunning = false; + + public readonly ValueInput Condition; + + public LocalStopwatch() + { + ElapsedTime = new ValueOutput(this); + IsRunning = new ValueOutput(this); + Start = new Operation(this, 0); + Stop = new Operation(this, 1); + Reset = new Operation(this, 2); + } + + protected override void ComputeOutputs(FrooxEngineContext context) + { + double currentTime = context.World.Time.WorldTime; + + // Check if Condition is true + bool conditionMet = Condition.Evaluate(context); // Directly read the value of Condition + + // Update elapsed time if running and condition is true + if (_isRunning && conditionMet) + { + if (_startTime > 0) + { + _elapsedTime += currentTime - _startTime; + } + _startTime = currentTime; + } + else + { + _startTime = -1.0; + } + + // Write outputs + ElapsedTime.Write((float)_elapsedTime, context); + IsRunning.Write(_isRunning, context); + } + + + + + + + + private IOperation DoStart(FrooxEngineContext context) + { + _isRunning = true; + if (_startTime < 0) + { + _startTime = context.World.Time.WorldTime; + } + return OnStart.Target; + } + + private IOperation DoStop(FrooxEngineContext context) + { + _isRunning = false; + return OnStop.Target; + } + + private IOperation DoReset(FrooxEngineContext context) + { + _isRunning = false; + _elapsedTime = 0.0; + _startTime = -1.0; + return OnReset.Target; + } + } +} \ No newline at end of file From a081a15c828460bd9a10b4100876485510bffe23 Mon Sep 17 00:00:00 2001 From: xLinka Date: Fri, 13 Dec 2024 01:01:39 +0000 Subject: [PATCH 2/4] Update Local Stopwatch.cs Removed conditional --- .../ProtoFlux/Flow/Time/Local Stopwatch.cs | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/ProjectObsidian/ProtoFlux/Flow/Time/Local Stopwatch.cs b/ProjectObsidian/ProtoFlux/Flow/Time/Local Stopwatch.cs index ea4d03b..e7e31e6 100644 --- a/ProjectObsidian/ProtoFlux/Flow/Time/Local Stopwatch.cs +++ b/ProjectObsidian/ProtoFlux/Flow/Time/Local Stopwatch.cs @@ -31,8 +31,6 @@ public class LocalStopwatch : VoidNode private double _elapsedTime = 0.0; private bool _isRunning = false; - public readonly ValueInput Condition; - public LocalStopwatch() { ElapsedTime = new ValueOutput(this); @@ -46,11 +44,8 @@ protected override void ComputeOutputs(FrooxEngineContext context) { double currentTime = context.World.Time.WorldTime; - // Check if Condition is true - bool conditionMet = Condition.Evaluate(context); // Directly read the value of Condition - - // Update elapsed time if running and condition is true - if (_isRunning && conditionMet) + // Update elapsed time if running + if (_isRunning) { if (_startTime > 0) { @@ -68,12 +63,6 @@ protected override void ComputeOutputs(FrooxEngineContext context) IsRunning.Write(_isRunning, context); } - - - - - - private IOperation DoStart(FrooxEngineContext context) { _isRunning = true; @@ -98,4 +87,4 @@ private IOperation DoReset(FrooxEngineContext context) return OnReset.Target; } } -} \ No newline at end of file +} From 01e160f2d4b3cff47019dcc7f6f7037b17e1080b Mon Sep 17 00:00:00 2001 From: xLinka Date: Fri, 13 Dec 2024 20:55:40 +0000 Subject: [PATCH 3/4] Update Local Stopwatch.cs --- .../ProtoFlux/Flow/Time/Local Stopwatch.cs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/ProjectObsidian/ProtoFlux/Flow/Time/Local Stopwatch.cs b/ProjectObsidian/ProtoFlux/Flow/Time/Local Stopwatch.cs index e7e31e6..b94d899 100644 --- a/ProjectObsidian/ProtoFlux/Flow/Time/Local Stopwatch.cs +++ b/ProjectObsidian/ProtoFlux/Flow/Time/Local Stopwatch.cs @@ -47,15 +47,11 @@ protected override void ComputeOutputs(FrooxEngineContext context) // Update elapsed time if running if (_isRunning) { - if (_startTime > 0) + if (_startTime >= 0) // Only update if startTime is initialized { _elapsedTime += currentTime - _startTime; } - _startTime = currentTime; - } - else - { - _startTime = -1.0; + _startTime = currentTime; } // Write outputs @@ -66,7 +62,7 @@ protected override void ComputeOutputs(FrooxEngineContext context) private IOperation DoStart(FrooxEngineContext context) { _isRunning = true; - if (_startTime < 0) + if (_startTime < 0) { _startTime = context.World.Time.WorldTime; } @@ -81,9 +77,8 @@ private IOperation DoStop(FrooxEngineContext context) private IOperation DoReset(FrooxEngineContext context) { - _isRunning = false; _elapsedTime = 0.0; - _startTime = -1.0; + _startTime = -1.0; return OnReset.Target; } } From 885cb2072756eeea4a7e56e7403f6b216f6abcb5 Mon Sep 17 00:00:00 2001 From: Nytra <14206961+Nytra@users.noreply.github.com> Date: Fri, 13 Dec 2024 21:15:54 +0000 Subject: [PATCH 4/4] Some adjustments to startTime --- .../ProtoFlux/Flow/Time/Local Stopwatch.cs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/ProjectObsidian/ProtoFlux/Flow/Time/Local Stopwatch.cs b/ProjectObsidian/ProtoFlux/Flow/Time/Local Stopwatch.cs index b94d899..7bfed1d 100644 --- a/ProjectObsidian/ProtoFlux/Flow/Time/Local Stopwatch.cs +++ b/ProjectObsidian/ProtoFlux/Flow/Time/Local Stopwatch.cs @@ -47,10 +47,7 @@ protected override void ComputeOutputs(FrooxEngineContext context) // Update elapsed time if running if (_isRunning) { - if (_startTime >= 0) // Only update if startTime is initialized - { - _elapsedTime += currentTime - _startTime; - } + _elapsedTime += currentTime - _startTime; _startTime = currentTime; } @@ -62,10 +59,7 @@ protected override void ComputeOutputs(FrooxEngineContext context) private IOperation DoStart(FrooxEngineContext context) { _isRunning = true; - if (_startTime < 0) - { - _startTime = context.World.Time.WorldTime; - } + _startTime = context.World.Time.WorldTime; return OnStart.Target; } @@ -78,7 +72,7 @@ private IOperation DoStop(FrooxEngineContext context) private IOperation DoReset(FrooxEngineContext context) { _elapsedTime = 0.0; - _startTime = -1.0; + _startTime = context.World.Time.WorldTime; return OnReset.Target; } }