All files / src/components Joystick.tsx

79.69% Statements 106/133
61.11% Branches 22/36
85.71% Functions 12/14
84.8% Lines 106/125

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247                            6x 6x 6x 6x 6x   6x       6x     6x 4x 4x 4x   4x 4x   4x 4x 4x 4x 4x 4x 4x 4x     6x 1x 1x 1x 1x 1x   1x 1x 1x 1x   6x 4x 4x 4x   4x 4x 4x 4x 4x 4x 4x   4x     4x   4x 2x 2x 2x 2x   2x     2x               6x 6x       6x       4x       4x 4x     4x 4x 4x     6x 2x 2x       2x 2x     6x 2x     2x 2x 2x       2x     6x                 6x 2x 2x 2x 2x 2x       2x                               6x 3x 3x 2x       2x 2x   1x 1x       1x       1x       6x                       6x 6x   6x 6x 6x     6x 6x 6x 6x   6x 6x 6x     6x 6x 6x     6x                                  
import React, {useEffect, useRef} from 'react';
import {Vec2} from '../utils/RTCGameUtils';
 
export interface JoystickProps {
  setIsMoving: (arg0: boolean) => void;
  setNextNormalizedDirectionVector: (arg0: Vec2) => void;
  setCameraScaleByPinch: (arg0: number) => void;
  // isClickRef: React.MutableRefObject<boolean>;
  getCameraScale: () => number;
  divContainer: HTMLDivElement;
}
 
