-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8725cb8
commit 60572a1
Showing
6 changed files
with
194 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,129 +1,141 @@ | ||
import React, { memo } from "react"; | ||
import { useRecoilState, useSetRecoilState } from "recoil"; | ||
import { NodeAtom, RoutePosAtom } from "../../../../state/pathFinder/atoms"; | ||
import { | ||
GridFuncProps, | ||
useGridFunc, | ||
} from "../../../../state/pathFinder/useGridFunc"; | ||
// import { | ||
// useRoutePos, | ||
// useRoutePosProps, | ||
// } from "../../../../state/pathFinder/useRoutePos"; | ||
import { | ||
useSelectedNode, | ||
useSelectedNodeProps, | ||
} from "../../../../state/pathFinder/useSelectedNode"; | ||
import { SelectedType } from "../../../../state/types"; | ||
|
||
interface Props { | ||
row: number; | ||
col: number; | ||
isMouseDown: boolean; | ||
setIsMouseDown: (val: boolean) => void; | ||
selectedNode: null | SelectedType; | ||
setSelectedNode: (val: null | SelectedType) => void; | ||
} | ||
|
||
export const Node = memo( | ||
({ | ||
col, | ||
row, | ||
isMouseDown, | ||
setIsMouseDown, | ||
selectedNode, | ||
setSelectedNode, | ||
}: Props) => { | ||
const [currentNode, setCurrentNode] = useRecoilState( | ||
NodeAtom({ row, col }) | ||
); | ||
const setRoutePos = useSetRecoilState(RoutePosAtom); | ||
const gridFuncSelector = (state: GridFuncProps) => ({ | ||
isMouseDown: state.isMouseDown, | ||
gridFunc: state.updateFunc, | ||
}); | ||
const selectedNodeSelector = (state: useSelectedNodeProps) => ({ | ||
selectedNode: state.selectedNode, | ||
setSelectedNode: state.setSelectedNode, | ||
}); | ||
// const routePosSelector = (state: useRoutePosProps) => ({ | ||
// setRoutePos: state.setRoutePos, | ||
// }); | ||
|
||
const nodeIndex = `${row}-${col}`; | ||
export const Node = memo(({ col, row }: Props) => { | ||
const [currentNode, setCurrentNode] = useRecoilState(NodeAtom({ row, col })); | ||
const setRoutePos = useSetRecoilState(RoutePosAtom); | ||
const { isMouseDown, gridFunc } = useGridFunc(gridFuncSelector); | ||
// const { setRoutePos } = useRoutePos(routePosSelector); | ||
const { selectedNode, setSelectedNode } = | ||
useSelectedNode(selectedNodeSelector); | ||
|
||
const notStartNodeOrEndNode = | ||
!currentNode.startNode && !currentNode.endNode; | ||
const isSelectedNodeStartNode = selectedNode === SelectedType.startNode; | ||
const isSelectedNodeEndNode = selectedNode === SelectedType.endNode; | ||
const nodeIndex = `${row}-${col}`; | ||
|
||
const handleCurrentNode = () => { | ||
if (notStartNodeOrEndNode) { | ||
if (currentNode.isWall) { | ||
setCurrentNode((node) => ({ ...node, isWall: false })); | ||
} else { | ||
setCurrentNode((node) => ({ ...node, isWall: true })); | ||
} | ||
} | ||
}; | ||
const notStartNodeOrEndNode = !currentNode.startNode && !currentNode.endNode; | ||
const isSelectedNodeStartNode = selectedNode === SelectedType.startNode; | ||
const isSelectedNodeEndNode = selectedNode === SelectedType.endNode; | ||
|
||
const mouseDownHandler = () => { | ||
if (notStartNodeOrEndNode) { | ||
return setIsMouseDown(true); | ||
const handleCurrentNode = () => { | ||
if (notStartNodeOrEndNode) { | ||
if (currentNode.isWall) { | ||
setCurrentNode((node) => ({ ...node, isWall: false })); | ||
} else { | ||
setCurrentNode((node) => ({ ...node, isWall: true })); | ||
} | ||
if (isSelectedNodeStartNode && currentNode.endNode) return; | ||
if (isSelectedNodeEndNode && currentNode.startNode) return; | ||
} | ||
}; | ||
|
||
if (currentNode.startNode) { | ||
setSelectedNode(SelectedType.startNode); | ||
} | ||
if (currentNode.endNode) { | ||
setSelectedNode(SelectedType.endNode); | ||
} | ||
}; | ||
const mouseDownHandler = () => { | ||
if (notStartNodeOrEndNode) { | ||
return gridFunc({ isMouseDown: true }); | ||
} | ||
if (isSelectedNodeStartNode && currentNode.endNode) return; | ||
if (isSelectedNodeEndNode && currentNode.startNode) return; | ||
|
||
const mouseUpHandler = () => { | ||
if (isMouseDown) { | ||
setIsMouseDown(false); | ||
return; | ||
} | ||
if (isSelectedNodeStartNode && currentNode.endNode) return; | ||
if (isSelectedNodeEndNode && currentNode.startNode) return; | ||
if (currentNode.startNode) { | ||
setSelectedNode(SelectedType.startNode); | ||
} | ||
if (currentNode.endNode) { | ||
setSelectedNode(SelectedType.endNode); | ||
} | ||
}; | ||
|
||
if (isSelectedNodeStartNode) { | ||
setRoutePos((state) => ({ ...state, sourceIndex: nodeIndex })); | ||
} | ||
if (isSelectedNodeEndNode) { | ||
setRoutePos((state) => ({ ...state, destinationIndex: nodeIndex })); | ||
} | ||
const mouseUpHandler = () => { | ||
if (isMouseDown) { | ||
gridFunc({ isMouseDown: false }); | ||
return; | ||
} | ||
if (isSelectedNodeStartNode && currentNode.endNode) return; | ||
if (isSelectedNodeEndNode && currentNode.startNode) return; | ||
|
||
setSelectedNode(null); | ||
}; | ||
if (isSelectedNodeStartNode) { | ||
setRoutePos((pos) => ({ ...pos, sourceIndex: nodeIndex })); | ||
} | ||
if (isSelectedNodeEndNode) { | ||
setRoutePos((pos) => ({ ...pos, destinationIndex: nodeIndex })); | ||
} | ||
|
||
const onMouseEnter = () => { | ||
if (isMouseDown && notStartNodeOrEndNode) { | ||
if (currentNode.isWall) { | ||
setCurrentNode((node) => ({ ...node, isWall: false })); | ||
} | ||
if (!currentNode.isWall && notStartNodeOrEndNode) { | ||
setCurrentNode((node) => ({ ...node, isWall: true })); | ||
} | ||
return; | ||
} | ||
if (isSelectedNodeStartNode && !currentNode.endNode) { | ||
setCurrentNode((node) => ({ ...node, startNode: true, isWall: false })); | ||
} | ||
if (isSelectedNodeEndNode && !currentNode.startNode) { | ||
setCurrentNode((node) => ({ ...node, endNode: true, isWall: false })); | ||
} | ||
}; | ||
setSelectedNode(null); | ||
}; | ||
|
||
const onMouseOut = () => { | ||
if (isSelectedNodeStartNode) { | ||
setCurrentNode((node) => ({ | ||
...node, | ||
startNode: false, | ||
isWall: false, | ||
})); | ||
const onMouseEnter = () => { | ||
if (isMouseDown && notStartNodeOrEndNode) { | ||
if (currentNode.isWall) { | ||
setCurrentNode((node) => ({ ...node, isWall: false })); | ||
} | ||
if (isSelectedNodeEndNode) { | ||
setCurrentNode((node) => ({ ...node, endNode: false, isWall: false })); | ||
if (!currentNode.isWall && notStartNodeOrEndNode) { | ||
setCurrentNode((node) => ({ ...node, isWall: true })); | ||
} | ||
}; | ||
return; | ||
} | ||
if (isSelectedNodeStartNode && !currentNode.endNode) { | ||
setCurrentNode((node) => ({ ...node, startNode: true, isWall: false })); | ||
} | ||
if (isSelectedNodeEndNode && !currentNode.startNode) { | ||
setCurrentNode((node) => ({ ...node, endNode: true, isWall: false })); | ||
} | ||
}; | ||
|
||
const onMouseOut = () => { | ||
if (isSelectedNodeStartNode) { | ||
setCurrentNode((node) => ({ | ||
...node, | ||
startNode: false, | ||
isWall: false, | ||
})); | ||
} | ||
if (isSelectedNodeEndNode) { | ||
setCurrentNode((node) => ({ ...node, endNode: false, isWall: false })); | ||
} | ||
}; | ||
|
||
const isStartNode = currentNode.startNode ? "startNode" : ""; | ||
const isEndNode = currentNode.endNode ? "endNode" : ""; | ||
const wall = currentNode.isWall ? "wall" : ""; | ||
return ( | ||
<div | ||
id={nodeIndex} | ||
onClick={handleCurrentNode} | ||
onMouseDown={mouseDownHandler} | ||
onMouseUp={mouseUpHandler} | ||
onMouseLeave={onMouseOut} | ||
onMouseEnter={onMouseEnter} | ||
draggable={false} | ||
className={`grid__node ${isStartNode} ${isEndNode} ${wall}`} | ||
> | ||
{isStartNode && <div className="arrow-right"></div>} | ||
{isEndNode && <div className="star">⭐</div>} | ||
</div> | ||
); | ||
} | ||
); | ||
const isStartNode = currentNode.startNode ? "startNode" : ""; | ||
const isEndNode = currentNode.endNode ? "endNode" : ""; | ||
const wall = currentNode.isWall ? "wall" : ""; | ||
return ( | ||
<div | ||
id={nodeIndex} | ||
onClick={handleCurrentNode} | ||
onMouseDown={mouseDownHandler} | ||
onMouseUp={mouseUpHandler} | ||
onMouseLeave={onMouseOut} | ||
onMouseEnter={onMouseEnter} | ||
draggable={false} | ||
className={`grid__node ${isStartNode} ${isEndNode} ${wall}`} | ||
> | ||
{isStartNode && <div className="arrow-right"></div>} | ||
{isEndNode && <div className="star">⭐</div>} | ||
</div> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.