|
@@ -1,27 +1,98 @@
|
|
module BABYLON {
|
|
module BABYLON {
|
|
|
|
+ /**
|
|
|
|
+ * This is a list of all the different input types that are available in the application.
|
|
|
|
+ * Fo instance: ArcRotateCameraGamepadInput...
|
|
|
|
+ */
|
|
export var CameraInputTypes = {};
|
|
export var CameraInputTypes = {};
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * This is the contract to implement in order to create a new input class.
|
|
|
|
+ * Inputs are dealing with listening to user actions and moving the camera accordingly.
|
|
|
|
+ */
|
|
export interface ICameraInput<TCamera extends Camera> {
|
|
export interface ICameraInput<TCamera extends Camera> {
|
|
|
|
+ /**
|
|
|
|
+ * Defines the camera the input is attached to.
|
|
|
|
+ */
|
|
camera: Nullable<TCamera>;
|
|
camera: Nullable<TCamera>;
|
|
|
|
+ /**
|
|
|
|
+ * Gets the class name of the current intput.
|
|
|
|
+ * @returns the class name
|
|
|
|
+ */
|
|
getClassName(): string;
|
|
getClassName(): string;
|
|
|
|
+ /**
|
|
|
|
+ * Get the friendly name associated with the input class.
|
|
|
|
+ * @returns the input friendly name
|
|
|
|
+ */
|
|
getSimpleName(): string;
|
|
getSimpleName(): string;
|
|
- attachControl: (element: HTMLElement, noPreventDefault?: boolean) => void;
|
|
|
|
- detachControl: (element: Nullable<HTMLElement>) => void;
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Attach the input controls to a specific dom element to get the input from.
|
|
|
|
+ * @param element Defines the element the controls should be listened from
|
|
|
|
+ * @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
|
|
|
|
+ */
|
|
|
|
+ attachControl(element: HTMLElement, noPreventDefault?: boolean): void;
|
|
|
|
+ /**
|
|
|
|
+ * Detach the current controls from the specified dom element.
|
|
|
|
+ * @param element Defines the element to stop listening the inputs from
|
|
|
|
+ */
|
|
|
|
+ detachControl(element: Nullable<HTMLElement>): void;
|
|
|
|
+ /**
|
|
|
|
+ * Update the current camera state depending on the inputs that have been used this frame.
|
|
|
|
+ * This is a dynamically created lambda to avoid the performance penalty of looping for inputs in the render loop.
|
|
|
|
+ */
|
|
checkInputs?: () => void;
|
|
checkInputs?: () => void;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Represents a map of input types to input instance or input index to input instance.
|
|
|
|
+ */
|
|
export interface CameraInputsMap<TCamera extends Camera> {
|
|
export interface CameraInputsMap<TCamera extends Camera> {
|
|
|
|
+ /**
|
|
|
|
+ * Accessor to the input by input type.
|
|
|
|
+ */
|
|
[name: string]: ICameraInput<TCamera>;
|
|
[name: string]: ICameraInput<TCamera>;
|
|
|
|
+ /**
|
|
|
|
+ * Accessor to the input by input index.
|
|
|
|
+ */
|
|
[idx: number]: ICameraInput<TCamera>;
|
|
[idx: number]: ICameraInput<TCamera>;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * This represents the input manager used within a camera.
|
|
|
|
+ * It helps dealing with all the different kind of input attached to a camera.
|
|
|
|
+ * @see http://doc.babylonjs.com/how_to/customizing_camera_inputs
|
|
|
|
+ */
|
|
export class CameraInputsManager<TCamera extends Camera> {
|
|
export class CameraInputsManager<TCamera extends Camera> {
|
|
- attached: CameraInputsMap<TCamera>;
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Defines the list of inputs attahed to the camera.
|
|
|
|
+ */
|
|
|
|
+ public attached: CameraInputsMap<TCamera>;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Defines the dom element the camera is collecting inputs from.
|
|
|
|
+ * This is null if the controls have not been attached.
|
|
|
|
+ */
|
|
public attachedElement: Nullable<HTMLElement>;
|
|
public attachedElement: Nullable<HTMLElement>;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
|
|
|
|
+ */
|
|
public noPreventDefault: boolean;
|
|
public noPreventDefault: boolean;
|
|
- camera: TCamera;
|
|
|
|
- checkInputs: () => void;
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Defined the camera the input manager belongs to.
|
|
|
|
+ */
|
|
|
|
+ public camera: TCamera;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Update the current camera state depending on the inputs that have been used this frame.
|
|
|
|
+ * This is a dynamically created lambda to avoid the performance penalty of looping for inputs in the render loop.
|
|
|
|
+ */
|
|
|
|
+ public checkInputs: () => void;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Instantiate a new Camera Input Manager.
|
|
|
|
+ * @param camera Defines the camera the input manager blongs to
|
|
|
|
+ */
|
|
constructor(camera: TCamera) {
|
|
constructor(camera: TCamera) {
|
|
this.attached = {};
|
|
this.attached = {};
|
|
this.camera = camera;
|
|
this.camera = camera;
|
|
@@ -33,7 +104,7 @@ module BABYLON {
|
|
* @see http://doc.babylonjs.com/how_to/customizing_camera_inputs
|
|
* @see http://doc.babylonjs.com/how_to/customizing_camera_inputs
|
|
* @param input camera input method
|
|
* @param input camera input method
|
|
*/
|
|
*/
|
|
- public add(input: ICameraInput<TCamera>) {
|
|
|
|
|
|
+ public add(input: ICameraInput<TCamera>): void {
|
|
var type = input.getSimpleName();
|
|
var type = input.getSimpleName();
|
|
if (this.attached[type]) {
|
|
if (this.attached[type]) {
|
|
Tools.Warn("camera input of type " + type + " already exists on camera");
|
|
Tools.Warn("camera input of type " + type + " already exists on camera");
|
|
@@ -54,12 +125,13 @@ module BABYLON {
|
|
input.attachControl(this.attachedElement);
|
|
input.attachControl(this.attachedElement);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Remove a specific input method from a camera
|
|
* Remove a specific input method from a camera
|
|
* example: camera.inputs.remove(camera.inputs.attached.mouse);
|
|
* example: camera.inputs.remove(camera.inputs.attached.mouse);
|
|
* @param inputToRemove camera input method
|
|
* @param inputToRemove camera input method
|
|
*/
|
|
*/
|
|
- public remove(inputToRemove: ICameraInput<TCamera>) {
|
|
|
|
|
|
+ public remove(inputToRemove: ICameraInput<TCamera>): void {
|
|
for (var cam in this.attached) {
|
|
for (var cam in this.attached) {
|
|
var input = this.attached[cam];
|
|
var input = this.attached[cam];
|
|
if (input === inputToRemove) {
|
|
if (input === inputToRemove) {
|
|
@@ -71,7 +143,12 @@ module BABYLON {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- public removeByType(inputType: string) {
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Remove a specific input type from a camera
|
|
|
|
+ * example: camera.inputs.remove("ArcRotateCameraGamepadInput");
|
|
|
|
+ * @param inputType the type of the input to remove
|
|
|
|
+ */
|
|
|
|
+ public removeByType(inputType: string): void {
|
|
for (var cam in this.attached) {
|
|
for (var cam in this.attached) {
|
|
var input = this.attached[cam];
|
|
var input = this.attached[cam];
|
|
if (input.getClassName() === inputType) {
|
|
if (input.getClassName() === inputType) {
|
|
@@ -91,13 +168,22 @@ module BABYLON {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- public attachInput(input: ICameraInput<TCamera>) {
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Attach the input controls to the currently attached dom element to listen the events from.
|
|
|
|
+ * @param input Defines the input to attach
|
|
|
|
+ */
|
|
|
|
+ public attachInput(input: ICameraInput<TCamera>): void {
|
|
if (this.attachedElement) {
|
|
if (this.attachedElement) {
|
|
input.attachControl(this.attachedElement, this.noPreventDefault);
|
|
input.attachControl(this.attachedElement, this.noPreventDefault);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- public attachElement(element: HTMLElement, noPreventDefault: boolean = false) {
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Attach the current manager inputs controls to a specific dom element to listen the events from.
|
|
|
|
+ * @param element Defines the dom element to collect the events from
|
|
|
|
+ * @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
|
|
|
|
+ */
|
|
|
|
+ public attachElement(element: HTMLElement, noPreventDefault: boolean = false): void {
|
|
if (this.attachedElement) {
|
|
if (this.attachedElement) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
@@ -111,7 +197,12 @@ module BABYLON {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- public detachElement(element: HTMLElement, disconnect = false) {
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Detach the current manager inputs controls from a specific dom element.
|
|
|
|
+ * @param element Defines the dom element to collect the events from
|
|
|
|
+ * @param disconnect Defines whether the input should be removed from the current list of attached inputs
|
|
|
|
+ */
|
|
|
|
+ public detachElement(element: HTMLElement, disconnect = false): void {
|
|
if (this.attachedElement !== element) {
|
|
if (this.attachedElement !== element) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
@@ -127,7 +218,11 @@ module BABYLON {
|
|
this.attachedElement = null;
|
|
this.attachedElement = null;
|
|
}
|
|
}
|
|
|
|
|
|
- public rebuildInputCheck() {
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Rebuild the dynamic inputCheck function from the current list of
|
|
|
|
+ * defined inputs in the manager.
|
|
|
|
+ */
|
|
|
|
+ public rebuildInputCheck(): void {
|
|
this.checkInputs = () => { };
|
|
this.checkInputs = () => { };
|
|
|
|
|
|
for (var cam in this.attached) {
|
|
for (var cam in this.attached) {
|
|
@@ -141,7 +236,7 @@ module BABYLON {
|
|
/**
|
|
/**
|
|
* Remove all attached input methods from a camera
|
|
* Remove all attached input methods from a camera
|
|
*/
|
|
*/
|
|
- public clear() {
|
|
|
|
|
|
+ public clear(): void {
|
|
if (this.attachedElement) {
|
|
if (this.attachedElement) {
|
|
this.detachElement(this.attachedElement, true);
|
|
this.detachElement(this.attachedElement, true);
|
|
}
|
|
}
|
|
@@ -150,7 +245,13 @@ module BABYLON {
|
|
this.checkInputs = () => { };
|
|
this.checkInputs = () => { };
|
|
}
|
|
}
|
|
|
|
|
|
- public serialize(serializedCamera: any) {
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Serialize the current input manager attached to a camera.
|
|
|
|
+ * This ensures than once parsed,
|
|
|
|
+ * the input associated to the camera will be identical to the current ones
|
|
|
|
+ * @param serializedCamera Defines the camera serialization JSON the input serialization should write to
|
|
|
|
+ */
|
|
|
|
+ public serialize(serializedCamera: any): void {
|
|
var inputs: { [key: string]: any } = {};
|
|
var inputs: { [key: string]: any } = {};
|
|
for (var cam in this.attached) {
|
|
for (var cam in this.attached) {
|
|
var input = this.attached[cam];
|
|
var input = this.attached[cam];
|
|
@@ -161,7 +262,12 @@ module BABYLON {
|
|
serializedCamera.inputsmgr = inputs;
|
|
serializedCamera.inputsmgr = inputs;
|
|
}
|
|
}
|
|
|
|
|
|
- public parse(parsedCamera: any) {
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Parses an input manager serialized JSON to restore the previous list of inputs
|
|
|
|
+ * and states associated to a camera.
|
|
|
|
+ * @param parsedCamera Defines the JSON to parse
|
|
|
|
+ */
|
|
|
|
+ public parse(parsedCamera: any): void {
|
|
var parsedInputs = parsedCamera.inputsmgr;
|
|
var parsedInputs = parsedCamera.inputsmgr;
|
|
if (parsedInputs) {
|
|
if (parsedInputs) {
|
|
this.clear();
|
|
this.clear();
|