Add direction parameter to statbars in HUD API

Fix health bar overlap when a larger texture is used
This commit is contained in:
kwolekr 2013-04-20 00:21:32 -04:00
parent c45c530f74
commit 527deb947c
4 changed files with 54 additions and 33 deletions

View File

@ -407,6 +407,8 @@ The position field is used for all element types.
To account for differing resolutions, the position coordinates are the percentage of the screen, To account for differing resolutions, the position coordinates are the percentage of the screen,
ranging in value from 0 to 1. ranging in value from 0 to 1.
The name field is not yet used, but should contain a description of what the HUD element represents. The name field is not yet used, but should contain a description of what the HUD element represents.
The direction field is the direction in which something is drawn.
0 draws from left to right, 1 draws from right to left, 2 draws from top to bottom, and 3 draws from bottom to top.
Below are the specific uses for fields in each type; fields not listed for that type are ignored. Below are the specific uses for fields in each type; fields not listed for that type are ignored.
Note: Future revisions to the HUD API may be incompatible; the HUD API is still in the experimental stages. Note: Future revisions to the HUD API may be incompatible; the HUD API is still in the experimental stages.
@ -428,15 +430,12 @@ Note: Future revisions to the HUD API may be incompatible; the HUD API is still
- text: The name of the texture that is used. - text: The name of the texture that is used.
- number: The number of half-textures that are displayed. - number: The number of half-textures that are displayed.
If odd, will end with a vertically center-split texture. If odd, will end with a vertically center-split texture.
- direction
- inventory - inventory
- text: The name of the inventory list to be displayed. - text: The name of the inventory list to be displayed.
- number: Number of items in the inventory to be displayed. - number: Number of items in the inventory to be displayed.
- item: Position of item that is selected. - item: Position of item that is selected.
- direction: Direction in which the inventory list is drawn. - direction
0 draws from left to right,
1 draws from right to left,
2 draws from top to bottom, and
3 draws from bottom to top.
Representations of simple things Representations of simple things
-------------------------------- --------------------------------
@ -1856,6 +1855,6 @@ HUD Definition (hud_add, hud_get)
number = 2, number = 2,
item = 3, item = 3,
^ Selected item in inventory. 0 for no item selected. ^ Selected item in inventory. 0 for no item selected.
dir = 0, direction = 0,
^ Direction: 0: left-right, 1: right-left, 2: top-bottom, 3: bottom-top ^ Direction: 0: left-right, 1: right-left, 2: top-bottom, 3: bottom-top
} }

View File

