Skip to content

Commit

Permalink
346 add a gui controller (#347)
Browse files Browse the repository at this point in the history
* Add a GUI controller. Not sure about referencing the UI abstraction project from the domain project...

* Get rid of duplicate IGuiController and FrEee.UI project

* Use GuiController in place of Gui

* Use GuiController for Blazor history log
  • Loading branch information
ekolis authored Dec 22, 2024
1 parent d5c4a7d commit 06b5c67
Show file tree
Hide file tree
Showing 15 changed files with 315 additions and 97 deletions.
5 changes: 5 additions & 0 deletions FrEee.Core.Domain/Utility/DIRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,9 @@ public static class DIRoot
/// Creates construction queues.
/// </summary>
public static IConstructionQueueFactory ConstructionQueues => DI.Get<IConstructionQueueFactory>();

/// <summary>
/// Manages the GUI. Only available when there is a GUI.
/// </summary>
public static IGuiController Gui => DI.Get<IGuiController>();
}
79 changes: 79 additions & 0 deletions FrEee.Core.Domain/Utility/IGuiController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using FrEee.Objects.Civilization.Diplomacy.Messages;
using FrEee.Objects.Space;
using FrEee.Objects.Technology;
using FrEee.Processes.Combat;
using FrEee.Vehicles;

namespace FrEee.Utility;

public interface IGuiController
{
/// <summary>
/// Shows a particular screen.
/// </summary>
/// <param name="screen"></param>
void Show(Screen screen);

/// <summary>
/// Hides a particular screen.
/// </summary>
/// <param name="screen"></param>
void Hide(Screen screen);

/// <summary>
/// Closes a particular screen.
/// </summary>
/// <param name="screen"></param>
void Close(Screen screen);

/// <summary>
/// Exits the game.
/// </summary>
void Exit();

/// <summary>
/// Closes all windows except the main menu. Shows the main menu.
/// </summary>
void ReturnToMainMenu();

/// <summary>
/// Exits or returns to the main menu, per the player's preference.
/// </summary>
void CloseGame();

/// <summary>
/// Focuses a space object on the map.
/// </summary>
/// <param name="sobj"></param>
void Focus(ISpaceObject context);

/// <summary>
/// Focuses a star system on the map.
/// </summary>
/// <param name="sobj"></param>
void Focus(StarSystem context);

/// <summary>
/// Focuses a technology on the research screen.
/// </summary>
/// <param name="sobj"></param>
void Focus(Technology context);

/// <summary>
/// Focuses a hull on the design screen.
/// </summary>
/// <param name="sobj"></param>
void Focus(IHull context);

/// <summary>
/// Focuses a battle on the battle summary screen.
/// </summary>
/// <param name="sobj"></param>
void Focus(IBattle context);

/// <summary>
/// Focuses a message on the diplomacy screen.
/// </summary>
/// <param name="sobj"></param>
void Focus(IMessage context);
}
53 changes: 53 additions & 0 deletions FrEee.Core.Domain/Utility/Screen.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FrEee.Utility;

/// <summary>
/// The various screens in the UI.
/// </summary>
public enum Screen
{
ActivateAbility,
BattleReplay,
BattleResults,
BlazorTest,
CargoTransfer,
CombatSimulator,
Commands,
ConstructionQueue,
ConstructionQueueList,
CultureComparison,
Debug,
DesignList,
Diplomacy,
Editor,
EmpireList,
EmpireSetup,
FleetTransfer,
GameOver,
GameSetup,
HostConsole,
HullPicker,
Log,
MainGame,
MainMenu,
Ministers,
ModErrors,
ModPicker,
MountPicker,
Options,
PlanetList,
Recycle,
Research,
Scores,
SearchBoxResults,
ShipList,
SpaceObjectPicker,
Status,
TechTree,
VehicleDesign
}
5 changes: 4 additions & 1 deletion FrEee.Root/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static class Configuration
/// <summary>
/// Sets up any dependencies which need to be injected.
/// </summary>
public static void ConfigureDI()
public static void ConfigureDI(Action additionlConfig = null)
{
// TODO: load dependencies from configuration file in mod data so we can really modularize this thing!

Expand All @@ -55,6 +55,9 @@ public static void ConfigureDI()
DI.RegisterSingleton<IDesignFactory, DesignFactory>();
DI.RegisterSingleton<IVehicleFactory, VehicleFactory>();

// addtional configuration for the GUI or whatever
additionlConfig?.Invoke();

// run this in the background, without awaiting it
DI.Run();
}
Expand Down
1 change: 1 addition & 0 deletions FrEee.UI.Blazor/FrEee.UI.Blazor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<Import Project="../FrEee.Assets/CommonProjectProperties.xml" />

