1
1
import { useCallback , useEffect , useMemo , useState } from 'react' ;
2
2
3
- import type { ProjectPathTrainResult , Waypoint } from '@osrd-project/ui-manchette/dist/types' ;
4
- import type {
5
- SpaceScale ,
6
- SpaceTimeChartProps ,
7
- } from '@osrd-project/ui-spacetimechart/dist/lib/types' ;
3
+ import {
4
+ CAPTION_SIZE ,
5
+ type SpaceScale ,
6
+ type SpaceTimeChartProps ,
7
+ } from '@osrd-project/ui-spacetimechart' ;
8
+ import { type Waypoint , type ProjectPathTrainResult } from '@osrd-project/ui-manchette' ;
8
9
9
10
import usePaths from './usePaths' ;
10
11
import {
@@ -14,16 +15,18 @@ import {
14
15
DEFAULT_ZOOM_MS_PER_PX ,
15
16
MAX_ZOOM_MS_PER_PX ,
16
17
MIN_ZOOM_MS_PER_PX ,
17
- MAX_ZOOM_METRE_PER_PX ,
18
- MIN_ZOOM_METRE_PER_PX ,
18
+ BASE_WAYPOINT_HEIGHT ,
19
+ FOOTER_HEIGHT ,
19
20
} from '../consts' ;
20
21
import {
21
22
computeWaypointsToDisplay ,
22
23
getScales ,
23
24
zoomX ,
24
25
zoomValueToTimeScale ,
26
+ zoomValueToSpaceScale ,
25
27
timeScaleToZoomValue ,
26
28
spaceScaleToZoomValue ,
29
+ getExtremaScales ,
27
30
} from '../helpers' ;
28
31
import { getDiff } from '../utils/point' ;
29
32
import { clamp } from 'lodash' ;
@@ -90,7 +93,6 @@ const useManchettesWithSpaceTimeChart = (
90
93
isProportional,
91
94
} = state ;
92
95
93
- console . log ( yZoom ) ;
94
96
const paths = usePaths ( projectPathTrainResult , selectedTrain ) ;
95
97
96
98
const waypointsToDisplay = useMemo (
@@ -109,6 +111,36 @@ const useManchettesWithSpaceTimeChart = (
109
111
[ waypointsToDisplay ]
110
112
) ;
111
113
114
+ const canvasDrawingHeight = height - FOOTER_HEIGHT ; // 521
115
+ const drawingHeightWithoutTopPadding = canvasDrawingHeight - BASE_WAYPOINT_HEIGHT / 2 ; // 505
116
+ const drawingHeightWithoutBothPadding = canvasDrawingHeight - BASE_WAYPOINT_HEIGHT ; // 489
117
+
118
+ const computedScales = useMemo (
119
+ ( ) => getScales ( simplifiedWaypoints , { height, isProportional, yZoom } ) ,
120
+ [ simplifiedWaypoints , height , isProportional , yZoom ]
121
+ ) ;
122
+
123
+ console . log ( 'computedScales' , computedScales . at ( - 1 ) ) ;
124
+ const { minZoomMillimetrePerPx, maxZoomMillimetrePerPx } = getExtremaScales (
125
+ drawingHeightWithoutTopPadding ,
126
+ drawingHeightWithoutBothPadding ,
127
+ computedScales . at ( - 1 ) ! . to
128
+ ) ;
129
+ console . log (
130
+ 'state' ,
131
+ zoomValueToSpaceScale ( minZoomMillimetrePerPx , maxZoomMillimetrePerPx , yZoom )
132
+ ) ;
133
+
134
+ // console.log(computedScales, minZoomMillimetrePerPx, maxZoomMillimetrePerPx);
135
+
136
+ // console.log(
137
+ // '\n',
138
+ // `x: zoom=${xZoom}, ${zoomValueToTimeScale(xZoom)} ms/px`,
139
+ // '\n',
140
+ // `y: zoom=${yZoom}, ${zoomValueToSpaceScale(minZoomMillimetrePerPx, maxZoomMillimetrePerPx, yZoom)} m/px
141
+ // ${zoomValueToSpaceScale(minZoomMillimetrePerPx, maxZoomMillimetrePerPx, yZoom) * canvasDrawingHeight} m`
142
+ // );
143
+
112
144
const handleRectangleZoom = useCallback (
113
145
( {
114
146
scales : { chosenTimeScale, chosenSpaceScale } ,
@@ -121,21 +153,30 @@ const useManchettesWithSpaceTimeChart = (
121
153
if ( prev . zoomMode || ! prev . rect ) {
122
154
return prev ;
123
155
}
124
-
125
156
const newTimeScale = clamp ( chosenTimeScale , MAX_ZOOM_MS_PER_PX , MIN_ZOOM_MS_PER_PX ) ;
126
- const newSpaceScale = clamp ( chosenSpaceScale , MAX_ZOOM_METRE_PER_PX , MIN_ZOOM_METRE_PER_PX ) ;
157
+ const newSpaceScale = clamp (
158
+ chosenSpaceScale ,
159
+ maxZoomMillimetrePerPx ,
160
+ minZoomMillimetrePerPx
161
+ ) ;
162
+ console . log ( 2 , newSpaceScale ) ;
127
163
const timeZoomValue = timeScaleToZoomValue ( newTimeScale ) ;
128
- const spaceZoomValue = spaceScaleToZoomValue ( newSpaceScale ) ;
164
+ const spaceZoomValue = spaceScaleToZoomValue (
165
+ minZoomMillimetrePerPx ,
166
+ maxZoomMillimetrePerPx ,
167
+ newSpaceScale
168
+ ) ;
129
169
170
+ // console.log(newSpaceScale, spaceZoomValue);
130
171
const leftRectSide = Math . min ( Number ( prev . rect . timeStart ) , Number ( prev . rect . timeEnd ) ) ;
131
172
const topRectSide = Math . min ( prev . rect . spaceStart , prev . rect . spaceEnd ) ;
132
173
const newXOffset = ( timeOrigin - leftRectSide ) / newTimeScale ;
133
174
const newYOffset = ( spaceOrigin - topRectSide ) / newSpaceScale ;
134
175
135
176
return {
136
177
...prev ,
137
- timeZoomValue : timeZoomValue ,
138
- spaceZoomValue : spaceZoomValue ,
178
+ xZoom : timeZoomValue ,
179
+ yZoom : spaceZoomValue ,
139
180
xOffset : newXOffset ,
140
181
yOffset : newYOffset ,
141
182
...overrideState ,
@@ -145,6 +186,21 @@ const useManchettesWithSpaceTimeChart = (
145
186
[ timeOrigin , spaceOrigin ]
146
187
) ;
147
188
189
+ useEffect ( ( ) => {
190
+ if ( rect && ! zoomMode && spaceTimeChartRef ?. current ) {
191
+ const { timeStart, timeEnd, spaceStart, spaceEnd } = rect ;
192
+ const timeRange = Math . abs ( Number ( timeEnd ) - Number ( timeStart ) ) ; // width of rect in ms
193
+ const spaceRange = Math . abs ( spaceEnd - spaceStart ) ; // height of rect in metre
194
+ const chosenTimeScale = timeRange / spaceTimeChartRef . current . clientWidth ;
195
+ const chosenSpaceScale = spaceRange / drawingHeightWithoutTopPadding ;
196
+ console . log ( 1 , spaceRange , drawingHeightWithoutTopPadding , chosenSpaceScale ) ;
197
+ handleRectangleZoom ( {
198
+ scales : { chosenTimeScale, chosenSpaceScale } ,
199
+ overrideState : { rect : null } ,
200
+ } ) ;
201
+ }
202
+ } , [ state . rect , state . zoomMode , handleRectangleZoom ] ) ;
203
+
148
204
const zoomYIn = useCallback ( ( ) => {
149
205
if ( yZoom < MAX_ZOOM_Y ) {
150
206
const newYZoom = yZoom + ZOOM_Y_DELTA ;
@@ -230,15 +286,9 @@ const useManchettesWithSpaceTimeChart = (
230
286
setState ( ( prev ) => ( { ...prev , isProportional : ! prev . isProportional } ) ) ;
231
287
} , [ ] ) ;
232
288
233
- const computedScales = useMemo (
234
- ( ) => getScales ( simplifiedWaypoints , { height, isProportional, yZoom } ) ,
235
- [ simplifiedWaypoints , height , isProportional , yZoom ]
236
- ) ;
237
-
238
289
const toggleZoomMode = useCallback ( ( ) => {
239
290
setState ( ( prev ) => ( { ...prev , zoomMode : ! prev . zoomMode } ) ) ;
240
291
} , [ ] ) ;
241
- // console.log('computedScales', computedScales);
242
292
243
293
const manchetteProps = useMemo (
244
294
( ) => ( {
@@ -344,8 +394,18 @@ const useManchettesWithSpaceTimeChart = (
344
394
xZoom,
345
395
toggleZoomMode,
346
396
zoomMode,
397
+ rect,
347
398
} ) ,
348
- [ manchetteProps , spaceTimeChartProps , handleScroll , handleXZoom , xZoom ]
399
+ [
400
+ manchetteProps ,
401
+ spaceTimeChartProps ,
402
+ handleScroll ,
403
+ handleXZoom ,
404
+ xZoom ,
405
+ toggleMode ,
406
+ zoomMode ,
407
+ rect ,
408
+ ]
349
409
) ;
350
410
} ;
351
411
0 commit comments