UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 3.97 kB
import{type as e}from"../../core/core.js";import{ACTION_KEYBOARD as s,ACTION_MOUSE as t,ACTION_GAMEPAD as a,PAD_1 as i,PAD_L_STICK_Y as o,PAD_L_STICK_X as r,PAD_R_STICK_Y as h,PAD_R_STICK_X as n,EVENT_MOUSEMOVE as _}from"./constants.js";import{Keyboard as d}from"./keyboard.js";import{Mouse as u}from"./mouse.js";class c{constructor(e,s={}){this._keyboard=void 0,this._mouse=void 0,this._gamepads=void 0,this._element=null,this._actions={},this._axes={},this._axesValues={},this._keyboard=s.keyboard||null,this._mouse=s.mouse||null,this._gamepads=s.gamepads||null,e&&this.attach(e)}attach(e){this._element=e,this._keyboard&&this._keyboard.attach(e),this._mouse&&this._mouse.attach(e)}detach(){this._keyboard&&this._keyboard.detach(),this._mouse&&this._mouse.detach(),this._element=null}disableContextMenu(){this._mouse||this._enableMouse(),this._mouse.disableContextMenu()}enableContextMenu(){this._mouse||this._enableMouse(),this._mouse.enableContextMenu()}update(e){this._keyboard&&this._keyboard.update(),this._mouse&&this._mouse.update(),this._gamepads&&this._gamepads.update(),this._axesValues={};for(const e in this._axes)this._axesValues[e]=[]}appendAction(e,s){this._actions[e]=this._actions[e]||[],this._actions[e].push(s)}registerKeys(e,t){if(this._keyboard||this._enableKeyboard(),this._actions[e])throw new Error(`Action: ${e} already registered`);if(void 0===t)throw new Error("Invalid button");t.length||(t=[t]),this.appendAction(e,{type:s,keys:t})}registerMouse(e,s){if(this._mouse||this._enableMouse(),void 0===s)throw new Error("Invalid button");this.appendAction(e,{type:t,button:s})}registerPadButton(e,s,t){if(void 0===t)throw new Error("Invalid button");this.appendAction(e,{type:a,button:t,pad:s})}registerAxis(e){const s=e.name;this._axes[s]||(this._axes[s]=[]);const t=this._axes[s].push(s);(e=e||{}).pad=e.pad||i;const a=function(a,i,d,u){switch(i){case"mousex":a._mouse.on(_,(e=>{a._axesValues[s][t]=e.dx/10}));break;case"mousey":a._mouse.on(_,(e=>{a._axesValues[s][t]=e.dy/10}));break;case"key":a._axes[s].push((()=>a._keyboard.isPressed(u)?d:0));break;case"padrx":a._axes[s].push((()=>a._gamepads.getAxis(e.pad,n)));break;case"padry":a._axes[s].push((()=>a._gamepads.getAxis(e.pad,h)));break;case"padlx":a._axes[s].push((()=>a._gamepads.getAxis(e.pad,r)));break;case"padly":a._axes[s].push((()=>a._gamepads.getAxis(e.pad,o)));break;default:throw new Error("Unknown axis")}};a(this,e.positive,1,e.positiveKey),(e.negativeKey||e.negative!==e.positive)&&a(this,e.negative,-1,e.negativeKey)}isPressed(e){if(!this._actions[e])return!1;const i=this._actions[e].length;for(let o=0;o<i;++o){const i=this._actions[e][o];switch(i.type){case s:if(this._keyboard){const e=i.keys.length;for(let s=0;s<e;s++)if(this._keyboard.isPressed(i.keys[s]))return!0}break;case t:if(this._mouse&&this._mouse.isPressed(i.button))return!0;break;case a:if(this._gamepads&&this._gamepads.isPressed(i.pad,i.button))return!0}}return!1}wasPressed(e){if(!this._actions[e])return!1;const i=this._actions[e].length;for(let o=0;o<i;++o){const i=this._actions[e][o];switch(i.type){case s:if(this._keyboard){const e=i.keys.length;for(let s=0;s<e;s++)if(this._keyboard.wasPressed(i.keys[s]))return!0}break;case t:if(this._mouse&&this._mouse.wasPressed(i.button))return!0;break;case a:if(this._gamepads&&this._gamepads.wasPressed(i.pad,i.button))return!0}}return!1}getAxis(s){let t=0;if(this._axes[s]){const a=this._axes[s].length;for(let i=0;i<a;i++)if("function"===e(this._axes[s][i])){const e=this._axes[s][i]();Math.abs(e)>Math.abs(t)&&(t=e)}else this._axesValues[s]&&Math.abs(this._axesValues[s][i])>Math.abs(t)&&(t=this._axesValues[s][i])}return t}_enableMouse(){if(this._mouse=new u,!this._element)throw new Error("Controller must be attached to an Element");this._mouse.attach(this._element)}_enableKeyboard(){if(this._keyboard=new d,!this._element)throw new Error("Controller must be attached to an Element");this._keyboard.attach(this._element)}}export{c as Controller};