actionEvent.ts 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { AbstractMesh } from "../Meshes/abstractMesh";
  2. import { Nullable } from "../types";
  3. import { Sprite } from "../Sprites/sprite";
  4. import { Scene } from "../scene";
  5. import { Vector2 } from "../Maths/math.vector";
  6. /**
  7. * Interface used to define ActionEvent
  8. */
  9. export interface IActionEvent {
  10. /** The mesh or sprite that triggered the action */
  11. source: any;
  12. /** The X mouse cursor position at the time of the event */
  13. pointerX: number;
  14. /** The Y mouse cursor position at the time of the event */
  15. pointerY: number;
  16. /** The mesh that is currently pointed at (can be null) */
  17. meshUnderPointer: Nullable<AbstractMesh>;
  18. /** the original (browser) event that triggered the ActionEvent */
  19. sourceEvent?: any;
  20. /** additional data for the event */
  21. additionalData?: any;
  22. }
  23. /**
  24. * ActionEvent is the event being sent when an action is triggered.
  25. */
  26. export class ActionEvent implements IActionEvent {
  27. /**
  28. * Creates a new ActionEvent
  29. * @param source The mesh or sprite that triggered the action
  30. * @param pointerX The X mouse cursor position at the time of the event
  31. * @param pointerY The Y mouse cursor position at the time of the event
  32. * @param meshUnderPointer The mesh that is currently pointed at (can be null)
  33. * @param sourceEvent the original (browser) event that triggered the ActionEvent
  34. * @param additionalData additional data for the event
  35. */
  36. constructor(
  37. /** The mesh or sprite that triggered the action */
  38. public source: any,
  39. /** The X mouse cursor position at the time of the event */
  40. public pointerX: number,
  41. /** The Y mouse cursor position at the time of the event */
  42. public pointerY: number,
  43. /** The mesh that is currently pointed at (can be null) */
  44. public meshUnderPointer: Nullable<AbstractMesh>,
  45. /** the original (browser) event that triggered the ActionEvent */
  46. public sourceEvent?: any,
  47. /** additional data for the event */
  48. public additionalData?: any) {
  49. }
  50. /**
  51. * Helper function to auto-create an ActionEvent from a source mesh.
  52. * @param source The source mesh that triggered the event
  53. * @param evt The original (browser) event
  54. * @param additionalData additional data for the event
  55. * @returns the new ActionEvent
  56. */
  57. public static CreateNew(source: AbstractMesh, evt?: Event, additionalData?: any): ActionEvent {
  58. var scene = source.getScene();
  59. return new ActionEvent(source, scene.pointerX, scene.pointerY, scene.meshUnderPointer || source, evt, additionalData);
  60. }
  61. /**
  62. * Helper function to auto-create an ActionEvent from a source sprite
  63. * @param source The source sprite that triggered the event
  64. * @param scene Scene associated with the sprite
  65. * @param evt The original (browser) event
  66. * @param additionalData additional data for the event
  67. * @returns the new ActionEvent
  68. */
  69. public static CreateNewFromSprite(source: Sprite, scene: Scene, evt?: Event, additionalData?: any): ActionEvent {
  70. return new ActionEvent(source, scene.pointerX, scene.pointerY, scene.meshUnderPointer, evt, additionalData);
  71. }
  72. /**
  73. * Helper function to auto-create an ActionEvent from a scene. If triggered by a mesh use ActionEvent.CreateNew
  74. * @param scene the scene where the event occurred
  75. * @param evt The original (browser) event
  76. * @returns the new ActionEvent
  77. */
  78. public static CreateNewFromScene(scene: Scene, evt: Event): ActionEvent {
  79. return new ActionEvent(null, scene.pointerX, scene.pointerY, scene.meshUnderPointer, evt);
  80. }
  81. /**
  82. * Helper function to auto-create an ActionEvent from a primitive
  83. * @param prim defines the target primitive
  84. * @param pointerPos defines the pointer position
  85. * @param evt The original (browser) event
  86. * @param additionalData additional data for the event
  87. * @returns the new ActionEvent
  88. */
  89. public static CreateNewFromPrimitive(prim: any, pointerPos: Vector2, evt?: Event, additionalData?: any): ActionEvent {
  90. return new ActionEvent(prim, pointerPos.x, pointerPos.y, null, evt, additionalData);
  91. }
  92. }