From 3743868cc6a766a45cc52ccac3fd6f4c86b44df4 Mon Sep 17 00:00:00 2001 From: jberg Date: Tue, 10 Sep 2019 09:17:26 -0700 Subject: [PATCH] Implement timer (#909) * Implement timer * change isRunning in objects to match Winamp behavior, std.mi is wrong * need to set start time for timer * add comment to updated objects array --- modern/src/maki-interpreter/objects.js | 2 +- modern/src/maki-interpreter/objects.test.js | 6 --- modern/src/runtime/Timer.ts | 51 +++++++++++++++------ 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/modern/src/maki-interpreter/objects.js b/modern/src/maki-interpreter/objects.js index 1821c06f..83ef8416 100644 --- a/modern/src/maki-interpreter/objects.js +++ b/modern/src/maki-interpreter/objects.js @@ -2171,7 +2171,7 @@ const objects = { { parameters: [], name: "isRunning", - result: "", + result: "boolean", // The std.mi has this set as void, but we checked in Winamp and confirmed it returns 0/1 }, { parameters: [], diff --git a/modern/src/maki-interpreter/objects.test.js b/modern/src/maki-interpreter/objects.test.js index ddd22781..368208e3 100644 --- a/modern/src/maki-interpreter/objects.test.js +++ b/modern/src/maki-interpreter/objects.test.js @@ -208,12 +208,6 @@ Set { "Region.getboundingboxy", "Region.getboundingboxw", "Region.getboundingboxh", - "Timer.setdelay", - "Timer.start", - "Timer.stop", - "Timer.ontimer", - "Timer.getdelay", - "Timer.isrunning", "Timer.getskipped", "Group.getnumobjects", "Group.enumobject", diff --git a/modern/src/runtime/Timer.ts b/modern/src/runtime/Timer.ts index 7684f03a..f7cc54b5 100644 --- a/modern/src/runtime/Timer.ts +++ b/modern/src/runtime/Timer.ts @@ -2,6 +2,28 @@ import MakiObject from "./MakiObject"; import { unimplementedWarning } from "../utils"; class Timer extends MakiObject { + _speed: number; + _animationStartTime: number; + _animationCancelID: number; + + constructor(node, parent, annotations, store) { + super(node, parent, annotations, store); + + this._speed = 200; + this._animationStartTime = 0; + this._animationCancelID = null; + } + + _animationLoop(): void { + this._animationCancelID = window.requestAnimationFrame(currentTime => { + if (currentTime > this._animationStartTime + this._speed) { + this._animationStartTime = currentTime; + this.ontimer(); + } + this._animationLoop(); + }); + } + /** * getclassname() * @@ -12,31 +34,30 @@ class Timer extends MakiObject { return "Timer"; } - setdelay(millisec: number) { - unimplementedWarning("setDelay"); + setdelay(millisec: number): void { + this._speed = millisec; } - start() { - unimplementedWarning("start"); + start(): void { + this._animationStartTime = performance.now(); + this._animationLoop(); } - stop() { - unimplementedWarning("stop"); + stop(): void { + window.cancelAnimationFrame(this._animationCancelID); + this._animationCancelID = null; } - ontimer() { - unimplementedWarning("ontimer"); - return; + ontimer(): void { + this.js_trigger("onTimer"); } - getdelay() { - unimplementedWarning("getdelay"); - return; + getdelay(): number { + return this._speed; } - isrunning() { - unimplementedWarning("isrunning"); - return; + isrunning(): boolean { + return this._animationCancelID != null; } getskipped() {