2021-07-22 01:24:35 -04:00
|
|
|
import { useEffect, useState } from "react";
|
2021-07-22 02:40:43 -04:00
|
|
|
import SelectionDragOverlay from "../components/selection/SelectionDragOverlay";
|
2021-07-21 23:16:44 -04:00
|
|
|
import SelectionMenu from "../components/selection/SelectionMenu";
|
|
|
|
import SelectTool from "../components/tools/SelectTool";
|
2021-07-22 02:40:43 -04:00
|
|
|
import {
|
|
|
|
SelectionItemsChangeEventHandler,
|
2021-07-23 01:41:07 -04:00
|
|
|
SelectionItemsCreateEventHandler,
|
2021-07-22 02:40:43 -04:00
|
|
|
SelectionItemsRemoveEventHandler,
|
|
|
|
} from "../types/Events";
|
2021-07-21 23:16:44 -04:00
|
|
|
import { Map, MapToolId } from "../types/Map";
|
2021-07-22 01:21:36 -04:00
|
|
|
import { MapState } from "../types/MapState";
|
2021-07-21 23:16:44 -04:00
|
|
|
import { Selection } from "../types/Select";
|
|
|
|
import { SelectToolSettings } from "../types/Select";
|
|
|
|
|
|
|
|
function useMapSelection(
|
|
|
|
map: Map | null,
|
2021-07-22 01:21:36 -04:00
|
|
|
mapState: MapState | null,
|
2021-07-21 23:16:44 -04:00
|
|
|
onSelectionItemsChange: SelectionItemsChangeEventHandler,
|
2021-07-22 02:40:43 -04:00
|
|
|
onSelectionItemsRemove: SelectionItemsRemoveEventHandler,
|
2021-07-23 01:41:07 -04:00
|
|
|
onSelectionItemsCreate: SelectionItemsCreateEventHandler,
|
2021-07-21 23:16:44 -04:00
|
|
|
selectedToolId: MapToolId,
|
|
|
|
settings: SelectToolSettings
|
|
|
|
) {
|
|
|
|
const [isSelectionMenuOpen, setIsSelectionMenuOpen] =
|
|
|
|
useState<boolean>(false);
|
2021-07-22 02:40:43 -04:00
|
|
|
const [isSelectionDragging, setIsSelectionDragging] = useState(false);
|
2021-07-21 23:16:44 -04:00
|
|
|
const [selection, setSelection] = useState<Selection | null>(null);
|
|
|
|
|
|
|
|
function handleSelectionMenuOpen(open: boolean) {
|
|
|
|
setIsSelectionMenuOpen(open);
|
|
|
|
}
|
|
|
|
|
2021-07-22 01:24:35 -04:00
|
|
|
const active = selectedToolId === "select";
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!active) {
|
|
|
|
setSelection(null);
|
|
|
|
setIsSelectionMenuOpen(false);
|
|
|
|
}
|
|
|
|
}, [active]);
|
|
|
|
|
2021-07-22 02:40:43 -04:00
|
|
|
function handleSelectionDragStart() {
|
|
|
|
setIsSelectionDragging(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleSelectionDragEnd() {
|
|
|
|
setIsSelectionDragging(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleSelectionItemsRemove(
|
|
|
|
tokenStateIds: string[],
|
|
|
|
noteIds: string[]
|
|
|
|
) {
|
|
|
|
setSelection(null);
|
|
|
|
onSelectionItemsRemove(tokenStateIds, noteIds);
|
|
|
|
}
|
|
|
|
|
2021-07-21 23:16:44 -04:00
|
|
|
const selectionTool = (
|
|
|
|
<SelectTool
|
2021-07-22 01:24:35 -04:00
|
|
|
active={active}
|
2021-07-21 23:16:44 -04:00
|
|
|
toolSettings={settings}
|
|
|
|
onSelectionItemsChange={onSelectionItemsChange}
|
|
|
|
selection={selection}
|
|
|
|
onSelectionChange={setSelection}
|
|
|
|
onSelectionMenuOpen={handleSelectionMenuOpen}
|
2021-07-22 02:40:43 -04:00
|
|
|
onSelectionDragStart={handleSelectionDragStart}
|
|
|
|
onSelectionDragEnd={handleSelectionDragEnd}
|
2021-07-21 23:16:44 -04:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
const selectionMenu = (
|
|
|
|
<SelectionMenu
|
|
|
|
isOpen={isSelectionMenuOpen}
|
2021-07-23 01:41:07 -04:00
|
|
|
active={active}
|
2021-07-21 23:16:44 -04:00
|
|
|
onRequestClose={() => setIsSelectionMenuOpen(false)}
|
2021-07-23 01:41:07 -04:00
|
|
|
onRequestOpen={() => setIsSelectionMenuOpen(true)}
|
2021-07-21 23:16:44 -04:00
|
|
|
selection={selection}
|
2021-07-23 01:41:07 -04:00
|
|
|
onSelectionChange={setSelection}
|
2021-07-21 23:16:44 -04:00
|
|
|
onSelectionItemsChange={onSelectionItemsChange}
|
2021-07-23 01:41:07 -04:00
|
|
|
onSelectionItemsCreate={onSelectionItemsCreate}
|
2021-07-21 23:16:44 -04:00
|
|
|
map={map}
|
2021-07-22 01:21:36 -04:00
|
|
|
mapState={mapState}
|
2021-07-21 23:16:44 -04:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
2021-07-22 02:40:43 -04:00
|
|
|
const selectionDragOverlay = selection ? (
|
|
|
|
<SelectionDragOverlay
|
|
|
|
dragging={isSelectionDragging}
|
|
|
|
selection={selection}
|
|
|
|
onSelectionItemsRemove={handleSelectionItemsRemove}
|
|
|
|
/>
|
|
|
|
) : null;
|
|
|
|
|
|
|
|
return { selectionTool, selectionMenu, selectionDragOverlay };
|
2021-07-21 23:16:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export default useMapSelection;
|