StarSim

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

commit 99c788db2b7a10da0b98a9771d7250752e332717
parent a129cdcbaf33b6c3fdbfbf730210f627e0fef414
Author: Mikolaj Lenczewski <33129490+EnderRifter@users.noreply.github.com>
Date:   Mon, 15 Jul 2019 14:19:15 +0100

Started work on component system.

Diffstat:
MStarSim/StarSim/Program.cs | 28+++++++++++++---------------
MStarSim/StarSim/StarSim.csproj | 6++++++
AStarSim/StarSim/resources/chessboard.jpg | 0
MStarSim/StarSimLib/GUI/Components/Component.cs | 20++++----------------
MStarSim/StarSimLib/GUI/Components/Image.cs | 2+-
MStarSim/StarSimLib/GUI/IInputHandler.cs | 5+++++
MStarSim/StarSimLib/GUI/Screen.cs | 7++++---
MStarSim/StarSimLib/StarSimLib.xml | 45++++++++++++++++++++++++++++++---------------
AStarSim/StarSimLib/UI/MainMenuInputHandler.cs | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
MStarSim/StarSimLib/UI/MainMenuScreen.cs | 34+++++++++++++++++++++++++++++++++-
MStarSim/StarSimLib/UI/SimulationInputHandler.cs | 8+++++---
11 files changed, 153 insertions(+), 54 deletions(-)

