All files / src/components/backgrounds BackgroundViewer.tsx

0% Statements 0/42
0% Branches 0/18
0% Functions 0/12
0% Lines 0/41

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                                                                                                                                                                                                                                                                                                                                                                 
import React, { useEffect, useState } from "react";
import { useAppSelector } from "store/hooks";
import styled from "styled-components";
import {
  backgroundSelectors,
  paletteSelectors,
  sceneSelectors,
  tilesetSelectors,
} from "store/features/entities/entitiesState";
import ColorizedImage from "components/world/ColorizedImage";
import { DMG_PALETTE, TILE_SIZE } from "consts";
import { Palette } from "shared/lib/entities/entitiesTypes";
import { assetURL } from "shared/lib/helpers/assets";
import AutoColorizedImage from "components/world/AutoColorizedImage";
 
interface MetaspriteEditorProps {
  backgroundId: string;
}
 
const ScrollWrapper = styled.div`
  overflow-y: scroll;
  overflow-x: auto;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
`;
 
const ContentWrapper = styled.div`
  display: flex;
  height: 100%;
  justify-content: center;
  align-items: center;
  overflow: hidden;
 
  canvas {
    image-rendering: pixelated;
  }
`;
 
const ImageContainer = styled.div`
  border: 1px solid ${(props) => props.theme.colors.sidebar.border};
`;
 
const ImageScale = styled.div`
  display: flex;
  transform-origin: top left;
`;
 
const emptyPalettes: Palette[] = [
  DMG_PALETTE,
  DMG_PALETTE,
  DMG_PALETTE,
  DMG_PALETTE,
  DMG_PALETTE,
  DMG_PALETTE,
  DMG_PALETTE,
  DMG_PALETTE,
] as Palette[];
 
const BackgroundViewer = ({ backgroundId }: MetaspriteEditorProps) => {
  const background = useAppSelector((state) =>
    backgroundSelectors.selectById(state, backgroundId)
  );
  const tileset = useAppSelector((state) =>
    tilesetSelectors.selectById(state, backgroundId)
  );
 
  const zoom = useAppSelector((state) => state.editor.zoomImage) / 100;
  const previewAsSceneId = useAppSelector(
    (state) => state.editor.previewAsSceneId
  );
  const scene = useAppSelector((state) =>
    sceneSelectors.selectById(state, previewAsSceneId)
  );
  const palettesLookup = useAppSelector((state) =>
    paletteSelectors.selectEntities(state)
  );
  const defaultPaletteIds = useAppSelector(
    (state) => state.project.present.settings.defaultBackgroundPaletteIds
  );
  const gbcEnabled = useAppSelector(
    (state) => state.project.present.settings.colorMode !== "mono"
  );
  const [palettes, setPalettes] = useState<Palette[]>(emptyPalettes);
 
  useEffect(() => {
    setPalettes(
      Array.from(Array(8).keys()).map((paletteIndex) => {
        const paletteId =
          scene?.paletteIds?.[paletteIndex] || defaultPaletteIds[paletteIndex];
        return palettesLookup[paletteId] || (DMG_PALETTE as Palette);
      })
    );
  }, [scene, palettesLookup, defaultPaletteIds]);
 
  Iif (background) {
    return (
      <ScrollWrapper>
        <ContentWrapper
          style={{
            minWidth: background.imageWidth * zoom + 100,
            minHeight: background.imageHeight * zoom + 110,
          }}
        >
          <ImageContainer
            style={{
              width: background.imageWidth * zoom,
              height: background.imageHeight * zoom,
            }}
          >
            <ImageScale
              style={{
                transform: `translate3d(0px, 0px, 0px) scale(${zoom})`,
              }}
            >
              {gbcEnabled && background.autoColor ? (
                <AutoColorizedImage
                  width={background.width * TILE_SIZE}
                  height={background.height * TILE_SIZE}
                  src={assetURL("backgrounds", background)}
                />
              ) : (
                <ColorizedImage
                  width={background.width * TILE_SIZE}
                  height={background.height * TILE_SIZE}
                  src={assetURL("backgrounds", background)}
                  tiles={background.tileColors}
                  palettes={palettes}
                />
              )}
            </ImageScale>
          </ImageContainer>
        </ContentWrapper>
      </ScrollWrapper>
    );
  }
  Iif (tileset) {
    return (
      <ScrollWrapper>
        <ContentWrapper
          style={{
            minWidth: tileset.imageWidth * zoom + 100,
            minHeight: tileset.imageHeight * zoom + 110,
          }}
        >
          <ImageContainer
            style={{
              width: tileset.imageWidth * zoom,
              height: tileset.imageHeight * zoom,
            }}
          >
            <ImageScale
              style={{
                transform: `translate3d(0px, 0px, 0px) scale(${zoom})`,
              }}
            >
              <ColorizedImage
                width={tileset.width * TILE_SIZE}
                height={tileset.height * TILE_SIZE}
                src={assetURL("tilesets", tileset)}
                tiles={[]}
                palettes={palettes}
              />
            </ImageScale>
          </ImageContainer>
        </ContentWrapper>
      </ScrollWrapper>
    );
  }
 
  return <div />;
};
 
export default BackgroundViewer;