@wonderlandengine/components
Version:
Wonderland Engine's official component library.
178 lines • 4.43 kB
JavaScript
import { Component, Emitter } from '@wonderlandengine/api';
/**
* Click/hover/move/button target for [cursor](#cursor).
*
* To trigger code when clicking, hovering, unhovering, moving cursor, pressing
* cursor button or releasing cursor button, use `.addClickFunction(f)`,
* `.addHoverFunction(f)`, `.addUnHoverFunction(f)`,
* `.addMoveFunction(f)`, `.addDownFunction(f)` and
* `.addUpFunction(f)` respectively with any `function f() {}`.
*
* To call members on a different component, you can set up a cursor target like
* so:
*
* ```js
* start: function() {
* let target = this.object.addComponent('cursor-target');
* target.onClick.add(this.onClick.bind(this));
* },
* onClick: function() {
* console.log(this.object.name, "was clicked!");
* }
* ```
* **Functions:**
*
* ```js
* const target = this.object.getComponent(CursorTarget);
* const callback = function(object, cursorComponent) {};
*
* target.onHover.add(callback);
* target.onHover.remove(callback);
*
* target.onUnHover.add(callback);
* target.onUnHover.remove(callback);
*
* target.onClick.add(callback);
* target.onClick.remove(callback);
*
* target.onMove.add(callback);
* target.onMove.remove(callback);
*
* target.onDown.add(callback);
* target.onDown.remove(callback);
*
* target.onUp.add(callback);
* target.onUp.remove(callback);
* ```
*
* **Requirements:**
* - a `collision` component to be attached to the same object.
*
* See [Animation Example](/showcase/animation).
*/
class CursorTarget extends Component {
static TypeName = 'cursor-target';
static Properties = {};
/** Emitter for events when the target is hovered */
onHover = new Emitter();
/** Emitter for events when the target is unhovered */
onUnhover = new Emitter();
/** Emitter for events when the target is clicked */
onClick = new Emitter();
/** Emitter for events when the cursor moves on the target */
onMove = new Emitter();
/** Emitter for events when the user pressed the select button on the target */
onDown = new Emitter();
/** Emitter for events when the user unpressed the select button on the target */
onUp = new Emitter();
/**
* @deprecated Use the emitter instead.
*
* @example
* this.onHover.add(f);
*/
addHoverFunction(f) {
this.onHover.add(f);
}
/**
* @deprecated Use the emitter instead.
*
* @example
* this.onHover.remove(f);
*/
removeHoverFunction(f) {
this.onHover.remove(f);
}
/**
* @deprecated Use the emitter instead.
*
* @example
* this.onUnhover.add(f);
*/
addUnHoverFunction(f) {
this.onUnhover.add(f);
}
/**
* @deprecated Use the emitter instead.
*
* @example
* this.onUnhover.remove(f);
*/
removeUnHoverFunction(f) {
this.onUnhover.remove(f);
}
/**
* @deprecated Use the emitter instead.
*
* @example
* this.onClick.add(f);
*/
addClickFunction(f) {
this.onClick.add(f);
}
/**
* @deprecated Use the emitter instead.
*
* @example
* component.onClick.remove(f);
*/
removeClickFunction(f) {
this.onClick.remove(f);
}
/**
* @deprecated Use the emitter instead.
*
* @example
* component.onMove.add(f);
*/
addMoveFunction(f) {
this.onMove.add(f);
}
/**
* @deprecated Use the emitter instead.
*
* @example
* component.onMove.remove(f);
*/
removeMoveFunction(f) {
this.onMove.remove(f);
}
/**
* @deprecated Use the emitter instead.
*
* @example
* component.onDown.add(f);
*/
addDownFunction(f) {
this.onDown.add(f);
}
/**
* @deprecated Use the emitter instead.
*
* @example
* component.onDown.remove(f);
*/
removeDownFunction(f) {
this.onDown.remove(f);
}
/**
* @deprecated Use the emitter instead.
*
* @example
* component.onUp.add(f);
*/
addUpFunction(f) {
this.onUp.add(f);
}
/**
* @deprecated Use the emitter instead.
*
* @example
* component.onUp.remove(f);
*/
removeUpFunction(f) {
this.onUp.remove(f);
}
}
export { CursorTarget };
//# sourceMappingURL=cursor-target.js.map