Skip to content

Commit 666aae3

Browse files
committedApr 18, 2013
Split HUD code off to hud.cpp, make into a class, extensive Lua HUD modification
1 parent 49f6e34 commit 666aae3

12 files changed

+790
-813
lines changed
 

‎doc/lua_api.txt

+50-23
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,43 @@ Currently supported flags: absheight
401401
Also produce this same ore between the height range of -height_max and -height_min.
402402
Useful for having ore in sky realms without having to duplicate ore entries.
403403

404+
HUD element types
405+
-------------------
406+
The position field is used for all element types.
407+
To account for differing resolutions, the position coordinates are the percentage of the screen,
408+
ranging in value from 0 to 1.
409+
The name field is not yet used, but should contain a description of what the HUD element represents.
410+
Below are the specific uses for fields in each type; fields not listed for that type are ignored.
411+
412+
Note: Future revisions to the HUD API may be incompatible; the HUD API is still in the experimental stages.
413+
414+
- image
415+
Displays an image on the HUD.
416+
- scale: The scale of the image, with 1 being the original texture size.
417+
Only the X coordinate scale is used.
418+
- text: The name of the texture that is displayed.
419+
- text
420+
Displays text on the HUD.
421+
- scale: Defines the bounding rectangle of the text.
422+
A value such as {x=100, y=100} should work.
423+
- text: The text to be displayed in the HUD element.
424+
- number: An integer containing the RGB value of the color used to draw the text.
425+
Specify 0xFFFFFF for white text, 0xFF0000 for red, and so on.
426+
- statbar
427+
Displays a horizontal bar made up of half-images.
428+
- text: The name of the texture that is used.
429+
- number: The number of half-textures that are displayed.
430+
If odd, will end with a vertically center-split texture.
431+
- inventory
432+
- text: The name of the inventory list to be displayed.
433+
- number: Number of items in the inventory to be displayed.
434+
- item: Position of item that is selected.
435+
- direction: Direction in which the inventory list is drawn.
436+
0 draws from left to right,
437+
1 draws from right to left,
438+
2 draws from top to bottom, and
439+
3 draws from bottom to top.
440+
404441
Representations of simple things
405442
--------------------------------
406443
Position/vector:
@@ -1379,19 +1416,11 @@ Player-only: (no-op for other objects)
13791416
modifies per-player walking speed, jump height, and gravity.
13801417
Values default to 1 and act as offsets to the physics settings
13811418
in minetest.conf. nil will keep the current setting.
1382-
- hud_add(id, hud definition)
1383-
^ id is used for later reference
1384-
^ If this id has already been used, it will reset its drawform
1385-
- hud_rm(id): remove an id from the lua hud
1386-
- hud_change(id, stat, value): change a value of a previously added element
1387-
^ stat/table key: 0/position, 1/name, 2/scale, 3/text, 4/number,
1388-
^ 5/item, 6/dir
1389-
- hud_get_next_id(): get the next available id for a hud element
1390-
- hud_lock_next_bar(right): add a non-conflicting statbar
1391-
^ if right, will claim spot on right side, rather then left
1392-
^ returns element id on success, false otherwise
1393-
- hud_unlock_bar(id): remove a non-conflicting statbar
1394-
^ id is the value returned by calling hud_lock_next_bar()
1419+
- hud_add(hud definition): add a HUD element described by HUD def, returns ID number on success
1420+
- hud_remove(id): remove the HUD element of the specified id
1421+
- hud_change(id, stat, value): change a value of a previously added HUD element
1422+
^ element stat values: position, name, scale, text, number, item, dir
1423+
- hud_get(id): gets the HUD element definition structure of the specified ID
13951424

13961425
InvRef: Reference to an inventory
13971426
methods:
@@ -1815,20 +1844,18 @@ Detached inventory callbacks
18151844
^ No return value
18161845
}
18171846

1818-
HUD Definition (hud_add)
1847+
HUD Definition (hud_add, hud_get)
18191848
{
1820-
type = "I", -- One of "I"(image), "S"(statbar),
1821-
^ "T"(text), "i"(inv)
1822-
position = {x=0.5, y=0.5}, -- Left corner position
1849+
type = "image",
1850+
^ type of HUD element, can be either of "image", "text", "statbar", or "inventory"
1851+
position = {x=0.5, y=0.5},
1852+
^ Left corner position of element
18231853
name = "<name>",
18241854
scale = {x=2, y=2},
18251855
text = "<text>",
1826-
^ Used as texture name for statbars and images, and as list name
1827-
^ for inv
18281856
number = 2,
1829-
^ Used as stat for statbar, and as # of items for inv
1830-
item = 3, -- Selected item in inv. 0 -> no item selected
1857+
item = 3,
1858+
^ Selected item in inventory. 0 for no item selected.
18311859
dir = 0,
1832-
^ dir/inv direction: 0/left-right, 1/right-left,
1833-
^ 2/top-bottom, 3/bottom-top
1860+
^ Direction: 0: left-right, 1: right-left, 2: top-bottom, 3: bottom-top
18341861
}

