mirror of
https://github.com/D4M13N-D3V/godot_grid_inventory.git
synced 2025-06-28 02:58:51 +00:00
started building out UI classes
This commit is contained in:
parent
c9c03b3b35
commit
d26a28fafe
@ -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)
|
||||
|
@ -15,6 +15,6 @@ public class InventoryModel
|
||||
public List<Item> Items { get; set; } = new List<Item>();
|
||||
public List<InventoryCell> Cells { get; set; } = new List<InventoryCell>();
|
||||
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;
|
||||
}
|
@ -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<int,InventoryModel> _inventories { get; set; } = new Dictionary<int,InventoryModel>();
|
||||
|
||||
public override void _Ready()
|
||||
private int _inventoryIdCounter = 0;
|
||||
#endregion
|
||||
|
||||
[Export] public float CellSize { get; set; } = 25;
|
||||
|
||||
#region Public Methods
|
||||
public List<InventoryModel> 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<InventoryModel> GetInventoriesLoaded()
|
||||
#endregion
|
||||
private Dictionary<int,InventoryModel> _inventories { get; set; } = new Dictionary<int,InventoryModel>();
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
}
|
110
Code/UI/Inventory.cs
Normal file
110
Code/UI/Inventory.cs
Normal file
@ -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
|
||||
}
|
58
Code/UI/InventoryGrid.cs
Normal file
58
Code/UI/InventoryGrid.cs
Normal file
@ -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<PackedScene>(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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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<PackedScene>(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
12
Code/UI/InventoryGridItem.cs
Normal file
12
Code/UI/InventoryGridItem.cs
Normal file
@ -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;
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
using Godot;
|
||||
|
||||
namespace GodotGridInventory.Code.UI;
|
||||
|
||||
public partial class InventoryGridSlot : Control
|
||||
{
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user