All files / src/components/world renderSceneContextMenu.tsx

0% Statements 0/24
0% Branches 0/24
0% Functions 0/11
0% Lines 0/24

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                                                                                                                                                                                                                                                                                                                                                                                                     
import DirectionPicker from "components/forms/DirectionPicker";
import React, { Dispatch } from "react";
import { UnknownAction } from "redux";
import { ActorDirection } from "shared/lib/entities/entitiesTypes";
import l10n from "shared/lib/lang/l10n";
import buildGameActions from "store/features/buildGame/buildGameActions";
import entitiesActions from "store/features/entities/entitiesActions";
import settingsActions from "store/features/settings/settingsActions";
import { LabelButton } from "ui/buttons/LabelButton";
import { BlankIcon, CheckIcon } from "ui/icons/Icons";
import { MenuDivider, MenuItem, MenuSection } from "ui/menu/Menu";
 
interface SceneContextMenuProps {
  dispatch: Dispatch<UnknownAction>;
  sceneId: string;
  additionalSceneIds: string[];
  startSceneId: string;
  startDirection: ActorDirection;
  hoverX: number;
  hoverY: number;
  runSceneSelectionOnly: boolean;
  onRename?: () => void;
  onClose?: () => void;
}
 
const renderSceneContextMenu = ({
  sceneId,
  additionalSceneIds,
  onRename,
  dispatch,
  startSceneId,
  startDirection,
  hoverX,
  hoverY,
  runSceneSelectionOnly,
  onClose,
}: SceneContextMenuProps) => {
  return [
    <MenuSection key="label" style={{ paddingRight: 10, marginBottom: 5 }}>
      <div style={{ display: "flex" }}>
        <div style={{ marginRight: 5 }}>
          <LabelButton
            onClick={() =>
              dispatch(
                additionalSceneIds.length > 1
                  ? entitiesActions.editScenes(
                      additionalSceneIds.map((id) => ({
                        id,
                        changes: {
                          labelColor: "",
                        },
                      }))
                    )
                  : entitiesActions.editScene({
                      sceneId,
                      changes: {
                        labelColor: "",
                      },
                    })
              )
            }
          />
        </div>
        {["red", "orange", "yellow", "green", "blue", "purple", "gray"].map(
          (color) => (
            <div key={color} style={{ marginRight: color === "gray" ? 0 : 5 }}>
              <LabelButton
                color={color}
                onClick={() => {
                  dispatch(
                    additionalSceneIds.length > 1
                      ? entitiesActions.editScenes(
                          additionalSceneIds.map((id) => ({
                            id,
                            changes: {
                              labelColor: color,
                            },
                          }))
                        )
                      : entitiesActions.editScene({
                          sceneId,
                          changes: {
                            labelColor: color,
                          },
                        })
                  );
                  onClose?.();
                }}
              />
            </div>
          )
        )}
      </div>
    </MenuSection>,
    <MenuDivider key="div-direction" />,
    <MenuItem
      key="startingScene"
      onClick={() =>
        dispatch(
          settingsActions.editSettings({
            startSceneId: sceneId,
            startX: hoverX,
            startY: hoverY,
          })
        )
      }
    >
      {l10n("FIELD_SET_AS_STARTING_SCENE")}
    </MenuItem>,
    <MenuSection key="direction">
      <div style={{ width: 200 }}>
        <DirectionPicker
          id="startDirection"
          value={sceneId === startSceneId ? startDirection : undefined}
          onChange={(direction) => {
            dispatch(
              settingsActions.editSettings({
                startSceneId: sceneId,
                startDirection: direction,
                startX: hoverX,
                startY: hoverY,
              })
            );
            onClose?.();
          }}
        />
      </div>
    </MenuSection>,
    <MenuDivider key="div-preview" />,
    <MenuItem
      key="preview"
      subMenu={[
        <MenuItem
          key="preview-here"
          icon={<BlankIcon />}
          onClick={() =>
            dispatch(
              buildGameActions.buildGame({
                startSceneId: sceneId,
                startX: hoverX,
                startY: hoverY,
              })
            )
          }
        >
          {l10n("FIELD_RUN_FROM_HERE")}
        </MenuItem>,
        <MenuDivider key="div-preview-sub" />,
        <MenuItem
          key="preview-selection"
          icon={runSceneSelectionOnly ? <CheckIcon /> : <BlankIcon />}
          onClick={() => {
            dispatch(
              settingsActions.editSettings({
                runSceneSelectionOnly: !runSceneSelectionOnly,
              })
            );
          }}
        >
          {l10n("FIELD_INCLUDE_SELECTION_ONLY")}
        </MenuItem>,
      ]}
    >
      {l10n("FIELD_RUN_SCENE")}
    </MenuItem>,
    ...(onRename
      ? [
          <MenuDivider key="div-rename" />,
          <MenuItem key="rename" onClick={onRename}>
            {l10n("FIELD_RENAME")}
          </MenuItem>,
        ]
      : []),
    <MenuDivider key="div-delete" />,
    <MenuItem
      key="delete"
      onClick={() =>
        additionalSceneIds.length > 1
          ? dispatch(
              entitiesActions.removeScenes({ sceneIds: additionalSceneIds })
            )
          : dispatch(entitiesActions.removeScene({ sceneId }))
      }
    >
      {l10n(
        additionalSceneIds.length > 1
          ? "MENU_DELETE_SCENES"
          : "MENU_DELETE_SCENE"
      )}
    </MenuItem>,
  ];
};
 
export default renderSceneContextMenu;