Spaces:
Running

File size: 3,120 Bytes
5e1643b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// 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());}
}