Skip to content

Commit 9894167

Browse files
kaezaceleron55
authored andcommittedApr 23, 2013
Added offset support for HUD items
1 parent 7c37b18 commit 9894167

9 files changed

+43
-8
lines changed
 

‎doc/lua_api.txt

+8
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,9 @@ The direction field is the direction in which something is drawn.
412412
The alignment field specifies how the item will be aligned. It ranges from -1 to 1,
413413
with 0 being the center, -1 is moved to the left/up, and 1 is to the right/down. Fractional
414414
values can be used.
415+
The offset field specifies a pixel offset from the position. Contrary to position,
416+
the offset is not scaled to screen size. This allows for some precisely-positioned
417+
items in the HUD.
415418
Below are the specific uses for fields in each type; fields not listed for that type are ignored.
416419

417420
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
422425
Only the X coordinate scale is used.
423426
- text: The name of the texture that is displayed.
424427
- alignment: The alignment of the image.
428+
- offset: offset in pixels from position.
425429
- text
426430
Displays text on the HUD.
427431
- 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
430434
- number: An integer containing the RGB value of the color used to draw the text.
431435
Specify 0xFFFFFF for white text, 0xFF0000 for red, and so on.
432436
- alignment: The alignment of the text.
437+
- offset: offset in pixels from position.
433438
- statbar
434439
Displays a horizontal bar made up of half-images.
435440
- text: The name of the texture that is used.
436441
- number: The number of half-textures that are displayed.
437442
If odd, will end with a vertically center-split texture.
438443
- direction
444+
- offset: offset in pixels from position.
439445
- inventory
440446
- text: The name of the inventory list to be displayed.
441447
- number: Number of items in the inventory to be displayed.
@@ -1864,4 +1870,6 @@ HUD Definition (hud_add, hud_get)
18641870
^ Direction: 0: left-right, 1: right-left, 2: top-bottom, 3: bottom-top
18651871
alignment = {x=0, y=0},
18661872
^ See "HUD Element Types"
1873+
offset = {x=0, y=0},
1874+
^ See "HUD Element Types"
18671875
}

‎src/client.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -2055,6 +2055,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
20552055
u32 item = readU32(is);
20562056
u32 dir = readU32(is);
20572057
v2f align = readV2F1000(is);
2058+
v2f offset = readV2F1000(is);
20582059

20592060
ClientEvent event;
20602061
event.type = CE_HUDADD;
@@ -2068,6 +2069,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
20682069
event.hudadd.item = item;
20692070
event.hudadd.dir = dir;
20702071
event.hudadd.align = new v2f(align);
2072+
event.hudadd.offset = new v2f(offset);
20712073
m_client_event_queue.push_back(event);
20722074
}
20732075
else if(command == TOCLIENT_HUDRM)

‎src/client.h

