@m2c2kit/core 0.1.3 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,67 +1,6 @@
1
- import { FontMgr, Typeface, Image, CanvasKit, Canvas } from 'canvaskit-wasm';
2
-
3
- interface FontData {
4
- fontUrl: string;
5
- fontArrayBuffer: ArrayBuffer;
6
- }
7
-
8
- declare class FontManager {
9
- _fontMgr?: FontMgr;
10
- private _typefaces;
11
- _getTypeface(name: string): Typeface;
12
- FetchFontsAsArrayBuffers(fontUrls: Array<string>): Promise<FontData>[];
13
- LoadFonts(fonts: Array<ArrayBuffer>): void;
14
- }
15
-
16
- declare class LoadedImage {
17
- name: string;
18
- image: Image;
19
- width: number;
20
- height: number;
21
- constructor(name: string, image: Image, width: number, height: number);
22
- }
23
-
24
- declare class RenderedDataUrlImage {
25
- name: string;
26
- dataUrlImage: string;
27
- width: number;
28
- height: number;
29
- constructor(name: string, dataUrlImage: string, width: number, height: number);
30
- }
31
-
32
- /**
33
- * SVG image to be rendered and loaded from a URL or HTML svg tag in string form.
34
- */
35
- interface SvgImage {
36
- /** Name that will be used to refer to the SVG image. Must be unique among all images */
37
- name: string;
38
- /** Width to scale SVG image to */
39
- width: number;
40
- /** Height to scale SVG image to */
41
- height: number;
42
- /** The HTML SVG tag, in string form, that will be rendered and loaded. Must begin with &#60;svg> and end with &#60;/svg> */
43
- svgString?: string;
44
- /** URL of SVG asset to render and load */
45
- url?: string;
46
- }
47
-
48
- declare class ImageManager {
49
- private scratchCanvas?;
50
- private ctx?;
51
- private scale?;
52
- _renderedDataUrlImages: Record<string, RenderedDataUrlImage>;
53
- _loadedImages: Record<string, LoadedImage>;
54
- initialize(scratchCanvas: HTMLCanvasElement): void;
55
- renderSvgImage(svgImage: SvgImage): Promise<RenderedDataUrlImage>;
56
- LoadRenderedSvgImages(urls: RenderedDataUrlImage[]): void;
57
- private convertRenderedDataUrlImage;
58
- private dataURLtoArrayBuffer;
59
- }
1
+ import { Canvas, CanvasKit, Image, FontMgr, Typeface } from 'canvaskit-wasm';
60
2
 
