File size: 1,376 Bytes
1afe868
 
 
 
 
 
aa1b508
7cefe36
9d0c4b5
7cefe36
 
aa74807
 
 
 
1afe868
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useFrame } from '@react-three/fiber';
import { Fragment, forwardRef, useImperativeHandle } from 'react';

import { FacingDirection, RotationStep } from './consts';
import { frameCallback, rotationController } from './rotation-controller';
import { RotationPanel } from './rotation-panel';

export type RotatorRef = {
  rotate: (steps: Array<RotationStep>) => void;
};

type RotatorProps = {
  cubeSpeed: number;
};

export const Rotator = forwardRef<RotatorRef, RotatorProps>(({ cubeSpeed }: RotatorProps, ref) => {
  rotationController.setCubeSpeed(cubeSpeed);

  useImperativeHandle(ref, () => ({
    rotate: (steps: Array<RotationStep>) => rotationController.addRotationStep(...steps),
  }));

  useFrame(frameCallback);

  return (
    <>
      {['front', 'back', 'left', 'right', 'top', 'bottom'].map((facingDirection) => (
        <Fragment key={facingDirection}>
          <RotationPanel
            direction="clockwise"
            facingDirection={facingDirection as FacingDirection}
            onClick={(s) => rotationController.addRotationStep(s)}
          />
          <RotationPanel
            direction="counter-clockwise"
            facingDirection={facingDirection as FacingDirection}
            onClick={(s) => rotationController.addRotationStep(s)}
          />
        </Fragment>
      ))}
    </>
  );
});

Rotator.displayName = 'Rotator';