<ItemGroup>
<SupportedPlatform Include="browser" />
Expand Down
33 changes: 11 additions & 22 deletions FrEee.UI.Blazor/Views/HistoryLogViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using FrEee.Vehicles.Types;
using FrEee.Vehicles;
using FrEee.Extensions;
using FrEee.Utility;

namespace FrEee.UI.Blazor.Views;

Expand All @@ -39,7 +40,7 @@ public void Navigate(LogMessage message)
if (context is ISpaceObject sobj)
{
// go to space object
OnSpaceObjectSelected(this, sobj);
DIRoot.Gui.Focus(sobj);
}
else if (context is IUnit unit)
{
Expand All @@ -49,49 +50,49 @@ public void Navigate(LogMessage message)
{
if (container is Sector)
{
OnSpaceObjectSelected(this, (ISpaceObject)unit);
DIRoot.Gui.Focus((ISpaceObject)unit);
}
else
{
OnSpaceObjectSelected(this, (ISpaceObject)container);
DIRoot.Gui.Focus((ISpaceObject)container);
}
}
}
else if (context is Facility facility)
{
// go to the planet
var container = facility.Container;
OnSpaceObjectSelected(this, container);
DIRoot.Gui.Focus(container);
}
else if (context is Technology tech)
{
// go to research screen
OnTechnologySelected(this, tech);
DIRoot.Gui.Focus(tech);
}
else if (context is IHull hull)
{
// go to design screen and create a new design using this hull
OnHullSelected(this, hull);
DIRoot.Gui.Focus(hull);
}
else if (context is ComponentTemplate || context is Mount)
{
// go to design screen
OnHullSelected(this, null);
DIRoot.Gui.Show(Screen.VehicleDesign);
}
else if (context is IBattle battle)
{
// show battle results
OnBattleSelected(this, battle);
DIRoot.Gui.Focus(battle);
}
else if (context is IMessage msg)
{
// show diplomacy screen
OnMessageSelected(this, msg);
DIRoot.Gui.Focus(msg);
}
else if (context is StarSystem sys)
{
// navigate game form to that system
OnStarSystemSelected(this, sys);
DIRoot.Gui.Focus(sys);
}

// TODO - more types of goto-messages
Expand All @@ -115,16 +116,4 @@ public bool CanNavigate(LogMessage message)
}
return false;
}

public event EventHandler<ISpaceObject> OnSpaceObjectSelected = (sender, e) => { };

public event EventHandler<Technology> OnTechnologySelected = (sender, e) => { };

public event EventHandler<IHull?> OnHullSelected = (sender, e) => { };

public event EventHandler<IBattle> OnBattleSelected = (sender, e) => { };

public event EventHandler<IMessage> OnMessageSelected = (sender, e) => { };

public event EventHandler<StarSystem> OnStarSystemSelected = (sender, e) => { };
}
16 changes: 1 addition & 15 deletions FrEee.UI.WinForms/Forms/LogForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,11 @@ public LogForm(MainGameForm mainGameForm, IList<LogMessage> log)
ShowInTaskbar = !mainGameForm.HasLogBeenShown;