+1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ struct ClientEvent
231231
u32 item;
232232
u32 dir;
233233
v2f *align;
234+
v2f *offset;
234235
} hudadd;
235236
struct{
236237
u32 id;

‎src/clientserver.h

+1
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ enum ToClientCommand
453453
u32 item
454454
u32 dir
455455
v2f1000 align
456+
v2f1000 offset
456457
*/
457458

458459
TOCLIENT_HUDRM = 0x50,

‎src/game.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -2107,6 +2107,7 @@ void the_game(
21072107
delete event.hudadd.scale;
21082108
delete event.hudadd.text;
21092109
delete event.hudadd.align;
2110+
delete event.hudadd.offset;
21102111
continue;
21112112
}
21122113

@@ -2120,6 +2121,7 @@ void the_game(
21202121
e->item = event.hudadd.item;
21212122
e->dir = event.hudadd.dir;
21222123
e->align = *event.hudadd.align;
2124+
e->offset = *event.hudadd.offset;
21232125

21242126
if (id == nhudelem)
21252127
player->hud.push_back(e);
@@ -2131,6 +2133,7 @@ void the_game(
21312133
delete event.hudadd.scale;
21322134
delete event.hudadd.text;
21332135
delete event.hudadd.align;
2136+
delete event.hudadd.offset;
21342137
}
21352138
else if (event.type == CE_HUDRM)
21362139
{
@@ -2175,6 +2178,9 @@ void the_game(
21752178
case HUD_STAT_ALIGN:
21762179
e->align = *event.hudchange.v2fdata;
21772180
break;
2181+
case HUD_STAT_OFFSET:
2182+
e->offset = *event.hudchange.v2fdata;
2183+
break;
21782184
}
21792185

21802186
delete event.hudchange.v2fdata;

‎src/hud.cpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ void Hud::drawLuaElements() {
189189
v2s32 offset((e->align.X - 1.0) * ((imgsize.Width * e->scale.X) / 2),
190190
(e->align.Y - 1.0) * ((imgsize.Height * e->scale.X) / 2));
191191
rect += offset;
192+
rect += v2s32(e->offset.X, e->offset.Y);
192193
driver->draw2DImage(texture, rect,
193194
core::rect<s32>(core::position2d<s32>(0,0), imgsize),
194195
NULL, colors, true);
@@ -202,11 +203,13 @@ void Hud::drawLuaElements() {
202203
core::dimension2d<u32> textsize = font->getDimension(text.c_str());
203204
v2s32 offset((e->align.X - 1.0) * (textsize.Width / 2),
204205
(e->align.Y - 1.0) * (textsize.Height / 2));
205-
font->draw(text.c_str(), size + pos + offset, color);
206+
v2s32 offs(e->offset.X, e->offset.Y);
207+
font->draw(text.c_str(), size + pos + offset + offs, color);
208+
break; }
209+
case HUD_ELEM_STATBAR: {
210+
v2s32 offs(e->offset.X, e->offset.Y);
211+
drawStatbar(pos, HUD_CORNER_UPPER, e->dir, e->text, e->number, offs);
206212
break; }
207-
case HUD_ELEM_STATBAR:
208-
drawStatbar(pos, HUD_CORNER_UPPER, e->dir, e->text, e->number);
209-
break;
210213
case HUD_ELEM_INVENTORY: {
211214
InventoryList *inv = inventory->getList(e->text);
212215
drawItem(pos, hotbar_imagesize, e->number, inv, e->item, e->dir);
@@ -219,7 +222,7 @@ void Hud::drawLuaElements() {
219222
}
220223

221224

222-
void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture, s32 count) {
225+
void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture, s32 count, v2s32 offset) {
223226
const video::SColor color(255, 255, 255, 255);
224227
const video::SColor colors[] = {color, color, color, color};
225228

@@ -234,6 +237,8 @@ void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture, s
234237
if (corner & HUD_CORNER_LOWER)
235238
p -= srcd.Height;
236239

240+
p += offset;
241+
237242
v2s32 steppos;
238243
switch (drawdir) {
239244
case HUD_DIR_RIGHT_LEFT:
@@ -285,7 +290,7 @@ void Hud::drawHotbar(v2s32 centerlowerpos, s32 halfheartcount, u16 playeritem) {
285290

286291
drawItem(pos, hotbar_imagesize, hotbar_itemcount, mainlist, playeritem + 1, 0);
287292
drawStatbar(pos - v2s32(0, 4), HUD_CORNER_LOWER, HUD_DIR_LEFT_RIGHT,
288-
"heart.png", halfheartcount);
293+
"heart.png", halfheartcount, v2s32(0, 0));
289294
}
290295

291296

‎src/hud.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ enum HudElementStat {
4848
HUD_STAT_NUMBER,
4949
HUD_STAT_ITEM,
5050
HUD_STAT_DIR,
51-
HUD_STAT_ALIGN
51+
HUD_STAT_ALIGN,
52+
HUD_STAT_OFFSET
5253
};
5354

5455
struct HudElement {
@@ -61,6 +62,7 @@ struct HudElement {
6162
u32 item;
6263
u32 dir;
6364
v2f align;
65+
v2f offset;
6466
};
6567

6668

@@ -108,7 +110,7 @@ class Hud {
108110
void drawItem(v2s32 upperleftpos, s32 imgsize, s32 itemcount,
109111
InventoryList *mainlist, u16 selectitem, u16 direction);
110112
void drawLuaElements();
111-
void drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture, s32 count);
113+
void drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture, s32 count, v2s32 offset);
112114

113115
void drawHotbar(v2s32 centerlowerpos, s32 halfheartcount, u16 playeritem);
114116
void resizeHotbar();

‎src/scriptapi_object.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct EnumString es_HudElementStat[] =
4848
{HUD_STAT_ITEM, "item"},
4949
{HUD_STAT_DIR, "direction"},
5050
{HUD_STAT_ALIGN, "alignment"},
51+
{HUD_STAT_OFFSET, "offset"},
5152
{0, NULL},
5253
};
5354

@@ -756,6 +757,10 @@ int ObjectRef::l_hud_add(lua_State *L)
756757
elem->align = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
757758
lua_pop(L, 1);
758759

760+
lua_getfield(L, 2, "offset");
761+
elem->offset = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
762+
lua_pop(L, 1);
763+
759764
u32 id = get_server(L)->hudAdd(player, elem);
760765
if (id == (u32)-1) {
761766
delete elem;
@@ -841,6 +846,9 @@ int ObjectRef::l_hud_change(lua_State *L)
841846
case HUD_STAT_ALIGN:
842847
e->align = read_v2f(L, 4);
843848
value = &e->align;
849+
case HUD_STAT_OFFSET:
850+
e->offset = read_v2f(L, 4);
851+
value = &e->offset;
844852
}
845853

846854
get_server(L)->hudChange(player, id, stat, value);

‎src/server.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -3617,6 +3617,7 @@ void Server::SendHUDAdd(u16 peer_id, u32 id, HudElement *form)
36173617
writeU32(os, form->item);
36183618
writeU32(os, form->dir);
36193619
writeV2F1000(os, form->align);
3620+
writeV2F1000(os, form->offset);
36203621

36213622
// Make data buffer
36223623
std::string s = os.str();
@@ -3652,6 +3653,7 @@ void Server::SendHUDChange(u16 peer_id, u32 id, HudElementStat stat, void *value
36523653
case HUD_STAT_POS:
36533654
case HUD_STAT_SCALE:
36543655
case HUD_STAT_ALIGN:
3656+
case HUD_STAT_OFFSET:
36553657
writeV2F1000(os, *(v2f *)value);
36563658
break;
36573659
case HUD_STAT_NAME:

0 commit comments

Comments
 (0)
Please sign in to comment.