All files / src/components/world ActorView.tsx

0% Statements 0/58
0% Branches 0/86
0% Functions 0/16
0% Lines 0/57

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                                                                                                                                                                                                                                                                                                                                                                                                                     
import React, { memo, useCallback, useEffect } from "react";
import SpriteSheetCanvas from "./SpriteSheetCanvas";
import { MIDDLE_MOUSE, TILE_SIZE, TOOL_COLLISIONS } from "consts";
import {
  actorPrefabSelectors,
  actorSelectors,
  spriteSheetSelectors,
} from "store/features/entities/entitiesState";
import editorActions from "store/features/editor/editorActions";
import styled, { css } from "styled-components";
import { useAppDispatch, useAppSelector } from "store/hooks";
import renderActorContextMenu from "./renderActorContextMenu";
import { SpriteBoundingBox } from "components/sprites/MetaspriteEditor";
import { MonoOBJPalette, Palette } from "shared/lib/resources/types";
import { useContextMenu } from "ui/hooks/use-context-menu";
 
interface ActorViewProps {
  id: string;
  sceneId: string;
  palettes?: Palette[];
  monoPalettes?: [MonoOBJPalette, MonoOBJPalette];
  editable?: boolean;
}
 
interface WrapperProps {
  $selected?: boolean;
  $halfWidth: boolean;
}
 
const Wrapper = styled.div<WrapperProps>`
  position: absolute;
  height: 8px;
  background-color: rgba(247, 45, 220, 0.5);
  outline: 1px solid rgba(140, 0, 177, 0.8);
  -webkit-transform: translate3d(0, 0, 0);
 
  ${(props) =>
    props.$halfWidth
      ? css`
          width: 8px;
        `
      : css`
          width: 16px;
        `}
 
  ${(props) =>
    props.$selected
      ? css`
          background-color: rgba(247, 45, 220, 0.8);
          outline: 1px solid rgba(140, 0, 177, 1);
          z-index: 100;
        `
      : ""}
`;
 
const PinScreenPreview = styled.div`
  position: absolute;
  top: 0;
  left: 0;
  width: 160px;
  height: 144px;
  pointer-events: none;
  z-index: 100;
  outline: 2000px solid rgba(0, 0, 0, 0.5);
`;
 
const CanvasWrapper = styled.div`
  pointer-events: none;
`;
 
const ActorView = memo(
  ({ id, sceneId, palettes, monoPalettes, editable }: ActorViewProps) => {
    const dispatch = useAppDispatch();
 
    const actor = useAppSelector((state) =>
      actorSelectors.selectById(state, id),
    );
    const prefab = useAppSelector((state) =>
      actorPrefabSelectors.selectById(state, actor?.prefabId ?? ""),
    );
 
    const sprite = useAppSelector((state) =>
      spriteSheetSelectors.selectById(
        state,
        prefab?.spriteSheetId ?? actor?.spriteSheetId ?? "",
      ),
    );
    const selected = useAppSelector(
      (state) =>
        state.editor.type === "actor" &&
        state.editor.scene === sceneId &&
        state.editor.entityId === id,
    );
    const isDragging = useAppSelector(
      (state) => selected && state.editor.dragging,
    );
    const showSprite = useAppSelector((state) => state.editor.zoom > 80);
    const previewAsMono = useAppSelector(
      (state) =>
        state.project.present.settings.colorMode === "mono" ||
        (state.project.present.settings.colorMode === "mixed" &&
          state.project.present.settings.previewAsMono),
    );
    const boundsWidth = sprite?.boundsWidth || 16;
    const boundsHeight = sprite?.boundsHeight || 16;
    const boundsX = sprite?.boundsX || 0;
    const boundsY = sprite?.boundsY || 0;
    const showBoundingBox = useAppSelector(
      (state) => state.editor.tool === TOOL_COLLISIONS,
    );
 
    const onMouseUp = useCallback(() => {
      dispatch(editorActions.dragActorStop());
      window.removeEventListener("mouseup", onMouseUp);
    }, [dispatch]);
 
    const onMouseDown = useCallback(
      (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
        Iif (editable && e.nativeEvent.which !== MIDDLE_MOUSE) {
          dispatch(editorActions.dragActorStart({ sceneId, actorId: id }));
          dispatch(editorActions.setTool({ tool: "select" }));
          window.addEventListener("mouseup", onMouseUp);
        }
      },
      [dispatch, editable, id, onMouseUp, sceneId],
    );
 
    useEffect(() => {
      Iif (isDragging) {
        window.addEventListener("mouseup", onMouseUp);
      }
      return () => {
        window.removeEventListener("mouseup", onMouseUp);
      };
    }, [onMouseUp, isDragging]);
 
    //#region Context Menu
 
    const getContextMenu = useCallback(() => {
      return renderActorContextMenu({
        dispatch,
        actorId: id,
        sceneId,
      });
    }, [dispatch, id, sceneId]);
 
    const { onContextMenu, contextMenuElement } = useContextMenu({
      getMenu: getContextMenu,
    });
 
    //#endregion Context Menu
 
    Iif (!actor) {
      return <></>;
    }
 
    const UNIT_SIZE = actor.coordinateType === "pixels" ? 1 : TILE_SIZE;
 
    return (
      <>
        {selected && actor.isPinned && <PinScreenPreview />}
        <Wrapper
          $selected={selected}
          $halfWidth={sprite?.canvasWidth === 8}
          onMouseDown={onMouseDown}
          onContextMenu={onContextMenu}
          style={{
            left: actor.x * UNIT_SIZE,
            top: actor.y * UNIT_SIZE,
          }}
        >
          {showSprite && (
            <CanvasWrapper>
              <SpriteSheetCanvas
                spriteSheetId={sprite?.id ?? ""}
                direction={actor.direction}
                frame={0}
                palettes={palettes}
                previewAsMono={previewAsMono}
                monoPalettes={monoPalettes}
                offsetPosition
              />
              {showBoundingBox && (
                <SpriteBoundingBox
                  style={{
                    left: boundsX,
                    top: boundsY,
                    width: boundsWidth,
                    height: boundsHeight,
                  }}
                />
              )}
            </CanvasWrapper>
          )}
          {contextMenuElement}
        </Wrapper>
      </>
    );
  },
);
 
export default ActorView;