@@ -3,74 +3,111 @@ import { usePathFinding } from "../hooks/usePathFinding";
3
3
import { MAX_COLS , MAX_ROWS } from "../utils/constants" ;
4
4
import { Tile } from "./Tile" ;
5
5
import { MutableRefObject , useState } from "react" ;
6
- import { checkIfStartOrEnd , createNewGrid } from "../utils/helpers" ;
6
+ import { checkIfStartOrEnd , createNewGrid , resetCurrentStart , setNewStartTile } from "../utils/helpers" ;
7
+ import { useTile } from "../hooks/useTile" ;
7
8
8
- export function Grid ( { isVisualizationRunningRef} :{ isVisualizationRunningRef : MutableRefObject < boolean > } ) {
9
+ export function Grid ( {
10
+ isVisualizationRunningRef,
11
+ isChangeStartSelectedRef,
12
+ } : {
13
+ isVisualizationRunningRef : MutableRefObject < boolean > ;
14
+ isChangeStartSelectedRef : MutableRefObject < boolean > ;
15
+ } ) {
16
+ const { grid, setGrid } = usePathFinding ( ) ;
17
+ const [ isMouseDown , setIsMouseDown ] = useState < boolean > ( false ) ;
18
+ const { startTile, setStartTile} = useTile ( ) ;
9
19
10
- const { grid, setGrid} = usePathFinding ( ) ;
11
- const [ isMouseDown , setIsMouseDown ] = useState < boolean > ( false ) ;
20
+ const handleMouseDown = ( row : number , col : number ) => {
21
+ if (
22
+ isVisualizationRunningRef . current ||
23
+ checkIfStartOrEnd ( row , col ) ||
24
+ isChangeStartSelectedRef . current
25
+ ) {
26
+ return ;
27
+ }
12
28
13
- const handleMouseDown = ( row : number , col : number ) => {
14
- if ( isVisualizationRunningRef . current || checkIfStartOrEnd ( row , col ) ) {
15
- return ;
16
- }
29
+ setIsMouseDown ( true ) ;
30
+ const newGrid = createNewGrid ( grid , row , col ) ;
31
+ setGrid ( newGrid ) ;
32
+ } ;
17
33
18
- setIsMouseDown ( true ) ;
19
- const newGrid = createNewGrid ( grid , row , col ) ;
20
- setGrid ( newGrid )
34
+ const handleMouseUp = ( row : number , col : number ) => {
35
+ if (
36
+ isVisualizationRunningRef . current ||
37
+ checkIfStartOrEnd ( row , col ) ||
38
+ isChangeStartSelectedRef . current
39
+ ) {
40
+ return ;
21
41
}
22
42
23
- const handleMouseUp = ( row : number , col : number ) => {
24
- if ( isVisualizationRunningRef . current || checkIfStartOrEnd ( row , col ) ) {
25
- return ;
26
- }
43
+ setIsMouseDown ( false ) ;
44
+ } ;
27
45
28
- setIsMouseDown ( false ) ;
46
+ const handleMouseEnter = ( row : number , col : number ) => {
47
+ if (
48
+ isVisualizationRunningRef . current ||
49
+ checkIfStartOrEnd ( row , col ) ||
50
+ isChangeStartSelectedRef . current
51
+ ) {
52
+ return ;
29
53
}
30
54
31
- const handleMouseEnter = ( row : number , col : number ) => {
32
- if ( isVisualizationRunningRef . current || checkIfStartOrEnd ( row , col ) ) {
33
- return ;
34
- }
55
+ if ( isMouseDown ) {
56
+ const newGrid = createNewGrid ( grid , row , col ) ;
57
+ setGrid ( newGrid ) ;
58
+ }
59
+ } ;
35
60
36
- if ( isMouseDown ) {
37
- const newGrid = createNewGrid ( grid , row , col ) ;
38
- setGrid ( newGrid )
39
- }
61
+ const handleMouseClick = ( row : number , col : number ) => {
62
+ if ( ! isChangeStartSelectedRef . current ) {
63
+ return ;
40
64
}
65
+
66
+ const newSelectedStartTile = setNewStartTile ( grid , row , col ) ;
67
+ setGrid ( newSelectedStartTile ) ;
68
+ setGrid ( resetCurrentStart ( grid , startTile , row , col , setStartTile ) )
69
+ isChangeStartSelectedRef . current = false
70
+ } ;
41
71
42
- return (
43
- < div
44
- className = { twMerge (
45
- // base classes
46
- "flex items-center flex-col justify-center border-sky-300 my-3" ,
47
- // manage height of grid
48
- `lg:min-h-[${ MAX_ROWS * 17 } px] md:min-h-[${ MAX_ROWS * 15 } px] xs:min-h-[${ MAX_ROWS * 8 } ] min-h-[${ MAX_ROWS * 7 } px]` ,
49
- // manage width of grid
50
- `lg:w-[${ MAX_COLS * 17 } px] md:w-[${ MAX_COLS * 15 } px] xs:w-[${ MAX_COLS * 8 } px] w-[${ MAX_COLS * 7 } px]`
51
- ) } >
52
- { grid . map ( ( r , rowIndex ) => (
53
- < div key = { rowIndex } className = "flex" >
54
- { r . map ( ( tile , tileIndex ) => {
55
- const { row, col, isEnd, isStart, isPath, isTraversed, isWall} = tile ;
56
- return (
57
- < Tile
58
- key = { tileIndex }
59
- row = { tile . row }
60
- col = { tile . col }
61
- isEnd = { isEnd }
62
- isStart = { isStart }
63
- isPath = { isPath }
64
- isTraversed = { isTraversed }
65
- isWall = { isWall }
66
- handleMouseDown = { ( ) => handleMouseDown ( row , col ) }
67
- handleMouseUp = { ( ) => handleMouseUp ( row , col ) }
68
- handleMouseEnter = { ( ) => handleMouseEnter ( row , col ) } />
69
- )
70
- } ) }
71
- </ div >
72
- ) ) }
72
+ return (
73
+ < div
74
+ className = { twMerge (
75
+ // base classes
76
+ "flex items-center flex-col justify-center border-sky-300 my-3" ,
77
+ // manage height of grid
78
+ `lg:min-h-[${ MAX_ROWS * 17 } px] md:min-h-[${
79
+ MAX_ROWS * 15
80
+ } px] xs:min-h-[${ MAX_ROWS * 8 } ] min-h-[${ MAX_ROWS * 7 } px]`,
81
+ // manage width of grid
82
+ `lg:w-[${ MAX_COLS * 17 } px] md:w-[${ MAX_COLS * 15 } px] xs:w-[${
83
+ MAX_COLS * 8
84
+ } px] w-[${ MAX_COLS * 7 } px]`
85
+ ) }
86
+ >
87
+ { grid . map ( ( r , rowIndex ) => (
88
+ < div key = { rowIndex } className = "flex" >
89
+ { r . map ( ( tile , tileIndex ) => {
90
+ const { row, col, isEnd, isStart, isPath, isTraversed, isWall } =
91
+ tile ;
92
+ return (
93
+ < Tile
94
+ key = { tileIndex }
95
+ row = { tile . row }
96
+ col = { tile . col }
97
+ isEnd = { isEnd }
98
+ isStart = { isStart }
99
+ isPath = { isPath }
100
+ isTraversed = { isTraversed }
101
+ isWall = { isWall }
102
+ handleMouseDown = { ( ) => handleMouseDown ( row , col ) }
103
+ handleMouseUp = { ( ) => handleMouseUp ( row , col ) }
104
+ handleMouseEnter = { ( ) => handleMouseEnter ( row , col ) }
105
+ handleMouseClick = { ( ) => handleMouseClick ( row , col ) }
106
+ />
107
+ ) ;
108
+ } ) }
73
109
</ div >
74
- )
75
-
76
- }
110
+ ) ) }
111
+ </ div >
112
+ ) ;
113
+ }
0 commit comments