Skip to content

Commit

Permalink
refactor: changing timer to be static and shared across actuators wit…
Browse files Browse the repository at this point in the history
…h lazy intantiation
  • Loading branch information
guifes committed Mar 30, 2024
1 parent ced590e commit f0b56ff
Showing 1 changed file with 27 additions and 32 deletions.
59 changes: 27 additions & 32 deletions src/motion/actuators/SimpleActuator.hx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class SimpleActuator<T, U> extends GenericActuator<T> {

private static var actuators = new Array<SimpleActuator<Dynamic, Dynamic>> ();
private static var actuatorsLength = 0;
private static var addedEvent = false;
private static var time:Float;
private static var timerInitialized: Bool;

#if (!flash && !nme && !openfl && !lime && !js)
private static var timer:Timer;
Expand All @@ -49,9 +50,31 @@ class SimpleActuator<T, U> extends GenericActuator<T> {
private var startTime:Float;
private var toggleVisible:Bool;

private static function setup()
{
if (timerInitialized)
return;

timerInitialized = true;

#if !actuate_manual_update
#if (flash || nme || openfl)
Lib.current.stage.addEventListener(Event.ENTER_FRAME, stage_onEnterFrame);
#elseif lime
Application.current.onUpdate.add(stage_onEnterFrame);
#elseif js
Browser.window.requestAnimationFrame(stage_onEnterFrame);
#else
timer = new Timer(Std.int(1000 / 30));
timer.run = stage_onEnterFrame;
#end
#end
}

public function new (target:T, duration:Float, properties:Dynamic) {

setup();

active = true;
propertyDetails = new Array ();
sendChange = false;
Expand All @@ -61,39 +84,9 @@ class SimpleActuator<T, U> extends GenericActuator<T> {
setVisible = false;
toggleVisible = false;

#if !actuate_manual_time
#if (flash || nme || openfl)
startTime = Lib.getTimer () / 1000;
#elseif lime
startTime = System.getTimer () / 1000;
#elseif js
startTime = Browser.window.performance.now () / 1000;
#else
startTime = Timer.stamp ();
#end
#else
startTime = getTime();
#end
startTime = time;

super (target, duration, properties);

if (!addedEvent) {

addedEvent = true;
#if !actuate_manual_update
#if (flash || nme || openfl)
Lib.current.stage.addEventListener (Event.ENTER_FRAME, stage_onEnterFrame);
#elseif lime
Application.current.onUpdate.add (stage_onEnterFrame);
#elseif js
Browser.window.requestAnimationFrame(stage_onEnterFrame);
#else
timer = new Timer (Std.int(1000 / 30));
timer.run = stage_onEnterFrame;
#end
#end
}

}

//For instant transition to start state without shaking
Expand Down Expand Up @@ -621,6 +614,8 @@ class SimpleActuator<T, U> extends GenericActuator<T> {
#else
var currentTime = getTime ();
#end

time = currentTime;

var actuator:SimpleActuator<Dynamic, Dynamic>;

Expand Down

0 comments on commit f0b56ff

Please sign in to comment.