Explorar el Código

Merge branch 'master' of https://github.com/BabylonJS/Babylon.js

David Catuhe hace 6 años
padre
commit
502cf784c2

+ 118 - 0
src/Actions/abstractActionManager.ts

@@ -0,0 +1,118 @@
+import { IDisposable } from '../scene';
+import { IActionEvent } from './actionEvent';
+import { IAction } from './action';
+import { Constants } from "../Engines/constants";
+
+/**
+ * Abstract class used to decouple action Manager from scene and meshes.
+ * Do not instantiate.
+ * @see http://doc.babylonjs.com/how_to/how_to_use_actions
+ */
+export abstract class AbstractActionManager implements IDisposable {
+
+    /** Gets the list of active triggers */
+    public static Triggers: { [key: string]: number } = {};
+
+    /** Gets the cursor to use when hovering items */
+    public hoverCursor: string = '';
+
+    /** Gets the list of actions */
+    public actions = new Array<IAction>();
+
+    /**
+     * Releases all associated resources
+     */
+    public abstract dispose(): void;
+
+    /**
+     * Does this action manager has pointer triggers
+     */
+    public abstract get hasPointerTriggers(): boolean;
+
+    /**
+     * Does this action manager has pick triggers
+     */
+    public abstract get hasPickTriggers(): boolean;
+
+    /**
+     * Process a specific trigger
+     * @param trigger defines the trigger to process
+     * @param evt defines the event details to be processed
+     */
+    public abstract processTrigger(trigger: number, evt?: IActionEvent): void;
+
+    /**
+     * Does this action manager handles actions of any of the given triggers
+     * @param triggers defines the triggers to be tested
+     * @return a boolean indicating whether one (or more) of the triggers is handled
+     */
+    public abstract hasSpecificTriggers(triggers: number[]): boolean;
+
+    /**
+     * Does this action manager handles actions of any of the given triggers. This function takes two arguments for
+     * speed.
+     * @param triggerA defines the trigger to be tested
+     * @param triggerB defines the trigger to be tested
+     * @return a boolean indicating whether one (or more) of the triggers is handled
+     */
+    public abstract hasSpecificTriggers2(triggerA: number, triggerB: number): boolean;
+
+    /**
+     * Does this action manager handles actions of a given trigger
+     * @param trigger defines the trigger to be tested
+     * @param parameterPredicate defines an optional predicate to filter triggers by parameter
+     * @return whether the trigger is handled
+     */
+    public abstract hasSpecificTrigger(trigger: number, parameterPredicate?: (parameter: any) => boolean): boolean;
+
+    /**
+         * Serialize this manager to a JSON object
+         * @param name defines the property name to store this manager
+         * @returns a JSON representation of this manager
+         */
+    public abstract serialize(name: string): any;
+
+    /**
+     * Does exist one action manager with at least one trigger
+     **/
+    public static get HasTriggers(): boolean {
+        for (var t in AbstractActionManager.Triggers) {
+            if (AbstractActionManager.Triggers.hasOwnProperty(t)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Does exist one action manager with at least one pick trigger
+     **/
+    public static get HasPickTriggers(): boolean {
+        for (var t in AbstractActionManager.Triggers) {
+            if (AbstractActionManager.Triggers.hasOwnProperty(t)) {
+                let t_int = parseInt(t);
+                if (t_int >= Constants.ACTION_OnPickTrigger && t_int <= Constants.ACTION_OnPickUpTrigger) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Does exist one action manager that handles actions of a given trigger
+     * @param trigger defines the trigger to be tested
+     * @return a boolean indicating whether the trigger is handeled by at least one action manager
+    **/
+    public static HasSpecificTrigger(trigger: number): boolean {
+        for (var t in AbstractActionManager.Triggers) {
+            if (AbstractActionManager.Triggers.hasOwnProperty(t)) {
+                let t_int = parseInt(t);
+                if (t_int === trigger) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+}

+ 33 - 1
src/Actions/action.ts

@@ -12,10 +12,42 @@ declare type Camera = import("../Cameras/camera").Camera;
 declare type Node = import("../node").Node;
 
 /**
+ * Interface used to define Action
+ */
+export interface IAction {
+    /**
+   * Trigger for the action
+   */
+    trigger: number;
+
+    /** the trigger, with or without parameters, for the action */
+    triggerOptions: any;
+
+    /**
+     * Gets the trigger parameters
+     * @returns the trigger parameters
+     */
+    getTriggerParameter(): any;
+
+    /**
+     * Internal only - executes current action event
+     * @hidden
+     */
+    _executeCurrent(evt?: ActionEvent): void;
+
+    /**
+     * Serialize placeholder for child classes
+     * @param parent of child
+     * @returns the serialized object
+     */
+    serialize(parent: any): any;
+}
+
+/**
  * The action to be carried out following a trigger
  * @see http://doc.babylonjs.com/how_to/how_to_use_actions#available-actions
  */
-export class Action {
+export class Action implements IAction {
     /**
      * Trigger for the action
      */

+ 19 - 2
src/Actions/actionEvent.ts

@@ -5,9 +5,27 @@ import { Scene } from "../scene";
 import { Vector2 } from "../Maths/math";
 
 /**
+ * Interface used to define ActionEvent
+ */
+export interface IActionEvent {
+    /** The mesh or sprite that triggered the action */
+    source: any;
+    /** The X mouse cursor position at the time of the event */
+    pointerX: number;
+    /** The Y mouse cursor position at the time of the event */
+    pointerY: number;
+    /** The mesh that is currently pointed at (can be null) */
+    meshUnderPointer: Nullable<AbstractMesh>;
+    /** the original (browser) event that triggered the ActionEvent */
+    sourceEvent?: any;
+    /** additional data for the event */
+    additionalData?: any;
+}
+
+/**
  * ActionEvent is the event being sent when an action is triggered.
  */
-export class ActionEvent {
+export class ActionEvent implements IActionEvent {
     /**
      * Creates a new ActionEvent
      * @param source The mesh or sprite that triggered the action
@@ -30,7 +48,6 @@ export class ActionEvent {
         public sourceEvent?: any,
         /** additional data for the event */
         public additionalData?: any) {
-
     }
 
     /**

+ 7 - 58
src/Actions/actionManager.ts

@@ -7,19 +7,20 @@ import { Condition, ValueCondition } from "./condition";
 import { Action } from "./action";
 import { DoNothingAction } from "./directActions";
 
-import { Constants } from "../Engines/constants";
 import { EngineStore } from "../Engines/engineStore";
-import { ActionEvent } from "../Actions/actionEvent";
+import { IActionEvent } from "../Actions/actionEvent";
 import { Logger } from "../Misc/logger";
 import { DeepCopier } from "../Misc/deepCopier";
 import { _TypeStore } from "../Misc/typeStore";
+import { AbstractActionManager } from './abstractActionManager';
+import { Constants } from "../Engines/constants";
 
 /**
  * Action Manager manages all events to be triggered on a given mesh or the global scene.
  * A single scene can have many Action Managers to handle predefined actions on specific meshes.
  * @see http://doc.babylonjs.com/how_to/how_to_use_actions
  */
-export class ActionManager {
+export class ActionManager extends AbstractActionManager {
     /**
      * Nothing
      * @see http://doc.babylonjs.com/how_to/how_to_use_actions#triggers
@@ -121,16 +122,7 @@ export class ActionManager {
      */
     public static readonly OnKeyUpTrigger = 15;
 
-    /** Gets the list of active triggers */
-    public static Triggers: { [key: string]: number } = {};
-
     // Members
-    /** Gets the list of actions */
-    public actions = new Array<Action>();
-
-    /** Gets the cursor to use when hovering items */
-    public hoverCursor: string = '';
-
     private _scene: Scene;
 
     /**
@@ -138,6 +130,7 @@ export class ActionManager {
      * @param scene defines the hosting scene
      */
     constructor(scene: Scene) {
+        super();
         this._scene = scene || EngineStore.LastCreatedScene;
 
         scene.actionManagers.push(this);
@@ -263,50 +256,6 @@ export class ActionManager {
     }
 
     /**
-     * Does exist one action manager with at least one trigger
-     **/
-    public static get HasTriggers(): boolean {
-        for (var t in ActionManager.Triggers) {
-            if (ActionManager.Triggers.hasOwnProperty(t)) {
-                return true;
-            }
-        }
-        return false;
-    }
-
-    /**
-     * Does exist one action manager with at least one pick trigger
-     **/
-    public static get HasPickTriggers(): boolean {
-        for (var t in ActionManager.Triggers) {
-            if (ActionManager.Triggers.hasOwnProperty(t)) {
-                let t_int = parseInt(t);
-                if (t_int >= ActionManager.OnPickTrigger && t_int <= ActionManager.OnPickUpTrigger) {
-                    return true;
-                }
-            }
-        }
-        return false;
-    }
-
-    /**
-     * Does exist one action manager that handles actions of a given trigger
-     * @param trigger defines the trigger to be tested
-     * @return a boolean indicating whether the trigger is handeled by at least one action manager
-    **/
-    public static HasSpecificTrigger(trigger: number): boolean {
-        for (var t in ActionManager.Triggers) {
-            if (ActionManager.Triggers.hasOwnProperty(t)) {
-                let t_int = parseInt(t);
-                if (t_int === trigger) {
-                    return true;
-                }
-            }
-        }
-        return false;
-    }
-
-    /**
      * Registers an action to this action manager
      * @param action defines the action to be registered
      * @return the action amended (prepared) after registration
@@ -358,7 +307,7 @@ export class ActionManager {
      * @param trigger defines the trigger to process
      * @param evt defines the event details to be processed
      */
-    public processTrigger(trigger: number, evt?: ActionEvent): void {
+    public processTrigger(trigger: number, evt?: IActionEvent): void {
         for (var index = 0; index < this.actions.length; index++) {
             var action = this.actions[index];
 
@@ -707,4 +656,4 @@ export class ActionManager {
             default: return "";
         }
     }
-}
+}

+ 2 - 2
src/Meshes/abstractMesh.ts

@@ -18,11 +18,11 @@ import { ICullable, BoundingInfo } from "../Culling/boundingInfo";
 import { Material } from "../Materials/material";
 import { MaterialDefines } from "../Materials/materialDefines";
 import { Light } from "../Lights/light";
-import { ActionManager } from "../Actions/actionManager";
 import { Skeleton } from "../Bones/skeleton";
 import { IEdgesRenderer } from "../Rendering/edgesRenderer";
 import { SolidParticle } from "../Particles/solidParticle";
 import { Constants } from "../Engines/constants";
+import { AbstractActionManager } from '../Actions/abstractActionManager';
 
 /** @hidden */
 class _FacetDataStorage {
@@ -488,7 +488,7 @@ export class AbstractMesh extends TransformNode implements IDisposable, ICullabl
      * Gets or sets the current action manager
      * @see http://doc.babylonjs.com/how_to/how_to_use_actions
      */
-    public actionManager: Nullable<ActionManager> = null;
+    public actionManager: Nullable<AbstractActionManager> = null;
 
     // Collisions
     private _checkCollisions = false;

+ 2 - 2
src/abstractScene.ts

@@ -12,7 +12,7 @@ import { AnimationGroup } from "./Animations/animationGroup";
 import { BaseTexture } from "./Materials/Textures/baseTexture";
 import { Material } from "./Materials/material";
 import { MultiMaterial } from "./Materials/multiMaterial";
-import { ActionManager } from "./Actions/actionManager";
+import { AbstractActionManager } from "./Actions/abstractActionManager";
 import { Camera } from "./Cameras/camera";
 import { Light } from "./Lights/light";
 import { Node } from "./node";
@@ -186,7 +186,7 @@ export abstract class AbstractScene {
     /**
      * ActionManagers available on the scene.
      */
-    public actionManagers = new Array<ActionManager>();
+    public actionManagers = new Array<AbstractActionManager>();
 
     /**
      * Textures to keep.

+ 11 - 11
src/scene.ts

@@ -37,7 +37,6 @@ import { ICollisionCoordinator, CollisionCoordinatorLegacy } from "./Collisions/
 import { PointerEventTypes, PointerInfoPre, PointerInfo } from "./Events/pointerEvents";
 import { KeyboardInfoPre, KeyboardInfo, KeyboardEventTypes } from "./Events/keyboardEvents";
 import { ActionEvent } from "./Actions/actionEvent";
-import { ActionManager } from "./Actions/actionManager";
 import { PostProcess } from "./PostProcesses/postProcess";
 import { PostProcessManager } from "./PostProcesses/postProcessManager";
 import { IOfflineProvider } from "./Offline/IOfflineProvider";
@@ -51,6 +50,7 @@ import { Constants } from "./Engines/constants";
 import { DomManagement } from "./Misc/domManagement";
 import { Logger } from "./Misc/logger";
 import { EngineStore } from "./Engines/engineStore";
+import { AbstractActionManager } from './Actions/abstractActionManager';
 
 /**
  * Define an interface for all classes that will hold resources
@@ -639,7 +639,7 @@ export class Scene extends AbstractScene implements IAnimatable {
     public static ExclusiveDoubleClickMode = false;
 
     private _initClickEvent: (obs1: Observable<PointerInfoPre>, obs2: Observable<PointerInfo>, evt: PointerEvent, cb: (clickInfo: ClickInfo, pickResult: Nullable<PickingInfo>) => void) => void;
-    private _initActionManager: (act: Nullable<ActionManager>, clickInfo: ClickInfo) => Nullable<ActionManager>;
+    private _initActionManager: (act: Nullable<AbstractActionManager>, clickInfo: ClickInfo) => Nullable<AbstractActionManager>;
     private _delayedSimpleClick: (btn: number, clickInfo: ClickInfo, cb: (clickInfo: ClickInfo, pickResult: Nullable<PickingInfo>) => void) => void;
     private _delayedSimpleClickTimeout: number;
     private _previousDelayedSimpleClickTimeout: number;
@@ -989,7 +989,7 @@ export class Scene extends AbstractScene implements IAnimatable {
      * Gets or sets the action manager associated with the scene
      * @see http://doc.babylonjs.com/how_to/how_to_use_actions
     */
-    public actionManager: ActionManager;
+    public actionManager: AbstractActionManager;
 
     private _meshesForIntersections = new SmartArrayNoDuplicate<AbstractMesh>(256);
 
@@ -1874,7 +1874,7 @@ export class Scene extends AbstractScene implements IAnimatable {
     * @param attachMove defines if you want to attach events to pointermove
     */
     public attachControl(attachUp = true, attachDown = true, attachMove = true): void {
-        this._initActionManager = (act: Nullable<ActionManager>, clickInfo: ClickInfo): Nullable<ActionManager> => {
+        this._initActionManager = (act: Nullable<AbstractActionManager>, clickInfo: ClickInfo): Nullable<AbstractActionManager> => {
             if (!this._meshPickProceed) {
                 let pickResult = this.pick(this._unTranslatedPointerX, this._unTranslatedPointerY, this.pointerDownPredicate, false, this.cameraToUseForPointers);
                 this._currentPickResult = pickResult;
@@ -1900,12 +1900,12 @@ export class Scene extends AbstractScene implements IAnimatable {
         this._initClickEvent = (obs1: Observable<PointerInfoPre>, obs2: Observable<PointerInfo>, evt: PointerEvent, cb: (clickInfo: ClickInfo, pickResult: Nullable<PickingInfo>) => void): void => {
             let clickInfo = new ClickInfo();
             this._currentPickResult = null;
-            let act: Nullable<ActionManager> = null;
+            let act: Nullable<AbstractActionManager> = null;
 
             let checkPicking = obs1.hasSpecificMask(PointerEventTypes.POINTERPICK) || obs2.hasSpecificMask(PointerEventTypes.POINTERPICK)
                 || obs1.hasSpecificMask(PointerEventTypes.POINTERTAP) || obs2.hasSpecificMask(PointerEventTypes.POINTERTAP)
                 || obs1.hasSpecificMask(PointerEventTypes.POINTERDOUBLETAP) || obs2.hasSpecificMask(PointerEventTypes.POINTERDOUBLETAP);
-            if (!checkPicking && ActionManager) {
+            if (!checkPicking && AbstractActionManager) {
                 act = this._initActionManager(act, clickInfo);
                 if (act) {
                     checkPicking = act.hasPickTriggers;
@@ -1925,7 +1925,7 @@ export class Scene extends AbstractScene implements IAnimatable {
                         checkSingleClickImmediately = !obs1.hasSpecificMask(PointerEventTypes.POINTERDOUBLETAP) &&
                             !obs2.hasSpecificMask(PointerEventTypes.POINTERDOUBLETAP);
 
-                        if (checkSingleClickImmediately && !ActionManager.HasSpecificTrigger(Constants.ACTION_OnDoublePickTrigger)) {
+                        if (checkSingleClickImmediately && !AbstractActionManager.HasSpecificTrigger(Constants.ACTION_OnDoublePickTrigger)) {
                             act = this._initActionManager(act, clickInfo);
                             if (act) {
                                 checkSingleClickImmediately = !act.hasSpecificTrigger(Constants.ACTION_OnDoublePickTrigger);
@@ -1951,7 +1951,7 @@ export class Scene extends AbstractScene implements IAnimatable {
 
                     let checkDoubleClick = obs1.hasSpecificMask(PointerEventTypes.POINTERDOUBLETAP) ||
                         obs2.hasSpecificMask(PointerEventTypes.POINTERDOUBLETAP);
-                    if (!checkDoubleClick && ActionManager.HasSpecificTrigger(Constants.ACTION_OnDoublePickTrigger)) {
+                    if (!checkDoubleClick && AbstractActionManager.HasSpecificTrigger(Constants.ACTION_OnDoublePickTrigger)) {
                         act = this._initActionManager(act, clickInfo);
                         if (act) {
                             checkDoubleClick = act.hasSpecificTrigger(Constants.ACTION_OnDoublePickTrigger);
@@ -2128,7 +2128,7 @@ export class Scene extends AbstractScene implements IAnimatable {
                 }
 
                 // Meshes
-                if (!this._meshPickProceed && (ActionManager && ActionManager.HasTriggers || this.onPointerObservable.hasObservers())) {
+                if (!this._meshPickProceed && (AbstractActionManager && AbstractActionManager.HasTriggers || this.onPointerObservable.hasObservers())) {
                     this._initActionManager(null, clickInfo);
                 }
                 if (!pickResult) {
@@ -3293,7 +3293,7 @@ export class Scene extends AbstractScene implements IAnimatable {
      * @param toRemove The action manager to remove
      * @returns The index of the removed action manager
      */
-    public removeActionManager(toRemove: ActionManager): number {
+    public removeActionManager(toRemove: AbstractActionManager): number {
         var index = this.actionManagers.indexOf(toRemove);
         if (index !== -1) {
             this.actionManagers.splice(index, 1);
@@ -3427,7 +3427,7 @@ export class Scene extends AbstractScene implements IAnimatable {
      * Adds the given action manager to this scene
      * @param newActionManager The action manager to add
      */
-    public addActionManager(newActionManager: ActionManager): void {
+    public addActionManager(newActionManager: AbstractActionManager): void {
         this.actionManagers.push(newActionManager);
     }