@@ -28,19 +28,23 @@ namespace Orts.Viewer3D
2828{
2929 public class EditorShapes : StaticShape , IDisposable
3030 {
31- public readonly MouseCrosshair MouseCrosshair ;
31+ readonly MouseCrosshair MouseCrosshair ;
3232 public bool MouseCrosshairEnabled { get ; set ; }
3333 public bool CrosshairPositionUpdateEnabled { get ; set ; } = true ;
3434
35- public readonly HandleX HandleX ;
36- public readonly HandleY HandleY ;
37- public readonly HandleZ HandleZ ;
35+ readonly HandleX HandleX ;
36+ readonly HandleY HandleY ;
37+ readonly HandleZ HandleZ ;
3838 public bool HandleEnabled { get ; set ; } = true ;
39- public WorldPosition HandleLocation ;
39+ public WorldPosition HandleLocation { get ; set ; }
4040
41- StaticShape _selectedObject ;
41+ StaticShape PreviousSelectedObject ;
4242 public StaticShape SelectedObject { get ; set ; }
4343
44+ public StaticShape MovedObject { get ; set ; }
45+ public WorldPosition MovedObjectLocation { get ; set ; }
46+ BoundingBoxPrimitive MovedObjectPrimitive ;
47+
4448 public ConcurrentBag < StaticShape > BoundingBoxShapes = new ConcurrentBag < StaticShape > ( ) ;
4549 readonly ConcurrentDictionary < ( int tileX , int tileZ , int uid , Matrix matrix , int number ) , BoundingBoxPrimitive > BoundingBoxPrimitives = new ConcurrentDictionary < ( int , int , int , Matrix , int ) , BoundingBoxPrimitive > ( ) ;
4650 readonly ConcurrentBag < EditorPrimitive > UnusedPrimitives = new ConcurrentBag < EditorPrimitive > ( ) ;
@@ -56,43 +60,43 @@ public EditorShapes(Viewer viewer) : base(viewer, "", null, ShapeFlags.None, nul
5660
5761 public override void PrepareFrame ( RenderFrame frame , ElapsedTime elapsedTime )
5862 {
59- if ( _selectedObject != SelectedObject )
63+ if ( PreviousSelectedObject != SelectedObject )
6064 {
61- if ( _selectedObject != null )
62- for ( var i = 0 ; i < 5 ; i ++ )
63- BoundingBoxPrimitives . TryRemove ( ( _selectedObject . Location . TileX , _selectedObject . Location . TileZ , _selectedObject . Uid , _selectedObject . Location . XNAMatrix , i ) , out _ ) ;
65+ if ( PreviousSelectedObject ? . BoundingBox != null )
66+ for ( var i = 0 ; i < PreviousSelectedObject . BoundingBox . Length ; i ++ )
67+ if ( BoundingBoxPrimitives . TryRemove ( ( PreviousSelectedObject . Location . TileX , PreviousSelectedObject . Location . TileZ , PreviousSelectedObject . Uid , PreviousSelectedObject . Location . XNAMatrix , i ) , out var primitive ) )
68+ UnusedPrimitives . Add ( primitive ) ;
6469
65- _selectedObject = SelectedObject ;
70+ PreviousSelectedObject = SelectedObject ;
6671
67- if ( _selectedObject ? . BoundingBox ? . Length > 0 )
72+ if ( SelectedObject ? . BoundingBox ? . Length > 0 )
6873 {
69- for ( var i = 0 ; i < _selectedObject . BoundingBox . Length ; i ++ )
74+ for ( var i = 0 ; i < SelectedObject . BoundingBox . Length ; i ++ )
7075 {
71- BoundingBoxPrimitives . TryAdd ( ( _selectedObject . Location . TileX , _selectedObject . Location . TileZ , _selectedObject . Uid , _selectedObject . Location . XNAMatrix , i ) ,
72- new BoundingBoxPrimitive ( Viewer , _selectedObject . BoundingBox [ i ] , Color . CornflowerBlue ) ) ;
76+ BoundingBoxPrimitives . TryAdd ( ( SelectedObject . Location . TileX , SelectedObject . Location . TileZ , SelectedObject . Uid , SelectedObject . Location . XNAMatrix , i ) ,
77+ new BoundingBoxPrimitive ( Viewer , SelectedObject . BoundingBox [ i ] , Color . CornflowerBlue ) ) ;
7378 }
7479 }
7580 }
7681 for ( var i = 0 ; i < BoundingBoxPrimitives . Count ; i ++ )
7782 {
83+ // Sweep out the not displayable bb-s
7884 var bb = BoundingBoxPrimitives . Keys . ElementAtOrDefault ( i ) ;
7985 if ( bb != default ( ( int , int , int , Matrix , int ) )
80- && ! BoundingBoxShapes . Any ( s => s ? . Location . TileX == bb . tileX && s ? . Location . TileZ == bb . tileZ && s ? . Uid == bb . uid ) )
86+ && ! BoundingBoxShapes . Any ( s => s ? . Location . TileX == bb . tileX && s ? . Location . TileZ == bb . tileZ && s ? . Uid == bb . uid )
87+ && bb . tileX != SelectedObject ? . Location . TileX && bb . tileZ != SelectedObject ? . Location . TileZ && bb . uid != SelectedObject ? . Uid )
8188 {
82- if ( bb . tileX != _selectedObject ? . Location . TileX && bb . tileZ != _selectedObject ? . Location . TileZ && bb . uid != _selectedObject ? . Uid )
83- {
84- if ( BoundingBoxPrimitives . TryRemove ( bb , out var primitive ) )
85- UnusedPrimitives . Add ( primitive ) ;
86- }
89+ if ( BoundingBoxPrimitives . TryRemove ( bb , out var primitive ) )
90+ UnusedPrimitives . Add ( primitive ) ;
8791 }
8892 }
89-
9093 for ( var j = 0 ; j < BoundingBoxShapes . Count ; j ++ )
9194 {
9295 var s = BoundingBoxShapes . ElementAtOrDefault ( j ) ;
9396 if ( s ? . BoundingBox == null || s . BoundingBox . Length == 0 )
9497 continue ;
9598
99+ // Complement with all the displayable bb-s
96100 if ( ! BoundingBoxPrimitives . Keys . Any ( bb => s . Location . TileX == bb . tileX && s . Location . TileZ == bb . tileZ && s . Uid == bb . uid ) )
97101 {
98102 for ( var i = 0 ; i < s . BoundingBox . Length ; i ++ )
@@ -102,7 +106,6 @@ public override void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
102106 }
103107 }
104108 }
105-
106109 if ( BoundingBoxPrimitives ? . Count > 0 )
107110 {
108111 foreach ( var boundingBox in BoundingBoxPrimitives . Keys )
@@ -116,6 +119,25 @@ public override void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
116119 frame . AddPrimitive ( BoundingBoxPrimitives [ boundingBox ] . Material , BoundingBoxPrimitives [ boundingBox ] , RenderPrimitiveGroup . Labels , ref xnaDTileTranslation ) ;
117120 }
118121 }
122+ if ( MovedObject == null && MovedObjectPrimitive != null )
123+ {
124+ UnusedPrimitives . Add ( MovedObjectPrimitive ) ;
125+ MovedObjectPrimitive = null ;
126+ }
127+ else if ( MovedObject != null && MovedObjectPrimitive == null )
128+ {
129+ if ( MovedObject . BoundingBox ? . Length > 0 )
130+ MovedObjectPrimitive = new BoundingBoxPrimitive ( Viewer , MovedObject . BoundingBox [ 0 ] , Color . MediumVioletRed ) ;
131+ }
132+ if ( MovedObjectPrimitive != null )
133+ {
134+ var dTileX = MovedObjectLocation . TileX - Viewer . Camera . TileX ;
135+ var dTileZ = MovedObjectLocation . TileZ - Viewer . Camera . TileZ ;
136+ var xnaDTileTranslation = MovedObjectLocation . XNAMatrix ;
137+ xnaDTileTranslation . M41 += dTileX * 2048 ;
138+ xnaDTileTranslation . M43 -= dTileZ * 2048 ;
139+ frame . AddPrimitive ( MovedObjectPrimitive . Material , MovedObjectPrimitive , RenderPrimitiveGroup . Labels , ref xnaDTileTranslation ) ;
140+ }
119141 if ( MouseCrosshairEnabled )
120142 {
121143 if ( CrosshairPositionUpdateEnabled )
@@ -127,7 +149,7 @@ public override void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
127149 }
128150 if ( HandleEnabled )
129151 {
130- var handleLocation = HandleLocation ?? _selectedObject ? . Location ;
152+ var handleLocation = HandleLocation ?? SelectedObject ? . Location ;
131153 if ( handleLocation != null )
132154 {
133155 var dTileX = handleLocation . TileX - Viewer . Camera . TileX ;
@@ -231,10 +253,16 @@ public class BoundingBoxPrimitive : BoxPrimitive
231253 public readonly Matrix ComplexTransform ;
232254
233255 public BoundingBoxPrimitive ( Viewer viewer , BoundingBox boundingBox , Color color )
234- : base ( viewer , boundingBox . Min , boundingBox . Max , color )
256+ : this ( viewer , boundingBox . Min , boundingBox . Max , color )
235257 {
236- Material = viewer . MaterialManager . Load ( "EditorPrimitive" ) ;
237258 ComplexTransform = boundingBox . ComplexTransform ;
259+ }
260+
261+ public BoundingBoxPrimitive ( Viewer viewer , Vector3 min , Vector3 max , Color color )
262+ : base ( viewer , min , max , color )
263+ {
264+ Material = viewer . MaterialManager . Load ( "EditorPrimitive" ) ;
265+ ComplexTransform = Matrix . Identity ;
238266 if ( BoundingBoxIndexBuffer == null )
239267 {
240268 var indexData = new short [ ] { 0 , 1 , 0 , 2 , 0 , 4 , 1 , 3 , 1 , 5 , 2 , 3 , 2 , 6 , 3 , 7 , 4 , 5 , 4 , 6 , 5 , 7 , 6 , 7 } ;
@@ -312,12 +340,7 @@ protected VertexPositionColor[] GetVertexData(int x, int y, int z, Color color)
312340 new [ ] { c , + b , + b } ,
313341 new [ ] { c , - b , - b } ,
314342 } ;
315- var vertexData = new VertexPositionColor [ data . Length ] ;
316- for ( var i = 0 ; i < data . Length ; i ++ )
317- {
318- vertexData [ i ] = new VertexPositionColor ( new Vector3 ( data [ i ] [ x ] , data [ i ] [ y ] , data [ i ] [ z ] ) , color ) ;
319- }
320- return vertexData ;
343+ return data . Select ( v => new VertexPositionColor ( new Vector3 ( v [ x ] , v [ y ] , v [ z ] ) , color ) ) . ToArray ( ) ;
321344 }
322345 }
323346
0 commit comments