Skip to content

Commit

Permalink
Restructure the UI methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pzgulyas committed Dec 7, 2023
1 parent 685315b commit 167e2e4
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 264 deletions.
2 changes: 1 addition & 1 deletion Source/Contrib/TrackViewer/Drawing/Labels/DrawLabels.cs
Expand Up @@ -156,7 +156,7 @@ private void CreateContextMenu()

SetLocationMenuItem = new MenuItem() { Header = "View scene here" };
SetLocationMenuItem.Click += new RoutedEventHandler((sender, e) => TrackViewer.menuControl.MenuSceneWindow_Click(sender, e));
SetLocationMenuItem.Click += new RoutedEventHandler(async (sender, e) => await TrackViewer.SceneViewer?.SetCameraLocation(
SetLocationMenuItem.Click += new RoutedEventHandler(async (sender, e) => await TrackViewer.SceneView?.SetCameraLocation(
SetLocationMenuItem.CommandParameter as WorldLocation? ?? new WorldLocation()));
ContextMenu.Items.Add(SetLocationMenuItem);
}
Expand Down
57 changes: 51 additions & 6 deletions Source/Contrib/TrackViewer/TrackViewer.cs
Expand Up @@ -53,7 +53,6 @@ namespace ORTS.TrackViewer
public class TrackViewer : Orts.Viewer3D.Processes.Game
{
#region Public members
public SceneViewer SceneViewer { get; private set; }
private static RenderTarget2D DummyRenderTarget;
public bool IsTrackViewerWindowActive { get; private set; }

Expand Down Expand Up @@ -132,6 +131,9 @@ public class TrackViewer : Orts.Viewer3D.Processes.Game

/// <summary>The fontmanager that we use to draw strings</summary>
public FontManager fontManager;

public SceneView SceneView;

/// <summary>The command-line arguments</summary>
private string[] commandLineArgs;
#endregion
Expand Down Expand Up @@ -173,8 +175,8 @@ public TrackViewer(string[] args) : base(new ORTS.Settings.UserSettings(new[] {
LanguageManager = new LanguageManager();
LanguageManager.LoadLanguage(); // need this before all menus and stuff are initialized.

this.Activated += ActivateTrackViewer;
this.Deactivated += DeactivateTrackViewer;
Activated += ActivateTrackViewer;
Deactivated += DeactivateTrackViewer;

PushState(new GameStateStandBy());
}
Expand Down Expand Up @@ -236,9 +238,48 @@ protected override void Initialize()
base.Initialize();
}

public void InitializeSceneViewer(string[] args)
public void InitializeSceneView(string[] args)
{
SceneViewer = SceneViewer ?? new SceneViewer(this, args);
// Inject the secondary window into RunActivity
SwapChainWindow = GameWindow.Create(this,
GraphicsDevice.PresentationParameters.BackBufferWidth,
GraphicsDevice.PresentationParameters.BackBufferHeight);

RenderFrame.FinalRenderTarget = new SwapChainRenderTarget(GraphicsDevice,
SwapChainWindow.Handle,
GraphicsDevice.PresentationParameters.BackBufferWidth,
GraphicsDevice.PresentationParameters.BackBufferHeight,
false,
GraphicsDevice.PresentationParameters.BackBufferFormat,
GraphicsDevice.PresentationParameters.DepthStencilFormat,
1,
RenderTargetUsage.PlatformContents,
PresentInterval.Two);

SceneView = new SceneView(SwapChainWindow.Handle);

// The primary window activation events should not affect RunActivity
Activated -= ActivateRunActivity;
Deactivated -= DeactivateRunActivity;

/// A workaround for a MonoGame bug where the <see cref="Microsoft.Xna.Framework.Input.Keyboard.GetState()" />
/// doesn't return the valid keyboard state. Needs to be enabled via reflection in a private method.
var keyboardSetActive = typeof(Microsoft.Xna.Framework.Input.Keyboard)
.GetMethod("SetActive", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);

// The secondary window activation events should affect RunActivity
SceneView.Activated += ActivateRunActivity;
SceneView.Activated += new System.EventHandler((sender, e) => keyboardSetActive.Invoke(null, new object[] { true }));
SceneView.Deactivated += DeactivateRunActivity;
SceneView.Deactivated += new System.EventHandler((sender, e) => keyboardSetActive.Invoke(null, new object[] { false }));

ReplaceState(new GameStateRunActivity(new[] { "-start", "-viewer", CurrentRoute.Path + "\\dummy\\.pat", "", "10:00", "1", "0" }));
}

public void ShowSceneView()
{
SceneView.Show();
SceneView.Activate();
}

/// <summary>
Expand Down Expand Up @@ -324,7 +365,11 @@ private void DrawLoadingMessage(string message)
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
SceneViewer?.Update(gameTime);
if (RenderProcess?.Viewer != null && SceneView != null)
{
SceneView.Viewer = SceneView.Viewer ?? RenderProcess.Viewer;
SceneView.Update(gameTime);
}

if (!this.IsTrackViewerWindowActive)
{
Expand Down
4 changes: 2 additions & 2 deletions Source/Contrib/TrackViewer/UserInterface/MenuControl.xaml.cs
Expand Up @@ -1232,8 +1232,8 @@ public void MenuSceneWindow_Click(object sender, RoutedEventArgs e)
{
if (trackViewer.CurrentRoute == null)
return;
trackViewer.InitializeSceneViewer(null);
trackViewer.SceneViewer.Show();
trackViewer.InitializeSceneView(null);
trackViewer.ShowSceneView();
}
}

Expand Down
@@ -1,13 +1,25 @@
<Window x:Class="ORTS.TrackViewer.UserInterface.SceneWindow"
<Window x:Class="ORTS.TrackViewer.UserInterface.SceneView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ORTS.TrackViewer"
xmlns:local="clr-namespace:ORTS.TrackViewer.UserInterface"
mc:Ignorable="d"
Name="window"
d:DataContext="{d:DesignInstance local:SceneViewer, IsDesignTimeCreatable=False}"
Title="Scene View" SizeToContent="WidthAndHeight">
<Window.CommandBindings>
<CommandBinding Command="Undo" Executed="UndoCommand" CanExecute="UndoRedoCanExecute"/>
<CommandBinding Command="Redo" Executed="RedoCommand" CanExecute="UndoRedoCanExecute"/>
<CommandBinding Command="Stop" Executed="CancelCommand"/>
<CommandBinding Command="MoveLeft" Executed="RotateCommand"/>
<CommandBinding Command="MoveRight" Executed="MoveCommand"/>
<CommandBinding Command="MoveUp" Executed="MoveHandleCommand"/>
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Key="Esc" Command="Stop"/><!--This doesn't work-->
<KeyBinding Key="M" Command="MoveRight"/>
<KeyBinding Key="R" Command="MoveLeft"/>
<KeyBinding Key="H" Command="MoveUp"/>
</Window.InputBindings>
<DockPanel LastChildFill="True">
<Menu DockPanel.Dock="Top" Height="22" Name="menuMain" Width="Auto">
<MenuItem Header="_File">
Expand All @@ -17,9 +29,9 @@
<MenuItem Header="_Undo" Name="menuUndo" Command="Undo" InputGestureText="Ctrl+Z"/>
<MenuItem Header="_Redo" Name="menuRedo" Command="Redo" InputGestureText="Ctrl+Y"/>
<Separator/>
<MenuItem Header="_Move" Name="menuName" InputGestureText="M"/>
<MenuItem Header="_Rotate" Name="menuRotate" InputGestureText="R"/>
<MenuItem Header="Move _handle" Name="menuHandle" InputGestureText="H"/>
<MenuItem Header="_Move" Name="menuName" Command="MoveRight" InputGestureText="M"/>
<MenuItem Header="_Rotate" Name="menuRotate" Command="MoveLeft" InputGestureText="R"/>
<MenuItem Header="Move _handle" Name="menuHandle" Command="MoveUp" InputGestureText="H"/>
</MenuItem>
<MenuItem Header="_View">
<MenuItem Header="Nothing to see here" Name="menuNothing3" />
Expand Down Expand Up @@ -112,21 +124,21 @@
<StatusBarItem Grid.Column="12">
<Border BorderThickness="1 1 0 0" BorderBrush="Gray">
<Border BorderThickness="0 0 1 1" BorderBrush="AntiqueWhite">
<TextBlock Name="DeltaX" Width="55" TextAlignment="Right" Background="#FFF7DCDC"/>
<TextBlock Name="DeltaXBlock" Width="55" TextAlignment="Right" Background="#FFF7DCDC"/>
</Border>
</Border>
</StatusBarItem>
<StatusBarItem Grid.Column="13">
<Border BorderThickness="1 1 0 0" BorderBrush="Gray">
<Border BorderThickness="0 0 1 1" BorderBrush="AntiqueWhite">
<TextBlock Name="DeltaY" Width="55" TextAlignment="Right" Background="#FFC3CFFB"/>
<TextBlock Name="DeltaYBlock" Width="55" TextAlignment="Right" Background="#FFC3CFFB"/>
</Border>
</Border>
</StatusBarItem>
<StatusBarItem Grid.Column="14">
<Border BorderThickness="1 1 0 0" BorderBrush="Gray">
<Border BorderThickness="0 0 1 1" BorderBrush="AntiqueWhite">
<TextBlock Name="DeltaZ" Width="55" TextAlignment="Right" Background="#FFCDF9D8"/>
<TextBlock Name="DeltaZBlock" Width="55" TextAlignment="Right" Background="#FFCDF9D8"/>
</Border>
</Border>
</StatusBarItem>
Expand Down Expand Up @@ -179,8 +191,4 @@
</Border>
<Grid DockPanel.Dock="Bottom" x:Name="GraphicsHostElement" />
</DockPanel>
<Window.CommandBindings>
<!--CommandBinding Command="Undo" Executed="{Binding UndoCommand}"/>
<CommandBinding Command="Redo" Executed="{Binding RedoCommand}"/-->
</Window.CommandBindings>
</Window>

0 comments on commit 167e2e4

Please sign in to comment.