From c9f90b4ed0d62b580107a580ca06b61be31cf1bd Mon Sep 17 00:00:00 2001 From: cherrynik Date: Fri, 1 Mar 2024 13:25:47 +0300 Subject: [PATCH] feat(ui): inventory --- .../Components/ComponentsCompositionRoot.cs | 3 +- .../Features/RootFeatureCompositionRoot.cs | 7 +++ src/Apps/GameDesktop/Game.cs | 54 ++++++------------- src/Libs/UI/Blocks/Inventory.cs | 37 +++++++++++++ src/Libs/UI/Blocks/InventorySlot.cs | 10 ---- 5 files changed, 62 insertions(+), 49 deletions(-) create mode 100644 src/Libs/UI/Blocks/Inventory.cs delete mode 100644 src/Libs/UI/Blocks/InventorySlot.cs diff --git a/src/Apps/GameDesktop/CompositionRoots/Components/ComponentsCompositionRoot.cs b/src/Apps/GameDesktop/CompositionRoots/Components/ComponentsCompositionRoot.cs index 9ec5d3e..c06576d 100644 --- a/src/Apps/GameDesktop/CompositionRoots/Components/ComponentsCompositionRoot.cs +++ b/src/Apps/GameDesktop/CompositionRoots/Components/ComponentsCompositionRoot.cs @@ -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); }); diff --git a/src/Apps/GameDesktop/CompositionRoots/Features/RootFeatureCompositionRoot.cs b/src/Apps/GameDesktop/CompositionRoots/Features/RootFeatureCompositionRoot.cs index d07fbe7..a80a516 100644 --- a/src/Apps/GameDesktop/CompositionRoots/Features/RootFeatureCompositionRoot.cs +++ b/src/Apps/GameDesktop/CompositionRoots/Features/RootFeatureCompositionRoot.cs @@ -140,6 +140,12 @@ private static void RegisterEntryPoint(IServiceRegistry serviceRegistry) // UI serviceRegistry.RegisterSingleton(_ => new Panel()); + serviceRegistry.RegisterSingleton(factory => + { + var filter = factory.GetInstance().Filter.With().Build(); + ref var inventory = ref filter.First().GetComponent(); + return new Inventory(factory.GetInstance(), ref inventory); + }); serviceRegistry.RegisterSingleton(factory => new Counter(factory.GetInstance(), new Label @@ -175,6 +181,7 @@ private static void RegisterEntryPoint(IServiceRegistry serviceRegistry) Desktop desktop = new(); desktop.Root = factory.GetInstance(); + factory.GetInstance(); factory.GetInstance(); return desktop; diff --git a/src/Apps/GameDesktop/Game.cs b/src/Apps/GameDesktop/Game.cs index 7966d2e..f4eaaa3 100644 --- a/src/Apps/GameDesktop/Game.cs +++ b/src/Apps/GameDesktop/Game.cs @@ -43,15 +43,10 @@ protected override void Initialize() _logger.ForContext().Verbose($"Initialize(): start; available {GraphicsDevice}"); _logger.ForContext().Verbose("Circular dependencies (external) initialization..."); RegisterSpriteBatch(); - RegisterMyraUIEnvironment(); -#if DEBUG - RegisterImGuiRenderer(); -#endif _logger.ForContext().Verbose("Circular dependencies (external) initialized"); _logger.ForContext().Verbose("Game services initialization..."); RegisterRootFeature(); - RegisterMyraUI(); _logger.ForContext().Verbose("Game services initialized"); base.Initialize(); @@ -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().Verbose("LoadContent(): end"); } @@ -131,7 +135,11 @@ private void RegisterSpriteBatch() _spriteBatch = _container.GetInstance(); } - private void RegisterMyraUIEnvironment() => MyraEnvironment.Game = this; + private void RegisterRootFeature() + { + _container.RegisterFrom(); + _rootFeature = _container.GetInstance(); + } private void RegisterImGuiRenderer() { @@ -147,37 +155,7 @@ private void RegisterImGuiRenderer() ImGui.GetIO().ConfigFlags = ImGuiConfigFlags.DockingEnable | ImGuiConfigFlags.ViewportsEnable; } - private void RegisterRootFeature() - { - _container.RegisterFrom(); - _rootFeature = _container.GetInstance(); - } + 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(); - // new UIFactory(grid, - // new Label { Id = "label", Text = "Hello, World!" }, - // combo, - // button, - // new SpinButton { Width = 100, Nullable = true }) - // .Build(); - // --- - - _desktop = _container.GetInstance(); - } + private void RegisterMyraUI() => _desktop = _container.GetInstance(); } diff --git a/src/Libs/UI/Blocks/Inventory.cs b/src/Libs/UI/Blocks/Inventory.cs new file mode 100644 index 0000000..68afb85 --- /dev/null +++ b/src/Libs/UI/Blocks/Inventory.cs @@ -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); + } + } +} diff --git a/src/Libs/UI/Blocks/InventorySlot.cs b/src/Libs/UI/Blocks/InventorySlot.cs deleted file mode 100644 index 702810a..0000000 --- a/src/Libs/UI/Blocks/InventorySlot.cs +++ /dev/null @@ -1,10 +0,0 @@ -using Components.Data; - -namespace UI.Blocks; - -public class InventorySlot -{ - public InventorySlot(ref Slot slot) - { - } -}