41
41
using System . Threading . Tasks ;
42
42
using System . IO ;
43
43
using System . Globalization ;
44
+ using ORTS . Common . Input ;
44
45
45
46
namespace ORTS . TrackViewer
46
47
{
@@ -53,6 +54,9 @@ public class SceneViewer
53
54
public readonly TrackViewer TrackViewer ;
54
55
SwapChainRenderTarget SwapChain ;
55
56
internal StaticShape SelectedObject ;
57
+ internal Orts . Formats . Msts . WorldObject SelectedWorldObject ;
58
+ Viewer Viewer ;
59
+ ViewerCamera Camera ;
56
60
57
61
/// <summary>The command-line arguments</summary>
58
62
private string [ ] CommandLineArgs ;
@@ -88,7 +92,6 @@ public SceneViewer(TrackViewer trackViewer, string[] args)
88
92
RenderFrame . FinalRenderTarget = SwapChain ;
89
93
90
94
SceneWindow = new SceneWindow ( new SceneViewerHwndHost ( SwapChainWindow ) ) ;
91
- //SceneWindow = new SceneWindow(new SceneViewerVisualHost(GameWindow));
92
95
93
96
// The primary window activation events should not affect RunActivity
94
97
TrackViewer . Activated -= TrackViewer . ActivateRunActivity ;
@@ -105,6 +108,8 @@ public SceneViewer(TrackViewer trackViewer, string[] args)
105
108
LoadContent ( ) ;
106
109
}
107
110
111
+ public SceneViewer ( ) { }
112
+
108
113
/// <summary>
109
114
/// Allows the game to perform any initialization it needs to before starting to run.
110
115
/// This is where it can query for any required services and load any non-graphic
@@ -130,21 +135,48 @@ public void LoadContent()
130
135
/// <param name="gameTime">Provides a snapshot of timing values.</param>
131
136
public void Update ( GameTime gameTime )
132
137
{
133
- if ( TrackViewer . RenderProcess ? . Viewer == null )
138
+ Viewer = Viewer ?? TrackViewer . RenderProcess ? . Viewer ;
139
+ if ( Viewer == null )
134
140
return ;
141
+ Camera = Camera ?? Viewer . ViewerCamera ;
135
142
136
- TrackViewer . RenderProcess . Viewer . EditorShapes . MouseCrosshairEnabled = true ;
143
+ Viewer . EditorShapes . MouseCrosshairEnabled = true ;
137
144
138
- if ( UserInput . IsMouseLeftButtonPressed && UserInput . ModifiersMaskShiftCtrlAlt ( false , false , false ) )
145
+ if ( UserInput . IsMouseLeftButtonPressed && UserInput . ModifiersMaskShiftCtrlAlt ( false , false , false )
146
+ && Camera . PickByMouse ( out SelectedObject ) )
139
147
{
140
- if ( PickByMouse ( out SelectedObject ) )
141
- {
142
- TrackViewer . RenderProcess . Viewer . EditorShapes . SelectedObject = SelectedObject ;
143
- FillSelectedObjectData ( ) ;
144
- }
148
+ SelectedObjectChanged ( ) ;
149
+ }
150
+ if ( UserInput . IsMouseMiddleButtonPressed && UserInput . ModifiersMaskShiftCtrlAlt ( false , false , false ) )
151
+ {
152
+ Camera . StoreRotationOrigin ( Viewer . TerrainPoint ) ;
153
+ Viewer . EditorShapes . CrosshairPositionUpdateEnabled = false ;
154
+ }
155
+ if ( UserInput . IsMouseMiddleButtonDown && UserInput . ModifiersMaskShiftCtrlAlt ( false , false , false ) )
156
+ {
157
+ Camera . RotateByMouse ( ) ;
158
+ }
159
+ else
160
+ {
161
+ Viewer . EditorShapes . CrosshairPositionUpdateEnabled = true ;
162
+ }
163
+ if ( UserInput . IsMouseMiddleButtonDown && UserInput . ModifiersMaskShiftCtrlAlt ( true , false , false ) )
164
+ {
165
+ Camera . PanByMouse ( ) ;
166
+ }
167
+ else
168
+ {
169
+ Camera . ZoomByMouseWheel ( 1 ) ;
145
170
}
146
- //SetCameraLocationStatus(TrackViewer.RenderProcess?.Viewer?.Camera?.CameraWorldLocation ?? new WorldLocation());
147
- FillCursorPositionStatus ( ( TrackViewer . RenderProcess ? . Viewer ? . Camera as ViewerCamera ) ? . CursorPoint ?? new Vector3 ( ) ) ;
171
+
172
+ if ( UserInput . IsPressed ( UserCommand . EditorUnselectAll ) )
173
+ {
174
+ SelectedObject = null ;
175
+ SelectedObjectChanged ( ) ;
176
+ }
177
+
178
+ SetCameraLocationStatus ( TrackViewer . RenderProcess ? . Viewer ? . Camera ? . CameraWorldLocation ?? new WorldLocation ( ) ) ;
179
+ //FillCursorPositionStatus(TrackViewer.RenderProcess?.Viewer?.TerrainPoint ?? new Vector3());
148
180
}
149
181
150
182
public void EndDraw ( )
@@ -181,8 +213,6 @@ private void SetCameraLocationStatus(WorldLocation cameraLocation)
181
213
182
214
private void FillCursorPositionStatus ( Vector3 cursorPosition )
183
215
{
184
- //SceneWindow.tileXZ.Text = string.Format(System.Globalization.CultureInfo.CurrentCulture,
185
- // "{0,-7} {1,-7}", cameraLocation.TileX, cameraLocation.TileZ);
186
216
SceneWindow . LocationX . Text = string . Format ( CultureInfo . InvariantCulture , "{0,3:F3} " , cursorPosition . X ) ;
187
217
SceneWindow . LocationY . Text = string . Format ( CultureInfo . InvariantCulture , "{0,3:F3} " , cursorPosition . Y ) ;
188
218
SceneWindow . LocationZ . Text = string . Format ( CultureInfo . InvariantCulture , "{0,3:F3} " , - cursorPosition . Z ) ;
@@ -211,100 +241,22 @@ public async Task SetCameraLocation()
211
241
TrackViewer . RenderProcess . Viewer . ViewerCamera . SetLocation ( mouseLocation ) ;
212
242
}
213
243
214
- protected bool PickByMouse ( out StaticShape pickedObjectOut )
244
+ void SelectedObjectChanged ( )
215
245
{
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 ( )
301
- {
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 ( "," , "" ) ;
246
+ Viewer . EditorShapes . SelectedObject = SelectedObject ;
247
+
248
+ SelectedWorldObject = Viewer . World . Scenery . WorldFiles
249
+ . SingleOrDefault ( w => w . TileX == SelectedObject ? . Location . TileX && w . TileZ == SelectedObject ? . Location . TileZ )
250
+ ? . MstsWFile ? . Tr_Worldfile
251
+ ? . SingleOrDefault ( o => o . UID == SelectedObject ? . Uid ) ;
252
+
253
+ //SceneWindow.Filename.Text = SelectedObject != null ? System.IO.Path.GetFileName(SelectedObject.SharedShape.FilePath) : "";
254
+ //SceneWindow.TileX.Text = SelectedObject?.Location.TileX.ToString(CultureInfo.InvariantCulture).Replace(",", "");
255
+ //SceneWindow.TileZ.Text = SelectedObject?.Location.TileZ.ToString(CultureInfo.InvariantCulture).Replace(",", "");
256
+ //SceneWindow.PosX.Text = SelectedObject?.Location.Location.X.ToString("N3", CultureInfo.InvariantCulture).Replace(",", "");
257
+ //SceneWindow.PosY.Text = SelectedObject?.Location.Location.Y.ToString("N3", CultureInfo.InvariantCulture).Replace(",", "");
258
+ //SceneWindow.PosZ.Text = SelectedObject?.Location.Location.Z.ToString("N3", CultureInfo.InvariantCulture).Replace(",", "");
259
+ //SceneWindow.Uid.Text = SelectedObject.Uid.ToString(CultureInfo.InvariantCulture).Replace(",", "");
308
260
var q = new Quaternion ( ) ;
309
261
if ( SelectedObject ? . Location . XNAMatrix . Decompose ( out var _ , out q , out var _ ) ?? false )
310
262
{
@@ -317,7 +269,16 @@ void FillSelectedObjectData()
317
269
{
318
270
SceneWindow . RotY . Text = "" ;
319
271
}
320
- SceneWindow . Uid . Text = SelectedObject . Uid . ToString ( CultureInfo . InvariantCulture ) . Replace ( "," , "" ) ;
272
+
273
+ if ( SelectedObject is StaticShape ppp )
274
+ {
275
+ var sb = new StringBuilder ( ) ;
276
+ var aaa = Viewer . World . Scenery . WorldFiles
277
+ . SingleOrDefault ( w => w . TileX == SelectedObject . Location . TileX && w . TileZ == SelectedObject . Location . TileZ )
278
+ ? . MstsWFile ? . Tr_Worldfile ;
279
+ aaa . Serialize ( sb ) ;
280
+ var ccc = sb . ToString ( ) ;
281
+ }
321
282
}
322
283
323
284
}
0 commit comments