Class InventoryDisplay
java.lang.Object
com.csse3200.game.components.Component
com.csse3200.game.rendering.RenderComponent
com.csse3200.game.ui.UIComponent
com.csse3200.game.components.inventory.InventoryDisplay
- All Implemented Interfaces:
com.badlogic.gdx.utils.Disposable
,Renderable
,Comparable<Renderable>
- Direct Known Subclasses:
CombatInventoryDisplay
,PlayerInventoryDisplay
Abstract class for displaying an inventory. Subclasses can extend this to implement specific types of inventory
displays, such as player inventories, with additional features (e.g., hotBar, drag-and-drop).
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Inventory
protected static final org.slf4j.Logger
protected final int
protected final int
Fields inherited from class com.csse3200.game.ui.UIComponent
skin, stage
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
InventoryDisplay
(Inventory inventory, int numCols, int hotBarCapacity, boolean displayHotBar, GdxGame game) Constructs a PlayerInventoryDisplay with the specified capacity and number of columns. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addSlotListeners
(com.badlogic.gdx.scenes.scene2d.ui.ImageButton slot, AbstractItem item, int index) Adds listeners to the inventory slots for handling hover and click events.protected void
consumeItem
(AbstractItem item, ItemUsageContext context, int index) Consumes an item from the inventory and triggers an event if successful.void
create()
Initializes the component by setting up event listeners for toggling the inventory display and adding items.void
dispose()
Disposes of the resources used by the component, including the window, table, and slots.void
draw
(com.badlogic.gdx.graphics.g2d.SpriteBatch batch) Handles drawing of the component.protected abstract void
enterSlot
(AbstractItem item) protected abstract void
exitSlot
(AbstractItem item) int
return the num of colsboolean
Determines if the toggle is activefloat
Z index controls rendering order within a layer.protected boolean
Checks if the current screen is an instance of MainGameScreen.void
Regenerates the entire inventory and hotBar display (recreates every image) This should only be used if the inventory has been changed (ie after sorting or loading).void
Toggles the inventory display on or off based on its current state.abstract String
void
Updates the display of the inventory (does not regenerate any of the images for any slot, simply creates a new table to hold them).protected abstract void
useItem
(AbstractItem item, int index) Methods inherited from class com.csse3200.game.ui.UIComponent
getLayer, setStage
Methods inherited from class com.csse3200.game.rendering.RenderComponent
compareTo, render
Methods inherited from class com.csse3200.game.components.Component
earlyUpdate, getEntity, setEnabled, setEntity, toString, triggerEarlyUpdate, triggerUpdate, update
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Comparable
compareTo
Methods inherited from interface com.csse3200.game.rendering.Renderable
render
-
Field Details
-
logger
protected static final org.slf4j.Logger logger -
inventory
-
numCols
protected final int numCols -
numRows
protected final int numRows
-
-
Constructor Details
-
InventoryDisplay
protected InventoryDisplay(Inventory inventory, int numCols, int hotBarCapacity, boolean displayHotBar, GdxGame game) Constructs a PlayerInventoryDisplay with the specified capacity and number of columns. The capacity must be evenly divisible by the number of columns.- Parameters:
inventory
- The inventory from which to build the displaynumCols
- The number of columns in the inventory display.- Throws:
IllegalArgumentException
- if numCols is less than 1 or if capacity is not divisible by numCols.
-
-
Method Details
-
isMainGameScreen
protected boolean isMainGameScreen()Checks if the current screen is an instance of MainGameScreen.- Returns:
- true if the current screen is MainGameScreen, false otherwise.
-
create
public void create()Initializes the component by setting up event listeners for toggling the inventory display and adding items.- Overrides:
create
in classUIComponent
-
toggleMsg
-
draw
public void draw(com.badlogic.gdx.graphics.g2d.SpriteBatch batch) Handles drawing of the component. The actual rendering is managed by the stage.- Specified by:
draw
in classRenderComponent
- Parameters:
batch
- The SpriteBatch used for drawing.
-
getToggle
public boolean getToggle()Determines if the toggle is active- Returns:
- returns the toggle
-
toggleDisplay
public void toggleDisplay()Toggles the inventory display on or off based on its current state. -
addSlotListeners
public void addSlotListeners(com.badlogic.gdx.scenes.scene2d.ui.ImageButton slot, AbstractItem item, int index) Adds listeners to the inventory slots for handling hover and click events. This allows items to be used and inventory to be regenerated.- Parameters:
slot
- The ImageButton representing the inventory slot.item
- The item in the slot.index
- The index of the slot in the inventory.
-
enterSlot
-
exitSlot
-
useItem
-
updateDisplay
public void updateDisplay()Updates the display of the inventory (does not regenerate any of the images for any slot, simply creates a new table to hold them). -
regenerateDisplay
public void regenerateDisplay()Regenerates the entire inventory and hotBar display (recreates every image) This should only be used if the inventory has been changed (ie after sorting or loading). Otherwise, use updateDisplay. -
dispose
public void dispose()Disposes of the resources used by the component, including the window, table, and slots.- Specified by:
dispose
in interfacecom.badlogic.gdx.utils.Disposable
- Overrides:
dispose
in classRenderComponent
-
getZIndex
public float getZIndex()Description copied from interface:Renderable
Z index controls rendering order within a layer. Higher Z index is drawn on top.- Specified by:
getZIndex
in interfaceRenderable
- Overrides:
getZIndex
in classUIComponent
- Returns:
- The z-index for this component.
-
getNumCols
public int getNumCols()return the num of cols- Returns:
- num of cols
-
consumeItem
Consumes an item from the inventory and triggers an event if successful.- 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.
-