61
3
  declare class GlobalVariables {
62
- canvasKit: CanvasKit;
63
- fontManager: FontManager;
64
- imageManager: ImageManager;
65
4
  now: number;
66
5
  deltaTime: number;
67
6
  canvasScale: number;
@@ -75,6 +14,16 @@ declare global {
75
14
  }
76
15
  //# sourceMappingURL=Globals.d.ts.map
77
16
 
17
+ interface EntityEvent {
18
+ target: Entity;
19
+ }
20
+
21
+ interface EntityEventListener {
22
+ eventType: string;
23
+ entityName: string;
24
+ callback: (event: EntityEvent) => void;
25
+ }
26
+
78
27
  /**
79
28
  * Position in two-dimensional space.
80
29
  */
@@ -86,16 +35,7 @@ declare class Point {
86
35
  constructor(x?: number, y?: number);
87
36
  }
88
37
 
89
- declare class TapListener {
90
- entityName?: string;
91
- codeCallback?: (tapevent: TapEvent) => void;
92
- }
93
- /**
94
- * Object passed to the tap event handler when the entity is tapped.
95
- */
96
- interface TapEvent {
97
- /** The entity that was tapped */
98
- tappedEntity: Entity;
38
+ interface TapEvent extends EntityEvent {
99
39
  /** Point that was tapped on entity, relative to the entity coordinate system */
100
40
  point: Point;
101
41
  }
@@ -193,7 +133,7 @@ declare abstract class Entity implements EntityOptions {
193
133
  actions: Action[];
194
134
  queuedAction?: Action;
195
135
  originalActions: Action[];
196
- tapListeners: TapListener[];
136
+ eventListeners: EntityEventListener[];
197
137
  uuid: string;
198
138
  needsInitialization: boolean;
199
139
  userData: any;
@@ -234,12 +174,12 @@ declare abstract class Entity implements EntityOptions {
234
174
  */
235
175
  get descendants(): Array<Entity>;
236
176
  /**
237
- * Provides the callback function to be executed when the user taps the entity.
177
+ * Takes the callback function to be executed when the user taps down on the entity. A TapDown is either a mouse click within the bounds of an entity OR the beginning of touches within the bounds of an entity.
238
178
  *
239
- * @param codeCallback - function to execute
240
- * @param replaceExistingCodeCallback - should the provided callback replace any existing callbacks? Usually we want to have only one callback defined, instead of chaining multiple ones. It is strongly recommended not to change this, unless you have a special use case. Default is true.
179
+ * @param callback - function to execute
180
+ * @param replaceExistingCallback - should the provided callback replace any existing callbacks? Usually we want to have only one callback defined, instead of chaining multiple ones. It is strongly recommended not to change this, unless you have a special use case. Default is true.
241
181
  */
242
- onTap(codeCallback: (tapEvent: TapEvent) => void, replaceExistingCodeCallback?: boolean): void;
182
+ onTapDown(callback: (tapEvent: TapEvent) => void, replaceExistingCallback?: boolean): void;
243
183
  private parseLayoutConstraints;
244
184
  private calculateYFromConstraint;
245
185
  private calculateXFromConstraint;
@@ -277,8 +217,8 @@ declare abstract class Entity implements EntityOptions {
277
217
  *
278
218
  * @returns Scene that contains this entity
279
219
  */
220
+ get canvasKit(): CanvasKit;
280
221
  get parentSceneAsEntity(): Entity;
281
- private generateUUID;
282
222
  /**
283
223
  * For a given directed acyclic graph, topological ordering of the vertices will be identified using BFS
284
224
  * @param adjList Adjacency List that represent a graph with vertices and edges
@@ -414,8 +354,8 @@ declare class GroupAction extends Action implements IActionContainer {
414
354
  }
415
355
  declare class CustomAction extends Action {
416
356
  type: ActionType;
417
- codeCallback: () => void;
418
- constructor(codeCallback: () => void, runDuringTransition?: boolean);
357
+ callback: () => void;
358
+ constructor(callback: () => void, runDuringTransition?: boolean);
419
359
  }
420
360
  declare class WaitAction extends Action {
421
361
  type: ActionType;
@@ -435,73 +375,171 @@ declare class ScaleAction extends Action {
435
375
  constructor(scale: number, duration: number, runDuringTransition?: boolean);
436
376
  }
437
377
 
438
- interface IDrawable {
439
- draw(canvas: Canvas): void;
440
- anchorPoint: Point;
441
- zPosition: number;
378
+ declare class LoadedImage {
379
+ name: string;
380
+ image: Image;
381
+ width: number;
382
+ height: number;
383
+ constructor(name: string, image: Image, width: number, height: number);
442
384
  }
443
385
 
444
- interface DrawableOptions {
445
- anchorPoint?: Point;
446
- zPosition?: number;
386
+ /**
387
+ * Image that can be rendered by a browser and loaded from a URL or HTML svg tag in string form.
388
+ */
389
+ interface BrowserImage {
390
+ /** Name that will be used to refer to the SVG image. Must be unique among all images */
391
+ name: string;
392
+ /** Width to scale SVG image to */
393
+ width: number;
394
+ /** Height to scale SVG image to */
395
+ height: number;
396
+ /** The HTML SVG tag, in string form, that will be rendered and loaded. Must begin with &#60;svg> and end with &#60;/svg> */
397
+ svgString?: string;
398
+ /** URL of SVG asset to render and load */
399
+ url?: string;
447
400
  }
448
401
 
449
- interface CompositeOptions extends EntityOptions, DrawableOptions {
402
+ interface GameImages {
403
+ uuid: string;
404
+ images: Array<BrowserImage>;
450
405
  }
451
406
 
452
- declare abstract class Composite extends Entity implements IDrawable {
453
- readonly type = EntityType.composite;
454
- compositeType: string;
455
- isDrawable: boolean;
456
- anchorPoint: Point;
457
- zPosition: number;
407
+ declare class ImageManager {
408
+ canvasKit?: CanvasKit;
409
+ private renderedImages;
410
+ private loadedImages;
411
+ private _scratchCanvas?;
412
+ private ctx?;
413
+ private scale?;
414
+ getLoadedImage(gameUuid: string, imageName: string): LoadedImage;
458
415
  /**
459
- * Base Drawable object for creating custom entities ("composites") composed of primitive entities.
416
+ * Adds a CanvasKit image to the images available to a given game.
417
+ * Typically, this won't be called directly because images will be
418
+ * automatically rendered and loaded in the Activity async init.
419
+ * The only time this function is called in-game is to add
420
+ * screenshot images needed for transitions
460
421
  *
461
- * @param options
422
+ * @param loadedImage
423
+ * @param gameUuid
462
424
  */
463
- constructor(options?: CompositeOptions);
464
- initialize(): void;
465
- update(): void;
466
- draw(canvas: Canvas): void;
425
+ addLoadedImage(loadedImage: LoadedImage, gameUuid: string): void;
426
+ renderImages(allGamesImages: Array<GameImages>): Promise<void[]>;
427
+ loadAllGamesRenderedImages(): void;
428
+ private renderBrowserImage;
429
+ private convertRenderedDataUrlImageToCanvasKitImage;
430
+ /**
431
+ * scratchCanvas is an extra, non-visible canvas in the DOM we use so the native browser can render images
432
+ */
433
+ private get scratchCanvas();
434
+ private dataURLtoArrayBuffer;
467
435
  }
468
436
 
469
- /**
470
- * Color in red (0-255), green (0-255), blue (0-255), alpha (0-1) format. Must be numeric array of length 4.
471
- */
472
- declare type RgbaColor = [number, number, number, number];
437
+ interface GameFontUrls {
438
+ uuid: string;
439
+ fontUrls: Array<string>;
440
+ }
473
441
 
474
- /**
475
- * Reasonable defaults to use if values are not specified.
476
- */
477
- declare class Constants {
478
- static readonly FPS_DISPLAY_TEXT_FONT_SIZE = 12;
479
- static readonly FPS_DISPLAY_TEXT_COLOR: RgbaColor;
480
- static readonly FPS_DISPLAY_UPDATE_INTERVAL = 500;
481
- static readonly DEFAULT_SCENE_BACKGROUND_COLOR: RgbaColor;
482
- static readonly DEFAULT_SHAPE_FILL_COLOR: RgbaColor;
483
- static readonly DEFAULT_FONT_COLOR: RgbaColor;
484
- static readonly DEFAULT_FONT_SIZE = 16;
485
- static readonly LIMITED_FPS_RATE = 5;
442
+ declare class FontManager {
443
+ canvasKit?: CanvasKit;
444
+ fontMgr?: FontMgr;
445
+ private gameTypefaces;
446
+ private allGamesFontData?;
447
+ /**
448
+ * Gets a typeface that was previously loaded for the specified game.
449
+ *
450
+ * @param gameUuid
451
+ * @param fontFamily
452
+ * @returns the requested Typeface
453
+ */
454
+ getTypeface(gameUuid: string, fontFamily: string): Typeface;
455
+ /**
456
+ * Fetches all fonts for all games.
457
+ *
458
+ * @param allGamesFontUrls
459
+ * @returns
460
+ */
461
+ fetchFonts(allGamesFontUrls: Array<GameFontUrls>): Promise<void>;
462
+ /**
463
+ * Takes the fonts, which have been previously fetched and converted into
464
+ * Array Buffers using FontManager.fetchFonts(), and makes them available
465
+ * to our engine
466
+ */
467
+ loadAllGamesFontData(): void;
468
+ /**
469
+ * For the specified game, fetches all fonts in the array of urls and
470
+ * stores fonts as array buffers.
471
+ *
472
+ * @param gameUuid
473
+ * @param fontUrls - array of font urls
474
+ * @returns
475
+ */
476
+ private fetchGameFontsAsArrayBuffers;
477
+ /**
478
+ * For the specified game, loads all fonts from array buffers and makes
479
+ * fonts available within canvaskit as a Typeface
480
+ *
481
+ * @param gameUuid
482
+ * @param fonts - array of fonts in array buffer form
483
+ */
484
+ private loadGameFonts;
485
+ }
486
+
487
+ interface EventBase {
488
+ eventType: EventType;
489
+ }
490
+ /** Note: I would have named it Event, but that would collide with
491
+ * the existing, and much more well-known, Web API Event */
492
+ declare enum EventType {
493
+ sessionLifecycle = "SessionLifecycle",
494
+ gameLifecycle = "GameLifecycle",
495
+ gameTrial = "GameTrial"
496
+ }
497
+
498
+ interface SessionEvent extends EventBase {
499
+ }
500
+
501
+ interface SessionLifecycleEvent extends SessionEvent {
502
+ initialized: boolean;
503
+ ended: boolean;
504
+ }
505
+
506
+ interface SessionCallbacks {
507
+ /** Callback executed when the session lifecycle changes, such as when it is initialized. */
508
+ onSessionLifecycleChange?: (event: SessionLifecycleEvent) => void;
509
+ }
510
+
511
+ interface GameEvent extends EventBase {
512
+ gameUuid: string;
513
+ gameName: string;
514
+ }
515
+
516
+ interface GameLifecycleEvent extends GameEvent {
517
+ ended: boolean;
518
+ }
519
+
520
+ interface GameParameters {
521
+ [key: string]: DefaultParameter;
522
+ }
523
+ interface DefaultParameter {
524
+ value: any;
525
+ type?: "number" | "string" | "boolean" | "object";
526
+ description?: string;
527
+ }
528
+
529
+ interface IDrawable {
530
+ draw(canvas: Canvas): void;
531
+ anchorPoint: Point;
532
+ zPosition: number;
486
533
  }
487
534
 
488
535
  /**
489
- * This enum is used interally for processing the layout constraints. We use
490
- * an enum to avoid magic strings.
536
+ * Color in red (0-255), green (0-255), blue (0-255), alpha (0-1) format. Must be numeric array of length 4.
491
537
  */
492
- declare enum ConstraintType {
493
- topToTopOf = "topToTopOf",
494
- topToBottomOf = "topToBottomOf",
495
- bottomToTopOf = "bottomToTopOf",
496
- bottomToBottomOf = "bottomToBottomOf",
497
- startToStartOf = "startToStartOf",
498
- startToEndOf = "startToEndOf",
499
- endToEndOf = "endToEndOf",
500
- endToStartOf = "endToStartOf"
501
- }
538
+ declare type RgbaColor = [number, number, number, number];
502
539
 
503
- declare enum Dimensions {
504
- MATCH_CONSTRAINT = 0
540
+ interface DrawableOptions {
541
+ anchorPoint?: Point;
542
+ zPosition?: number;
505
543
  }
506
544
 
507
545
  interface SceneOptions extends EntityOptions, DrawableOptions {
@@ -534,13 +572,13 @@ declare class Scene extends Entity implements IDrawable, SceneOptions {
534
572
  get backgroundColor(): RgbaColor;
535
573
  set backgroundColor(backgroundColor: RgbaColor);
536
574
  /**
537
- * Code that will be called every time the screen is shown.
575
+ * Code that will be called every time the screen is first presented.
538
576
  *
539
- * @remarks Use this callback to "reset" entities to their initial state. For example, if a screen allows players to place dots on a grid, the setup() method should ensure the grid is clear of any prior dots from previous times this screen may have been displayed. In addition, if entities should vary in each iteration, that should be done here.
577
+ * @remarks Use this callback to set entities to their initial state, if that state might be changed later. For example, if a scene allows players to place dots on a grid, the setup() method should ensure the grid is clear of any prior dots from previous times this scene may have been displayed. In addition, if entities should vary in each iteration, that should be done here.
540
578
  *
541
- * @param codeCallback
579
+ * @param callback
542
580
  */
543
- setup(codeCallback: (scene: Scene) => void): void;
581
+ setup(callback: (scene: Scene) => void): void;
544
582
  draw(canvas: Canvas): void;
545
583
  }
546
584
 
@@ -576,10 +614,28 @@ declare class SceneTransition {
576
614
  constructor(scene: Scene, transition?: Transition | undefined);
577
615
  }
578
616
 
617
+ interface TrialSchema {
618
+ [key: string]: PropertySchema;
619
+ }
620
+ interface PropertySchema {
621
+ type: "number" | "string" | "boolean" | "object";
622
+ description?: string;
623
+ }
624
+
579
625
  /**
580
626
  * Options to specify HTML canvas, set game canvas size, and load game assets.
581
627
  */
582
- interface GameInitOptions {
628
+ interface GameOptions {
629
+ /** Human-friendly name of this game */
630
+ name: string;
631
+ /** Version of this game */
632
+ version?: string;
633
+ /** Uri (repository, webpage, or other location where full information about the game can be found) */
634
+ uri?: string;
635
+ /** Brief description of game */
636
+ shortDescription?: string;
637
+ /** Full description of game */
638
+ longDescription?: string;
583
639
  /** Id of the HTML canvas that game will be drawn on. If not provided, the first canvas found will be used */
584
640
  canvasId?: string;
585
641
  /** Width of game canvas */
@@ -589,13 +645,13 @@ interface GameInitOptions {
589
645
  /** Stretch to fill screen? Default is false */
590
646
  stretch?: boolean;
591
647
  /** Schema of trial data; JSON object where key is variable name, value is data type */
592
- trialSchema?: object;
648
+ trialSchema?: TrialSchema;
593
649
  /** Default game parameters; JSON object where key is the game parameter, value is default value */
594
- defaultParameters?: object;
650
+ parameters?: GameParameters;
595
651
  /** String array of urls from which to load fonts. The first element will be the default font */
596
652
  fontUrls?: Array<string>;
597
- /** Array of SvgImage objects to render and load */
598
- svgImages?: SvgImage[];
653
+ /** Array of BrowserImage objects to render and load */
654
+ images?: Array<BrowserImage>;
599
655
  /** Show FPS in upper left corner? Default is false */
600
656
  showFps?: boolean;
601
657
  /** Color of the html body, if the game does not fill the screen. Useful for showing scene boundaries. Default is the scene background color */
@@ -610,25 +666,21 @@ interface TrialData {
610
666
  interface Metadata {
611
667
  userAgent?: string;
612
668
  }
613
- interface GameData {
614
- trials: Array<TrialData>;
615
- metadata: Metadata;
616
- }
617
- interface LifecycleCallbacks {
618
- trialComplete: (trialNumber: number, data: GameData, trialSchema: object) => void;
619
- allTrialsComplete: (data: GameData, trialSchema: object) => void;
620
- }
621
- declare class Game {
669
+ declare class Game implements Activity {
670
+ _canvasKit?: CanvasKit;
671
+ _session?: Session;
672
+ uuid: string;
673
+ options: GameOptions;
674
+ constructor(options: GameOptions);
675
+ setParameters(newParameters: any): void;
676
+ get canvasKit(): CanvasKit;
677
+ set canvasKit(canvasKit: CanvasKit);
678
+ get session(): Session;
679
+ set session(session: Session);
622
680
  entryScene?: Scene | string;
623
- parameters: any;
624
- private defaultParameters;
625
681
  data: GameData;
626
- trialNumber: number;
627
- trialSchema: {};
628
- lifecycle: LifecycleCallbacks;
629
- private trialSchemaMap;
682
+ trialIndex: number;
630
683
  private htmlCanvas?;
631
- private scratchHtmlCanvas?;
632
684
  private surface?;
633
685
  private showFps?;
634
686
  private bodyBackgroundColor?;
@@ -643,32 +695,12 @@ declare class Game {
643
695
  private animationFramesRequested;
644
696
  private limitFps;
645
697
  private unitTesting;
698
+ private gameStopRequested;
646
699
  canvasCssWidth: number;
647
700
  canvasCssHeight: number;
648
701
  private scenes;
649
702
  private incomingSceneTransitions;
650
703
  private currentSceneSnapshot?;
651
- /**
652
- * Asynchronously initializes the game engine and load assets
653
- *
654
- * @param gameInitOptions
655
- * @returns Promise<void>
656
- */
657
- init(gameInitOptions: GameInitOptions): Promise<void>;
658
- /**
659
- * Provide a callback function to be invoked when a trial has completed.
660
- * It is the responsibility of the the game programmer to call this
661
- * at the appropriate time. It is not triggered automatically.
662
- * @param codeCallback
663
- */
664
- onTrialComplete(codeCallback: (trialNumber: number, data: GameData, trialSchema: object) => void): void;
665
- /**
666
- * Provide a callback function to be invoked when all trials are complete.
667
- * It is the responsibility of the the game programmer to call this
668
- * at the appropriate time. It is not triggered automatically.
669
- * @param codeCallback
670
- */
671
- onAllTrialsComplete(codeCallback: (data: GameData, trialSchema: object) => void): void;
672
704
  /**
673
705
  * Adds a scene to the game.
674
706
  *
@@ -691,10 +723,8 @@ declare class Game {
691
723
  */
692
724
  presentScene(scene: string | Scene, transition?: Transition): void;
693
725
  /**
694
- * Gets the value of the specified game parameter. If the value was not
695
- * provided in the parameters property above, then return the value in
696
- * defaultParameters. If the parameterName is still not found, then
697
- * throw exception.
726
+ * Gets the value of the game parameter. If parameterName
727
+ * is not found, then throw exception.
698
728
  * @param parameterName - the name of the game parameter whose value is requested
699
729
  * @returns
700
730
  */
@@ -705,7 +735,8 @@ declare class Game {
705
735
  * @param entryScene - The scene (Scene object or its string name) to display when the game starts
706
736
  */
707
737
  start(entryScene?: Scene | string): void;
708
- initData(trialSchema: object): void;
738
+ stop(): void;
739
+ initData(): void;
709
740
  /**
710
741
  * Adds data to the game's TrialData object.
711
742
  *
@@ -715,11 +746,26 @@ declare class Game {
715
746
  * @param value - value of the variable to set
716
747
  */
717
748
  addTrialData(variableName: string, value: any): void;
718
- private loadCanvasKit;
749
+ /**
750
+ * Should be called when the current trial has completed. It will
751
+ * also increment the trial index.
752
+ * Calling this will trigger the onGameTrialComplete callback function,
753
+ * if one was provided in SessionOptions. This is how the game communicates
754
+ * trial data to the parent session, which can then save or process the data.
755
+ * It is the responsibility of the the game programmer to call this at
756
+ * the appropriate time. It is not triggered automatically.
757
+ */
758
+ trialComplete(): void;
759
+ /**
760
+ * Should be called when the current game has ended. This will trigger
761
+ * the onGameLifecycleChange callback function, if one was provided in
762
+ * SessionOptions. This is how the game can communicate its ended or
763
+ * "finished" state to the parent session.
764
+ * It is the responsibility of the the game programmer to call this at
765
+ * the appropriate time. It is not triggered automatically.
766
+ */
767
+ end(): void;
719
768
  private setupHtmlCanvases;
720
- private fetchFonts;
721
- private renderSvgImages;
722
- private loadFonts;
723
769
  private setupCanvasKitSurface;
724
770
  private setupFpsFont;
725
771
  private setupEventHandlers;
@@ -732,27 +778,180 @@ declare class Game {
732
778
  private animateSceneTransition;
733
779
  private drawFps;
734
780
  /**
735
- * Creates a tap listener for an entity based on the entity name
781
+ * Creates an event listener for an entity based on the entity name
736
782
  *
737
- * @remarks Typically, tap listeners will be created using the onTap() method of each entity. This alternative allows creation with entity name.
783
+ * @remarks Typically, event listeners will be created using a method specific to the event, such as onTapDown(). This alternative allows creation with entity name.
738
784
  *
739
- * @param entityName
740
- * @param codeCallback
741
- * @param replaceExistingCodeCallback
785
+ * @param eventType - the type of event to listen for, e.g., "tapdown"
786
+ * @param entityName - the entity name for which an event will be listened
787
+ * @param callback
788
+ * @param replaceExistingCallback
742
789
  */
743
- createTapListener(entityName: string, codeCallback: (tapEvent: TapEvent) => void, replaceExistingCodeCallback?: boolean): void;
790
+ createEventListener(eventType: string, entityName: string, callback: (event: EntityEvent) => void, replaceExistingCallback?: boolean): void;
744
791
  /**
745
792
  * Returns array of all entities that have been added to the game object.
746
793
  */
747
794
  get entities(): Array<Entity>;
748
795
  private htmlCanvasMouseDownHandler;
749
796
  private htmlCanvasTouchStartHandler;
797
+ private sceneCanReceiveUserInteraction;
750
798
  private processTaps;
751
799
  private handleEntityTapped;
752
800
  private tapIsWithinEntityBounds;
753
801
  private calculateEntityAbsoluteBoundingBox;
754
802
  }
755
803
 
804
+ interface GameData {
805
+ trials: Array<TrialData>;
806
+ metadata: Metadata;
807
+ }
808
+
809
+ interface GameTrialEvent extends GameEvent {
810
+ trialIndex: number;
811
+ trialSchema: TrialSchema;
812
+ gameData: GameData;
813
+ gameParameters: GameParameters;
814
+ }
815
+
816
+ interface GameCallbacks {
817
+ /** Callback executed when the game lifecycle changes, such as when it ends. */
818
+ onGameLifecycleChange?: (event: GameLifecycleEvent) => void;
819
+ /** Callback executed when a game trial completes. */
820
+ onGameTrialComplete?: (event: GameTrialEvent) => void;
821
+ }
822
+
823
+ interface SessionOptions {
824
+ /** The activities that compose this session */
825
+ activities: Array<Activity>;
826
+ /** Callbacks executed when trials are completed and when game ends */
827
+ gameCallbacks?: GameCallbacks;
828
+ /** Callbacks executed when session events occur */
829
+ sessionCallbacks?: SessionCallbacks;
830
+ }
831
+
832
+ declare class Session {
833
+ options: SessionOptions;
834
+ fontManager: FontManager;
835
+ imageManager: ImageManager;
836
+ currentActivity?: Activity;
837
+ uuid: string;
838
+ private canvasKit?;
839
+ /**
840
+ * A Session contains one or more activities; currently, the only
841
+ * class that implements Activity is Game, but Survey is planned.
842
+ * The session manages the start and stop of activities, and
843
+ * advancement to next activity
844
+ *
845
+ * @param options
846
+ */
847
+ constructor(options: SessionOptions);
848
+ /**
849
+ * Asynchronously initializes the m2c2kit engine and loads assets
850
+ */
851
+ init(): Promise<void>;
852
+ /**
853
+ * Starts the session and starts the first activity.
854
+ */
855
+ start(): void;
856
+ /**
857
+ * Declares the session ended and sends callback.
858
+ */
859
+ end(): void;
860
+ /**
861
+ * Stops the current activity and advances to next activity in the session.
862
+ * If there is no activity after the current activity, throws error
863
+ */
864
+ advanceToNextActivity(): void;
865
+ /**
866
+ * Gets the next activity after the current one, or undefined if
867
+ * this is the last activity.
868
+ */
869
+ get nextActivity(): Activity | undefined;
870
+ private logStartingActivity;
871
+ /**
872
+ * Gets asynchronous assets, including initialization of canvaskit wasm,
873
+ * fetching of fonts from specified urls, and rendering and fetching
874
+ * of images
875
+ * @returns
876
+ */
877
+ private getAsynchronousAssets;
878
+ private loadCanvasKit;
879
+ private loadAssets;
880
+ private assignCanvasKit;
881
+ private getFontsConfigurationFromGames;
882
+ private getImagesConfigurationFromGames;
883
+ }
884
+
885
+ interface Activity {
886
+ /** Starts the activity */
887
+ start(): void;
888
+ /** Stops the activity */
889
+ stop(): void;
890
+ /** The activity's parent session */
891
+ session: Session;
892
+ /** The activity's unique identifier. NOTE: This is newly generated each session. The uuid for an activity will vary across sessions. */
893
+ uuid: string;
894
+ }
895
+
896
+ interface CompositeOptions extends EntityOptions, DrawableOptions {
897
+ }
898
+
899
+ declare abstract class Composite extends Entity implements IDrawable {
900
+ readonly type = EntityType.composite;
901
+ compositeType: string;
902
+ isDrawable: boolean;
903
+ anchorPoint: Point;
904
+ zPosition: number;
905
+ /**
906
+ * Base Drawable object for creating custom entities ("composites") composed of primitive entities.
907
+ *
908
+ * @param options
909
+ */
910
+ constructor(options?: CompositeOptions);
911
+ initialize(): void;
912
+ update(): void;
913
+ draw(canvas: Canvas): void;
914
+ }
915
+
916
+ /**
917
+ * Reasonable defaults to use if values are not specified.
918
+ */
919
+ declare class Constants {
920
+ static readonly FPS_DISPLAY_TEXT_FONT_SIZE = 12;
921
+ static readonly FPS_DISPLAY_TEXT_COLOR: RgbaColor;
922
+ static readonly FPS_DISPLAY_UPDATE_INTERVAL = 500;
923
+ static readonly DEFAULT_SCENE_BACKGROUND_COLOR: RgbaColor;
924
+ static readonly DEFAULT_SHAPE_FILL_COLOR: RgbaColor;
925
+ static readonly DEFAULT_FONT_COLOR: RgbaColor;
926
+ static readonly DEFAULT_FONT_SIZE = 16;
927
+ static readonly LIMITED_FPS_RATE = 5;
928
+ }
929
+
930
+ /**
931
+ * This enum is used interally for processing the layout constraints. We use
932
+ * an enum to avoid magic strings.
933
+ */
934
+ declare enum ConstraintType {
935
+ topToTopOf = "topToTopOf",
936
+ topToBottomOf = "topToBottomOf",
937
+ bottomToTopOf = "bottomToTopOf",
938
+ bottomToBottomOf = "bottomToBottomOf",
939
+ startToStartOf = "startToStartOf",
940
+ startToEndOf = "startToEndOf",
941
+ endToEndOf = "endToEndOf",
942
+ endToStartOf = "endToStartOf"
943
+ }
944
+
945
+ declare enum Dimensions {
946
+ MATCH_CONSTRAINT = 0
947
+ }
948
+
949
+ interface FontData {
950
+ gameUuid: string;
951
+ fontUrl: string;
952
+ fontArrayBuffer: ArrayBuffer;
953
+ }
954
+
756
955
  interface IText {
757
956
  text?: string;
758
957
  fontName?: string;
@@ -1042,6 +1241,10 @@ declare class Timer {
1042
1241
  static RemoveAll(): void;
1043
1242
  }
1044
1243
 
1244
+ declare class Uuid {
1245
+ static generate(): string;
1246
+ }
1247
+
1045
1248
  declare class WebColors {
1046
1249
  static Transparent: RgbaColor;
1047
1250
  static MediumVioletRed: RgbaColor;
@@ -1187,4 +1390,4 @@ declare class WebColors {
1187
1390
  static RebeccaPurple: RgbaColor;
1188
1391
  }
1189
1392
 
1190
- export { Action, Composite, CompositeOptions, Constants, ConstraintType, Constraints, CustomAction, CustomActionOptions, Dimensions, DrawableOptions, Entity, EntityOptions, EntityType, FontData, FontManager, Game, GameData, GameInitOptions, GlobalVariables, GroupAction, IDrawable, IText, ImageManager, Label, LabelHorizontalAlignmentMode, LabelOptions, Layout, LayoutConstraint, LoadedImage, MoveAction, MoveActionOptions, Point, PushTransition, RandomDraws, Rect, RectOptions, RgbaColor, ScaleAction, ScaleActionOptions, Scene, SceneOptions, SceneTransition, SequenceAction, Shape, ShapeOptions, ShapeType, Size, Sprite, SpriteOptions, Story, StoryOptions, SvgImage, TapEvent, TapListener, TextLine, TextLineOptions, TextOptions, Timer, Transition, TransitionDirection, TransitionType, WaitAction, WaitActionOptions, WebColors, handleInterfaceOptions };
1393
+ export { Action, Activity, BrowserImage, Composite, CompositeOptions, Constants, ConstraintType, Constraints, CustomAction, CustomActionOptions, DefaultParameter, Dimensions, DrawableOptions, Entity, EntityEventListener, EntityOptions, EntityType, EventBase, EventType, FontData, FontManager, Game, GameCallbacks, GameData, GameLifecycleEvent, GameOptions, GameParameters, GameTrialEvent, GlobalVariables, GroupAction, IDrawable, IText, ImageManager, Label, LabelHorizontalAlignmentMode, LabelOptions, Layout, LayoutConstraint, LoadedImage, Metadata, MoveAction, MoveActionOptions, Point, PropertySchema, PushTransition, RandomDraws, Rect, RectOptions, RgbaColor, ScaleAction, ScaleActionOptions, Scene, SceneOptions, SceneTransition, SequenceAction, Session, SessionLifecycleEvent, SessionOptions, Shape, ShapeOptions, ShapeType, Size, Sprite, SpriteOptions, Story, StoryOptions, TextLine, TextLineOptions, TextOptions, Timer, Transition, TransitionDirection, TransitionType, TrialData, TrialSchema, Uuid, WaitAction, WaitActionOptions, WebColors, handleInterfaceOptions };