@m2c2kit/core 0.1.4 → 0.1.8

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
@@ -484,22 +484,137 @@ declare class FontManager {
484
484
  private loadGameFonts;
485
485
  }
486
486
 
487
- interface GameEvent {
488
- gameUuid: string;
489
- gameName: string;
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
+ activityData = "ActivityData",
494
+ activityLifecycle = "ActivityLifecycle",
495
+ sessionLifecycle = "SessionLifecycle"
490
496
  }
491
497
 
492
- interface GameLifecycleEvent extends GameEvent {
493
- ended: boolean;
498
+ interface ActivityEvent extends EventBase {
499
+ uuid: string;
500
+ name: string;
494
501
  }
495
502
 
496
- interface GameParameters {
497
- [key: string]: DefaultParameter;
498
- }
499
- interface DefaultParameter {
500
- value: any;
501
- type?: "number" | "string" | "boolean" | "object";
502
- description?: string;
503
+ interface ActivityData {
504
+ [key: string]: string | number | boolean | object | undefined | null;
505
+ }
506
+
507
+ interface ActivityDataEvent extends ActivityEvent {
508
+ newData: ActivityData;
509
+ newDataSchema: any;
510
+ data: ActivityData;
511
+ dataSchema: any;
512
+ activityConfiguration: any;
513
+ }
514
+
515
+ interface ActivityLifecycleEvent extends ActivityEvent {
516
+ /** the activity started. */
517
+ started?: boolean;
518
+ /** the activity ended. the user fully completed the activity and */
519
+ ended?: boolean;
520
+ /** the activity ended. the user canceled without fully completing the activity */
521
+ userCanceled?: boolean;
522
+ }
523
+
524
+ interface ActivityCallbacks {
525
+ /** Callback executed when the activity lifecycle changes, such as when it ends. */
526
+ onActivityLifecycleChange?: (event: ActivityLifecycleEvent) => void;
527
+ /** Callback executed when an activity creates some data. */
528
+ onActivityDataCreate?: (event: ActivityDataEvent) => void;
529
+ }
530
+
531
+ interface SessionEvent extends EventBase {
532
+ }
533
+
534
+ interface SessionLifecycleEvent extends SessionEvent {
535
+ initialized?: boolean;
536
+ ended?: boolean;
537
+ started?: boolean;
538
+ }
539
+
540
+ interface SessionCallbacks {
541
+ /** Callback executed when the session lifecycle changes, such as when it is initialized. */
542
+ onSessionLifecycleChange?: (event: SessionLifecycleEvent) => void;
543
+ }
544
+
545
+ interface SessionOptions {
546
+ /** The activities that compose this session */
547
+ activities: Array<Activity>;
548
+ /** Callbacks executed when activity events occurs, such as when activity creates data or ends */
549
+ activityCallbacks?: ActivityCallbacks;
550
+ /** Callbacks executed when session events occur */
551
+ sessionCallbacks?: SessionCallbacks;
552
+ }
553
+
554
+ declare class Session {
555
+ options: SessionOptions;
556
+ fontManager: FontManager;
557
+ imageManager: ImageManager;
558
+ currentActivity?: Activity;
559
+ uuid: string;
560
+ private canvasKit?;
561
+ /**
562
+ * A Session contains one or more activities; currently, the only
563
+ * class that implements Activity is Game, but Survey is planned.
564
+ * The session manages the start and stop of activities, and
565
+ * advancement to next activity
566
+ *
567
+ * @param options
568
+ */
569
+ constructor(options: SessionOptions);
570
+ /**
571
+ * Asynchronously initializes the m2c2kit engine and loads assets
572
+ */
573
+ init(): Promise<void>;
574
+ /**
575
+ * Starts the session and starts the first activity.
576
+ */
577
+ start(): void;
578
+ /**
579
+ * Declares the session ended and sends callback.
580
+ */
581
+ end(): void;
582
+ /**
583
+ * Stops the current activity and advances to next activity in the session.
584
+ * If there is no activity after the current activity, throws error
585
+ */
586
+ advanceToNextActivity(): void;
587
+ /**
588
+ * Gets the next activity after the current one, or undefined if
589
+ * this is the last activity.
590
+ */
591
+ get nextActivity(): Activity | undefined;
592
+ private logStartingActivity;
593
+ /**
594
+ * Gets asynchronous assets, including initialization of canvaskit wasm,
595
+ * fetching of fonts from specified urls, and rendering and fetching
596
+ * of images
597
+ * @returns
598
+ */
599
+ private getAsynchronousAssets;
600
+ private loadCanvasKit;
601
+ private loadAssets;
602
+ private assignCanvasKit;
603
+ private getFontsConfigurationFromGames;
604
+ private getImagesConfigurationFromGames;
605
+ }
606
+
607
+ interface Activity {
608
+ /** Starts the activity */
609
+ start(): void;
610
+ /** Stops the activity */
611
+ stop(): void;
612
+ /** The activity's parent session */
613
+ session: Session;
614
+ /** The activity's unique identifier. NOTE: This is newly generated each session. The uuid for an activity will vary across sessions. */
615
+ uuid: string;
616
+ /** The activity's human friendly name */
617
+ name: string;
503
618
  }
504
619
 
505
620
  interface IDrawable {
@@ -508,14 +623,73 @@ interface IDrawable {
508
623
  zPosition: number;
509
624
  }
510
625
 
626
+ interface DrawableOptions {
627
+ anchorPoint?: Point;
628
+ zPosition?: number;
629
+ }
630
+
631
+ interface CompositeOptions extends EntityOptions, DrawableOptions {
632
+ }
633
+
634
+ declare abstract class Composite extends Entity implements IDrawable {
635
+ readonly type = EntityType.composite;
636
+ compositeType: string;
637
+ isDrawable: boolean;
638
+ anchorPoint: Point;
639
+ zPosition: number;
640
+ /**
641
+ * Base Drawable object for creating custom entities ("composites") composed of primitive entities.
642
+ *
643
+ * @param options
644
+ */
645
+ constructor(options?: CompositeOptions);
646
+ initialize(): void;
647
+ update(): void;
648
+ draw(canvas: Canvas): void;
649
+ }
650
+
511
651
  /**
512
652
  * Color in red (0-255), green (0-255), blue (0-255), alpha (0-1) format. Must be numeric array of length 4.
513
653
  */
514
654
  declare type RgbaColor = [number, number, number, number];
515
655
 
516
- interface DrawableOptions {
517
- anchorPoint?: Point;
518
- zPosition?: number;
656
+ /**
657
+ * Reasonable defaults to use if values are not specified.
658
+ */
659
+ declare class Constants {
660
+ static readonly FPS_DISPLAY_TEXT_FONT_SIZE = 12;
661
+ static readonly FPS_DISPLAY_TEXT_COLOR: RgbaColor;
662
+ static readonly FPS_DISPLAY_UPDATE_INTERVAL = 500;
663
+ static readonly DEFAULT_SCENE_BACKGROUND_COLOR: RgbaColor;
664
+ static readonly DEFAULT_SHAPE_FILL_COLOR: RgbaColor;
665
+ static readonly DEFAULT_FONT_COLOR: RgbaColor;
666
+ static readonly DEFAULT_FONT_SIZE = 16;
667
+ static readonly LIMITED_FPS_RATE = 5;
668
+ }
669
+
670
+ /**
671
+ * This enum is used interally for processing the layout constraints. We use
672
+ * an enum to avoid magic strings.
673
+ */
674
+ declare enum ConstraintType {
675
+ topToTopOf = "topToTopOf",
676
+ topToBottomOf = "topToBottomOf",
677
+ bottomToTopOf = "bottomToTopOf",
678
+ bottomToBottomOf = "bottomToBottomOf",
679
+ startToStartOf = "startToStartOf",
680
+ startToEndOf = "startToEndOf",
681
+ endToEndOf = "endToEndOf",
682
+ endToStartOf = "endToStartOf"
683
+ }
684
+
685
+ declare enum Dimensions {
686
+ MATCH_CONSTRAINT = 0
687
+ }
688
+
689
+ interface FontData {
690
+ gameUuid: string;
691
+ fontUrl: string;
692
+ fontArrayBuffer: ArrayBuffer;
519
693
  }
520
694
 
521
695
  interface SceneOptions extends EntityOptions, DrawableOptions {
@@ -548,9 +722,9 @@ declare class Scene extends Entity implements IDrawable, SceneOptions {
548
722
  get backgroundColor(): RgbaColor;
549
723
  set backgroundColor(backgroundColor: RgbaColor);
550
724
  /**
551
- * Code that will be called every time the screen is shown.
725
+ * Code that will be called every time the screen is first presented.
552
726
  *
553
- * @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.
727
+ * @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.
554
728
  *
555
729
  * @param callback
556
730
  */
@@ -594,7 +768,16 @@ interface TrialSchema {
594
768
  [key: string]: PropertySchema;
595
769
  }
596
770
  interface PropertySchema {
597
- type: "number" | "string" | "boolean" | "object";
771
+ type: "number" | "string" | "boolean" | "object" | "array";
772
+ description?: string;
773
+ }
774
+
775
+ interface GameParameters {
776
+ [key: string]: DefaultParameter;
777
+ }
778
+ interface DefaultParameter {
779
+ value: any;
780
+ type?: "number" | "string" | "boolean" | "object";
598
781
  description?: string;
599
782
  }
600
783
 
@@ -636,18 +819,44 @@ interface GameOptions {
636
819
  _unitTesting?: boolean;
637
820
  }
638
821
 
822
+ interface GameData extends ActivityData {
823
+ trials: Array<TrialData>;
824
+ metadata: Metadata;
825
+ }
826
+
639
827
  interface TrialData {
640
828
  [key: string]: string | number | boolean | undefined | null;
641
829
  }
642
830
  interface Metadata {
643
831
  userAgent?: string;
832
+ devicePixelRatio?: number;
833
+ screen?: screenMetadata;
834
+ }
835
+ /**
836
+ * screenMetadata is similar to window.Screen, except we don't want the
837
+ * methods on the window.Screen.ScreenOrientation
838
+ */
839
+ interface screenMetadata {
840
+ readonly availHeight: number;
841
+ readonly availWidth: number;
842
+ readonly colorDepth: number;
843
+ readonly height: number;
844
+ /** ScreenOrientation has some methods on it; we only want these two properties
845
+ * However, when unit testing, orientation is not available to us. Thus, make this
846
+ * property optional
847
+ */
848
+ readonly orientation?: Pick<ScreenOrientation, "type" | "angle">;
849
+ readonly pixelDepth: number;
850
+ readonly width: number;
644
851
  }
645
852
  declare class Game implements Activity {
646
853
  _canvasKit?: CanvasKit;
647
854
  _session?: Session;
648
855
  uuid: string;
856
+ name: string;
649
857
  options: GameOptions;
650
- constructor(options: GameOptions, specifiedParameters?: any);
858
+ constructor(options: GameOptions);
859
+ setParameters(newParameters: any): void;
651
860
  get canvasKit(): CanvasKit;
652
861
  set canvasKit(canvasKit: CanvasKit);
653
862
  get session(): Session;
@@ -712,6 +921,7 @@ declare class Game implements Activity {
712
921
  start(entryScene?: Scene | string): void;
713
922
  stop(): void;
714
923
  initData(): void;
924
+ private getScreenMetadata;
715
925
  /**
716
926
  * Adds data to the game's TrialData object.
717
927
  *
@@ -724,7 +934,7 @@ declare class Game implements Activity {
724
934
  /**
725
935
  * Should be called when the current trial has completed. It will
726
936
  * also increment the trial index.
727
- * Calling this will trigger the onTrialComplete callback function,
937
+ * Calling this will trigger the onActivityDataCreate callback function,
728
938
  * if one was provided in SessionOptions. This is how the game communicates
729
939
  * trial data to the parent session, which can then save or process the data.
730
940
  * It is the responsibility of the the game programmer to call this at
@@ -733,9 +943,9 @@ declare class Game implements Activity {
733
943
  trialComplete(): void;
734
944
  /**
735
945
  * Should be called when the current game has ended. This will trigger
736
- * the onGameEnd callback function, if one was provided in SessionOptions.
737
- * This is how the game communicates its ended or "finished" state to the
738
- * parent session.
946
+ * the onActivityLifecycleChange callback function, if one was provided in
947
+ * SessionOptions. This is how the game can communicate its ended or
948
+ * "finished" state to the parent session.
739
949
  * It is the responsibility of the the game programmer to call this at
740
950
  * the appropriate time. It is not triggered automatically.
741
951
  */
@@ -776,151 +986,6 @@ declare class Game implements Activity {
776
986
  private calculateEntityAbsoluteBoundingBox;
777
987
  }
778
988
 
779
- interface GameData {
780
- trials: Array<TrialData>;
781
- metadata: Metadata;
782
- }
783
-
784
- interface GameTrialEvent extends GameEvent {
785
- trialIndex: number;
786
- trialSchema: TrialSchema;
787
- gameData: GameData;
788
- gameParameters: GameParameters;
789
- }
790
-
791
- interface GameCallbacks {
792
- /** Callback executed when the current game has ended. */
793
- onGameEnd?: (event: GameLifecycleEvent) => void;
794
- /** Callback executed when a game trial has completed. */
795
- onGameTrialComplete?: (event: GameTrialEvent) => void;
796
- }
797
-
798
- interface SessionOptions {
799
- /** The activities that compose this session */
800
- activities: Array<Activity>;
801
- /** Callbacks executed when trials are completed and when game ends */
802
- gameCallbacks?: GameCallbacks;
803
- }
804
-
805
- declare class Session {
806
- options: SessionOptions;
807
- fontManager: FontManager;
808
- imageManager: ImageManager;
809
- currentActivity?: Activity;
810
- uuid: string;
811
- private canvasKit?;
812
- /**
813
- * A Session contains one or more activities; currently, the only
814
- * class that implements Activity is Game, but Survey is planned.
815
- * The session manages the start and stop of activities, and
816
- * advancement to next activity
817
- *
818
- * @param options
819
- */
820
- constructor(options: SessionOptions);
821
- /**
822
- * Asynchronously initializes the m2c2kit engine and loads assets
823
- */
824
- init(): Promise<void>;
825
- /**
826
- * Starts the session and starts the first activity.
827
- */
828
- start(): void;
829
- /**
830
- * Stops the current activity and advances to next activity in the session.
831
- * If there is no activity after the current activity, throws error
832
- */
833
- advanceToNextActivity(): void;
834
- /**
835
- * Gets the next activity after the current one, or undefined if
836
- * this is the last activity.
837
- */
838
- get nextActivity(): Activity | undefined;
839
- private logStartingActivity;
840
- /**
841
- * Gets asynchronous assets, including initialization of canvaskit wasm,
842
- * fetching of fonts from specified urls, and rendering and fetching
843
- * of images
844
- * @returns
845
- */
846
- private getAsynchronousAssets;
847
- private loadCanvasKit;
848
- private loadAssets;
849
- private assignCanvasKit;
850
- private getFontsConfigurationFromGames;
851
- private getImagesConfigurationFromGames;
852
- }
853
-
854
- interface Activity {
855
- /** Starts the activity */
856
- start(): void;
857
- /** Stops the activity */
858
- stop(): void;
859
- /** The activity's parent session */
860
- session: Session;
861
- /** The activity's unique identifier. NOTE: This is newly generated each session. The uuid for an activity will vary across sessions. */
862
- uuid: string;
863
- }
864
-
865
- interface CompositeOptions extends EntityOptions, DrawableOptions {
866
- }
867
-
868
- declare abstract class Composite extends Entity implements IDrawable {
869
- readonly type = EntityType.composite;
870
- compositeType: string;
871
- isDrawable: boolean;
872
- anchorPoint: Point;
873
- zPosition: number;
874
- /**
875
- * Base Drawable object for creating custom entities ("composites") composed of primitive entities.
876
- *
877
- * @param options
878
- */
879
- constructor(options?: CompositeOptions);
880
- initialize(): void;
881
- update(): void;
882
- draw(canvas: Canvas): void;
883
- }
884
-
885
- /**
886
- * Reasonable defaults to use if values are not specified.
887
- */
888
- declare class Constants {
889
- static readonly FPS_DISPLAY_TEXT_FONT_SIZE = 12;
890
- static readonly FPS_DISPLAY_TEXT_COLOR: RgbaColor;
891
- static readonly FPS_DISPLAY_UPDATE_INTERVAL = 500;
892
- static readonly DEFAULT_SCENE_BACKGROUND_COLOR: RgbaColor;
893
- static readonly DEFAULT_SHAPE_FILL_COLOR: RgbaColor;
894
- static readonly DEFAULT_FONT_COLOR: RgbaColor;
895
- static readonly DEFAULT_FONT_SIZE = 16;
896
- static readonly LIMITED_FPS_RATE = 5;
897
- }
898
-
899
- /**
900
- * This enum is used interally for processing the layout constraints. We use
901
- * an enum to avoid magic strings.
902
- */
903
- declare enum ConstraintType {
904
- topToTopOf = "topToTopOf",
905
- topToBottomOf = "topToBottomOf",
906
- bottomToTopOf = "bottomToTopOf",
907
- bottomToBottomOf = "bottomToBottomOf",
908
- startToStartOf = "startToStartOf",
909
- startToEndOf = "startToEndOf",
910
- endToEndOf = "endToEndOf",
911
- endToStartOf = "endToStartOf"
912
- }
913
-
914
- declare enum Dimensions {
915
- MATCH_CONSTRAINT = 0
916
- }
917
-
918
- interface FontData {
919
- gameUuid: string;
920
- fontUrl: string;
921
- fontArrayBuffer: ArrayBuffer;
922
- }
923
-
924
989
  interface IText {
925
990
  text?: string;
926
991
  fontName?: string;
@@ -1359,4 +1424,4 @@ declare class WebColors {
1359
1424
  static RebeccaPurple: RgbaColor;
1360
1425
  }
1361
1426
 
1362
- export { Action, Activity, BrowserImage, Composite, CompositeOptions, Constants, ConstraintType, Constraints, CustomAction, CustomActionOptions, DefaultParameter, Dimensions, DrawableOptions, Entity, EntityEventListener, EntityOptions, EntityType, 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, SessionOptions, Shape, ShapeOptions, ShapeType, Size, Sprite, SpriteOptions, Story, StoryOptions, TextLine, TextLineOptions, TextOptions, Timer, Transition, TransitionDirection, TransitionType, TrialData, TrialSchema, Uuid, WaitAction, WaitActionOptions, WebColors, handleInterfaceOptions };
1427
+ export { Action, Activity, ActivityDataEvent, ActivityLifecycleEvent, BrowserImage, Composite, CompositeOptions, Constants, ConstraintType, Constraints, CustomAction, CustomActionOptions, DefaultParameter, Dimensions, DrawableOptions, Entity, EntityEventListener, EntityOptions, EntityType, EventBase, EventType, FontData, FontManager, Game, GameData, GameOptions, GameParameters, 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 };