All files / src/components/forms DMGPaletteSelectModal.tsx

24.07% Statements 13/54
0% Branches 0/26
0% Functions 0/15
22.64% Lines 12/53

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 1811x 1x 1x 1x   1x 1x 1x 1x 1x                                         1x                               1x                         1x                                                                                                                                                                                                                                                  
import { DMG_PALETTE } from "consts";
import React, { useEffect, useLayoutEffect, useRef, useCallback } from "react";
import { castEventToInt } from "renderer/lib/helpers/castEventValue";
import l10n from "shared/lib/lang/l10n";
import { MonoOBJPalette, MonoBGPPalette } from "shared/lib/resources/types";
import styled from "styled-components";
import { Button } from "ui/buttons/Button";
import { Label } from "ui/form/Label";
import { FormRow } from "ui/form/layout/FormLayout";
import { NumberInput } from "ui/form/NumberInput";
 
type DMGPaletteSelectModalProps = {
  onBlur: () => void;
  onReset: () => void;
  name: string;
  label?: string;
  showName?: boolean;
} & (
  | {
      isSpritePalette: true;
      value: MonoOBJPalette;
      onChange: (palette: MonoOBJPalette) => void;
    }
  | {
      isSpritePalette?: false;
      value: MonoBGPPalette;
      onChange: (palette: MonoBGPPalette) => void;
    }
);
 
const Form = styled.form<{ $showName?: boolean }>`
  min-width: 300px;
  padding-top: 5px;
 
  ${Label} {
    margin: 5px 10px;
  }
  ${(props) =>
    props.$showName
      ? `
        min-width: 200px;
  padding-top: 0px;
      `
      : ""}
`;
 
const InputGrid = styled.div<{ $isSpritePalette?: boolean }>`
  display: grid;
  width: 100%;
  grid-template-columns: repeat(2, 1fr);
  gap: 5px;
  ${(props) =>
    props.$isSpritePalette
      ? `
        grid-template-columns: repeat(3, 1fr);
      `
      : ""}
`;
 
export const DMGPaletteSelectModal = ({
  name,
  label,
  value,
  showName,
  onChange,
  onReset,
  isSpritePalette,
  onBlur,
}: DMGPaletteSelectModalProps) => {
  const timerRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
 
  const debouncedLeave = useCallback(() => {
    if (timerRef.current) {
      clearTimeout(timerRef.current);
    }
    timerRef.current = setTimeout(() => {
      onBlur();
    }, 250);
  }, [onBlur]);
 
  useEffect(() => {
    return () => {
      // Cleanup timer
      if (timerRef.current) {
        clearTimeout(timerRef.current);
      }
    };
  }, []);
 
  useLayoutEffect(() => {
    const firstInput = document.querySelector(
      `#${name}_0`,
    ) as HTMLInputElement | null;
    requestAnimationFrame(() => {
      if (firstInput) {
        firstInput.focus();
        firstInput.select();
      }
    });
  }, [name]);
 
  const onFocus = useCallback(() => {
    if (timerRef.current) {
      clearTimeout(timerRef.current);
    }
  }, []);
 
  const onFocusOut = useCallback(
    (event: React.FocusEvent<HTMLFormElement, Element>) => {
      if (event.currentTarget.contains(event.relatedTarget)) {
        return;
      }
      debouncedLeave();
    },
    [debouncedLeave],
  );
 
  const onResetBtn = useCallback(
    (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
      e.preventDefault();
      onReset();
    },
    [onReset],
  );
 
  const fields = isSpritePalette ? [0, 1, 2] : [0, 1, 2, 3];
 
  return (
    <Form onFocus={onFocus} onBlur={onFocusOut} $showName={showName}>
      {showName && (
        <Label>
          {l10n("FIELD_MONOCHROME_PALETTE")}: {label}
        </Label>
      )}
      <FormRow>
        <InputGrid $isSpritePalette={isSpritePalette}>
          {fields.map((index) => (
            <NumberInput
              key={index}
              style={{
                backgroundColor: `#${DMG_PALETTE.colors[value[index]]}`,
                color: `#${DMG_PALETTE.colors[(value[index] + 2) % 4]}`,
              }}
              id={`${name}_${index}`}
              name={`${name}_${index}`}
              min={0}
              max={3}
              value={value[index]}
              onChange={(e) => {
                const newSingleValue = castEventToInt(e, 0);
                if (
                  newSingleValue === 0 ||
                  newSingleValue === 1 ||
                  newSingleValue === 2 ||
                  newSingleValue === 3
                ) {
                  if (isSpritePalette) {
                    const newValue = [...value] as MonoOBJPalette;
                    newValue[index] = newSingleValue;
                    onChange(newValue);
                  } else {
                    const newValue = [...value] as MonoBGPPalette;
                    newValue[index] = newSingleValue;
                    onChange(newValue);
                  }
                }
              }}
              onFocus={(e) => e.currentTarget.select()}
            />
          ))}
        </InputGrid>
      </FormRow>
      <FormRow>
        <Button size="small" onClick={onResetBtn}>
          {l10n("FIELD_RESTORE_DEFAULT")}
        </Button>
      </FormRow>
    </Form>
  );
};