Skip to content

Commit

Permalink
feat(ui): inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
cherrynik committed Mar 1, 2024
1 parent 148117c commit c9f90b4
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ private static void RegisterInventoryComponent(IServiceRegistry serviceRegistry)
Slot[] slots = new Slot[count];

// Put items in slots like that:
// slots[0].Item = ItemId.Rock;
slots[3].Item = ItemId.Rock;
slots[3].Amount = 3;

return new InventoryComponent(slots);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ private static void RegisterEntryPoint(IServiceRegistry serviceRegistry)

// UI
serviceRegistry.RegisterSingleton(_ => new Panel());
serviceRegistry.RegisterSingleton(factory =>
{
var filter = factory.GetInstance<World>().Filter.With<InventoryComponent>().Build();
ref var inventory = ref filter.First().GetComponent<InventoryComponent>();
return new Inventory(factory.GetInstance<Panel>(), ref inventory);
});
serviceRegistry.RegisterSingleton(factory =>
new Counter(factory.GetInstance<Panel>(),
new Label
Expand Down Expand Up @@ -175,6 +181,7 @@ private static void RegisterEntryPoint(IServiceRegistry serviceRegistry)
Desktop desktop = new();
desktop.Root = factory.GetInstance<Panel>();

factory.GetInstance<Inventory>();
factory.GetInstance<MainScreen>();

return desktop;
Expand Down
54 changes: 16 additions & 38 deletions src/Apps/GameDesktop/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,10 @@ protected override void Initialize()
_logger.ForContext<Game>().Verbose($"Initialize(): start; available {GraphicsDevice}");
_logger.ForContext<Game>().Verbose("Circular dependencies (external) initialization...");
RegisterSpriteBatch();
RegisterMyraUIEnvironment();
#if DEBUG
RegisterImGuiRenderer();
#endif
_logger.ForContext<Game>().Verbose("Circular dependencies (external) initialized");

_logger.ForContext<Game>().Verbose("Game services initialization...");
RegisterRootFeature();
RegisterMyraUI();
_logger.ForContext<Game>().Verbose("Game services initialized");

base.Initialize();
Expand All @@ -68,6 +63,15 @@ protected override void LoadContent()

_rootFeature.OnAwake();

// Register UIs after systems initialization, because they have initialized,
// thus we can make the first queries to the world like:
// World.Filter.With<>() -> ref e.GetComponent<> -> Pass-through in UI
#if DEBUG
RegisterImGuiRenderer();
#endif
RegisterMyraUIEnvironment();
RegisterMyraUI();

_logger.ForContext<Game>().Verbose("LoadContent(): end");
}

Expand Down Expand Up @@ -131,7 +135,11 @@ private void RegisterSpriteBatch()
_spriteBatch = _container.GetInstance<SpriteBatch>();
}

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

private void RegisterImGuiRenderer()
{
Expand All @@ -147,37 +155,7 @@ private void RegisterImGuiRenderer()
ImGui.GetIO().ConfigFlags = ImGuiConfigFlags.DockingEnable | ImGuiConfigFlags.ViewportsEnable;
}

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

private void RegisterMyraUI()
{
// 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>();
}
private void RegisterMyraUI() => _desktop = _container.GetInstance<Desktop>();
}
37 changes: 37 additions & 0 deletions src/Libs/UI/Blocks/Inventory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Components.Data;
using FontStashSharp.RichText;
using Myra.Graphics2D.UI;

namespace UI.Blocks;

public class Inventory
{
public Inventory(Container container, ref InventoryComponent inventory)
{
var title = new Label
{
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
TextAlign = TextHorizontalAlignment.Left,
Text = "Inventory",
Top = 20,
Left = 30,
};
container.Widgets.Add(title);

for (int i = 0; i < inventory.Slots.Length; i++)
{
var slot = inventory.Slots[i];
var label = new Label
{
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
TextAlign = TextHorizontalAlignment.Left,
Text = $"{i + 1}: {slot.GetInfo().Name}, {slot.Amount}",
Top = 50 + 18 * i,
Left = 30,
};
container.Widgets.Add(label);
}
}
}
10 changes: 0 additions & 10 deletions src/Libs/UI/Blocks/InventorySlot.cs

This file was deleted.

0 comments on commit c9f90b4

Please sign in to comment.