4141using System . Threading . Tasks ;
4242using System . IO ;
4343using System . Globalization ;
44+ using ORTS . Common . Input ;
4445
4546namespace ORTS . TrackViewer
4647{
@@ -53,6 +54,9 @@ public class SceneViewer
5354 public readonly TrackViewer TrackViewer ;
5455 SwapChainRenderTarget SwapChain ;
5556 internal StaticShape SelectedObject ;
57+ internal Orts . Formats . Msts . WorldObject SelectedWorldObject ;
58+ Viewer Viewer ;
59+ ViewerCamera Camera ;
5660
5761 /// <summary>The command-line arguments</summary>
5862 private string [ ] CommandLineArgs ;
@@ -88,7 +92,6 @@ public SceneViewer(TrackViewer trackViewer, string[] args)
8892 RenderFrame . FinalRenderTarget = SwapChain ;
8993
9094 SceneWindow = new SceneWindow ( new SceneViewerHwndHost ( SwapChainWindow ) ) ;
91- //SceneWindow = new SceneWindow(new SceneViewerVisualHost(GameWindow));
9295
9396 // The primary window activation events should not affect RunActivity
9497 TrackViewer . Activated -= TrackViewer . ActivateRunActivity ;
@@ -105,6 +108,8 @@ public SceneViewer(TrackViewer trackViewer, string[] args)
105108 LoadContent ( ) ;
106109 }
107110
111+ public SceneViewer ( ) { }
112+
108113 /// <summary>
109114 /// Allows the game to perform any initialization it needs to before starting to run.
110115 /// This is where it can query for any required services and load any non-graphic
@@ -130,21 +135,26 @@ public void LoadContent()
130135 /// <param name="gameTime">Provides a snapshot of timing values.</param>
131136 public void Update ( GameTime gameTime )
132137 {
133- if ( TrackViewer . RenderProcess ? . Viewer == null )
138+ Viewer = Viewer ?? TrackViewer . RenderProcess ? . Viewer ;
139+ if ( Viewer == null )
134140 return ;
141+ Camera = Camera ?? Viewer . ViewerCamera ;
135142
136- TrackViewer . RenderProcess . Viewer . EditorShapes . MouseCrosshairEnabled = true ;
143+ Viewer . EditorShapes . MouseCrosshairEnabled = true ;
137144
138- if ( UserInput . IsMouseLeftButtonPressed && UserInput . ModifiersMaskShiftCtrlAlt ( false , false , false ) )
145+ if ( UserInput . IsMouseLeftButtonPressed && UserInput . ModifiersMaskShiftCtrlAlt ( false , false , false )
146+ && Camera . PickByMouse ( out SelectedObject ) )
139147 {
140- if ( PickByMouse ( out SelectedObject ) )
141- {
142- TrackViewer . RenderProcess . Viewer . EditorShapes . SelectedObject = SelectedObject ;
143- FillSelectedObjectData ( ) ;
144- }
148+ SelectedObjectChanged ( ) ;
145149 }
146- //SetCameraLocationStatus(TrackViewer.RenderProcess?.Viewer?.Camera?.CameraWorldLocation ?? new WorldLocation());
147- FillCursorPositionStatus ( ( TrackViewer . RenderProcess ? . Viewer ? . Camera as ViewerCamera ) ? . CursorPoint ?? new Vector3 ( ) ) ;
150+ if ( UserInput . IsPressed ( UserCommand . EditorUnselectAll ) )
151+ {
152+ SelectedObject = null ;
153+ SelectedObjectChanged ( ) ;
154+ }
155+
156+ SetCameraLocationStatus ( Camera ? . CameraWorldLocation ?? new WorldLocation ( ) ) ;
157+ //FillCursorPositionStatus(Viewer?.TerrainPoint ?? new Vector3());
148158 }
149159
150160 public void EndDraw ( )
@@ -181,8 +191,6 @@ private void SetCameraLocationStatus(WorldLocation cameraLocation)
181191
182192 private void FillCursorPositionStatus ( Vector3 cursorPosition )
183193 {
184- //SceneWindow.tileXZ.Text = string.Format(System.Globalization.CultureInfo.CurrentCulture,
185- // "{0,-7} {1,-7}", cameraLocation.TileX, cameraLocation.TileZ);
186194 SceneWindow . LocationX . Text = string . Format ( CultureInfo . InvariantCulture , "{0,3:F3} " , cursorPosition . X ) ;
187195 SceneWindow . LocationY . Text = string . Format ( CultureInfo . InvariantCulture , "{0,3:F3} " , cursorPosition . Y ) ;
188196 SceneWindow . LocationZ . Text = string . Format ( CultureInfo . InvariantCulture , "{0,3:F3} " , - cursorPosition . Z ) ;
@@ -211,100 +219,22 @@ public async Task SetCameraLocation()
211219 TrackViewer . RenderProcess . Viewer . ViewerCamera . SetLocation ( mouseLocation ) ;
212220 }
213221
214- protected bool PickByMouse ( out StaticShape pickedObjectOut )
215- {
216- var viewer = TrackViewer . RenderProcess . Viewer ;
217-
218- if ( viewer == null )
219- {
220- pickedObjectOut = null ;
221- return false ;
222- }
223-
224- var camera = viewer . Camera ;
225-
226- var direction = Vector3 . Normalize ( viewer . FarPoint - viewer . NearPoint ) ;
227- var pickRay = new Ray ( viewer . NearPoint , direction ) ;
228-
229- object pickedObject = null ;
230- var pickedDistance = float . MaxValue ;
231- var boundingBoxes = new Orts . Viewer3D . BoundingBox [ 1 ] ;
232- foreach ( var worldFile in viewer . World . Scenery . WorldFiles )
233- {
234- foreach ( var checkedObject in worldFile . sceneryObjects )
235- {
236- checkObject ( checkedObject , checkedObject . BoundingBox , checkedObject . Location ) ;
237- }
238- foreach ( var checkedObject in worldFile . forestList )
239- {
240- var min = new Vector3 ( - checkedObject . ForestArea . X / 2 , - checkedObject . ForestArea . Y / 2 , 0 ) ;
241- var max = new Vector3 ( checkedObject . ForestArea . X / 2 , checkedObject . ForestArea . Y / 2 , 15 ) ;
242- boundingBoxes [ 0 ] = new Orts . Viewer3D . BoundingBox ( Matrix . Identity , Matrix . Identity , Vector3 . Zero , 0 , min , max ) ;
243- checkObject ( checkedObject , boundingBoxes , checkedObject . Position ) ;
244- }
245- }
246-
247- void checkObject ( object checkedObject , Orts . Viewer3D . BoundingBox [ ] checkedBoundingBox , WorldPosition checkedLocation )
248- {
249- if ( checkedBoundingBox ? . Length > 0 )
250- {
251- foreach ( var boundingBox in checkedBoundingBox )
252- {
253- // Locate relative to the camera
254- var dTileX = checkedLocation . TileX - camera . TileX ;
255- var dTileZ = checkedLocation . TileZ - camera . TileZ ;
256- var xnaDTileTranslation = checkedLocation . XNAMatrix ;
257- xnaDTileTranslation . M41 += dTileX * 2048 ;
258- xnaDTileTranslation . M43 -= dTileZ * 2048 ;
259- xnaDTileTranslation = boundingBox . ComplexTransform * xnaDTileTranslation ;
260-
261- var min = Vector3 . Transform ( boundingBox . Min , xnaDTileTranslation ) ;
262- var max = Vector3 . Transform ( boundingBox . Max , xnaDTileTranslation ) ;
263-
264- var xnabb = new Microsoft . Xna . Framework . BoundingBox ( min , max ) ;
265- checkDistance ( checkedObject , pickRay . Intersects ( xnabb ) ) ;
266- }
267- }
268- else
269- {
270- var radius = 10f ;
271- var boundingSphere = new BoundingSphere ( camera . XnaLocation ( checkedLocation . WorldLocation ) , radius ) ;
272- checkDistance ( checkedObject , pickRay . Intersects ( boundingSphere ) ) ;
273- }
274- }
275-
276- void checkDistance ( object checkedObject , float ? checkedDistance )
277- {
278- if ( checkedDistance != null && checkedDistance < pickedDistance )
279- {
280- pickedDistance = checkedDistance . Value ;
281- pickedObject = checkedObject ;
282- }
283- }
284-
285- pickedObjectOut = pickedObject as StaticShape ;
286-
287- if ( pickedObjectOut != null )
288- {
289- var ppp = pickedObjectOut ;
290- var sb = new StringBuilder ( ) ;
291- var aaa = TrackViewer . RenderProcess . Viewer . World . Scenery . WorldFiles . Where ( w =>
292- w . TileX == ppp . Location . TileX && w . TileZ == ppp . Location . TileZ ) . ToArray ( ) ;
293- var bbb = aaa [ 0 ] . MstsWFile ;
294- bbb . Tr_Worldfile . Serialize ( sb ) ;
295- var ccc = sb . ToString ( ) ;
296- }
297- return pickedObjectOut != null ;
298- }
299-
300- void FillSelectedObjectData ( )
222+ void SelectedObjectChanged ( )
301223 {
302- SceneWindow . Filename . Text = SelectedObject != null ? System . IO . Path . GetFileName ( SelectedObject . SharedShape . FilePath ) : "" ;
303- SceneWindow . TileX . Text = SelectedObject ? . Location . TileX . ToString ( CultureInfo . InvariantCulture ) . Replace ( "," , "" ) ;
304- SceneWindow . TileZ . Text = SelectedObject ? . Location . TileZ . ToString ( CultureInfo . InvariantCulture ) . Replace ( "," , "" ) ;
305- SceneWindow . PosX . Text = SelectedObject ? . Location . Location . X . ToString ( "N3" , CultureInfo . InvariantCulture ) . Replace ( "," , "" ) ;
306- SceneWindow . PosY . Text = SelectedObject ? . Location . Location . Y . ToString ( "N3" , CultureInfo . InvariantCulture ) . Replace ( "," , "" ) ;
307- SceneWindow . PosZ . Text = SelectedObject ? . Location . Location . Z . ToString ( "N3" , CultureInfo . InvariantCulture ) . Replace ( "," , "" ) ;
224+ Viewer . EditorShapes . SelectedObject = SelectedObject ;
225+
226+ SelectedWorldObject = Viewer . World . Scenery . WorldFiles
227+ . SingleOrDefault ( w => w . TileX == SelectedObject ? . Location . TileX && w . TileZ == SelectedObject ? . Location . TileZ )
228+ ? . MstsWFile ? . Tr_Worldfile
229+ ? . SingleOrDefault ( o => o . UID == SelectedObject ? . Uid ) ;
230+
231+ //SceneWindow.Filename.Text = SelectedObject != null ? System.IO.Path.GetFileName(SelectedObject.SharedShape.FilePath) : "";
232+ //SceneWindow.TileX.Text = SelectedObject?.Location.TileX.ToString(CultureInfo.InvariantCulture).Replace(",", "");
233+ //SceneWindow.TileZ.Text = SelectedObject?.Location.TileZ.ToString(CultureInfo.InvariantCulture).Replace(",", "");
234+ //SceneWindow.PosX.Text = SelectedObject?.Location.Location.X.ToString("N3", CultureInfo.InvariantCulture).Replace(",", "");
235+ //SceneWindow.PosY.Text = SelectedObject?.Location.Location.Y.ToString("N3", CultureInfo.InvariantCulture).Replace(",", "");
236+ //SceneWindow.PosZ.Text = SelectedObject?.Location.Location.Z.ToString("N3", CultureInfo.InvariantCulture).Replace(",", "");
237+ //SceneWindow.Uid.Text = SelectedObject.Uid.ToString(CultureInfo.InvariantCulture).Replace(",", "");
308238 var q = new Quaternion ( ) ;
309239 if ( SelectedObject ? . Location . XNAMatrix . Decompose ( out var _ , out q , out var _ ) ?? false )
310240 {
@@ -317,7 +247,16 @@ void FillSelectedObjectData()
317247 {
318248 SceneWindow . RotY . Text = "" ;
319249 }
320- SceneWindow . Uid . Text = SelectedObject . Uid . ToString ( CultureInfo . InvariantCulture ) . Replace ( "," , "" ) ;
250+
251+ if ( SelectedObject is StaticShape ppp )
252+ {
253+ var sb = new StringBuilder ( ) ;
254+ var aaa = Viewer . World . Scenery . WorldFiles
255+ . SingleOrDefault ( w => w . TileX == SelectedObject . Location . TileX && w . TileZ == SelectedObject . Location . TileZ )
256+ ? . MstsWFile ? . Tr_Worldfile ;
257+ aaa . Serialize ( sb ) ;
258+ var ccc = sb . ToString ( ) ;
259+ }
321260 }
322261
323262 }
0 commit comments