Spaces:
Running
Running
Download assets/explore/pilot-thumbsticks.js from SimWorld-AI/Code4Scene: direct link, hf CLI and curl.
- Browser
- Download file 3.12 kB
-
https://huggingface.co/spaces/SimWorld-AI/Code4Scene/resolve/main/assets/explore/pilot-thumbsticks.js
- Command line
-
hf download hf://spaces/SimWorld-AI/Code4Scene/assets/explore/pilot-thumbsticks.js
-
curl -L -o pilot-thumbsticks.js https://huggingface.co/spaces/SimWorld-AI/Code4Scene/resolve/main/assets/explore/pilot-thumbsticks.js
3.12 kB
| // Each stick owns one captured pointer, so looking and moving work together. | |
| export class PilotThumbsticks { | |
| constructor(flight, elements) { | |
| this.flight=flight;this.handlers=[]; | |
| this.sticks=Object.entries(elements).map(([axis,element])=>({axis,element,knob:element.querySelector('.stick-knob'),pointer:null,x:0,y:0})); | |
| this.onReset=()=>this.reset();flight.inputResetListeners.add(this.onReset); | |
| for(const stick of this.sticks){ | |
| this.listen(stick.element,'pointerdown',e=>{ | |
| if(flight.mode!=='fly'||stick.pointer!==null||(e.pointerType==='mouse'&&e.button!==0))return; | |
| e.preventDefault();e.stopPropagation(); | |
| stick.pointer=e.pointerId;stick.element.setPointerCapture(e.pointerId); | |
| stick.element.classList.add('active');flight.canvas.focus({preventScroll:true});this.move(stick,e); | |
| }); | |
| this.listen(stick.element,'pointermove',e=>{ | |
| if(stick.pointer!==e.pointerId)return; | |
| e.preventDefault();e.stopPropagation();this.move(stick,e); | |
| }); | |
| for(const name of ['pointerup','pointercancel','lostpointercapture'])this.listen(stick.element,name,e=>{ | |
| if(stick.pointer!==e.pointerId)return; | |
| e.preventDefault();e.stopPropagation();this.release(stick); | |
| }); | |
| this.listen(stick.element,'contextmenu',e=>e.preventDefault()); | |
| } | |
| this.listen(window,'resize',()=>this.reset()); | |
| this.listen(window,'keydown',e=>{if(e.key==='Escape')parent.pilotMobileScreen?.leave();}); | |
| this.listen(window,'message',e=>{ | |
| if(e.source===parent&&e.origin===location.origin&&e.data?.type==='pilot-screen-change')flight.resetInputs(); | |
| }); | |
| } | |
| listen(target,name,fn){target.addEventListener(name,fn,{passive:false});this.handlers.push(()=>target.removeEventListener(name,fn));} | |
| move(stick,event){ | |
| const box=stick.element.getBoundingClientRect(),radius=Math.max(1,(box.width-stick.knob.offsetWidth)/2-5); | |
| let x=(event.clientX-box.left-box.width/2)/radius,y=(event.clientY-box.top-box.height/2)/radius; | |
| const distance=Math.hypot(x,y); | |
| if(distance>1){x/=distance;y/=distance;} | |
| stick.element.style.setProperty('--stick-x',`${x*radius}px`);stick.element.style.setProperty('--stick-y',`${y*radius}px`); | |
| // A small dead zone avoids drift. Beyond it, speed increases smoothly. | |
| const magnitude=Math.min(1,distance),scaled=magnitude>.12?(magnitude-.12)/.88:0; | |
| stick.x=magnitude?x/magnitude*scaled:0;stick.y=magnitude?y/magnitude*scaled:0; | |
| Object.assign(this.flight.analog[stick.axis],{x:stick.x,y:stick.y}); | |
| } | |
| release(stick){ | |
| const id=stick.pointer;stick.pointer=null;stick.x=0;stick.y=0; | |
| Object.assign(this.flight.analog[stick.axis],{x:0,y:0}); | |
| stick.element.classList.remove('active');stick.element.style.setProperty('--stick-x','0px');stick.element.style.setProperty('--stick-y','0px'); | |
| if(id!==null&&stick.element.hasPointerCapture(id))stick.element.releasePointerCapture(id); | |
| } | |
| reset(){for(const stick of this.sticks)this.release(stick);} | |
| destroy(){this.reset();this.flight.inputResetListeners.delete(this.onReset);this.handlers.forEach(remove=>remove());} | |
| } | |