‎src/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ set(minetest_SRCS
327327
particles.cpp
328328
clientobject.cpp
329329
chat.cpp
330+
hud.cpp
330331
guiMainMenu.cpp
331332
guiKeyChangeMenu.cpp
332333
guiMessageMenu.cpp

‎src/client.cpp

+37-36
Original file line numberDiff line numberDiff line change
@@ -2042,35 +2042,35 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
20422042
}
20432043
else if(command == TOCLIENT_HUDADD)
20442044
{
2045-
std::string datastring((char*)&data[2], datasize-2);
2045+
std::string datastring((char *)&data[2], datasize - 2);
20462046
std::istringstream is(datastring, std::ios_base::binary);
20472047

2048-
u32 id = readU32(is);
2049-
u8 type = readU8(is);
2050-
core::vector2df pos = readV2F1000(is);
2051-
std::string name = deSerializeString(is);
2052-
core::vector2df scale = readV2F1000(is);
2053-
std::string text = deSerializeString(is);
2054-
u32 number = readU32(is);
2055-
u32 item = readU32(is);
2056-
u32 dir = readU32(is);
2048+
u32 id = readU32(is);
2049+
u8 type = readU8(is);
2050+
v2f pos = readV2F1000(is);
2051+
std::string name = deSerializeString(is);
2052+
v2f scale = readV2F1000(is);
2053+
std::string text = deSerializeString(is);
2054+
u32 number = readU32(is);
2055+
u32 item = readU32(is);
2056+
u32 dir = readU32(is);
20572057

20582058
ClientEvent event;
20592059
event.type = CE_HUDADD;
2060-
event.hudadd.id = id;
2061-
event.hudadd.type = type;
2062-
event.hudadd.pos = new v2f(pos);
2063-
event.hudadd.name = new std::string(name);
2064-
event.hudadd.scale = new v2f(scale);
2065-
event.hudadd.text = new std::string(text);
2060+
event.hudadd.id = id;
2061+
event.hudadd.type = type;
2062+
event.hudadd.pos = new v2f(pos);
2063+
event.hudadd.name = new std::string(name);
2064+
event.hudadd.scale = new v2f(scale);
2065+
event.hudadd.text = new std::string(text);
20662066
event.hudadd.number = number;
2067-
event.hudadd.item = item;
2068-
event.hudadd.dir = dir;
2067+
event.hudadd.item = item;
2068+
event.hudadd.dir = dir;
20692069
m_client_event_queue.push_back(event);
20702070
}
20712071
else if(command == TOCLIENT_HUDRM)
20722072
{
2073-
std::string datastring((char*)&data[2], datasize-2);
2073+
std::string datastring((char *)&data[2], datasize - 2);
20742074
std::istringstream is(datastring, std::ios_base::binary);
20752075

20762076
u32 id = readU32(is);
@@ -2081,30 +2081,31 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
20812081
m_client_event_queue.push_back(event);
20822082
}
20832083
else if(command == TOCLIENT_HUDCHANGE)
2084-
{
2085-
std::string datastring((char*)&data[2], datasize-2);
2084+
{
2085+
std::string sdata;
2086+
v2f v2fdata;
2087+
u32 intdata = 0;
2088+
2089+
std::string datastring((char *)&data[2], datasize - 2);
20862090
std::istringstream is(datastring, std::ios_base::binary);
20872091

2088-
u32 id = readU32(is);
2089-
u8 stat = readU8(is);
2090-
core::vector2df v2fdata;
2091-
std::string sdata;
2092-
u32 data = 0;
2093-
if(stat == 0 || stat == 2) {
2092+
u32 id = readU32(is);
2093+
u8 stat = (HudElementStat)readU8(is);
2094+
2095+
if (stat == HUD_STAT_POS || stat == HUD_STAT_SCALE)
20942096
v2fdata = readV2F1000(is);
2095-
} else if(stat == 1 || stat == 3) {
2097+
else if (stat == HUD_STAT_NAME || stat == HUD_STAT_TEXT)
20962098
sdata = deSerializeString(is);
2097-
} else {
2098-
data = readU32(is);
2099-
}
2100-
2099+
else
2100+
intdata = readU32(is);
2101+
21012102
ClientEvent event;
21022103
event.type = CE_HUDCHANGE;
2103-
event.hudchange.id = id;
2104-
event.hudchange.stat = stat;
2104+
event.hudchange.id = id;
2105+
event.hudchange.stat = (HudElementStat)stat;
21052106
event.hudchange.v2fdata = new v2f(v2fdata);
2106-
event.hudchange.sdata = new std::string(sdata);
2107-
event.hudchange.data = data;
2107+
event.hudchange.sdata = new std::string(sdata);
2108+
event.hudchange.data = intdata;
21082109
m_client_event_queue.push_back(event);
21092110
}
21102111
else

‎src/client.h

+14-14
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ struct ClientEvent
186186
f32 camera_point_target_z;
187187
} deathscreen;
188188
struct{
189-
std::string* formspec;
190-
std::string* formname;
189+
std::string *formspec;
190+
std::string *formname;
191191
} show_formspec;
192192
struct{
193193
} textures_updated;
@@ -221,24 +221,24 @@ struct ClientEvent
221221
u32 id;
222222
} delete_particlespawner;
223223
struct{
224-
u32 id;
225-
u8 type;
226-
v2f* pos;
227-
std::string* name;
228-
v2f* scale;
229-
std::string* text;
230-
u32 number;
231-
u32 item;
232-
u32 dir;
224+
u32 id;
225+
u8 type;
226+
v2f *pos;
227+
std::string *name;
228+
v2f *scale;
229+
std::string *text;
230+
u32 number;
231+
u32 item;
232+
u32 dir;
233233
} hudadd;
234234
struct{
235235
u32 id;
236236
} hudrm;
237237
struct{
238238
u32 id;
239-
u8 stat;
240-
v2f* v2fdata;
241-
std::string* sdata;
239+
HudElementStat stat;
240+
v2f *v2fdata;
241+
std::string *sdata;
242242
u32 data;
243243
} hudchange;
244244
};

0 commit comments

Comments
 (0)
Please sign in to comment.