VM.Messages = log;
VM.OnBattleSelected += VM_OnBattleSelected;
VM.OnHullSelected += VM_OnHullSelected;
VM.OnMessageSelected += VM_OnMessageSelected;
VM.OnSpaceObjectSelected += VM_OnSpaceObjectSelected;
VM.OnStarSystemSelected += VM_OnStarSystemSelected;
VM.OnTechnologySelected += VM_OnTechnologySelected;
}

private void VM_OnTechnologySelected(object? sender, Technology e)
{
// go to research screen
mainGameForm.ShowResearchForm();
Close();
}

private void VM_OnStarSystemSelected(object? sender, StarSystem e)
{
mainGameForm.SelectStarSystem(e);
Close();

}

private void VM_OnSpaceObjectSelected(object? sender, ISpaceObject e)
Expand Down
2 changes: 1 addition & 1 deletion FrEee.UI.WinForms/Forms/MainGameForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ private void GameForm_FormClosed(object sender, FormClosedEventArgs e)
{
Instance = null;
if (QuitOnClose)
Gui.CloseGame();
DIRoot.Gui.CloseGame();
}

private void GameForm_FormClosing(object sender, FormClosingEventArgs e)
Expand Down
4 changes: 2 additions & 2 deletions FrEee.UI.WinForms/Forms/MainMenuForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private void LoadGalaxyFromFile(string filename, bool? loadPlr = null)

private void btnQuit_Click(object sender, EventArgs e)
{
Gui.Exit();
DIRoot.Gui.Exit();
}

private void btnMods_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -315,6 +315,6 @@ private void MainMenuForm_Load(object sender, EventArgs e)
private void MainMenuForm_FormClosed(object sender, FormClosedEventArgs e)
{
ClientSettings.Save();
Gui.Exit();
DIRoot.Gui.Exit();
}
}
7 changes: 5 additions & 2 deletions FrEee.UI.WinForms/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ void Log(Exception ex)
};

// set up dependency injection
Configuration.ConfigureDI();
Configuration.ConfigureDI(() =>
{
DI.RegisterSingleton<IGuiController, GuiController>();
});

// HACK - so many things are based on the working directory...
Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
Expand Down Expand Up @@ -296,7 +299,7 @@ private static int ProcessTurn(bool safe)
}
Game.SaveAll();
Console.WriteLine("Turn processed successfully. It is now turn " + Game.Current.TurnNumber + " (stardate " + Game.Current.Stardate + ").");
Gui.Exit();
DIRoot.Gui.Exit();
return 0;
}
catch (Exception ex)
Expand Down
9 changes: 5 additions & 4 deletions FrEee.UI.WinForms/Utility/Extensions/GuiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Windows.Forms;
using Microsoft.AspNetCore.Components.WebView.WindowsForms;
using Microsoft.Extensions.DependencyInjection;
using WFScreen = System.Windows.Forms.Screen;

using FrEee.Gameplay;
using FrEee.Gameplay.Commands.Fleets;
Expand Down Expand Up @@ -100,10 +101,10 @@ public static Form CreatePopupForm(this Control control, string text = "")
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.ClientSize = control.Size;
// TODO - deal with multiple screens
if (form.Width > Screen.PrimaryScreen.WorkingArea.Width)
form.Width = Screen.PrimaryScreen.WorkingArea.Width;
if (form.Height > Screen.PrimaryScreen.WorkingArea.Height)
form.Height = Screen.PrimaryScreen.WorkingArea.Height;
if (form.Width > WFScreen.PrimaryScreen.WorkingArea.Width)
form.Width = WFScreen.PrimaryScreen.WorkingArea.Width;
if (form.Height > WFScreen.PrimaryScreen.WorkingArea.Height)
form.Height = WFScreen.PrimaryScreen.WorkingArea.Height;
form.StartPosition = FormStartPosition.CenterParent;
form.Controls.Add(control);
form.KeyPreview = true;
Expand Down
Loading

0 comments on commit 06b5c67

Please sign in to comment.