StarSim

StarSim.git
git clone git://git.lenczewski.org/StarSim.git
Log | Files | Refs | README | LICENSE

commit a129cdcbaf33b6c3fdbfbf730210f627e0fef414
parent 10c5794a85801f2be3c83b8241bad54b3c56eb85
Author: Mikolaj Lenczewski <33129490+EnderRifter@users.noreply.github.com>
Date:   Mon, 15 Jul 2019 11:29:08 +0100

Started work on windowing system.

Diffstat:
MStarSim/StarSim/Program.cs | 6+++++-
AStarSim/StarSimLib/GUI/Components/Component.cs | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
AStarSim/StarSimLib/GUI/Components/Image.cs | 17+++++++++++++++++
AStarSim/StarSimLib/GUI/IInputHandler.cs | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
AStarSim/StarSimLib/GUI/Screen.cs | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MStarSim/StarSimLib/StarSimLib.xml | 253+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
AStarSim/StarSimLib/UI/MainMenuScreen.cs | 41+++++++++++++++++++++++++++++++++++++++++
MStarSim/StarSimLib/UI/SimulationInputHandler.cs | 63++++++++++++++++++++++++++-------------------------------------
MStarSim/StarSimLib/UI/SimulationScreen.cs | 98+++++++++++++++++++++++++++++++++++--------------------------------------------
9 files changed, 571 insertions(+), 136 deletions(-)

