DrawInventoryBar (BaseStatusBar)
void DrawInventoryBar(InventoryBarState parms, Vector2 position, int numfields, int flags = 0, double bgalpha = 1.)
Usage
A BaseStatusBar function designed to draw the inventory bar in your HUD. It draws a series of item icons with a background, and item amounts. Note, this function does not handle the inner workings of the bar itself (such as the player's ability to cycle items and use them), only the visual representation of it.
Since there's a lot of information to keep track of when drawing an inventory bar, most of that data is stored in a dedicated class, InventoryBarState, which is normally initialized in the HUD's Init() function.
Note, using this function to draw the inventory bar is not actually a requirement. It can be replaced with a completely custom method of representing it.
Parameters
- InventoryBarState parms
- A pointer to an instance of InventoryBarState class that contains relevant information about the inventory bar.
- Vector2 position
- The position to draw the bar at.
- int numfields
- How many items to show in the bar at the same time. If the total number of items is higher than this value, the player will have to scroll to them manually.
- int flags
- The DI_SCREEN* flags will change the origin point of the coordinates where the element is drawn (essentially, moving where the (0, 0) point is located.
- DI_SCREEN_LEFT_TOP - The coordinates begin at the top left corner of the screen
- DI_SCREEN_CENTER_TOP - The coordinates begin at the top center of the screen
- DI_SCREEN_RIGHT_TOP - The coordinates begin at the top right corner of the screen
- DI_SCREEN_LEFT_CENTER - The coordinates begin at the center left side of the screen
- DI_SCREEN_CENTER - The coordinates begin at the center of the screen
- DI_SCREEN_RIGHT_CENTER - The coordinates begin at the center right side of the screen
- DI_SCREEN_LEFT_BOTTOM - The coordinates begin at the bottom left corner of the screen
- DI_SCREEN_CENTER_BOTTOM - The coordinates begin at the bottom center of the screen
- DI_SCREEN_RIGHT_BOTTOM - The coordinates begin at the bottom right corner of the screen
- Note, these flags do not change the orientation of coordinates. Regardless of where the element is drawn, positive X moves it to the right, positive Y moves it down.
- More flags are defined in the StatusBarCore class, but they're mostly aliases of the above ones.
- In addition to the above, this function also supports unique flags:
- DI_DRAWCURSORFIRST - draws the selection cursor below the item's icon
- DI_ALWAYSSHOWCOUNT - always show the item count on top of the icon, even if the item's current amount is 0
- DI_ARTIFLASH - (Need more info)
- double bgalpha
- Translucency of the bar's background.
ZScript definition
Note: The ZScript definition below is for reference and may be different in the current version of GZDoom.The most up-to-date version of this code can be found on GZDoom GitHub. |
The inner workings of this function are fully handled in ZScript:
// Except for the placement information this gets all info from the struct that gets passed in.
void DrawInventoryBar(InventoryBarState parms, Vector2 position, int numfields, int flags = 0, double bgalpha = 1.)
{
double width = parms.boxsize.X * numfields;
[position, flags] = AdjustPosition(position, flags, width, parms.boxsize.Y);
CPlayer.mo.InvFirst = ValidateInvFirst(numfields);
if (CPlayer.mo.InvFirst == null) return; // Player has no listed inventory items.
Vector2 boxsize = parms.boxsize;
// First draw all the boxes
for(int i = 0; i < numfields; i++)
{
DrawTexture(parms.box, position + (boxsize.X * i, 0), flags | DI_ITEM_LEFT_TOP, bgalpha);
}
// now the items and the rest
Vector2 itempos = position + boxsize / 2;
Vector2 textpos = position + boxsize - (1, 1 + parms.amountfont.mFont.GetHeight());
int i = 0;
Inventory item;
for(item = CPlayer.mo.InvFirst; item != NULL && i < numfields; item = item.NextInv())
{
for(int j = 0; j < 2; j++)
{
if (j ^ !!(flags & DI_DRAWCURSORFIRST))
{
if (item == CPlayer.mo.InvSel)
{
double flashAlpha = bgalpha;
if (flags & DI_ARTIFLASH) flashAlpha *= itemflashFade;
DrawTexture(parms.selector, position + parms.selectofs + (boxsize.X * i, 0), flags | DI_ITEM_LEFT_TOP, flashAlpha);
}
}
else
{
DrawInventoryIcon(item, itempos + (boxsize.X * i, 0), flags | DI_ITEM_CENTER | DI_DIMDEPLETED );
}
}
if (parms.amountfont != null && (item.Amount > 1 || (flags & DI_ALWAYSSHOWCOUNTERS)))
{
DrawString(parms.amountfont, FormatNumber(item.Amount, 0, 5), textpos + (boxsize.X * i, 0), flags | DI_TEXT_ALIGN_RIGHT, parms.cr, parms.itemalpha);
}
i++;
}
// Is there something to the left?
if (CPlayer.mo.FirstInv() != CPlayer.mo.InvFirst)
{
DrawTexture(parms.left, position + (-parms.arrowoffset.X, parms.arrowoffset.Y), flags | DI_ITEM_RIGHT|DI_ITEM_VCENTER);
}
// Is there something to the right?
if (item != NULL)
{
DrawTexture(parms.right, position + parms.arrowoffset + (width, 0), flags | DI_ITEM_LEFT|DI_ITEM_VCENTER);
}
}
Examples
This example shows how the default Doom HUD uses this function. Note, most of the code was omitted for brevity, and comments were added. You can see the full hud code on GZDoom Github.
// This HUD is included with GZDoom.
class DoomStatusBar : BaseStatusBar
{
// The HUD needs to have a pointer to an instance
// of InventoryBarState, so it must be declared
// as a class field:
InventoryBarState diparms;
override void Init()
{
Super.Init();
[...]
// InventoryBarState is instantiated:
diparms = InventoryBarState.Create();
}
override void Draw (int state, double TicFrac)
{
Super.Draw (state, TicFrac);
// Draws Doom statusbar in statusbar mode:
if (state == HUD_StatusBar)
{
BeginStatusBar();
DrawMainBar (TicFrac);
}
// Otherwise draws fullscreen HUD:
else if (state == HUD_Fullscreen)
{
BeginHUD();
DrawFullScreenStuff ();
}
}
// Statusbar mode:
protected void DrawMainBar (double TicFrac)
{
[...] // omitted for brevity
if (isInventoryBarVisible())
{
// Draws the inventory bar on top of the statusbar:
DrawInventoryBar(diparms, (48, 169), 7, DI_ITEM_LEFT_TOP);
}
}
// Fullscreen HUD mode:
protected void DrawFullScreenStuff ()
{
[...] // omitted for brevity
if (isInventoryBarVisible())
{
// Draws the inventory bar at the bottom center of the screen:
DrawInventoryBar(diparms, (0, 0), 7, DI_SCREEN_CENTER_BOTTOM, HX_SHADOW);
}
}
}