@ -198,7 +198,7 @@ void Hud::drawLuaElements() {
font->draw(narrow_to_wide(e->text).c_str(), size + pos, color); font->draw(narrow_to_wide(e->text).c_str(), size + pos, color);
break; } break; }
case HUD_ELEM_STATBAR: case HUD_ELEM_STATBAR:
drawStatbar(pos, e->text, e->number); drawStatbar(pos, HUD_CORNER_UPPER, e->dir, e->text, e->number);
break; break;
case HUD_ELEM_INVENTORY: { case HUD_ELEM_INVENTORY: {
InventoryList *inv = inventory->getList(e->text); InventoryList *inv = inventory->getList(e->text);
@ -212,38 +212,55 @@ void Hud::drawLuaElements() {
} }
void Hud::drawStatbar(v2s32 upperleftpos, std::string texture, s32 count) { void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture, s32 count) {
const video::SColor color(255, 255, 255, 255);
const video::SColor colors[] = {color, color, color, color};
video::ITexture *stat_texture = video::ITexture *stat_texture =
gamedef->getTextureSource()->getTextureRaw(texture); gamedef->getTextureSource()->getTextureRaw(texture);
if (!stat_texture) if (!stat_texture)
return; return;
core::dimension2di srcd(stat_texture->getOriginalSize());
v2s32 p = upperleftpos; v2s32 p = pos;
if (corner & HUD_CORNER_LOWER)
p -= srcd.Height;
v2s32 steppos;
switch (drawdir) {
case HUD_DIR_RIGHT_LEFT:
steppos = v2s32(-1, 0);
break;
case HUD_DIR_TOP_BOTTOM:
steppos = v2s32(0, 1);
break;
case HUD_DIR_BOTTOM_TOP:
steppos = v2s32(0, -1);
break;
default:
steppos = v2s32(1, 0);
}
steppos.X *= srcd.Width;
steppos.Y *= srcd.Height;
for (s32 i = 0; i < count / 2; i++) for (s32 i = 0; i < count / 2; i++)
{ {
core::dimension2di srcd(stat_texture->getOriginalSize()); core::rect<s32> srcrect(0, 0, srcd.Width, srcd.Height);
const video::SColor color(255, 255, 255, 255); core::rect<s32> dstrect(srcrect);
const video::SColor colors[] = {color, color, color, color};
core::rect<s32> rect(0, 0, srcd.Width, srcd.Height); dstrect += p;
rect += p; driver->draw2DImage(stat_texture, dstrect, srcrect, NULL, colors, true);
driver->draw2DImage(stat_texture, rect, p += steppos;
core::rect<s32>(core::position2d<s32>(0, 0), srcd),
NULL, colors, true);
p += v2s32(srcd.Width, 0);
} }
if (count % 2 == 1) if (count % 2 == 1)
{ {
core::dimension2di srcd(stat_texture->getOriginalSize()); core::rect<s32> srcrect(0, 0, srcd.Width / 2, srcd.Height);
const video::SColor color(255, 255, 255, 255); core::rect<s32> dstrect(srcrect);
const video::SColor colors[] = {color, color, color, color};
core::rect<s32> rect(0, 0, srcd.Width / 2, srcd.Height); dstrect += p;
rect += p; driver->draw2DImage(stat_texture, dstrect, srcrect, NULL, colors, true);
srcd.Width /= 2;
driver->draw2DImage(stat_texture, rect,
core::rect<s32>(core::position2d<s32>(0, 0), srcd),
NULL, colors, true);
p += v2s32(srcd.Width * 2, 0);
} }
} }
@ -260,14 +277,15 @@ void Hud::drawHotbar(v2s32 centerlowerpos, s32 halfheartcount, u16 playeritem) {
v2s32 pos = centerlowerpos - v2s32(width / 2, hotbar_imagesize + padding * 2); v2s32 pos = centerlowerpos - v2s32(width / 2, hotbar_imagesize + padding * 2);
drawItem(pos, hotbar_imagesize, hotbar_itemcount, mainlist, playeritem + 1, 0); drawItem(pos, hotbar_imagesize, hotbar_itemcount, mainlist, playeritem + 1, 0);
drawStatbar(pos + v2s32(0, -20), "heart.png", halfheartcount); drawStatbar(pos - v2s32(0, 4), HUD_CORNER_LOWER, HUD_DIR_LEFT_RIGHT,
"heart.png", halfheartcount);
} }
void Hud::drawCrosshair() { void Hud::drawCrosshair() {
driver->draw2DLine(displaycenter - v2s32(10,0), driver->draw2DLine(displaycenter - v2s32(10, 0),
displaycenter + v2s32(10, 0), crosshair_argb); displaycenter + v2s32(10, 0), crosshair_argb);
driver->draw2DLine(displaycenter - v2s32(0,10), driver->draw2DLine(displaycenter - v2s32(0, 10),
displaycenter + v2s32(0, 10), crosshair_argb); displaycenter + v2s32(0, 10), crosshair_argb);
} }

View File

@ -27,6 +27,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define HUD_DIR_TOP_BOTTOM 2 #define HUD_DIR_TOP_BOTTOM 2
#define HUD_DIR_BOTTOM_TOP 3 #define HUD_DIR_BOTTOM_TOP 3
#define HUD_CORNER_UPPER 0
#define HUD_CORNER_LOWER 1
#define HUD_CORNER_CENTER 2
class Player; class Player;
enum HudElementType { enum HudElementType {
@ -102,7 +106,7 @@ public:
void drawItem(v2s32 upperleftpos, s32 imgsize, s32 itemcount, void drawItem(v2s32 upperleftpos, s32 imgsize, s32 itemcount,
InventoryList *mainlist, u16 selectitem, u16 direction); InventoryList *mainlist, u16 selectitem, u16 direction);
void drawLuaElements(); void drawLuaElements();
void drawStatbar(v2s32 upperleftpos, std::string texture, s32 count); void drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture, s32 count);
void drawHotbar(v2s32 centerlowerpos, s32 halfheartcount, u16 playeritem); void drawHotbar(v2s32 centerlowerpos, s32 halfheartcount, u16 playeritem);
void resizeHotbar(); void resizeHotbar();

View File

@ -749,7 +749,7 @@ int ObjectRef::l_hud_add(lua_State *L)
elem->text = getstringfield_default(L, 2, "text", ""); elem->text = getstringfield_default(L, 2, "text", "");
elem->number = getintfield_default(L, 2, "number", 0); elem->number = getintfield_default(L, 2, "number", 0);
elem->item = getintfield_default(L, 2, "item", 0); elem->item = getintfield_default(L, 2, "item", 0);
elem->dir = getintfield_default(L, 2, "dir", 0); elem->dir = getintfield_default(L, 2, "direction", 0);
u32 id = get_server(L)->hudAdd(player, elem); u32 id = get_server(L)->hudAdd(player, elem);
if (id == (u32)-1) { if (id == (u32)-1) {