@@ -249,46 +249,45 @@ public float GetUnusedYLocation(float startX, float wantY, string name)
249249 var desiredPositionY = ( int ) ( wantY / MapViewer . spacing ) ; // The positionY of the ideal row for the text.
250250 var endX = startX + ( name . Length * F . trainFont . Size ) ;
251251
252- if ( endX < 0 ) // Out of drawing area
252+ if ( ( endX < 0 ) || // Out of drawing area
253+ ( F . alignedTextY == null ) )
253254 return noFreeSlotFound ;
254255
255256 var positionY = desiredPositionY ;
256- if ( F . alignedTextY != null ) {
257- while ( positionY >= 0 && positionY < F . alignedTextY . Length )
258- {
259- // If the line contains no text yet, put it there
260- if ( F . alignedTextNum [ positionY ] == 0 )
261- return SaveLabelLocation ( startX , endX , positionY ) ;
257+ while ( positionY >= 0 && positionY < F . alignedTextY . Length )
258+ {
259+ // If the line contains no text yet, put it there
260+ if ( F . alignedTextNum [ positionY ] == 0 )
261+ return SaveLabelLocation ( startX , endX , positionY ) ;
262262
263- var conflict = false ;
263+ var conflict = false ;
264264
265- // Check if it intersects with any labels already in this row
266- for ( var col = 0 ; col < F . alignedTextNum [ positionY ] ; col ++ )
265+ // Check if it intersects with any labels already in this row
266+ for ( var col = 0 ; col < F . alignedTextNum [ positionY ] ; col ++ )
267+ {
268+ var v = F . alignedTextY [ positionY ] [ col ] ;
269+ //check conflict with a text, v.X is the start of the text, v.Y is the end of the text
270+ if ( endX >= v . X && startX <= v . Y )
267271 {
268- var v = F . alignedTextY [ positionY ] [ col ] ;
269- //check conflict with a text, v.X is the start of the text, v.Y is the end of the text
270- if ( endX >= v . X && startX <= v . Y )
271- {
272- conflict = true ;
273- break ;
274- }
272+ conflict = true ;
273+ break ;
275274 }
275+ }
276276
277- if ( conflict )
278- {
279- positionY -- ; // Try a different row: -1, -2, +2, +1
277+ if ( conflict )
278+ {
279+ positionY -- ; // Try a different row: -1, -2, +2, +1
280280
281- if ( positionY - desiredPositionY <= - 2 ) // Cannot move up (-ve Y), so try to move it down (+ve Y)
282- positionY = desiredPositionY + 2 ; // Try +2 then +1
281+ if ( positionY - desiredPositionY <= - 2 ) // Cannot move up (-ve Y), so try to move it down (+ve Y)
282+ positionY = desiredPositionY + 2 ; // Try +2 then +1
283283
284- if ( positionY == desiredPositionY ) // Back to original position again
285- return noFreeSlotFound ;
286- }
287- else
288- {
289- // Check that row has an unused column in its fixed size array
290- return F . alignedTextNum [ positionY ] >= F . alignedTextY [ positionY ] . Length ? noFreeSlotFound : SaveLabelLocation ( startX , endX , positionY ) ;
291- }
284+ if ( positionY == desiredPositionY ) // Back to original position again
285+ return noFreeSlotFound ;
286+ }
287+ else
288+ {
289+ // Check that row has an unused column in its fixed size array
290+ return F . alignedTextNum [ positionY ] >= F . alignedTextY [ positionY ] . Length ? noFreeSlotFound : SaveLabelLocation ( startX , endX , positionY ) ;
292291 }
293292 }
294293 return noFreeSlotFound ;
0 commit comments