diff --git a/StarSim/StarSim/Program.cs b/StarSim/StarSim/Program.cs @@ -13,6 +13,7 @@ using StarSimLib.Contexts; using StarSimLib.Cryptography; using StarSimLib.Data_Structures; using StarSimLib.Graphics; +using StarSimLib.GUI; using StarSimLib.Physics; using StarSimLib.UI; @@ -70,7 +71,10 @@ namespace StarSim UpdateDelegate bodyPositionUpdater = BodyUpdater.UpdateBodiesBarnesHut; #endif - simulationScreen = new SimulationScreen(ref bodies, ref bodyShapeMap, bodyPositionUpdater); + RenderWindow simulationWindow = new RenderWindow(VideoMode.DesktopMode, "N-Body Simulation: FPS "); + IInputHandler simulationInputHandler = new SimulationInputHandler(ref bodies); + + simulationScreen = new SimulationScreen(simulationWindow, simulationInputHandler, ref bodies, ref bodyShapeMap, bodyPositionUpdater); } /// <summary> diff --git a/StarSim/StarSimLib/GUI/Components/Component.cs b/StarSim/StarSimLib/GUI/Components/Component.cs @@ -0,0 +1,68 @@ +using SFML.Graphics; +using SFML.System; + +namespace StarSimLib.GUI.Components +{ + /// <summary> + /// Base class for all GUI components, such as text boxes, buttons, etc. + /// </summary> + public abstract class Component : Drawable + { + /// <summary> + /// The sprite representing this component. + /// </summary> + private readonly Sprite sprite; + + /// <summary> + /// The rectangular entity used for this instances sprite. + /// </summary> + private IntRect intRect; + + /// <summary> + /// The texture used for this instances sprite. + /// </summary> + private Texture texture; + + /// <summary> + /// Initialises a new instance of the <see cref="Component"/> class. + /// </summary> + /// <param name="position">The position of this instance.</param> + /// <param name="size">The size of this instance.</param> + /// <param name="texture">The texture of this instance.</param> + protected Component(Vector2i position, Vector2i size, Texture texture) + { + intRect = new IntRect(position, size); + this.texture = texture; + sprite = new Sprite(this.texture, intRect); + } + + /// <summary> + /// The position of this component. + /// </summary> + public Vector2f Position + { + get { return sprite.Position; } + set { sprite.Position = value; } + } + + /// <summary> + /// Draws this component to the given <see cref="RenderTarget"/>, with the given <see cref="RenderStates"/>. + /// </summary> + /// <param name="renderTarget">The <see cref="RenderTarget"/> to which to draw this instance.</param> + /// <param name="renderStates">The <see cref="RenderStates"/> for the drawing action.</param> + protected virtual void DrawInternal(RenderTarget renderTarget, RenderStates renderStates) + { + sprite.Draw(renderTarget, renderStates); + } + + #region Implementation of Drawable + + /// <inheritdoc /> + public void Draw(RenderTarget target, RenderStates states) + { + DrawInternal(target, states); + } + + #endregion Implementation of Drawable + } +} +\ No newline at end of file diff --git a/StarSim/StarSimLib/GUI/Components/Image.cs b/StarSim/StarSimLib/GUI/Components/Image.cs @@ -0,0 +1,16 @@ +using SFML.Graphics; +using SFML.System; + +namespace StarSimLib.GUI.Components +{ + /// <summary> + /// Represents an image. + /// </summary> + public class Image : Component + { + /// <inheritdoc /> + public Image(Vector2i position, Vector2i size, Texture texture) : base(position, size, texture) + { + } + } +} +\ No newline at end of file diff --git a/StarSim/StarSimLib/GUI/IInputHandler.cs b/StarSim/StarSimLib/GUI/IInputHandler.cs @@ -0,0 +1,60 @@ +using System; +using SFML.Window; + +namespace StarSimLib.GUI +{ + /// <summary> + /// Describes an input handler that allows screen interactivity. + /// </summary> + public interface IInputHandler + { + /// <summary> + /// Handles key presses. + /// </summary> + /// <param name="sender">The <see cref="Window"/> that sent the event.</param> + /// <param name="eventArgs">The <see cref="KeyEventArgs"/> associated with the key press.</param> + void HandleKeyPressed(object sender, KeyEventArgs eventArgs); + + /// <summary> + /// Handles key releases. + /// </summary> + /// <param name="sender">The <see cref="Window"/> that sent the event.</param> + /// <param name="eventArgs">The <see cref="KeyEventArgs"/> associated with the key release.</param> + void HandleKeyReleased(object sender, KeyEventArgs eventArgs); + + /// <summary> + /// Handles motions of the mouse. + /// </summary> + /// <param name="sender">The <see cref="Window"/> that sent the event.</param> + /// <param name="eventArgs">The <see cref="MouseMoveEventArgs"/> associated with the mouse movement.</param> + void HandleMouseMoved(object sender, MouseMoveEventArgs eventArgs); + + /// <summary> + /// Handles key presses of the mouse. + /// </summary> + /// <param name="sender">The <see cref="Window"/> that sent the event.</param> + /// <param name="eventArgs">The <see cref="MouseButtonEventArgs"/> associated with the mouse press.</param> + void HandleMousePressed(object sender, MouseButtonEventArgs eventArgs); + + /// <summary> + /// Handles key releases of the mouse. + /// </summary> + /// <param name="sender">The <see cref="Window"/> that sent the event.</param> + /// <param name="eventArgs">The <see cref="MouseButtonEventArgs"/> associated with the mouse release.</param> + void HandleMouseReleased(object sender, MouseButtonEventArgs eventArgs); + + /// <summary> + /// Handles scrolling of the mouse wheel. + /// </summary> + /// <param name="sender">The <see cref="Window"/> that sent the event.</param> + /// <param name="eventArgs">The <see cref="MouseWheelScrollEventArgs"/> associated with the mouse scroll.</param> + void HandleMouseScrolled(object sender, MouseWheelScrollEventArgs eventArgs); + + /// <summary> + /// Handles the screen closing. + /// </summary> + /// <param name="sender">The <see cref="Window"/> that sent the event.</param> + /// <param name="eventArgs">The <see cref="EventArgs"/> associated with the screen close.</param> + void HandleScreenClosed(object sender, EventArgs eventArgs); + } +} +\ No newline at end of file diff --git a/StarSim/StarSimLib/GUI/Screen.cs b/StarSim/StarSimLib/GUI/Screen.cs @@ -0,0 +1,98 @@ +using SFML.Graphics; + +namespace StarSimLib.GUI +{ + /// <summary> + /// Base class for screens that include user interactivity. + /// </summary> + public abstract class Screen + { + /// <summary> + /// The input handler to use for this instance. + /// </summary> + protected readonly IInputHandler inputHandler; + + /// <summary> + /// The window to which this instance is drawn. + /// </summary> + protected readonly RenderWindow renderWindow; + + /// <summary> + /// Initialises a new instance of the <see cref="Screen"/> class. + /// </summary> + /// <param name="window"></param> + /// <param name="inputHandler"></param> + protected Screen(RenderWindow window, IInputHandler inputHandler) + { + renderWindow = window; + + this.inputHandler = inputHandler; + + renderWindow.Closed += (sender, eventArgs) => ((RenderWindow)sender).Close(); + + // we apply event handlers to allow for interactivity inside the window + renderWindow.KeyPressed += this.inputHandler.HandleKeyPressed; + renderWindow.KeyReleased += this.inputHandler.HandleKeyReleased; + renderWindow.MouseButtonPressed += this.inputHandler.HandleMousePressed; + renderWindow.MouseButtonReleased += this.inputHandler.HandleMouseReleased; + renderWindow.MouseMoved += this.inputHandler.HandleMouseMoved; + renderWindow.MouseWheelScrolled += this.inputHandler.HandleMouseScrolled; + + renderWindow.SetVisible(false); + + ConstructScreen(); + } + + /// <summary> + /// Cleans up the contents of the screen to the given <see cref="RenderTarget"/> with the given <see cref="RenderStates"/>. + /// </summary> + /// <param name="renderTarget">The <see cref="RenderTarget"/> to which to draw this instance.</param> + /// <param name="renderStates">The <see cref="RenderStates"/> to use for the drawing.</param> + protected abstract void CleanupScreen(RenderTarget renderTarget, RenderStates renderStates); + + /// <summary> + /// Called during the constructor to allow for custom setup, before the child constructor is called. + /// </summary> + protected abstract void ConstructScreen(); + + /// <summary> + /// Draws the contents of the screen to the given <see cref="RenderTarget"/> with the given <see cref="RenderStates"/>. + /// </summary> + /// <param name="renderTarget">The <see cref="RenderTarget"/> to which to draw this instance.</param> + /// <param name="renderStates">The <see cref="RenderStates"/> to use for the drawing.</param> + protected abstract void DrawFrame(RenderTarget renderTarget, RenderStates renderStates); + + /// <summary> + /// Sets up the contents of the screen to the given <see cref="RenderTarget"/> with the given <see cref="RenderStates"/>. + /// </summary> + /// <param name="renderTarget">The <see cref="RenderTarget"/> to which to draw this instance.</param> + /// <param name="renderStates">The <see cref="RenderStates"/> to use for the drawing.</param> + protected abstract void SetupScreen(RenderTarget renderTarget, RenderStates renderStates); + + /// <summary> + /// Displays the screen until it is closed. + /// </summary> + public void Run() + { + // we reveal the window so that the user may interact with our program + renderWindow.SetVisible(true); + + // allows custom screen setup code + SetupScreen(renderWindow, RenderStates.Default); + + while (renderWindow.IsOpen) + { + renderWindow.Clear(); + renderWindow.DispatchEvents(); + + // allows custom screen content + DrawFrame(renderWindow, RenderStates.Default); + + renderWindow.Display(); + } + + // allows custom screen cleanup code + CleanupScreen(renderWindow, RenderStates.Default); + } + } +} +\ No newline at end of file diff --git a/StarSim/StarSimLib/StarSimLib.xml b/StarSim/StarSimLib/StarSimLib.xml @@ -1293,6 +1293,164 @@ </summary> <param name="scaleMultiplier">The multiplier by which to scale the viewport of this instances render target.</param> </member> + <member name="T:StarSimLib.GUI.Components.Component"> + <summary> + Base class for all GUI components, such as text boxes, buttons, etc. + </summary> + </member> + <member name="F:StarSimLib.GUI.Components.Component.sprite"> + <summary> + The sprite representing this component. + </summary> + </member> + <member name="F:StarSimLib.GUI.Components.Component.intRect"> + <summary> + The rectangular entity used for this instances sprite. + </summary> + </member> + <member name="F:StarSimLib.GUI.Components.Component.texture"> + <summary> + The texture used for this instances sprite. + </summary> + </member> + <member name="M:StarSimLib.GUI.Components.Component.#ctor(SFML.System.Vector2i,SFML.System.Vector2i,SFML.Graphics.Texture)"> + <summary> + Initialises a new instance of the <see cref="T:StarSimLib.GUI.Components.Component"/> class. + </summary> + <param name="position">The position of this instance.</param> + <param name="size">The size of this instance.</param> + <param name="texture">The texture of this instance.</param> + </member> + <member name="P:StarSimLib.GUI.Components.Component.Position"> + <summary> + The position of this component. + </summary> + </member> + <member name="M:StarSimLib.GUI.Components.Component.DrawInternal(SFML.Graphics.RenderTarget,SFML.Graphics.RenderStates)"> + <summary> + Draws this component to the given <see cref="T:SFML.Graphics.RenderTarget"/>, with the given <see cref="T:SFML.Graphics.RenderStates"/>. + </summary> + <param name="renderTarget">The <see cref="T:SFML.Graphics.RenderTarget"/> to which to draw this instance.</param> + <param name="renderStates">The <see cref="T:SFML.Graphics.RenderStates"/> for the drawing action.</param> + </member> + <member name="M:StarSimLib.GUI.Components.Component.Draw(SFML.Graphics.RenderTarget,SFML.Graphics.RenderStates)"> + <inheritdoc /> + </member> + <member name="T:StarSimLib.GUI.Components.Image"> + <summary> + Represents an image. + </summary> + </member> + <member name="M:StarSimLib.GUI.Components.Image.#ctor(SFML.System.Vector2i,SFML.System.Vector2i,SFML.Graphics.Texture)"> + <inheritdoc /> + </member> + <member name="T:StarSimLib.GUI.IInputHandler"> + <summary> + Describes an input handler that allows screen interactivity. + </summary> + </member> + <member name="M:StarSimLib.GUI.IInputHandler.HandleKeyPressed(System.Object,SFML.Window.KeyEventArgs)"> + <summary> + Handles key presses. + </summary> + <param name="sender">The <see cref="T:SFML.Window.Window"/> that sent the event.</param> + <param name="eventArgs">The <see cref="T:SFML.Window.KeyEventArgs"/> associated with the key press.</param> + </member> + <member name="M:StarSimLib.GUI.IInputHandler.HandleKeyReleased(System.Object,SFML.Window.KeyEventArgs)"> + <summary> + Handles key releases. + </summary> + <param name="sender">The <see cref="T:SFML.Window.Window"/> that sent the event.</param> + <param name="eventArgs">The <see cref="T:SFML.Window.KeyEventArgs"/> associated with the key release.</param> + </member> + <member name="M:StarSimLib.GUI.IInputHandler.HandleMouseMoved(System.Object,SFML.Window.MouseMoveEventArgs)"> + <summary> + Handles motions of the mouse. + </summary> + <param name="sender">The <see cref="T:SFML.Window.Window"/> that sent the event.</param> + <param name="eventArgs">The <see cref="T:SFML.Window.MouseMoveEventArgs"/> associated with the mouse movement.</param> + </member> + <member name="M:StarSimLib.GUI.IInputHandler.HandleMousePressed(System.Object,SFML.Window.MouseButtonEventArgs)"> + <summary> + Handles key presses of the mouse. + </summary> + <param name="sender">The <see cref="T:SFML.Window.Window"/> that sent the event.</param> + <param name="eventArgs">The <see cref="T:SFML.Window.MouseButtonEventArgs"/> associated with the mouse press.</param> + </member> + <member name="M:StarSimLib.GUI.IInputHandler.HandleMouseReleased(System.Object,SFML.Window.MouseButtonEventArgs)"> + <summary> + Handles key releases of the mouse. + </summary> + <param name="sender">The <see cref="T:SFML.Window.Window"/> that sent the event.</param> + <param name="eventArgs">The <see cref="T:SFML.Window.MouseButtonEventArgs"/> associated with the mouse release.</param> + </member> + <member name="M:StarSimLib.GUI.IInputHandler.HandleMouseScrolled(System.Object,SFML.Window.MouseWheelScrollEventArgs)"> + <summary> + Handles scrolling of the mouse wheel. + </summary> + <param name="sender">The <see cref="T:SFML.Window.Window"/> that sent the event.</param> + <param name="eventArgs">The <see cref="T:SFML.Window.MouseWheelScrollEventArgs"/> associated with the mouse scroll.</param> + </member> + <member name="M:StarSimLib.GUI.IInputHandler.HandleScreenClosed(System.Object,System.EventArgs)"> + <summary> + Handles the screen closing. + </summary> + <param name="sender">The <see cref="T:SFML.Window.Window"/> that sent the event.</param> + <param name="eventArgs">The <see cref="T:System.EventArgs"/> associated with the screen close.</param> + </member> + <member name="T:StarSimLib.GUI.Screen"> + <summary> + Base class for screens that include user interactivity. + </summary> + </member> + <member name="F:StarSimLib.GUI.Screen.inputHandler"> + <summary> + The input handler to use for this instance. + </summary> + </member> + <member name="F:StarSimLib.GUI.Screen.renderWindow"> + <summary> + The window to which this instance is drawn. + </summary> + </member> + <member name="M:StarSimLib.GUI.Screen.#ctor(SFML.Graphics.RenderWindow,StarSimLib.GUI.IInputHandler)"> + <summary> + Initialises a new instance of the <see cref="T:StarSimLib.GUI.Screen"/> class. + </summary> + <param name="window"></param> + <param name="inputHandler"></param> + </member> + <member name="M:StarSimLib.GUI.Screen.CleanupScreen(SFML.Graphics.RenderTarget,SFML.Graphics.RenderStates)"> + <summary> + Cleans up the contents of the screen to the given <see cref="T:SFML.Graphics.RenderTarget"/> with the given <see cref="T:SFML.Graphics.RenderStates"/>. + </summary> + <param name="renderTarget">The <see cref="T:SFML.Graphics.RenderTarget"/> to which to draw this instance.</param> + <param name="renderStates">The <see cref="T:SFML.Graphics.RenderStates"/> to use for the drawing.</param> + </member> + <member name="M:StarSimLib.GUI.Screen.ConstructScreen"> + <summary> + Called during the constructor to allow for custom setup, before the child constructor is called. + </summary> + </member> + <member name="M:StarSimLib.GUI.Screen.DrawFrame(SFML.Graphics.RenderTarget,SFML.Graphics.RenderStates)"> + <summary> + Draws the contents of the screen to the given <see cref="T:SFML.Graphics.RenderTarget"/> with the given <see cref="T:SFML.Graphics.RenderStates"/>. + </summary> + <param name="renderTarget">The <see cref="T:SFML.Graphics.RenderTarget"/> to which to draw this instance.</param> + <param name="renderStates">The <see cref="T:SFML.Graphics.RenderStates"/> to use for the drawing.</param> + </member> + <member name="M:StarSimLib.GUI.Screen.SetupScreen(SFML.Graphics.RenderTarget,SFML.Graphics.RenderStates)"> + <summary> + Sets up the contents of the screen to the given <see cref="T:SFML.Graphics.RenderTarget"/> with the given <see cref="T:SFML.Graphics.RenderStates"/>. + </summary> + <param name="renderTarget">The <see cref="T:SFML.Graphics.RenderTarget"/> to which to draw this instance.</param> + <param name="renderStates">The <see cref="T:SFML.Graphics.RenderStates"/> to use for the drawing.</param> + </member> + <member name="M:StarSimLib.GUI.Screen.Run"> + <summary> + Displays the screen until it is closed. + </summary> + </member> <member name="T:StarSimLib.Models.Body"> <summary> Represents a known stellar body in the database. Maps to a <see cref="T:StarSimLib.Data_Structures.Body"/> instance. @@ -1604,6 +1762,26 @@ </summary> <returns>A 3D position vector.</returns> </member> + <member name="T:StarSimLib.UI.MainMenuScreen"> + <summary> + Encapsulates the main menu screen. + </summary> + </member> + <member name="M:StarSimLib.UI.MainMenuScreen.#ctor(SFML.Graphics.RenderWindow,StarSimLib.GUI.IInputHandler)"> + <inheritdoc /> + </member> + <member name="M:StarSimLib.UI.MainMenuScreen.CleanupScreen(SFML.Graphics.RenderTarget,SFML.Graphics.RenderStates)"> + <inheritdoc /> + </member> + <member name="M:StarSimLib.UI.MainMenuScreen.ConstructScreen"> + <inheritdoc /> + </member> + <member name="M:StarSimLib.UI.MainMenuScreen.DrawFrame(SFML.Graphics.RenderTarget,SFML.Graphics.RenderStates)"> + <inheritdoc /> + </member> + <member name="M:StarSimLib.UI.MainMenuScreen.SetupScreen(SFML.Graphics.RenderTarget,SFML.Graphics.RenderStates)"> + <inheritdoc /> + </member> <member name="T:StarSimLib.UI.SimulationInputHandler"> <summary> Provides user input handling functions. @@ -1619,16 +1797,13 @@ The renderer used to display the <see cref="T:StarSimLib.Data_Structures.Body"/> instances on the screen. </summary> </member> - <member name="M:StarSimLib.UI.SimulationInputHandler.#ctor(StarSimLib.Data_Structures.Body[]@,StarSimLib.Graphics.SimulationDrawer@)"> + <member name="M:StarSimLib.UI.SimulationInputHandler.#ctor(StarSimLib.Data_Structures.Body[]@)"> <summary> Initialises a new instance of the <see cref="T:StarSimLib.UI.SimulationInputHandler"/> class. </summary> <param name="bodies"> A reference to the <see cref="T:StarSimLib.Data_Structures.Body"/> instances which should be managed by this instance. </param> - <param name="simulationDrawer"> - A reference to the renderer used to display the <see cref="T:StarSimLib.Data_Structures.Body"/> instances on the screen. - </param> </member> <member name="P:StarSimLib.UI.SimulationInputHandler.IsSimulationPaused"> <summary> @@ -1642,46 +1817,31 @@ </summary> </member> <member name="M:StarSimLib.UI.SimulationInputHandler.HandleKeyPressed(System.Object,SFML.Window.KeyEventArgs)"> - <summary> - Handles key presses. - </summary> - <param name="sender">The <see cref="T:SFML.Window.Window"/> that sent the event.</param> - <param name="eventArgs">The <see cref="T:SFML.Window.KeyEventArgs"/> associated with the key press.</param> + <inheritdoc /> </member> <member name="M:StarSimLib.UI.SimulationInputHandler.HandleKeyReleased(System.Object,SFML.Window.KeyEventArgs)"> - <summary> - Handles key releases. - </summary> - <param name="sender">The <see cref="T:SFML.Window.Window"/> that sent the event.</param> - <param name="eventArgs">The <see cref="T:SFML.Window.KeyEventArgs"/> associated with the key release.</param> + <inheritdoc /> </member> <member name="M:StarSimLib.UI.SimulationInputHandler.HandleMouseMoved(System.Object,SFML.Window.MouseMoveEventArgs)"> - <summary> - Handles motions of the mouse. - </summary> - <param name="sender">The <see cref="T:SFML.Window.Window"/> that sent the event.</param> - <param name="eventArgs">The <see cref="T:SFML.Window.MouseMoveEventArgs"/> associated with the mouse movement.</param> + <inheritdoc /> </member> <member name="M:StarSimLib.UI.SimulationInputHandler.HandleMousePressed(System.Object,SFML.Window.MouseButtonEventArgs)"> - <summary> - Handles key presses of the mouse. - </summary> - <param name="sender">The <see cref="T:SFML.Window.Window"/> that sent the event.</param> - <param name="eventArgs">The <see cref="T:SFML.Window.MouseButtonEventArgs"/> associated with the mouse press.</param> + <inheritdoc /> </member> <member name="M:StarSimLib.UI.SimulationInputHandler.HandleMouseReleased(System.Object,SFML.Window.MouseButtonEventArgs)"> - <summary> - Handles key releases of the mouse. - </summary> - <param name="sender">The <see cref="T:SFML.Window.Window"/> that sent the event.</param> - <param name="eventArgs">The <see cref="T:SFML.Window.MouseButtonEventArgs"/> associated with the mouse release.</param> + <inheritdoc /> </member> <member name="M:StarSimLib.UI.SimulationInputHandler.HandleMouseScrolled(System.Object,SFML.Window.MouseWheelScrollEventArgs)"> + <inheritdoc /> + </member> + <member name="M:StarSimLib.UI.SimulationInputHandler.HandleScreenClosed(System.Object,System.EventArgs)"> + <inheritdoc /> + </member> + <member name="M:StarSimLib.UI.SimulationInputHandler.SetSimulationDrawer(StarSimLib.Graphics.SimulationDrawer)"> <summary> - Handles scrolling of the mouse wheel. + Sets the simulation drawer. </summary> - <param name="sender">The <see cref="T:SFML.Window.Window"/> that sent the event.</param> - <param name="eventArgs">The <see cref="T:SFML.Window.MouseWheelScrollEventArgs"/> associated with the mouse scroll.</param> + <param name="drawer"></param> </member> <member name="T:StarSimLib.UI.SimulationScreen"> <summary> @@ -1724,11 +1884,6 @@ The input handler to use to provide interactivity to the simulator. </summary> </member> - <member name="F:StarSimLib.UI.SimulationScreen.window"> - <summary> - The SFML.NET window to which everything is rendered. - </summary> - </member> <member name="F:StarSimLib.UI.SimulationScreen.fps"> <summary> The current amount of frames per second. @@ -1739,15 +1894,27 @@ Counts the frames elapsed since the last timer pulse, so that the FPS can be tracked. </summary> </member> - <member name="M:StarSimLib.UI.SimulationScreen.#ctor(StarSimLib.Data_Structures.Body[]@,System.Collections.Generic.Dictionary{StarSimLib.Data_Structures.Body,SFML.Graphics.CircleShape}@,StarSimLib.Physics.UpdateDelegate)"> + <member name="M:StarSimLib.UI.SimulationScreen.#ctor(SFML.Graphics.RenderWindow,StarSimLib.GUI.IInputHandler,StarSimLib.Data_Structures.Body[]@,System.Collections.Generic.Dictionary{StarSimLib.Data_Structures.Body,SFML.Graphics.CircleShape}@,StarSimLib.Physics.UpdateDelegate)"> <summary> - Initialises a new instance of the <see cref="T:StarSimLib.UI.SimulationScreen"/> class, + Initialises a new instance of the <see cref="T:StarSimLib.UI.SimulationScreen"/> class. </summary> + <param name="renderWindow">The render window to which to display this instance.</param> + <param name="inputHandler">The input handler for this instance.</param> + <param name="bodies">The bodies managed by this instance.</param> + <param name="bodyShapeMap">The shapes for the bodies managed by this instance.</param> + <param name="bodyPositionUpdater">The body position update delegate for this instance.</param> </member> - <member name="M:StarSimLib.UI.SimulationScreen.Run"> - <summary> - Runs the screen until it is closed. - </summary> + <member name="M:StarSimLib.UI.SimulationScreen.CleanupScreen(SFML.Graphics.RenderTarget,SFML.Graphics.RenderStates)"> + <inheritdoc /> + </member> + <member name="M:StarSimLib.UI.SimulationScreen.ConstructScreen"> + <inheritdoc /> + </member> + <member name="M:StarSimLib.UI.SimulationScreen.DrawFrame(SFML.Graphics.RenderTarget,SFML.Graphics.RenderStates)"> + <inheritdoc /> + </member> + <member name="M:StarSimLib.UI.SimulationScreen.SetupScreen(SFML.Graphics.RenderTarget,SFML.Graphics.RenderStates)"> + <inheritdoc /> </member> </members> </doc> diff --git a/StarSim/StarSimLib/UI/MainMenuScreen.cs b/StarSim/StarSimLib/UI/MainMenuScreen.cs @@ -0,0 +1,40 @@ +using SFML.Graphics; +using StarSimLib.GUI; + +namespace StarSimLib.UI +{ + /// <summary> + /// Encapsulates the main menu screen. + /// </summary> + public class MainMenuScreen : Screen + { + /// <inheritdoc /> + public MainMenuScreen(RenderWindow window, IInputHandler inputHandler) : base(window, inputHandler) + { + } + + #region Overrides of Screen + + /// <inheritdoc /> + protected override void CleanupScreen(RenderTarget renderTarget, RenderStates renderStates) + { + } + + /// <inheritdoc /> + protected override void ConstructScreen() + { + } + + /// <inheritdoc /> + protected override void DrawFrame(RenderTarget renderTarget, RenderStates renderStates) + { + } + + /// <inheritdoc /> + protected override void SetupScreen(RenderTarget renderTarget, RenderStates renderStates) + { + } + + #endregion Overrides of Screen + } +} +\ No newline at end of file diff --git a/StarSim/StarSimLib/UI/SimulationInputHandler.cs b/StarSim/StarSimLib/UI/SimulationInputHandler.cs @@ -1,14 +1,16 @@ using System; +using SFML.Graphics; using SFML.Window; using StarSimLib.Data_Structures; using StarSimLib.Graphics; +using StarSimLib.GUI; namespace StarSimLib.UI { /// <summary> /// Provides user input handling functions. /// </summary> - public class SimulationInputHandler + public class SimulationInputHandler : IInputHandler { /// <summary> /// Holds all the <see cref="Body"/> instances that should be simulated. @@ -18,7 +20,7 @@ namespace StarSimLib.UI /// <summary> /// The renderer used to display the <see cref="Body"/> instances on the screen. /// </summary> - private readonly SimulationDrawer simulationDrawer; + private SimulationDrawer simulationDrawer; /// <summary> /// Initialises a new instance of the <see cref="SimulationInputHandler"/> class. @@ -26,13 +28,9 @@ namespace StarSimLib.UI /// <param name="bodies"> /// A reference to the <see cref="Body"/> instances which should be managed by this instance. /// </param> - /// <param name="simulationDrawer"> - /// A reference to the renderer used to display the <see cref="Body"/> instances on the screen. - /// </param> - public SimulationInputHandler(ref Body[] bodies, ref SimulationDrawer simulationDrawer) + public SimulationInputHandler(ref Body[] bodies) { managedBodies = bodies; - this.simulationDrawer = simulationDrawer; } /// <summary> @@ -46,11 +44,7 @@ namespace StarSimLib.UI /// </summary> public bool RecordOrbitTracers { get; set; } - /// <summary> - /// Handles key presses. - /// </summary> - /// <param name="sender">The <see cref="Window"/> that sent the event.</param> - /// <param name="eventArgs">The <see cref="KeyEventArgs"/> associated with the key press.</param> + /// <inheritdoc /> public void HandleKeyPressed(object sender, KeyEventArgs eventArgs) { string msg = ""; @@ -138,11 +132,7 @@ namespace StarSimLib.UI } } - /// <summary> - /// Handles key releases. - /// </summary> - /// <param name="sender">The <see cref="Window"/> that sent the event.</param> - /// <param name="eventArgs">The <see cref="KeyEventArgs"/> associated with the key release.</param> + /// <inheritdoc /> public void HandleKeyReleased(object sender, KeyEventArgs eventArgs) { string msg = ""; @@ -159,41 +149,25 @@ namespace StarSimLib.UI } } - /// <summary> - /// Handles motions of the mouse. - /// </summary> - /// <param name="sender">The <see cref="Window"/> that sent the event.</param> - /// <param name="eventArgs">The <see cref="MouseMoveEventArgs"/> associated with the mouse movement.</param> + /// <inheritdoc /> public void HandleMouseMoved(object sender, MouseMoveEventArgs eventArgs) { //Console.WriteLine($"Mouse moved: {eventArgs.X} {eventArgs.Y}"); } - /// <summary> - /// Handles key presses of the mouse. - /// </summary> - /// <param name="sender">The <see cref="Window"/> that sent the event.</param> - /// <param name="eventArgs">The <see cref="MouseButtonEventArgs"/> associated with the mouse press.</param> + /// <inheritdoc /> public void HandleMousePressed(object sender, MouseButtonEventArgs eventArgs) { //Console.WriteLine($"Mouse pressed: {eventArgs.X} {eventArgs.Y}, {eventArgs.Button}"); } - /// <summary> - /// Handles key releases of the mouse. - /// </summary> - /// <param name="sender">The <see cref="Window"/> that sent the event.</param> - /// <param name="eventArgs">The <see cref="MouseButtonEventArgs"/> associated with the mouse release.</param> + /// <inheritdoc /> public void HandleMouseReleased(object sender, MouseButtonEventArgs eventArgs) { //Console.WriteLine($"Mouse released: {eventArgs.X} {eventArgs.Y}, {eventArgs.Button}"); } - /// <summary> - /// Handles scrolling of the mouse wheel. - /// </summary> - /// <param name="sender">The <see cref="Window"/> that sent the event.</param> - /// <param name="eventArgs">The <see cref="MouseWheelScrollEventArgs"/> associated with the mouse scroll.</param> + /// <inheritdoc /> public void HandleMouseScrolled(object sender, MouseWheelScrollEventArgs eventArgs) { if (eventArgs.Delta > 0) @@ -207,5 +181,20 @@ namespace StarSimLib.UI Console.WriteLine($"Current field of view (zoom level): {simulationDrawer.FOV} ({simulationDrawer.ZoomLevel})"); } + + /// <inheritdoc /> + public void HandleScreenClosed(object sender, EventArgs eventArgs) + { + ((RenderWindow)sender).Close(); + } + + /// <summary> + /// Sets the simulation drawer. + /// </summary> + /// <param name="drawer"></param> + public void SetSimulationDrawer(SimulationDrawer drawer) + { + simulationDrawer = drawer; + } } } \ No newline at end of file diff --git a/StarSim/StarSimLib/UI/SimulationScreen.cs b/StarSim/StarSimLib/UI/SimulationScreen.cs @@ -1,12 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; using System.Timers; using SFML.Graphics; -using SFML.Window; -using StarSimLib.Contexts; using StarSimLib.Data_Structures; using StarSimLib.Graphics; +using StarSimLib.GUI; using StarSimLib.Physics; namespace StarSimLib.UI @@ -14,7 +11,7 @@ namespace StarSimLib.UI /// <summary> /// Encapsulates the simulation. /// </summary> - public class SimulationScreen + public class SimulationScreen : Screen { /// <summary> /// The interval between timer refreshes, in milliseconds. @@ -53,11 +50,6 @@ namespace StarSimLib.UI private readonly SimulationInputHandler simulationInputHandler; /// <summary> - /// The SFML.NET window to which everything is rendered. - /// </summary> - private readonly RenderWindow window; - - /// <summary> /// The current amount of frames per second. /// </summary> private double fps; @@ -68,21 +60,24 @@ namespace StarSimLib.UI private uint framesElapsed; /// <summary> - /// Initialises a new instance of the <see cref="SimulationScreen"/> class, + /// Initialises a new instance of the <see cref="SimulationScreen"/> class. /// </summary> - public SimulationScreen(ref Body[] bodies, ref Dictionary<Body, CircleShape> bodyShapeMap, UpdateDelegate bodyPositionUpdater) + /// <param name="renderWindow">The render window to which to display this instance.</param> + /// <param name="inputHandler">The input handler for this instance.</param> + /// <param name="bodies">The bodies managed by this instance.</param> + /// <param name="bodyShapeMap">The shapes for the bodies managed by this instance.</param> + /// <param name="bodyPositionUpdater">The body position update delegate for this instance.</param> + public SimulationScreen(RenderWindow renderWindow, IInputHandler inputHandler, ref Body[] bodies, + ref Dictionary<Body, CircleShape> bodyShapeMap, UpdateDelegate bodyPositionUpdater) : base(renderWindow, inputHandler) { - // we construct a new window instance, but immediately hide it so that we can configure the rest of the app - window = new RenderWindow(VideoMode.DesktopMode, "N-Body Simulation: FPS ", Styles.Default, new ContextSettings()); - window.SetVisible(false); - this.bodies = bodies; this.bodyShapeMap = bodyShapeMap; this.bodyPositionUpdater = bodyPositionUpdater; - simulationDrawer = new SimulationDrawer(window, ref bodies, ref bodyShapeMap); - simulationInputHandler = new SimulationInputHandler(ref bodies, ref simulationDrawer); + simulationDrawer = new SimulationDrawer(renderWindow, ref bodies, ref bodyShapeMap); + simulationInputHandler = (SimulationInputHandler)inputHandler; + simulationInputHandler.SetSimulationDrawer(simulationDrawer); // constructs a new timer and attaches a timer event handler that updates the fps and window title every interval miscTimer = new Timer(TimerRefreshIntervalMs) { AutoReset = true, Enabled = true }; @@ -91,52 +86,45 @@ namespace StarSimLib.UI fps = framesElapsed / (TimerRefreshIntervalMs / 1000); framesElapsed = 0; - window.SetTitle($"N-Body Simulator: FPS {fps}"); + renderWindow.SetTitle($"N-Body Simulator: FPS {fps}"); }; } - /// <summary> - /// Runs the screen until it is closed. - /// </summary> - public void Run() - { - // we reveal the window so that the user may interact with our program - window.SetVisible(true); - miscTimer.Start(); - - // we configure the window and its close handler, so that we may close the window once we are done with it - window.SetFramerateLimit(Constants.FrameRate); - window.Closed += (sender, eventArgs) => ((RenderWindow)sender).Close(); + #region Overrides of Screen - // we apply event handlers to allow for interactivity inside the window - window.KeyPressed += simulationInputHandler.HandleKeyPressed; - window.KeyReleased += simulationInputHandler.HandleKeyReleased; - window.MouseButtonPressed += simulationInputHandler.HandleMousePressed; - window.MouseButtonReleased += simulationInputHandler.HandleMouseReleased; - window.MouseMoved += simulationInputHandler.HandleMouseMoved; - window.MouseWheelScrolled += simulationInputHandler.HandleMouseScrolled; + /// <inheritdoc /> + protected override void CleanupScreen(RenderTarget renderTarget, RenderStates renderStates) + { + miscTimer.Stop(); + } - simulationDrawer.DrawBodies(); + /// <inheritdoc /> + protected override void ConstructScreen() + { + // cap the frame rate of the screen to limit the speed of the simulation + renderWindow.SetFramerateLimit(Constants.FrameRate); + } - while (window.IsOpen) + /// <inheritdoc /> + protected override void DrawFrame(RenderTarget renderTarget, RenderStates renderStates) + { + if (!simulationInputHandler.IsSimulationPaused) { - window.Clear(); - window.DispatchEvents(); - - if (!simulationInputHandler.IsSimulationPaused) - { - bodyPositionUpdater(bodies, Constants.TimeStep); - } - - simulationDrawer.DrawBodies(); + bodyPositionUpdater(bodies, Constants.TimeStep); + } - window.Display(); + simulationDrawer.DrawBodies(); - // increment the fps counter - framesElapsed++; - } + // increment the fps counter + framesElapsed++; + } - miscTimer.Stop(); + /// <inheritdoc /> + protected override void SetupScreen(RenderTarget renderTarget, RenderStates renderStates) + { + miscTimer.Start(); } + + #endregion Overrides of Screen } } \ No newline at end of file