export default function Joystick(props: JoystickProps): JSX.Element {
  // useRef
  const joystickBaseRef = useRef<HTMLImageElement>(null);
  const joystickRef = useRef<HTMLImageElement>(null);
  const touchStartPosRef = useRef<Vec2>({x: 0, y: 0});
  const touchingPosRef = useRef<Vec2>({x: 0, y: 0});
  const isClickRef = useRef(false);
 
  const oldTouchesPositions = useRef<Vec2[]>([
    {x: 0, y: 0},
    {x: 0, y: 0},
  ]);
  const oldCameraScale = useRef(0);
  // useContext
 
  const revealJoystickBase = () => {
    Iif (!joystickRef.current || !joystickBaseRef.current) return;
    const joystickBase = joystickBaseRef.current;
    const joystick = joystickRef.current;
 
    const posX = touchStartPosRef.current.x;
    const posY = touchStartPosRef.current.y;
 
    Iif (joystickBase === null) return;
    Iif (joystick === null) return;
    joystickBase.style.left = `${posX - joystickBase.width / 2}px`;
    joystickBase.style.top = `${posY - joystickBase.height / 2}px`;
    joystickBase.style.visibility = 'visible';
    joystick.style.left = `${posX - joystick.width / 2}px`;
    joystick.style.top = `${posY - joystick.height / 2}px`;
    joystick.style.visibility = 'visible';
  };
 
  const hideJoystickBase = () => {
    Iif (!joystickRef.current || !joystickBaseRef.current) return;
    const joystickBase = joystickBaseRef.current;
    const joystick = joystickRef.current;
    joystickBase.style.visibility = 'hidden';
    joystick.style.visibility = 'hidden';
 
    joystickBase.style.left = '0px';
    joystickBase.style.top = '0px';
    joystick.style.left = '0px';
    joystick.style.top = '0px';
  };
  const moveJoystick = () => {
    Iif (!joystickRef.current || !joystickBaseRef.current) return;
    const joystickBase = joystickBaseRef.current;
    const joystick = joystickRef.current;
 
    const startPosX = touchStartPosRef.current.x;
    const startPosY = touchStartPosRef.current.y;
    const endPosX = touchingPosRef.current.x;
    const endPosY = touchingPosRef.current.y;
    Iif (joystick === null) return;
    Iif (joystickBase === null) return;
    Iif (!isClickRef.current) return;
 
    const dist2 = Math.sqrt(
      Math.pow(endPosX - startPosX, 2) + Math.pow(endPosY - startPosY, 2),
    );
    const dist1 = joystickBase.width / 2 - joystick.width / 2;
 
    if (dist2 <= dist1) {
      const x = endPosX - joystick.width / 2;
      const y = endPosY - joystick.height / 2;
      joystick.style.left = `${x}px`;
      joystick.style.top = `${y}px`;
    } else {
      joystick.style.left = `${
        (dist1 * (endPosX - startPosX)) / dist2 + startPosX - joystick.width / 2
      }px`;
      joystick.style.top = `${
        (dist1 * (endPosY - startPosY)) / dist2 +
        startPosY -
        joystick.height / 2
      }px`;
    }
  };
 
  const getLen = (pos1: Vec2, pos2: Vec2): number => {
    return Math.sqrt(
      Math.pow(pos1.x - pos2.x, 2) + Math.pow(pos1.y - pos2.y, 2),
    );
  };
  const getDir = (
    pos1: Vec2 = touchingPosRef.current,
    pos2: Vec2 = touchStartPosRef.current,
  ): Vec2 => {
    const result: Vec2 = {
      x: pos1.x - pos2.x,
      y: pos1.y - pos2.y,
    };
    const len = getLen(pos1, pos2);
    Iif (len === 0) {
      return {x: 0, y: 1};
    }
    result.x /= len;
    result.y /= len;
    return result;
  };
 
  const divMouseMoveEventHandler = (e: MouseEvent) => {
    e.preventDefault();
    touchingPosRef.current = {
      x: e.clientX,
      y: e.clientY,
    };
    props.setNextNormalizedDirectionVector(getDir());
    moveJoystick();
  };
 
  const divMouseDownEventHandler = (e: MouseEvent) => {
    e.preventDefault();
    // test.current = false;
    // console.log(`down ${test.current}`);
    isClickRef.current = true;
    props.setIsMoving(true);
    touchStartPosRef.current = {
      x: e.clientX,
      y: e.clientY,
    };
    revealJoystickBase();
  };
 
  const divMouseUpEventHandler = (e: MouseEvent) => {
    e.preventDefault();
    // test.current = true;
    // console.log(`up ${test.current}`);
    isClickRef.current = false;
    props.setIsMoving(false);
    hideJoystickBase();
  };
 
  const divTouchStartEventHandler = (e: TouchEvent) => {
    e.preventDefault();
    isClickRef.current = true;
    if (e.touches.length === 1) {
      props.setIsMoving(true);
      touchStartPosRef.current = {
        x: e.touches[0].clientX,
        y: e.touches[0].clientY,
      };
      revealJoystickBase();
    } else E{
      props.setIsMoving(false);
      hideJoystickBase();
      oldCameraScale.current = props.getCameraScale();
      oldTouchesPositions.current[0] = {
        x: e.touches[0].clientX,
        y: e.touches[0].clientY,
      };
      oldTouchesPositions.current[1] = {
        x: e.touches[1].clientX,
        y: e.touches[1].clientY,
      };
    }
  };
 
  const divTouchMoveEventHandler = (e: TouchEvent) => {
    e.preventDefault();
    if (e.touches.length === 1) {
      touchingPosRef.current = {
        x: e.touches[0].clientX,
        y: e.touches[0].clientY,
      };
      props.setNextNormalizedDirectionVector(getDir());
      moveJoystick();
    } else {
      hideJoystickBase();
      const oldLen = getLen(
        oldTouchesPositions.current[0],
        oldTouchesPositions.current[1],
      );
      const newLen = getLen(
        {x: e.touches[0].clientX, y: e.touches[0].clientY},
        {x: e.touches[1].clientX, y: e.touches[1].clientY},
      );
      props.setCameraScaleByPinch((oldCameraScale.current * newLen) / oldLen);
    }
  };
 
  const divTouchEndEventHandler = (e: TouchEvent) => {
    e.preventDefault();
    isClickRef.current = false;
    hideJoystickBase();
    props.setIsMoving(false);
    if (joystickRef.current && joystickBaseRef.current) {
      joystickRef.current.style.left = '0px';
      joystickRef.current.style.top = '0px';
      joystickBaseRef.current.style.left = '0px';
      joystickBaseRef.current.style.top = '0px';
    }
  };
  useEffect(() => {
    const divContainer = props.divContainer;
    //for Desktop
    divContainer.addEventListener('mousedown', divMouseDownEventHandler);
    divContainer.addEventListener('mousemove', divMouseMoveEventHandler);
    divContainer.addEventListener('mouseup', divMouseUpEventHandler);
 
    //for Phone
    divContainer.addEventListener('touchstart', divTouchStartEventHandler);
    divContainer.addEventListener('touchmove', divTouchMoveEventHandler);
    divContainer.addEventListener('touchend', divTouchEndEventHandler);
    return () => {
      //for Desktop
      divContainer.removeEventListener('mousedown', divMouseDownEventHandler);
      divContainer.removeEventListener('mousemove', divMouseMoveEventHandler);
      divContainer.removeEventListener('mouseup', divMouseUpEventHandler);
 
      //for Phone
      divContainer.removeEventListener('touchstart', divTouchStartEventHandler);
      divContainer.removeEventListener('touchmove', divTouchMoveEventHandler);
      divContainer.removeEventListener('touchend', divTouchEndEventHandler);
    };
  }, []);
  return (
    <>
      <img
        className="joystickBase"
        src="./assets/joystick/joystick.png"
        alt="조이스틱베이스"
        ref={joystickBaseRef}
      />
      <img
        className="joystick"
        src="./assets/joystick/joystick.png"
        alt="조이스틱"
        ref={joystickRef}
      />
    </>
  );
}