From d26a28fafeeb8988f77e18955fd090c097a1fc6f Mon Sep 17 00:00:00 2001 From: Damien Ostler Date: Sun, 14 Jan 2024 00:30:09 -0500 Subject: [PATCH] started building out UI classes --- Code/Grid/InventoryCell.cs | 2 + Code/Grid/InventoryModel.cs | 2 +- Code/InventoryController.cs | 34 ++++++--- Code/UI/Inventory.cs | 110 ++++++++++++++++++++++++++++++ Code/UI/InventoryGrid.cs | 58 ++++++++++++++++ Code/UI/InventoryGridInterface.cs | 38 ----------- Code/UI/InventoryGridItem.cs | 12 ++++ Code/UI/InventoryGridSlot.cs | 8 --- 8 files changed, 206 insertions(+), 58 deletions(-) create mode 100644 Code/UI/Inventory.cs create mode 100644 Code/UI/InventoryGrid.cs delete mode 100644 Code/UI/InventoryGridInterface.cs create mode 100644 Code/UI/InventoryGridItem.cs delete mode 100644 Code/UI/InventoryGridSlot.cs diff --git a/Code/Grid/InventoryCell.cs b/Code/Grid/InventoryCell.cs index 7e9ca47..0016fa7 100644 --- a/Code/Grid/InventoryCell.cs +++ b/Code/Grid/InventoryCell.cs @@ -1,5 +1,6 @@ using Godot; using GodotGridInventory.Code.Grid.Enums; +using GodotGridInventory.Code.UI; namespace GodotGridInventory.Code.Grid; @@ -10,6 +11,7 @@ public class InventoryCell public Vector2 Position { get; set; } public EnumInventoryGridCellState State { get; set; } public Item? Item { get; set; } = null; + public InventoryGridItem? ItemGraphic { get; set; } = null; public InventoryCell(Vector2 position, InventoryGrid parentGrid) diff --git a/Code/Grid/InventoryModel.cs b/Code/Grid/InventoryModel.cs index 1cc8d3b..b1e5034 100644 --- a/Code/Grid/InventoryModel.cs +++ b/Code/Grid/InventoryModel.cs @@ -15,6 +15,6 @@ public class InventoryModel public List Items { get; set; } = new List(); public List Cells { get; set; } = new List(); public InventoryGrid? Grid { get; set; } = null; - public InventoryGridInterface? GridInterface { get; set; } = null; + public UI.InventoryGrid? GridInterface { get; set; } = null; public bool Open { get; set; } = false; } \ No newline at end of file diff --git a/Code/InventoryController.cs b/Code/InventoryController.cs index eeb881d..8b1125d 100644 --- a/Code/InventoryController.cs +++ b/Code/InventoryController.cs @@ -3,21 +3,30 @@ using System.Collections.Generic; using System.Linq; using Godot; using GodotGridInventory.Code.Grid; +using GodotGridInventory.Code.UI; +using InventoryGrid = GodotGridInventory.Code.Grid.InventoryGrid; namespace GodotGridInventory.Code; -public partial class InventoryController : Node +public partial class InventoryController : Control { - private int _inventoryIdCounter = 0; + public static int PLAYER_INVENTORY_ID = 0; + #region Private Variables private readonly ItemDatabase _itemDatabase = ItemDatabase.Instance; - [Export] public float CellSize { get; set; } = 32; - private Dictionary _inventories { get; set; } = new Dictionary(); - public override void _Ready() + private int _inventoryIdCounter = 0; + #endregion + + [Export] public float CellSize { get; set; } = 25; + + #region Public Methods + public List GetInventoriesLoaded() { - base._Ready(); + var result = _inventories.Values.ToList(); + GD.Print($"Getting list of all _inventoriesLoaded loaded into memory ({result.Count})."); + return result; } - + public bool UpdateInventory(int id, InventoryModel inventoryModel) { var inventory = _inventories[id]; @@ -123,10 +132,13 @@ public partial class InventoryController : Node return true; } - public List GetInventoriesLoaded() + #endregion + private Dictionary _inventories { get; set; } = new Dictionary(); + + public override void _Ready() { - var result = _inventories.Values.ToList(); - GD.Print($"Getting list of all _inventoriesLoaded loaded into memory ({result.Count})."); - return result; + base._Ready(); } + + } \ No newline at end of file diff --git a/Code/UI/Inventory.cs b/Code/UI/Inventory.cs new file mode 100644 index 0000000..03e832a --- /dev/null +++ b/Code/UI/Inventory.cs @@ -0,0 +1,110 @@ +using Godot; +using GodotGridInventory.Code.Grid; + +namespace GodotGridInventory.Code.UI; + +public class Inventory: Control +{ + private InventoryGridItem _inventoryItemDragged = null; + private Vector2 _inventoryItemCursorOffset = Vector2.Zero; + private InventoryGrid _inventoryItemDraggedLastContainer = null; + private Vector2 _inventoryItemDraggedLastPos = Vector2.Zero; + private InventoryController _inventoryController; + + [Export] public string InventoryDragActionName = "inventory_drag"; + [Export] public string InventoryDropActionName = "inventory_release"; + [Export] public string InventoryRotateActionName = "inventory_rotate"; + + + #region Private Methods + + private InventoryModel GetInventoryUnderCursor(Vector2 cursor_pos) + { + GD.Print("Checking for inventory under cursor."); + foreach (var inventory in _inventoryController.GetInventoriesLoaded()) + { + if (inventory.GridInterface.GetGlobalRect().HasPoint(cursor_pos)) + { + //todo expected behaviour is if they are overlapping the one with the lower ID will take priority this may need to be reversed. + GD.Print($"Inventory found under cursor with ID {inventory.Id}."); + return inventory; + } + } + GD.Print("No inventory found under cursor."); + return null; + } + + private void Grab(Vector2 cursorPos) + { + var inventory = GetInventoryUnderCursor(cursorPos); + if (inventory != null) + { + var inventoryItem = inventory.Grid?.GetCell(cursorPos).ItemGraphic; + if (inventoryItem != null) + { + _inventoryItemDragged = inventoryItem; + _inventoryItemDraggedLastContainer = inventory.GridInterface; + _inventoryItemDraggedLastPos = _inventoryItemDragged.GlobalPosition; + _inventoryItemCursorOffset = _inventoryItemDragged.GlobalPosition - cursorPos; + } + } + } + + private void Release(Vector2 cursorPos) + { + if (_inventoryItemDragged == null) + return; + + var inventory = GetInventoryUnderCursor(cursorPos); + if (inventory == null) + { + ReturnItem(); + return; + } + } + + private void ReturnItem() + { + _inventoryItemDragged.GlobalPosition = _inventoryItemDraggedLastPos; + _inventoryItemDraggedLastContainer.AddItem(_inventoryItemDragged); + _inventoryItemDragged = null; + } + #endregion + + #region Public Methods + public void InitializeInventory(InventoryController inventoryController) + { + _inventoryController = inventoryController; + } + #endregion + + #region Overrides + + public override void _Ready() + { + base._Ready(); + } + + public override void _Process(double delta) + { + if(Input.IsActionJustPressed(InventoryRotateActionName) && _inventoryItemDragged != null) + _inventoryItemDragged.RotationDegrees+= 90.0f; + + if(_inventoryItemDragged != null && _inventoryItemDragged.RotationDegrees==360.0f) + _inventoryItemDragged.RotationDegrees=0.0f; + + var cursor_pos = GetGlobalMousePosition(); + + if (Input.IsActionJustPressed(InventoryDragActionName)) + { + Grab(cursor_pos); + } + + if (Input.IsActionJustReleased(InventoryDragActionName)) + { + Release(cursor_pos); + } + base._Process(delta); + } + #endregion +} \ No newline at end of file diff --git a/Code/UI/InventoryGrid.cs b/Code/UI/InventoryGrid.cs new file mode 100644 index 0000000..9a0139d --- /dev/null +++ b/Code/UI/InventoryGrid.cs @@ -0,0 +1,58 @@ + using Godot; +using GodotGridInventory.Code.Grid; + +namespace GodotGridInventory.Code.UI; + +public partial class InventoryGrid:Control +{ + + private PackedScene _gridSlotResource; + private Grid.InventoryGrid _inventoryGrid; + private InventoryController _inventoryController; + + + [Export] public Control GridContainer; + [Export] public string GridSlotPath; + + public override void _Ready() + { + _gridSlotResource = GD.Load(GridSlotPath); + base._Ready(); + } + + public void InitializeInventoryGrid(Grid.InventoryGrid inventoryGrid, InventoryController inventoryController) + { + _inventoryGrid = inventoryGrid; + _inventoryController = inventoryController; + + for (var x = 0; x < _inventoryGrid.GridSize.X; x++) + { + for (var y = 0; y < _inventoryGrid.GridSize.Y; y++) + { + var cellPosition = new Vector2(x, y); + var cell = _inventoryGrid.GetCell(cellPosition); + var gridSlot = _gridSlotResource.Instantiate() as Control; + gridSlot.Position = new Vector2(x * _inventoryController.CellSize, y * _inventoryController.CellSize); + GridContainer.AddChild(gridSlot); + } + } + } + + private Vector2 ScreenCoordsToInventoryGridCoords(Vector2 cursorPos) + { + var x = Mathf.FloorToInt(cursorPos.X / _inventoryController.CellSize); + var y = Mathf.FloorToInt(cursorPos.Y / _inventoryController.CellSize); + return new Vector2(x, y); + } + + public void AddItem(InventoryGridItem inventoryItemDragged) + { + var cursorPos = GetGlobalMousePosition(); + var gridCoords = ScreenCoordsToInventoryGridCoords(cursorPos); + if (_inventoryGrid.AddItem(inventoryItemDragged.Item.Id, gridCoords)) + { + inventoryItemDragged.Position = gridCoords * _inventoryController.CellSize; + AddChild(inventoryItemDragged); + } + } +} \ No newline at end of file diff --git a/Code/UI/InventoryGridInterface.cs b/Code/UI/InventoryGridInterface.cs deleted file mode 100644 index 80ce747..0000000 --- a/Code/UI/InventoryGridInterface.cs +++ /dev/null @@ -1,38 +0,0 @@ -using Godot; -using GodotGridInventory.Code.Grid; - -namespace GodotGridInventory.Code.UI; - -public partial class InventoryGridInterface:Control -{ - [Export] public Control GridContainer { get; set; } - [Export] public string GridSlotPath { get; set; } - - private PackedScene _gridSlotResource; - private InventoryGrid _inventoryGrid; - private InventoryController _inventoryController; - - public override void _Ready() - { - _gridSlotResource = GD.Load(GridSlotPath); - base._Ready(); - } - - public void InitializeInventoryGrid(InventoryGrid inventoryGrid, InventoryController inventoryController) - { - _inventoryGrid = inventoryGrid; - _inventoryController = inventoryController; - - for (var x = 0; x < _inventoryGrid.GridSize.X; x++) - { - for (var y = 0; y < _inventoryGrid.GridSize.Y; y++) - { - var cellPosition = new Vector2(x, y); - var cell = _inventoryGrid.GetCell(cellPosition); - var gridSlot = _gridSlotResource.Instantiate() as InventoryGridSlot; - gridSlot.Position = new Vector2(x * _inventoryController.CellSize, y * _inventoryController.CellSize); - GridContainer.AddChild(gridSlot); - } - } - } -} \ No newline at end of file diff --git a/Code/UI/InventoryGridItem.cs b/Code/UI/InventoryGridItem.cs new file mode 100644 index 0000000..dd764e2 --- /dev/null +++ b/Code/UI/InventoryGridItem.cs @@ -0,0 +1,12 @@ +using Godot; + +namespace GodotGridInventory.Code.UI; + +public partial class InventoryGridItem: Control +{ + public InventoryGridItem(Item item) + { + Item = item; + } + public Item Item { get; } = null; +} \ No newline at end of file diff --git a/Code/UI/InventoryGridSlot.cs b/Code/UI/InventoryGridSlot.cs deleted file mode 100644 index a915595..0000000 --- a/Code/UI/InventoryGridSlot.cs +++ /dev/null @@ -1,8 +0,0 @@ -using Godot; - -namespace GodotGridInventory.Code.UI; - -public partial class InventoryGridSlot : Control -{ - -} \ No newline at end of file