From 7c37b1891adcddc0e7d11e5faafddaa554443318 Mon Sep 17 00:00:00 2001 From: Diego Martínez Date: Mon, 22 Apr 2013 06:53:55 -0300 Subject: Added support for alignment in HUD items --- src/client.cpp | 2 ++ src/client.h | 1 + src/clientserver.h | 1 + src/game.cpp | 6 ++++++ src/hud.cpp | 9 ++++++++- src/hud.h | 4 +++- src/scriptapi_object.cpp | 8 ++++++++ src/server.cpp | 2 ++ 8 files changed, 31 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/client.cpp b/src/client.cpp index 03a710599..8db6f2f40 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -2054,6 +2054,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id) u32 number = readU32(is); u32 item = readU32(is); u32 dir = readU32(is); + v2f align = readV2F1000(is); ClientEvent event; event.type = CE_HUDADD; @@ -2066,6 +2067,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id) event.hudadd.number = number; event.hudadd.item = item; event.hudadd.dir = dir; + event.hudadd.align = new v2f(align); m_client_event_queue.push_back(event); } else if(command == TOCLIENT_HUDRM) diff --git a/src/client.h b/src/client.h index f59588680..ff42f3e05 100644 --- a/src/client.h +++ b/src/client.h @@ -230,6 +230,7 @@ struct ClientEvent u32 number; u32 item; u32 dir; + v2f *align; } hudadd; struct{ u32 id; diff --git a/src/clientserver.h b/src/clientserver.h index 6f7bb4402..0418d10b3 100644 --- a/src/clientserver.h +++ b/src/clientserver.h @@ -452,6 +452,7 @@ enum ToClientCommand u32 number u32 item u32 dir + v2f1000 align */ TOCLIENT_HUDRM = 0x50, diff --git a/src/game.cpp b/src/game.cpp index a2d94ac0a..2c73dfa6f 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -2106,6 +2106,7 @@ void the_game( delete event.hudadd.name; delete event.hudadd.scale; delete event.hudadd.text; + delete event.hudadd.align; continue; } @@ -2118,6 +2119,7 @@ void the_game( e->number = event.hudadd.number; e->item = event.hudadd.item; e->dir = event.hudadd.dir; + e->align = *event.hudadd.align; if (id == nhudelem) player->hud.push_back(e); @@ -2128,6 +2130,7 @@ void the_game( delete event.hudadd.name; delete event.hudadd.scale; delete event.hudadd.text; + delete event.hudadd.align; } else if (event.type == CE_HUDRM) { @@ -2169,6 +2172,9 @@ void the_game( case HUD_STAT_DIR: e->dir = event.hudchange.data; break; + case HUD_STAT_ALIGN: + e->align = *event.hudchange.v2fdata; + break; } delete event.hudchange.v2fdata; diff --git a/src/hud.cpp b/src/hud.cpp index 77cf23173..0f3ab40d2 100644 --- a/src/hud.cpp +++ b/src/hud.cpp @@ -186,6 +186,9 @@ void Hud::drawLuaElements() { core::rect rect(0, 0, imgsize.Width * e->scale.X, imgsize.Height * e->scale.X); rect += pos; + 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; driver->draw2DImage(texture, rect, core::rect(core::position2d(0,0), imgsize), NULL, colors, true); @@ -195,7 +198,11 @@ void Hud::drawLuaElements() { (e->number >> 8) & 0xFF, (e->number >> 0) & 0xFF); core::rect size(0, 0, e->scale.X, text_height * e->scale.Y); - font->draw(narrow_to_wide(e->text).c_str(), size + pos, color); + std::wstring text = narrow_to_wide(e->text); + 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); break; } case HUD_ELEM_STATBAR: drawStatbar(pos, HUD_CORNER_UPPER, e->dir, e->text, e->number); diff --git a/src/hud.h b/src/hud.h index 274a669c4..7a1dff3d8 100644 --- a/src/hud.h +++ b/src/hud.h @@ -47,7 +47,8 @@ enum HudElementStat { HUD_STAT_TEXT, HUD_STAT_NUMBER, HUD_STAT_ITEM, - HUD_STAT_DIR + HUD_STAT_DIR, + HUD_STAT_ALIGN }; struct HudElement { @@ -59,6 +60,7 @@ struct HudElement { u32 number; u32 item; u32 dir; + v2f align; }; diff --git a/src/scriptapi_object.cpp b/src/scriptapi_object.cpp index 9152c9eb3..c07f6565d 100644 --- a/src/scriptapi_object.cpp +++ b/src/scriptapi_object.cpp @@ -47,6 +47,7 @@ struct EnumString es_HudElementStat[] = {HUD_STAT_NUMBER, "number"}, {HUD_STAT_ITEM, "item"}, {HUD_STAT_DIR, "direction"}, + {HUD_STAT_ALIGN, "alignment"}, {0, NULL}, }; @@ -751,6 +752,10 @@ int ObjectRef::l_hud_add(lua_State *L) elem->item = getintfield_default(L, 2, "item", 0); elem->dir = getintfield_default(L, 2, "direction", 0); + lua_getfield(L, 2, "alignment"); + elem->align = 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; @@ -833,6 +838,9 @@ int ObjectRef::l_hud_change(lua_State *L) case HUD_STAT_DIR: e->dir = lua_tonumber(L, 4); value = &e->dir; + case HUD_STAT_ALIGN: + e->align = read_v2f(L, 4); + value = &e->align; } get_server(L)->hudChange(player, id, stat, value); diff --git a/src/server.cpp b/src/server.cpp index 7fad623c2..b8f3e4da8 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -3616,6 +3616,7 @@ void Server::SendHUDAdd(u16 peer_id, u32 id, HudElement *form) writeU32(os, form->number); writeU32(os, form->item); writeU32(os, form->dir); + writeV2F1000(os, form->align); // Make data buffer std::string s = os.str(); @@ -3650,6 +3651,7 @@ void Server::SendHUDChange(u16 peer_id, u32 id, HudElementStat stat, void *value switch (stat) { case HUD_STAT_POS: case HUD_STAT_SCALE: + case HUD_STAT_ALIGN: writeV2F1000(os, *(v2f *)value); break; case HUD_STAT_NAME: -- cgit v1.2.3