From 9894167bbf516c40bf2b8577179ff8f13b8b54e2 Mon Sep 17 00:00:00 2001 From: Diego Martínez Date: Mon, 22 Apr 2013 20:47:59 -0300 Subject: Added offset support for HUD items --- doc/lua_api.txt | 8 ++++++++ src/client.cpp | 2 ++ src/client.h | 1 + src/clientserver.h | 1 + src/game.cpp | 6 ++++++ src/hud.cpp | 17 +++++++++++------ src/hud.h | 6 ++++-- src/scriptapi_object.cpp | 8 ++++++++ src/server.cpp | 2 ++ 9 files changed, 43 insertions(+), 8 deletions(-) diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 774448778..76f687f8e 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -412,6 +412,9 @@ The direction field is the direction in which something is drawn. The alignment field specifies how the item will be aligned. It ranges from -1 to 1, with 0 being the center, -1 is moved to the left/up, and 1 is to the right/down. Fractional values can be used. +The offset field specifies a pixel offset from the position. Contrary to position, +the offset is not scaled to screen size. This allows for some precisely-positioned +items in the HUD. 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. @@ -422,6 +425,7 @@ Note: Future revisions to the HUD API may be incompatible; the HUD API is still Only the X coordinate scale is used. - text: The name of the texture that is displayed. - alignment: The alignment of the image. + - offset: offset in pixels from position. - text Displays text on the HUD. - scale: Defines the bounding rectangle of the text. @@ -430,12 +434,14 @@ Note: Future revisions to the HUD API may be incompatible; the HUD API is still - number: An integer containing the RGB value of the color used to draw the text. Specify 0xFFFFFF for white text, 0xFF0000 for red, and so on. - alignment: The alignment of the text. + - offset: offset in pixels from position. - statbar Displays a horizontal bar made up of half-images. - text: The name of the texture that is used. - number: The number of half-textures that are displayed. If odd, will end with a vertically center-split texture. - direction + - offset: offset in pixels from position. - inventory - text: The name of the inventory list to be displayed. - number: Number of items in the inventory to be displayed. @@ -1864,4 +1870,6 @@ HUD Definition (hud_add, hud_get) ^ Direction: 0: left-right, 1: right-left, 2: top-bottom, 3: bottom-top alignment = {x=0, y=0}, ^ See "HUD Element Types" + offset = {x=0, y=0}, + ^ See "HUD Element Types" } diff --git a/src/client.cpp b/src/client.cpp index 8db6f2f40..15fb6932a 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -2055,6 +2055,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id) u32 item = readU32(is); u32 dir = readU32(is); v2f align = readV2F1000(is); + v2f offset = readV2F1000(is); ClientEvent event; event.type = CE_HUDADD; @@ -2068,6 +2069,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id) event.hudadd.item = item; event.hudadd.dir = dir; event.hudadd.align = new v2f(align); + event.hudadd.offset = new v2f(offset); m_client_event_queue.push_back(event); } else if(command == TOCLIENT_HUDRM) diff --git a/src/client.h b/src/client.h index ff42f3e05..67ba6c565 100644 --- a/src/client.h +++ b/src/client.h @@ -231,6 +231,7 @@ struct ClientEvent u32 item; u32 dir; v2f *align; + v2f *offset; } hudadd; struct{ u32 id; diff --git a/src/clientserver.h b/src/clientserver.h index 0418d10b3..5e981c202 100644 --- a/src/clientserver.h +++ b/src/clientserver.h @@ -453,6 +453,7 @@ enum ToClientCommand u32 item u32 dir v2f1000 align + v2f1000 offset */ TOCLIENT_HUDRM = 0x50, diff --git a/src/game.cpp b/src/game.cpp index 2c73dfa6f..cbd9eab4d 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -2107,6 +2107,7 @@ void the_game( delete event.hudadd.scale; delete event.hudadd.text; delete event.hudadd.align; + delete event.hudadd.offset; continue; } @@ -2120,6 +2121,7 @@ void the_game( e->item = event.hudadd.item; e->dir = event.hudadd.dir; e->align = *event.hudadd.align; + e->offset = *event.hudadd.offset; if (id == nhudelem) player->hud.push_back(e); @@ -2131,6 +2133,7 @@ void the_game( delete event.hudadd.scale; delete event.hudadd.text; delete event.hudadd.align; + delete event.hudadd.offset; } else if (event.type == CE_HUDRM) { @@ -2175,6 +2178,9 @@ void the_game( case HUD_STAT_ALIGN: e->align = *event.hudchange.v2fdata; break; + case HUD_STAT_OFFSET: + e->offset = *event.hudchange.v2fdata; + break; } delete event.hudchange.v2fdata; diff --git a/src/hud.cpp b/src/hud.cpp index 0f3ab40d2..9db92db52 100644 --- a/src/hud.cpp +++ b/src/hud.cpp @@ -189,6 +189,7 @@ void Hud::drawLuaElements() { v2s32 offset((e->align.X - 1.0) * ((imgsize.Width * e->scale.X) / 2), (e->align.Y - 1.0) * ((imgsize.Height * e->scale.X) / 2)); rect += offset; + rect += v2s32(e->offset.X, e->offset.Y); driver->draw2DImage(texture, rect, core::rect(core::position2d(0,0), imgsize), NULL, colors, true); @@ -202,11 +203,13 @@ void Hud::drawLuaElements() { core::dimension2d textsize = font->getDimension(text.c_str()); v2s32 offset((e->align.X - 1.0) * (textsize.Width / 2), (e->align.Y - 1.0) * (textsize.Height / 2)); - font->draw(text.c_str(), size + pos + offset, color); + v2s32 offs(e->offset.X, e->offset.Y); + font->draw(text.c_str(), size + pos + offset + offs, color); + break; } + case HUD_ELEM_STATBAR: { + v2s32 offs(e->offset.X, e->offset.Y); + drawStatbar(pos, HUD_CORNER_UPPER, e->dir, e->text, e->number, offs); break; } - case HUD_ELEM_STATBAR: - drawStatbar(pos, HUD_CORNER_UPPER, e->dir, e->text, e->number); - break; case HUD_ELEM_INVENTORY: { InventoryList *inv = inventory->getList(e->text); drawItem(pos, hotbar_imagesize, e->number, inv, e->item, e->dir); @@ -219,7 +222,7 @@ void Hud::drawLuaElements() { } -void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture, s32 count) { +void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture, s32 count, v2s32 offset) { const video::SColor color(255, 255, 255, 255); const video::SColor colors[] = {color, color, color, color}; @@ -234,6 +237,8 @@ void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture, s if (corner & HUD_CORNER_LOWER) p -= srcd.Height; + p += offset; + v2s32 steppos; switch (drawdir) { case HUD_DIR_RIGHT_LEFT: @@ -285,7 +290,7 @@ void Hud::drawHotbar(v2s32 centerlowerpos, s32 halfheartcount, u16 playeritem) { drawItem(pos, hotbar_imagesize, hotbar_itemcount, mainlist, playeritem + 1, 0); drawStatbar(pos - v2s32(0, 4), HUD_CORNER_LOWER, HUD_DIR_LEFT_RIGHT, - "heart.png", halfheartcount); + "heart.png", halfheartcount, v2s32(0, 0)); } diff --git a/src/hud.h b/src/hud.h index 7a1dff3d8..104a2f00d 100644 --- a/src/hud.h +++ b/src/hud.h @@ -48,7 +48,8 @@ enum HudElementStat { HUD_STAT_NUMBER, HUD_STAT_ITEM, HUD_STAT_DIR, - HUD_STAT_ALIGN + HUD_STAT_ALIGN, + HUD_STAT_OFFSET }; struct HudElement { @@ -61,6 +62,7 @@ struct HudElement { u32 item; u32 dir; v2f align; + v2f offset; }; @@ -108,7 +110,7 @@ public: void drawItem(v2s32 upperleftpos, s32 imgsize, s32 itemcount, InventoryList *mainlist, u16 selectitem, u16 direction); void drawLuaElements(); - void drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture, s32 count); + void drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture, s32 count, v2s32 offset); void drawHotbar(v2s32 centerlowerpos, s32 halfheartcount, u16 playeritem); void resizeHotbar(); diff --git a/src/scriptapi_object.cpp b/src/scriptapi_object.cpp index c07f6565d..6669ad871 100644 --- a/src/scriptapi_object.cpp +++ b/src/scriptapi_object.cpp @@ -48,6 +48,7 @@ struct EnumString es_HudElementStat[] = {HUD_STAT_ITEM, "item"}, {HUD_STAT_DIR, "direction"}, {HUD_STAT_ALIGN, "alignment"}, + {HUD_STAT_OFFSET, "offset"}, {0, NULL}, }; @@ -756,6 +757,10 @@ int ObjectRef::l_hud_add(lua_State *L) elem->align = lua_istable(L, -1) ? read_v2f(L, -1) : v2f(); lua_pop(L, 1); + lua_getfield(L, 2, "offset"); + elem->offset = lua_istable(L, -1) ? read_v2f(L, -1) : v2f(); + lua_pop(L, 1); + u32 id = get_server(L)->hudAdd(player, elem); if (id == (u32)-1) { delete elem; @@ -841,6 +846,9 @@ int ObjectRef::l_hud_change(lua_State *L) case HUD_STAT_ALIGN: e->align = read_v2f(L, 4); value = &e->align; + case HUD_STAT_OFFSET: + e->offset = read_v2f(L, 4); + value = &e->offset; } get_server(L)->hudChange(player, id, stat, value); diff --git a/src/server.cpp b/src/server.cpp index b8f3e4da8..dda2165c8 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -3617,6 +3617,7 @@ void Server::SendHUDAdd(u16 peer_id, u32 id, HudElement *form) writeU32(os, form->item); writeU32(os, form->dir); writeV2F1000(os, form->align); + writeV2F1000(os, form->offset); // Make data buffer std::string s = os.str(); @@ -3652,6 +3653,7 @@ void Server::SendHUDChange(u16 peer_id, u32 id, HudElementStat stat, void *value case HUD_STAT_POS: case HUD_STAT_SCALE: case HUD_STAT_ALIGN: + case HUD_STAT_OFFSET: writeV2F1000(os, *(v2f *)value); break; case HUD_STAT_NAME: -- cgit v1.2.3