@m2c2kit/core 0.1.7 → 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
@@ -490,17 +490,51 @@ interface EventBase {
490
490
  /** Note: I would have named it Event, but that would collide with
491
491
  * the existing, and much more well-known, Web API Event */
492
492
  declare enum EventType {
493
- sessionLifecycle = "SessionLifecycle",
494
- gameLifecycle = "GameLifecycle",
495
- gameTrial = "GameTrial"
493
+ activityData = "ActivityData",
494
+ activityLifecycle = "ActivityLifecycle",
495
+ sessionLifecycle = "SessionLifecycle"
496
+ }
497
+
498
+ interface ActivityEvent extends EventBase {
499
+ uuid: string;
500
+ name: string;
501
+ }
502
+
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;
496
529
  }
497
530
 
498
531
  interface SessionEvent extends EventBase {
499
532
  }
500
533
 
501
534
  interface SessionLifecycleEvent extends SessionEvent {
502
- initialized: boolean;
503
- ended: boolean;
535
+ initialized?: boolean;
536
+ ended?: boolean;
537
+ started?: boolean;
504
538
  }
505
539
 
506
540
  interface SessionCallbacks {
@@ -508,22 +542,79 @@ interface SessionCallbacks {
508
542
  onSessionLifecycleChange?: (event: SessionLifecycleEvent) => void;
509
543
  }
510
544
 
511
- interface GameEvent extends EventBase {
512
- gameUuid: string;
513
- gameName: string;
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;
514
552
  }
515
553
 
516
- interface GameLifecycleEvent extends GameEvent {
517
- ended: boolean;
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;
518
605
  }
519
606
 
520
- interface GameParameters {
521
- [key: string]: DefaultParameter;
522
- }
523
- interface DefaultParameter {
524
- value: any;
525
- type?: "number" | "string" | "boolean" | "object";
526
- description?: string;
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;
527
618
  }
528
619
 
529
620
  interface IDrawable {
@@ -532,14 +623,73 @@ interface IDrawable {
532
623
  zPosition: number;
533
624
  }
534
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
+
535
651
  /**
536
652
  * Color in red (0-255), green (0-255), blue (0-255), alpha (0-1) format. Must be numeric array of length 4.
537
653
  */
538
654
  declare type RgbaColor = [number, number, number, number];
539
655
 
540
- interface DrawableOptions {
541
- anchorPoint?: Point;
542
- 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;
543
693
  }
544
694
 
545
695
  interface SceneOptions extends EntityOptions, DrawableOptions {
@@ -618,7 +768,16 @@ interface TrialSchema {
618
768
  [key: string]: PropertySchema;
619
769
  }
620
770
  interface PropertySchema {
621
- 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";
622
781
  description?: string;
623
782
  }
624
783
 
@@ -660,16 +819,41 @@ interface GameOptions {
660
819
  _unitTesting?: boolean;
661
820
  }
662
821
 
822
+ interface GameData extends ActivityData {
823
+ trials: Array<TrialData>;
824
+ metadata: Metadata;
825
+ }
826
+
663
827
  interface TrialData {
664
828
  [key: string]: string | number | boolean | undefined | null;
665
829
  }
666
830
  interface Metadata {
667
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;
668
851
  }
669
852
  declare class Game implements Activity {
670
853
  _canvasKit?: CanvasKit;
671
854
  _session?: Session;
672
855
  uuid: string;
856
+ name: string;
673
857
  options: GameOptions;
674
858
  constructor(options: GameOptions);
675
859
  setParameters(newParameters: any): void;
@@ -737,6 +921,7 @@ declare class Game implements Activity {
737
921
  start(entryScene?: Scene | string): void;
738
922
  stop(): void;
739
923
  initData(): void;
924
+ private getScreenMetadata;
740
925
  /**
741
926
  * Adds data to the game's TrialData object.
742
927
  *
@@ -749,7 +934,7 @@ declare class Game implements Activity {
749
934
  /**
750
935
  * Should be called when the current trial has completed. It will
751
936
  * also increment the trial index.
752
- * Calling this will trigger the onGameTrialComplete callback function,
937
+ * Calling this will trigger the onActivityDataCreate callback function,
753
938
  * if one was provided in SessionOptions. This is how the game communicates
754
939
  * trial data to the parent session, which can then save or process the data.
755
940
  * It is the responsibility of the the game programmer to call this at
@@ -758,7 +943,7 @@ declare class Game implements Activity {
758
943
  trialComplete(): void;
759
944
  /**
760
945
  * Should be called when the current game has ended. This will trigger
761
- * the onGameLifecycleChange callback function, if one was provided in
946
+ * the onActivityLifecycleChange callback function, if one was provided in
762
947
  * SessionOptions. This is how the game can communicate its ended or
763
948
  * "finished" state to the parent session.
764
949
  * It is the responsibility of the the game programmer to call this at
@@ -801,157 +986,6 @@ declare class Game implements Activity {
801
986
  private calculateEntityAbsoluteBoundingBox;
802
987
  }
803
988
 
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
-
955
989
  interface IText {
956
990
  text?: string;
957
991
  fontName?: string;
@@ -1390,4 +1424,4 @@ declare class WebColors {
1390
1424
  static RebeccaPurple: RgbaColor;
1391
1425
  }
1392
1426
 
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 };
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 };