diff --git a/StarSim/StarSim/Program.cs b/StarSim/StarSim/Program.cs @@ -1,18 +1,12 @@ using System; using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Reflection.Metadata.Ecma335; -using System.Security.Cryptography; using System.Text; -using System.Timers; using SFML.Graphics; using SFML.Window; using StarSimLib; using StarSimLib.Contexts; using StarSimLib.Cryptography; using StarSimLib.Data_Structures; -using StarSimLib.Graphics; using StarSimLib.GUI; using StarSimLib.Physics; using StarSimLib.UI; @@ -41,14 +35,14 @@ namespace StarSim private static readonly SimulatorContext databaseContext; /// <summary> - /// The simulation which we will render, once the user sets it up. + /// The main menu from which we will start other screens. /// </summary> - private static readonly SimulationScreen simulationScreen; + private static readonly MainMenuScreen mainMenuScreen; /// <summary> - /// The SFML.NET window to which everything is rendered. + /// The simulation which we will render, once the user sets it up. /// </summary> - private static readonly RenderWindow window; + private static readonly SimulationScreen simulationScreen; /// <summary> /// Initialises a new instance of the <see cref="Program"/> class, @@ -58,10 +52,6 @@ namespace StarSim // set up the database context for the program databaseContext = new SimulatorContext(); - // 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 Simulator: FPS ", Styles.Default, new ContextSettings()); - window.SetVisible(false); - bodies = BodyGenerator.GenerateBodies(Constants.BodyCount, true); bodyShapeMap = BodyGenerator.GenerateShapes(bodies); @@ -71,6 +61,11 @@ namespace StarSim UpdateDelegate bodyPositionUpdater = BodyUpdater.UpdateBodiesBarnesHut; #endif + RenderWindow mainMenuWindow = new RenderWindow(VideoMode.DesktopMode, "N-Body Simulation: Main Menu"); + IInputHandler mainMenuInputHandler = new MainMenuInputHandler(); + + mainMenuScreen = new MainMenuScreen(mainMenuWindow, mainMenuInputHandler); + RenderWindow simulationWindow = new RenderWindow(VideoMode.DesktopMode, "N-Body Simulation: FPS "); IInputHandler simulationInputHandler = new SimulationInputHandler(ref bodies); @@ -113,8 +108,11 @@ namespace StarSim Console.WriteLine("Press 'enter' to continue..."); Console.ReadLine(); + // run main menu + mainMenuScreen.Run(); + // run simulation - simulationScreen.Run(); + //simulationScreen.Run(); Console.WriteLine("Goodbye, World!"); Console.WriteLine("Press 'enter' to quit..."); diff --git a/StarSim/StarSim/StarSim.csproj b/StarSim/StarSim/StarSim.csproj @@ -22,4 +22,10 @@ <ItemGroup> <ProjectReference Include="..\StarSimLib\StarSimLib.csproj" /> </ItemGroup> + + <ItemGroup> + <None Update="resources\chessboard.jpg"> + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> + </None> + </ItemGroup> </Project> \ No newline at end of file diff --git a/StarSim/StarSim/resources/chessboard.jpg b/StarSim/StarSim/resources/chessboard.jpg Binary files differ. diff --git a/StarSim/StarSimLib/GUI/Components/Component.cs b/StarSim/StarSimLib/GUI/Components/Component.cs @@ -14,26 +14,14 @@ namespace StarSimLib.GUI.Components 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) + /// <param name="sprite">The sprite that should represent this instance.</param> + protected Component(Vector2f position, Sprite sprite) { - intRect = new IntRect(position, size); - this.texture = texture; - sprite = new Sprite(this.texture, intRect); + this.sprite = sprite; + this.sprite.Position = position; } /// <summary> diff --git a/StarSim/StarSimLib/GUI/Components/Image.cs b/StarSim/StarSimLib/GUI/Components/Image.cs @@ -9,7 +9,7 @@ namespace StarSimLib.GUI.Components public class Image : Component { /// <inheritdoc /> - public Image(Vector2i position, Vector2i size, Texture texture) : base(position, size, texture) + public Image(Vector2f position, Sprite sprite) : base(position, sprite) { } } diff --git a/StarSim/StarSimLib/GUI/IInputHandler.cs b/StarSim/StarSimLib/GUI/IInputHandler.cs @@ -9,6 +9,11 @@ namespace StarSimLib.GUI public interface IInputHandler { /// <summary> + /// The <see cref="Screen"/> instance whose input to manage. + /// </summary> + Screen HandledScreen { get; set; } + + /// <summary> /// Handles key presses. /// </summary> /// <param name="sender">The <see cref="Window"/> that sent the event.</param> diff --git a/StarSim/StarSimLib/GUI/Screen.cs b/StarSim/StarSimLib/GUI/Screen.cs @@ -1,4 +1,5 @@ -using SFML.Graphics; +using System.Threading.Tasks; +using SFML.Graphics; namespace StarSimLib.GUI { @@ -27,8 +28,7 @@ namespace StarSimLib.GUI renderWindow = window; this.inputHandler = inputHandler; - - renderWindow.Closed += (sender, eventArgs) => ((RenderWindow)sender).Close(); + inputHandler.HandledScreen = this; // we apply event handlers to allow for interactivity inside the window renderWindow.KeyPressed += this.inputHandler.HandleKeyPressed; @@ -37,6 +37,7 @@ namespace StarSimLib.GUI renderWindow.MouseButtonReleased += this.inputHandler.HandleMouseReleased; renderWindow.MouseMoved += this.inputHandler.HandleMouseMoved; renderWindow.MouseWheelScrolled += this.inputHandler.HandleMouseScrolled; + renderWindow.Closed += this.inputHandler.HandleScreenClosed; renderWindow.SetVisible(false); diff --git a/StarSim/StarSimLib/StarSimLib.xml b/StarSim/StarSimLib/StarSimLib.xml @@ -1303,23 +1303,12 @@ 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)"> + <member name="M:StarSimLib.GUI.Components.Component.#ctor(SFML.System.Vector2f,SFML.Graphics.Sprite)"> <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> + <param name="sprite">The sprite that should represent this instance.</param> </member> <member name="P:StarSimLib.GUI.Components.Component.Position"> <summary> @@ -1341,7 +1330,7 @@ Represents an image. </summary> </member> - <member name="M:StarSimLib.GUI.Components.Image.#ctor(SFML.System.Vector2i,SFML.System.Vector2i,SFML.Graphics.Texture)"> + <member name="M:StarSimLib.GUI.Components.Image.#ctor(SFML.System.Vector2f,SFML.Graphics.Sprite)"> <inheritdoc /> </member> <member name="T:StarSimLib.GUI.IInputHandler"> @@ -1762,6 +1751,32 @@ </summary> <returns>A 3D position vector.</returns> </member> + <member name="T:StarSimLib.UI.MainMenuInputHandler"> + <summary> + Provides user input handling functions for the main menu. + </summary> + </member> + <member name="M:StarSimLib.UI.MainMenuInputHandler.HandleKeyPressed(System.Object,SFML.Window.KeyEventArgs)"> + <inheritdoc /> + </member> + <member name="M:StarSimLib.UI.MainMenuInputHandler.HandleKeyReleased(System.Object,SFML.Window.KeyEventArgs)"> + <inheritdoc /> + </member> + <member name="M:StarSimLib.UI.MainMenuInputHandler.HandleMouseMoved(System.Object,SFML.Window.MouseMoveEventArgs)"> + <inheritdoc /> + </member> + <member name="M:StarSimLib.UI.MainMenuInputHandler.HandleMousePressed(System.Object,SFML.Window.MouseButtonEventArgs)"> + <inheritdoc /> + </member> + <member name="M:StarSimLib.UI.MainMenuInputHandler.HandleMouseReleased(System.Object,SFML.Window.MouseButtonEventArgs)"> + <inheritdoc /> + </member> + <member name="M:StarSimLib.UI.MainMenuInputHandler.HandleMouseScrolled(System.Object,SFML.Window.MouseWheelScrollEventArgs)"> + <inheritdoc /> + </member> + <member name="M:StarSimLib.UI.MainMenuInputHandler.HandleScreenClosed(System.Object,System.EventArgs)"> + <inheritdoc /> + </member> <member name="T:StarSimLib.UI.MainMenuScreen"> <summary> Encapsulates the main menu screen. @@ -1784,7 +1799,7 @@ </member> <member name="T:StarSimLib.UI.SimulationInputHandler"> <summary> - Provides user input handling functions. + Provides user input handling functions for the simulation. </summary> </member> <member name="F:StarSimLib.UI.SimulationInputHandler.managedBodies"> diff --git a/StarSim/StarSimLib/UI/MainMenuInputHandler.cs b/StarSim/StarSimLib/UI/MainMenuInputHandler.cs @@ -0,0 +1,51 @@ +using System; +using SFML.Window; +using StarSimLib.GUI; + +namespace StarSimLib.UI +{ + /// <summary> + /// Provides user input handling functions for the main menu. + /// </summary> + public class MainMenuInputHandler : IInputHandler + { + /// <inheritdoc /> + public Screen HandledScreen { get; set; } + + /// <inheritdoc /> + public void HandleKeyPressed(object sender, KeyEventArgs eventArgs) + { + } + + /// <inheritdoc /> + public void HandleKeyReleased(object sender, KeyEventArgs eventArgs) + { + } + + /// <inheritdoc /> + public void HandleMouseMoved(object sender, MouseMoveEventArgs eventArgs) + { + } + + /// <inheritdoc /> + public void HandleMousePressed(object sender, MouseButtonEventArgs eventArgs) + { + } + + /// <inheritdoc /> + public void HandleMouseReleased(object sender, MouseButtonEventArgs eventArgs) + { + } + + /// <inheritdoc /> + public void HandleMouseScrolled(object sender, MouseWheelScrollEventArgs eventArgs) + { + } + + /// <inheritdoc /> + public void HandleScreenClosed(object sender, EventArgs eventArgs) + { + ((Window)sender).Close(); + } + } +} +\ No newline at end of file diff --git a/StarSim/StarSimLib/UI/MainMenuScreen.cs b/StarSim/StarSimLib/UI/MainMenuScreen.cs @@ -1,5 +1,8 @@ -using SFML.Graphics; +using System.Collections.Generic; +using SFML.Graphics; +using SFML.System; using StarSimLib.GUI; +using StarSimLib.GUI.Components; namespace StarSimLib.UI { @@ -8,9 +11,34 @@ namespace StarSimLib.UI /// </summary> public class MainMenuScreen : Screen { + private readonly List<Component> components; + /// <inheritdoc /> public MainMenuScreen(RenderWindow window, IInputHandler inputHandler) : base(window, inputHandler) { + components = new List<Component> + { + new GUI.Components.Image(new Vector2f(10, 10), new Sprite( + new Texture(@"resources/chessboard.jpg", + new IntRect(new Vector2i(100, 100), new Vector2i(800, 800))), + new IntRect(new Vector2i(10, 10), new Vector2i(80, 80)))), + new GUI.Components.Image(new Vector2f(200, 10), new Sprite( + new Texture(@"resources/chessboard.jpg", + new IntRect(new Vector2i(100, 100), new Vector2i(800, 800))), + new IntRect(new Vector2i(10, 10), new Vector2i(80, 80)))), + new GUI.Components.Image(new Vector2f(10, 200), new Sprite( + new Texture(@"resources/chessboard.jpg", + new IntRect(new Vector2i(100, 100), new Vector2i(800, 800))), + new IntRect(new Vector2i(10, 10), new Vector2i(80, 80)))), + new GUI.Components.Image(new Vector2f(200, 200), new Sprite( + new Texture(@"resources/chessboard.jpg", + new IntRect(new Vector2i(100, 100), new Vector2i(800, 800))), + new IntRect(new Vector2i(10, 10), new Vector2i(80, 80)))), + new GUI.Components.Image(new Vector2f(400, 400), new Sprite( + new Texture(@"resources/chessboard.jpg", + new IntRect(new Vector2i(100, 100), new Vector2i(800, 800))), + new IntRect(new Vector2i(10, 10), new Vector2i(80, 80)))) + }; } #region Overrides of Screen @@ -28,6 +56,10 @@ namespace StarSimLib.UI /// <inheritdoc /> protected override void DrawFrame(RenderTarget renderTarget, RenderStates renderStates) { + foreach (Component component in components) + { + component.Draw(renderTarget, renderStates); + } } /// <inheritdoc /> diff --git a/StarSim/StarSimLib/UI/SimulationInputHandler.cs b/StarSim/StarSimLib/UI/SimulationInputHandler.cs @@ -1,5 +1,4 @@ using System; -using SFML.Graphics; using SFML.Window; using StarSimLib.Data_Structures; using StarSimLib.Graphics; @@ -8,7 +7,7 @@ using StarSimLib.GUI; namespace StarSimLib.UI { /// <summary> - /// Provides user input handling functions. + /// Provides user input handling functions for the simulation. /// </summary> public class SimulationInputHandler : IInputHandler { @@ -33,6 +32,9 @@ namespace StarSimLib.UI managedBodies = bodies; } + /// <inheritdoc /> + public Screen HandledScreen { get; set; } + /// <summary> /// Whether the simulation is paused at any given time. /// </summary> @@ -185,7 +187,7 @@ namespace StarSimLib.UI /// <inheritdoc /> public void HandleScreenClosed(object sender, EventArgs eventArgs) { - ((RenderWindow)sender).Close(); + ((Window)sender).Close(); } /// <summary>