Skip to content

Commit

Permalink
refactor: game entry class
Browse files Browse the repository at this point in the history
  • Loading branch information
cherrynik committed Nov 22, 2023
1 parent 0844e66 commit b91d540
Showing 1 changed file with 54 additions and 36 deletions.
90 changes: 54 additions & 36 deletions src/Apps/GameDesktop/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,11 @@ public Game(ILogger logger, IServiceContainer container)
protected override void Initialize()
{
_logger.ForContext<Game>().Verbose($"Initialize(): start; available {GraphicsDevice}");
_logger.ForContext<Game>().Warning("Circular dependencies initialization...");
_logger.ForContext<Game>().Verbose("Circular dependencies initialization...");

RegisterSpriteBatch();
RegisterMyraUI();
#if DEBUG
RegisterImGui();
#endif
RegisterOnInitialize();

_logger.ForContext<Game>().Warning("Circular dependencies initialized");
_logger.ForContext<Game>().Verbose("Circular dependencies initialized");

base.Initialize();

Expand All @@ -63,33 +59,7 @@ protected override void LoadContent()
// TODO: Error handling
_logger.ForContext<Game>().Verbose("LoadContent(): start");

_container.RegisterFrom<RootFeatureCompositionRoot>();

// ComboBox
var combo = new ComboBox();
combo.Items.Add(new ListItem("Red", Color.Red));
combo.Items.Add(new ListItem("Green", Color.Green));
combo.Items.Add(new ListItem("Blue", Color.Blue));

// Button
var button = new Button { Content = new Label { Text = "Show" } };
button.Click += (s, a) =>
{
var messageBox = Dialog.CreateMessageBox("Message", "Some message!");
messageBox.ShowModal(_desktop);
};

var grid = _container.GetInstance<Grid>();
new UIFactory(grid,
new Label { Id = "label", Text = "Hello, World!" },
combo,
button,
new SpinButton { Width = 100, Nullable = true })
.Build();

_desktop = _container.GetInstance<Desktop>();

_rootFeature = _container.GetInstance<RootFeature>();
RegisterOnLoadContent();

_rootFeature.OnAwake();

Expand Down Expand Up @@ -149,15 +119,30 @@ protected override void Dispose(bool disposing)
_logger.ForContext<Game>().Verbose("Disposed");
}

private void RegisterOnInitialize()
{
RegisterSpriteBatch();
RegisterMyra();
#if DEBUG
RegisterImGuiRenderer();
#endif
}

private void RegisterOnLoadContent()
{
RegisterRootFeature();
RegisterUI();
}

private void RegisterSpriteBatch()
{
_container.RegisterSingleton(_ => new SpriteBatch(GraphicsDevice));
_spriteBatch = _container.GetInstance<SpriteBatch>();
}

private void RegisterMyraUI() => MyraEnvironment.Game = this;
private void RegisterMyra() => MyraEnvironment.Game = this;

private void RegisterImGui()
private void RegisterImGuiRenderer()
{
_container.RegisterSingleton(factory =>
{
Expand All @@ -170,4 +155,37 @@ private void RegisterImGui()

ImGui.GetIO().ConfigFlags = ImGuiConfigFlags.DockingEnable;
}

private void RegisterRootFeature()
{
_container.RegisterFrom<RootFeatureCompositionRoot>();
_rootFeature = _container.GetInstance<RootFeature>();
}

private void RegisterUI()
{
// ComboBox
var combo = new ComboBox();
combo.Items.Add(new ListItem("Red", Color.Red));
combo.Items.Add(new ListItem("Green", Color.Green));
combo.Items.Add(new ListItem("Blue", Color.Blue));

// Button
var button = new Button { Content = new Label { Text = "Show" } };
button.Click += (s, a) =>
{
var messageBox = Dialog.CreateMessageBox("Message", "Some message!");
messageBox.ShowModal(_desktop);
};

var grid = _container.GetInstance<Grid>();
new UIFactory(grid,
new Label { Id = "label", Text = "Hello, World!" },
combo,
button,
new SpinButton { Width = 100, Nullable = true })
.Build();

_desktop = _container.GetInstance<Desktop>();
}
}

0 comments on commit b91d540

Please sign in to comment.