can toggle between paused and realtime

This commit is contained in:
Joeri Exelmans 2025-10-10 14:08:46 +02:00
parent 56c77de5bc
commit 3f095bb419
3 changed files with 130 additions and 25 deletions

View file

@ -1,11 +1,3 @@
// modal configuration: maps child-uid to modal configuration of the child
// for OR-states, only the modal configuration of the current state is kept
// for AND-states, the modal configuration of every child is kept
// for basic states (= empty AND-states), the modal configuration is just an empty object
// export type Mode = {[uid:string]: Mode};
import { Transition } from "./ast";
export type Timestamp = number; // milliseconds since begin of simulation
export type Event = string;
@ -20,12 +12,7 @@ export type RT_Statechart = {
// history: // TODO
}
export type BigStep = {
from: RT_Statechart;
to: RT_Statechart;
inputEvent: string;
outputEvents: string[];
}
export type BigStep = RT_Statechart & {outputEvents: string[]};
export type RaisedEvents = {
internalEvents: string[];

61
src/VisualEditor/time.ts Normal file
View file

@ -0,0 +1,61 @@
export type TimeMode = TimePaused | TimeRealTime;
export type TimePaused = {
kind: "paused",
simtime: number, // the current simulated time
}
export type TimeRealTime = {
kind: "realtime",
scale: number, // time scale relative to wall-clock time
since: {
simtime: number, // the simulated time at which the time was set to realtime
wallclktime: number, // the wall-clock time at which the time was set to realtime
}
}
export function getSimTime(currentMode: TimeMode, wallclktime: number): number {
if (currentMode.kind === "paused") {
return currentMode.simtime;
}
else {
const elapsedWallclk = wallclktime - currentMode.since.wallclktime;
return currentMode.since.simtime + currentMode.scale * elapsedWallclk;
}
}
export function setRealtime(currentMode: TimeMode, scale: number, wallclktime: number): TimeRealTime {
if (currentMode.kind === "paused") {
return {
kind: "realtime",
scale,
since: {
simtime: currentMode.simtime,
wallclktime,
},
};
}
else {
return {
kind: "realtime",
scale,
since: {
simtime: getSimTime(currentMode, wallclktime),
wallclktime,
},
};
}
}
export function setPaused(currentMode: TimeMode, wallclktime: number): TimePaused {
if (currentMode.kind === "paused") {
return currentMode; // no change
}
else {
return {
kind: "paused",
simtime: getSimTime(currentMode, wallclktime),
};
}
}