All files / src/components/forms ScrollBoundsInput.tsx

34.61% Statements 9/26
0% Branches 0/16
0% Functions 0/5
34.61% Lines 9/26

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 1481x 1x   1x 1x 1x 1x   1x                             1x                                                                                                                                                                                                                                                     1x  
import React, { useCallback } from "react";
import { CoordinateInput } from "ui/form/CoordinateInput";
import { SceneBoundsRect } from "shared/lib/resources/types";
import styled from "styled-components";
import clamp from "shared/lib/helpers/clamp";
import { ensureNumber } from "shared/types";
import { SCREEN_HEIGHT, SCREEN_WIDTH } from "consts";
 
const ScrollBoundsInputWrapper = styled.div`
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(2, 1fr);
  gap: 10px;
`;
 
interface ScrollBoundsInputProps {
  name: string;
  sceneWidth: number;
  sceneHeight: number;
  value: SceneBoundsRect;
  onChange?: (newValue: SceneBoundsRect | undefined) => void;
}
 
const ScrollBoundsInput = ({
  name,
  value,
  sceneWidth,
  sceneHeight,
  onChange,
}: ScrollBoundsInputProps) => {
  const onChangeBoundsX = useCallback(
    (e: React.ChangeEvent<HTMLInputElement>) => {
      const x = clamp(
        ensureNumber(parseInt(e.currentTarget.value, 10), 0),
        0,
        sceneWidth - SCREEN_WIDTH,
      );
      const width = clamp(
        value.width,
        SCREEN_WIDTH,
        Math.max(SCREEN_WIDTH, sceneWidth - x),
      );
      onChange?.({
        ...value,
        x,
        width,
      });
    },
    [onChange, sceneWidth, value],
  );
  const onChangeBoundsY = useCallback(
    (e: React.ChangeEvent<HTMLInputElement>) => {
      const y = clamp(
        ensureNumber(parseInt(e.currentTarget.value, 10), 0),
        0,
        sceneHeight - SCREEN_HEIGHT,
      );
      const height = clamp(
        value.height,
        SCREEN_HEIGHT,
        Math.max(SCREEN_HEIGHT, sceneHeight - y),
      );
 
      onChange?.({
        ...value,
        y,
        height,
      });
    },
    [onChange, sceneHeight, value],
  );
  const onChangeBoundsWidth = useCallback(
    (e: React.ChangeEvent<HTMLInputElement>) => {
      const width = clamp(
        ensureNumber(parseInt(e.currentTarget.value, 10), 0),
        SCREEN_WIDTH,
        Math.max(SCREEN_WIDTH, sceneWidth),
      );
      const x = clamp(value.x, 0, sceneWidth - width);
      onChange?.({
        ...value,
        x,
        width,
      });
    },
    [onChange, sceneWidth, value],
  );
  const onChangeBoundsHeight = useCallback(
    (e: React.ChangeEvent<HTMLInputElement>) => {
      const height = clamp(
        ensureNumber(parseInt(e.currentTarget.value, 10), 0),
        SCREEN_HEIGHT,
        Math.max(SCREEN_HEIGHT, sceneHeight),
      );
      const y = clamp(value.y, 0, sceneHeight - height);
      onChange?.({
        ...value,
        y,
        height,
      });
    },
    [onChange, sceneHeight, value],
  );
 
  return (
    <ScrollBoundsInputWrapper>
      <CoordinateInput
        name={name}
        coordinate="x"
        value={value.x}
        placeholder="0"
        min={0}
        max={255 - SCREEN_WIDTH}
        onChange={onChangeBoundsX}
      />
      <CoordinateInput
        name={`${name}_y`}
        coordinate="y"
        value={value.y}
        placeholder="0"
        min={0}
        max={255 - SCREEN_HEIGHT}
        onChange={onChangeBoundsY}
      />
      <CoordinateInput
        name={`${name}_width`}
        coordinate="w"
        value={value.width}
        placeholder={String(sceneWidth)}
        min={SCREEN_WIDTH}
        max={255}
        onChange={onChangeBoundsWidth}
      />
      <CoordinateInput
        name={`${name}_height`}
        coordinate="h"
        value={value.height}
        placeholder={String(sceneHeight)}
        min={SCREEN_HEIGHT}
        max={255}
        onChange={onChangeBoundsHeight}
      />
    </ScrollBoundsInputWrapper>
  );
};
 
export default ScrollBoundsInput;