Inventory Display - UQcsse3200/2024-studio-2 GitHub Wiki

Package: com.csse3200.game.components.inventory

Overview

The InventoryDisplay class is an abstract class for managing the display of inventories. Subclasses can extend it to implement specific types of inventory displays, such as Player Inventory Display, which may include features like hotbars and drag-and-drop functionality. It interacts with an inventory system and provides a customizable user interface to manage items.

Features

  • Hotbar Support: Supports displaying a hotbar for quick item access.
  • Drag-and-Drop Functionality: Allows drag-and-drop operations between inventory slots.
  • Event-Driven Inventory Updates: Handles events such as adding items, using items, and toggling inventory display.
  • Customizable Slot Display: Subclasses can extend and customize how items are displayed and interacted with in slots.

Key Components

Fields

  • Inventory inventory: The inventory being displayed.
  • Window mainInventoryDisplay: The main window that displays the inventory.
  • Table hotBarDisplay: The UI table used for the hotbar.
  • DragAndDrop dragAndDrop: Manages drag-and-drop functionality between inventory slots.
  • ImageButton[] slots: An array representing each slot in the inventory.
  • boolean hasHotBar: Specifies if a hotbar should be displayed.
  • int numCols: The number of columns in the inventory display.
  • int numRows: The number of rows in the inventory display.
  • int hotBarCapacity: Maximum number of items that can be displayed in the hotbar.

Constructor

InventoryDisplay(Inventory inventory, int numCols, int hotBarCapacity, boolean displayHotBar, GdxGame game)

  • Parameters:

    • inventory: The inventory to display.
    • numCols: Number of columns in the inventory display.
    • hotBarCapacity: Maximum number of items allowed in the hotbar.
    • displayHotBar: Boolean indicating whether to display a hotbar.
    • game: An instance of the game.
  • Throws:

    • IllegalArgumentException: If numCols is less than 1 or the capacity is not divisible by numCols.

Methods

void create()

  • Description: Initializes the component, setting up event listeners and generating the inventory and hotbar displays (if enabled).

void draw(SpriteBatch batch)

  • Description: This method is responsible for drawing the inventory. Rendering is handled by the stage.

void toggleDisplay()

  • Description: Toggles the visibility of the inventory display on or off.

boolean getToggle()

  • Description: Returns the current toggle state of the inventory display (on or off).

void setupDragAndDrop(ImageButton slot, int targetIndex, AbstractItem item)

  • Description: Configures drag-and-drop functionality for inventory slots, enabling items to be dragged between different slots.
  • Parameters:
    • slot: The UI button representing the inventory slot.
    • targetIndex: The target index of the slot.
    • item: The item to be dragged.

void generateInventory()

  • Description: Populates the inventory display window with inventory slots.

void generateHotBar()

  • Description: Generates the UI for the hotbar, positioning it on the screen and populating it with slots.

void createSlot(int index)

  • Description: Creates an individual inventory slot at the specified index.
  • Parameters:
    • index: The index of the slot in the inventory.

void addSlotListeners(ImageButton slot, AbstractItem item, int index)

  • Description: Adds listeners to the slot for hover and click events, allowing item use and display updates.
  • Parameters:
    • slot: The inventory slot.
    • item: The item in the slot.
    • index: The index of the slot in the inventory.

void addItem(AbstractItem item)

  • Description: Adds an item to the inventory and updates the display.
  • Parameters:
    • item: The item to be added.

void updateDisplay()

  • Description: Updates the inventory and hotbar displays without regenerating the images.

void regenerateDisplay()

  • Description: Regenerates the entire inventory and hotbar display, recreating all images.

void dispose()

  • Description: Disposes of resources used by the component (e.g., UI elements like windows, tables, and slots).

float getZIndex()

  • Description: Returns the Z-index of the component, which determines its drawing order.

int getNumCols()

  • Description: Returns the number of columns in the inventory display.

void consumeItem(AbstractItem item, ItemUsageContext context, int index)

  • Description: Consumes an item from the inventory and triggers an event upon successful usage.
  • Parameters:
    • item: The item to be consumed.
    • context: The context in which the item is being used.
    • index: The index of the item in the inventory.

Abstract Methods

String toggleMsg()

  • Description: Subclasses implement this method to define the event to listen for to toggle the inventory display.

void enterSlot(AbstractItem item)

  • Description: Subclasses implement this method to define what happens when an item is hovered over.

void exitSlot(AbstractItem item)

  • Description: Subclasses implement this method to define what happens when the cursor leaves an item slot.

void useItem(AbstractItem item, int index)

  • Description: Subclasses implement this method to define how an item is used when clicked.