using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; using static SimpleInventorySystem.InventorySystem; namespace SimpleInventorySystem { [RequireComponent(typeof (GridLayoutGroup))] public class GridUI : MonoBehaviour { private LayoutElement layoutElement; private GridLayoutGroup gridLayout; public GameObject CellPrefab; public bool generateCells; // will autopopulate grid public SlotUI[] slots; [SerializeField] public Size gridSize; internal void ChangeGridLayoutSize(int rows, int columns) { gridSize.Width = rows; gridSize.Height = columns; } private Cell[,] cells; public GameObject testItem; public GameObject[] testItems; private Size cellSize; public Size CellSize { get { return cellSize; } } private void Awake() { gridLayout = GetComponent(); SetUpCellsArray(); } void SetUpCellsArray() { if (gridSize.Width == 0 && gridLayout.constraint == GridLayoutGroup.Constraint.FixedColumnCount) { gridSize.Width = gridLayout.constraintCount; } else if (gridSize.Height == 0 && gridLayout.constraint == GridLayoutGroup.Constraint.FixedRowCount) { gridSize.Height = gridLayout.constraintCount; } if (gridSize.Width == 0 || gridSize.Height == 0) { Debug.LogError("GridUI needs a defined width and height, or width or height needs to be constrained through the GridLayoutGroup component."); } //Debug.Log("GridUI: GridSize: " + gridSize.Width + ", " + gridSize.Height); cells = new Cell[gridSize.Height, gridSize.Width]; } void GenerateCells() { SetUpCellsArray(); if (CellPrefab != null) { /*foreach(Transform child in transform) { // TODO: make sure to unequip items currently in the grid Destroy(child); }*/ Debug.Log(gridSize.Width + ", " + gridSize.Height); for (int y = 0; y < gridSize.Height; y++) { for (int x = 0; x < gridSize.Width; x++) { // TODO: load inventory from data here upon intialization GameObject cell = GameObject.Instantiate(CellPrefab, this.transform); cell.transform.localScale = new Vector3(1f, 1f, 1f); SlotUI slot = cell.GetComponentInChildren(); // WARNING: All cell prefabs must now have a slot //if (cell.GetComponentInChildren() != null) //{ slot.grid = this; slot.AllowedItemTags = new List(); slot.AllowedItemTags.Add(ItemTags.Any); cells[y, x] = new Cell(x, y, cell.GetComponentInChildren(), false); cells[y, x].GetSlot().SetCell(cells[y, x]); Debug.Log("Foo: " + (slot == null)); Debug.Log("Bar: " + (cells[y, x].GetSlot().cell == null)); //} } } if (cells != null && cells[0, 0] != null && testItem != null) { Debug.Log("Add item"); Cell cell = cells[0, 0]; GameObject item = Instantiate(testItem); cell.slot.DropOntoSlot(item); //item.transform.SetParent(.gameObject.transform); } if (testItems != null) { int i = 0; int x = 0, y = 0; foreach (GameObject testItemUI in testItems) { bool dropped = false; GameObject item = null; try { do { x = i % gridSize.Width; y = i / gridSize.Width; item = Instantiate(testItemUI); dropped = cells[y, x].slot.DropOntoSlot(item); if (!dropped) { Destroy(item); } else { } i++; } while (!dropped); } catch(IndexOutOfRangeException err) { Debug.LogWarning("GridUI: " + x + ", " + y); } if (item == null) { Debug.LogWarning("Could not add all test items"); break; } } } } } // Start is called before the first frame update void Start() { if (!generateCells) { //if (slots != null) for (int i = 0; i < gridLayout.transform.childCount; i++) { int x = i % gridSize.Width; int y = (i - x) / gridSize.Width; if (cells[y, x] != null) { continue; } Transform child = gridLayout.transform.GetChild(i); SlotUI slot = child.GetComponentInChildren(); if (slot != null) { cells[y, x] = new Cell(x, y, slot, true); slot.SetCell(cells[y, x]); slot.grid = this; if (slot.width > 1 || slot.height > 1) { for (int j = 0; j < slot.height; j++) { for (int k = 1; k < slot.width; k++) { cells[y + j, x + k] = new Cell(x + k, y + j, slot, false); } } } Debug.Log("START: " + (slot.cell == null)); } else { cells[y, x] = null;// new Cell(null, false, false); // Setting null so that we can reserve memory } } } else { GenerateCells(); } } internal void HideSlotsOfItem(ItemUI itemDrop) { if(itemDrop != null) { itemDrop.HideSlots(); } } public bool CanMoveInCells(ItemUI item, SlotUI slot, List occupiedCells = null) { if (slot == null || slot.cell == null || (slot.cell.inUse && (slot.itemUI != null && !slot.itemUI.Stackable))) { Debug.Log("GridUI: CanMoveInCells(): " + slot.gameObject.name + ": A" + (slot.cell == null)); return false; } else if (slot.IsSingleCellSlot() && item.IsSingleCellItem() && !slot.cell.inUse) { Debug.Log("GridUI: CanMoveInCells(): " + slot.gameObject.name + ": B"); //if (!slot.cell.inUse) return true; //else // return false; } else if (cells != null) { List oCells = new List(); if(occupiedCells != null) { oCells = occupiedCells; } Size size = item.SizeAfterOrientation(); // for (int h = 0; h < slot.height; h++){ // for (int w = 0; w < slot.width; w++) for (int h = 0; h < size.Height; h++) { for (int w = 0; w < size.Width; w++) { if(slot.cell.Y + h >= gridSize.Height || slot.cell.X + w >= gridSize.Width) { Debug.Log("GridUI: CanMoveInCells(): " + slot.gameObject.name + ": D"); return false; } Cell cell = cells[slot.cell.Y + h, slot.cell.X + w]; if (cell != null && cell.inUse) { Debug.Log("GridUI: CanMoveInCells(): " + slot.gameObject.name + ": E"); return false; } oCells.Add(cell); } } //Debug.Log(occupiedCells.Count); Debug.Log("GridUI: CanMoveInCells(): " + slot.gameObject.name + ": F"); Debug.Log(oCells.Count); } return true; } // Update is called once per frame void Update() { } public class Cell { protected int x, y; public int X { get { return x; } } public int Y { get { return y; } } public SlotUI slot; public bool ownsSlot; public bool inUse; public Cell(int x, int y, SlotUI slot, bool ownsSlot) { this.x = x; this.y = y; this.slot = slot; slot.SetCell(this); inUse = false; this.ownsSlot = ownsSlot; } public bool HasSlot() { return slot != null; } public SlotUI GetSlot() { return slot; } } } }