All files / src/components/sprites MetaspriteEditorPreviewSettings.tsx

0% Statements 0/86
0% Branches 0/34
0% Functions 0/31
0% Lines 0/84

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                                                                                                                                                                                                                                                                                                                                                                                                                                                             
import React, { useEffect, useRef, useState } from "react";
import styled from "styled-components";
import {
  metaspriteSelectors,
  sceneSelectors,
  spriteSheetSelectors,
} from "store/features/entities/entitiesState";
import editorActions from "store/features/editor/editorActions";
import l10n from "shared/lib/lang/l10n";
import { SceneSelect } from "components/forms/SceneSelect";
import { SelectMenu, selectMenuStyleProps } from "ui/form/Select";
import { RelativePortal } from "ui/layout/RelativePortal";
import { TooltipWrapper } from "ui/tooltips/Tooltip";
import { FixedSpacer } from "ui/spacing/Spacing";
import { sceneName } from "shared/lib/entities/entitiesHelpers";
import { useAppDispatch, useAppSelector } from "store/hooks";
 
interface MetaspriteEditorPreviewSettingsProps {
  spriteSheetId: string;
  metaspriteId: string;
}
 
const Wrapper = styled.div`
  position: absolute;
  display: flex;
  align-items: center;
  bottom: 10px;
  left: 10px;
  z-index: 11;
  border-radius: 16px;
  background: ${(props) => props.theme.colors.background};
  box-shadow: 0 0 0 2px ${(props) => props.theme.colors.background};
  font-size: ${(props) => props.theme.typography.fontSize};
`;
 
const Pill = styled.button`
  color: ${(props) => props.theme.colors.button.text};
  background: ${(props) => props.theme.colors.list.activeBackground};
  border: 0px;
  border-radius: 16px;
  padding: 3px 10px;
  font-size: ${(props) => props.theme.typography.fontSize};
 
  &:active {
    background: ${(props) => props.theme.colors.list.selectedBackground};
  }
`;
 
const Info = styled.div`
  color: ${(props) => props.theme.colors.secondaryText};
  padding: 0 5px;
  margin-left: -5px;
  border-radius: 16px;
 
  &:hover {
    background: ${(props) => props.theme.colors.list.selectedBackground};
  }
`;
 
const ButtonCover = styled.div`
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 60px;
`;
 
const MetaspriteEditorPreviewSettings = ({
  spriteSheetId,
  metaspriteId,
}: MetaspriteEditorPreviewSettingsProps) => {
  const dispatch = useAppDispatch();
 
  const buttonRef = useRef<HTMLButtonElement>(null);
  const timerRef = useRef<ReturnType<typeof setTimeout>>();
 
  const [isOpen, setIsOpen] = useState<boolean>(false);
  const [buttonFocus, setButtonFocus] = useState<boolean>(false);
  const value = useAppSelector((state) => state.editor.previewAsSceneId);
  const spriteSheet = useAppSelector((state) =>
    spriteSheetSelectors.selectById(state, spriteSheetId)
  );
  const metasprite = useAppSelector((state) =>
    metaspriteSelectors.selectById(state, metaspriteId)
  );
  const scene = useAppSelector((state) =>
    sceneSelectors.selectById(state, value)
  );
  const scenes = useAppSelector((state) => sceneSelectors.selectIds(state));
  const sceneIndex = scenes.indexOf(value);
  const colorsEnabled = useAppSelector(
    (state) => state.project.present.settings.colorMode !== "mono"
  );
 
  useEffect(() => {
    Iif (buttonFocus) {
      window.addEventListener("keydown", onKeyDownClosed);
    }
    return () => {
      window.removeEventListener("keydown", onKeyDownClosed);
    };
  }, [buttonFocus]);
 
  useEffect(() => {
    Iif (isOpen) {
      window.addEventListener("keydown", onKeyDownOpen);
    }
    return () => {
      window.removeEventListener("keydown", onKeyDownOpen);
    };
  }, [isOpen]);
 
  const onKeyDownClosed = (e: KeyboardEvent) => {
    Iif (e.key === "ArrowDown" || e.key === "ArrowUp") {
      setIsOpen(true);
    }
  };
 
  const onKeyDownOpen = (e: KeyboardEvent) => {
    Iif (e.key === "Escape") {
      setIsOpen(false);
      buttonRef.current?.focus();
    }
  };
 
  const openMenu = () => {
    setIsOpen(true);
    cancelDelayedButtonFocus();
  };
 
  const closeMenu = () => {
    setIsOpen(false);
  };
 
  const onSelectChange = (newValue: string) => {
    closeMenu();
    onChange?.(newValue);
    buttonRef.current?.focus();
  };
 
  const onButtonFocus = () => {
    setButtonFocus(true);
  };
 
  const onButtonBlur = () => {
    setButtonFocus(false);
  };
 
  const delayedButtonFocus = () => {
    timerRef.current = setTimeout(() => {
      buttonRef.current?.focus();
    }, 100);
  };
 
  const cancelDelayedButtonFocus = () => {
    Iif (timerRef.current) {
      clearTimeout(timerRef.current);
    }
  };
 
  const onChange = (newValue: string) => {
    dispatch(editorActions.setPreviewAsSceneId(newValue));
  };
 
  Iif (!spriteSheet || !metasprite) {
    return null;
  }
 
  return (
    <Wrapper>
      {isOpen && <ButtonCover onMouseDown={delayedButtonFocus} />}
      {colorsEnabled && (
        <>
          <RelativePortal pin="bottom-left" offsetY={-10}>
            {isOpen && (
              <SelectMenu>
                <SceneSelect
                  name="previewAs"
                  value={value}
                  onChange={onSelectChange}
                  onBlur={closeMenu}
                  maxMenuHeight={200}
                  optional
                  optionalLabel={l10n("FIELD_DEFAULT_COLORS")}
                  {...selectMenuStyleProps}
                />
              </SelectMenu>
            )}
          </RelativePortal>
          <Pill
            ref={buttonRef}
            onClick={openMenu}
            onFocus={onButtonFocus}
            onBlur={onButtonBlur}
          >
            ▲{" "}
            {scene
              ? l10n("FIELD_PREVIEW_AS_SCENE", {
                  sceneName: sceneName(scene, sceneIndex),
                })
              : l10n("FIELD_PREVIEW_AS_DEFAULT")}
          </Pill>
          <FixedSpacer width={10} />
        </>
      )}
      <TooltipWrapper tooltip={l10n("FIELD_SPRITE_TILES_TOOLTIP")}>
        <Info>
          {l10n("FIELD_TILES")}={metasprite.tiles.length}
        </Info>
      </TooltipWrapper>
      <FixedSpacer width={5} />
      <TooltipWrapper tooltip={l10n("FIELD_SPRITE_UNIQUE_TILES_TOOLTIP")}>
        <Info>
          {l10n("FIELD_UNIQUE")}={spriteSheet.numTiles}
        </Info>
      </TooltipWrapper>
      <FixedSpacer width={5} />
    </Wrapper>
  );
};
 
export default MetaspriteEditorPreviewSettings;