Generalize hud_builtin_enable into hud_set_flags

This commit is contained in:
kwolekr 2013-04-25 19:27:22 -04:00
parent d83602d98e
commit d3f0ce6224
12 changed files with 85 additions and 107 deletions

@ -1434,9 +1434,10 @@ Player-only: (no-op for other objects)
- hud_change(id, stat, value): change a value of a previously added HUD element - hud_change(id, stat, value): change a value of a previously added HUD element
^ element stat values: position, name, scale, text, number, item, dir ^ element stat values: position, name, scale, text, number, item, dir
- hud_get(id): gets the HUD element definition structure of the specified ID - hud_get(id): gets the HUD element definition structure of the specified ID
- hud_builtin_enable(what, flag): enable or disable built-in HUD items - hud_set_flags(flags): sets specified HUD flags to true/false
^ what: "hotbar", "healthbar", "crosshair", "wielditem" ^ flags: (is visible) hotbar, healthbar, crosshair, wielditem
^ flag: true/false ^ pass a table containing a true/false value of each flag to be set or unset
^ if a flag is nil, the flag is not modified
InvRef: Reference to an inventory InvRef: Reference to an inventory
methods: methods:

@ -2097,8 +2097,8 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
u32 id = readU32(is); u32 id = readU32(is);
u8 stat = (HudElementStat)readU8(is); u8 stat = (HudElementStat)readU8(is);
if (stat == HUD_STAT_POS || stat == HUD_STAT_SCALE if (stat == HUD_STAT_POS || stat == HUD_STAT_SCALE ||
|| stat == HUD_STAT_ALIGN || stat == HUD_STAT_OFFSET) stat == HUD_STAT_ALIGN || stat == HUD_STAT_OFFSET)
v2fdata = readV2F1000(is); v2fdata = readV2F1000(is);
else if (stat == HUD_STAT_NAME || stat == HUD_STAT_TEXT) else if (stat == HUD_STAT_NAME || stat == HUD_STAT_TEXT)
sdata = deSerializeString(is); sdata = deSerializeString(is);
@ -2114,19 +2114,19 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
event.hudchange.data = intdata; event.hudchange.data = intdata;
m_client_event_queue.push_back(event); m_client_event_queue.push_back(event);
} }
else if(command == TOCLIENT_HUD_BUILTIN_ENABLE) else if(command == TOCLIENT_HUD_SET_FLAGS)
{ {
std::string datastring((char *)&data[2], datasize - 2); std::string datastring((char *)&data[2], datasize - 2);
std::istringstream is(datastring, std::ios_base::binary); std::istringstream is(datastring, std::ios_base::binary);
u32 id = readU8(is); Player *player = m_env.getLocalPlayer();
bool flag = (readU8(is) ? true : false); assert(player != NULL);
ClientEvent event; u32 flags = readU32(is);
event.type = CE_HUD_BUILTIN_ENABLE; u32 mask = readU32(is);
event.hudbuiltin.id = (HudBuiltinElement)id;
event.hudbuiltin.flag = flag; player->hud_flags &= ~mask;
m_client_event_queue.push_back(event); player->hud_flags |= flags;
} }
else else
{ {

@ -163,8 +163,7 @@ enum ClientEventType
CE_DELETE_PARTICLESPAWNER, CE_DELETE_PARTICLESPAWNER,
CE_HUDADD, CE_HUDADD,
CE_HUDRM, CE_HUDRM,
CE_HUDCHANGE, CE_HUDCHANGE
CE_HUD_BUILTIN_ENABLE
}; };
struct ClientEvent struct ClientEvent
@ -244,10 +243,6 @@ struct ClientEvent
std::string *sdata; std::string *sdata;
u32 data; u32 data;
} hudchange; } hudchange;
struct{
u32 id;
u32 flag;
} hudbuiltin;
}; };
}; };

@ -474,11 +474,11 @@ enum ToClientCommand
u32 data] u32 data]
*/ */
TOCLIENT_HUD_BUILTIN_ENABLE = 0x4c, TOCLIENT_HUD_SET_FLAGS = 0x4c,
/* /*
u16 command u16 command
u8 id u32 flags
u8 flag u32 mask
*/ */
}; };

@ -2186,14 +2186,6 @@ void the_game(
delete event.hudchange.v2fdata; delete event.hudchange.v2fdata;
delete event.hudchange.sdata; delete event.hudchange.sdata;
} }
else if (event.type == CE_HUD_BUILTIN_ENABLE) {
u32 bit = (u32)event.hudbuiltin.id;
u32 mask = 1 << bit;
if (event.hudbuiltin.flag)
player->hud_flags |= mask;
else
player->hud_flags &= ~mask;
}
} }
} }
@ -3078,7 +3070,7 @@ void the_game(
/* /*
Wielded tool Wielded tool
*/ */
if(show_hud && (player->hud_flags & HUD_DRAW_WIELDITEM)) if(show_hud && (player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE))
{ {
// Warning: This clears the Z buffer. // Warning: This clears the Z buffer.
camera.drawWieldedTool(); camera.drawWieldedTool();
@ -3102,7 +3094,7 @@ void the_game(
/* /*
Draw crosshair Draw crosshair
*/ */
if (show_hud && (player->hud_flags & HUD_DRAW_CROSSHAIR)) if (show_hud)
hud.drawCrosshair(); hud.drawCrosshair();
} // timer } // timer
@ -3117,8 +3109,7 @@ void the_game(
if (show_hud) if (show_hud)
{ {
hud.drawHotbar(v2s32(displaycenter.X, screensize.Y), hud.drawHotbar(v2s32(displaycenter.X, screensize.Y),
client.getHP(), client.getPlayerItem(), client.getHP(), client.getPlayerItem());
player->hud_flags);
} }
/* /*

@ -277,7 +277,7 @@ void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture, s
} }
void Hud::drawHotbar(v2s32 centerlowerpos, s32 halfheartcount, u16 playeritem, u32 flags) { void Hud::drawHotbar(v2s32 centerlowerpos, s32 halfheartcount, u16 playeritem) {
InventoryList *mainlist = inventory->getList("main"); InventoryList *mainlist = inventory->getList("main");
if (mainlist == NULL) { if (mainlist == NULL) {
errorstream << "draw_hotbar(): mainlist == NULL" << std::endl; errorstream << "draw_hotbar(): mainlist == NULL" << std::endl;
@ -288,20 +288,22 @@ void Hud::drawHotbar(v2s32 centerlowerpos, s32 halfheartcount, u16 playeritem, u
s32 width = hotbar_itemcount * (hotbar_imagesize + padding * 2); s32 width = hotbar_itemcount * (hotbar_imagesize + padding * 2);
v2s32 pos = centerlowerpos - v2s32(width / 2, hotbar_imagesize + padding * 2); v2s32 pos = centerlowerpos - v2s32(width / 2, hotbar_imagesize + padding * 2);
if (flags & HUD_DRAW_HOTBAR) if (player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE)
drawItem(pos, hotbar_imagesize, hotbar_itemcount, mainlist, playeritem + 1, 0); drawItem(pos, hotbar_imagesize, hotbar_itemcount, mainlist, playeritem + 1, 0);
if (flags & HUD_DRAW_HEALTHBAR) if (player->hud_flags & HUD_FLAG_HEALTHBAR_VISIBLE)
drawStatbar(pos - v2s32(0, 4), HUD_CORNER_LOWER, HUD_DIR_LEFT_RIGHT, drawStatbar(pos - v2s32(0, 4), HUD_CORNER_LOWER, HUD_DIR_LEFT_RIGHT,
"heart.png", halfheartcount, v2s32(0, 0)); "heart.png", halfheartcount, v2s32(0, 0));
} }
void Hud::drawCrosshair() { void Hud::drawCrosshair() {
if (player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE) {
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);
} }
}
void Hud::drawSelectionBoxes(std::vector<aabb3f> &hilightboxes) { void Hud::drawSelectionBoxes(std::vector<aabb3f> &hilightboxes) {

@ -31,10 +31,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define HUD_CORNER_LOWER 1 #define HUD_CORNER_LOWER 1
#define HUD_CORNER_CENTER 2 #define HUD_CORNER_CENTER 2
#define HUD_DRAW_HOTBAR (1 << 0) #define HUD_FLAG_HOTBAR_VISIBLE (1 << 0)
#define HUD_DRAW_HEALTHBAR (1 << 1) #define HUD_FLAG_HEALTHBAR_VISIBLE (1 << 1)
#define HUD_DRAW_CROSSHAIR (1 << 2) #define HUD_FLAG_CROSSHAIR_VISIBLE (1 << 2)
#define HUD_DRAW_WIELDITEM (1 << 3) #define HUD_FLAG_WIELDITEM_VISIBLE (1 << 3)
class Player; class Player;
@ -71,14 +71,6 @@ struct HudElement {
}; };
enum HudBuiltinElement {
HUD_BUILTIN_HOTBAR = 0,
HUD_BUILTIN_HEALTHBAR,
HUD_BUILTIN_CROSSHAIR,
HUD_BUILTIN_WIELDITEM
};
inline u32 hud_get_free_id(Player *player) { inline u32 hud_get_free_id(Player *player) {
size_t size = player->hud.size(); size_t size = player->hud.size();
for (size_t i = 0; i != size; i++) { for (size_t i = 0; i != size; i++) {
@ -123,9 +115,10 @@ 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 pos, u16 corner, u16 drawdir, std::string texture, s32 count, v2s32 offset); void drawStatbar(v2s32 pos, u16 corner, u16 drawdir,
std::string texture, s32 count, v2s32 offset);
void drawHotbar(v2s32 centerlowerpos, s32 halfheartcount, u16 playeritem, u32 flags); void drawHotbar(v2s32 centerlowerpos, s32 halfheartcount, u16 playeritem);
void resizeHotbar(); void resizeHotbar();
void drawCrosshair(); void drawCrosshair();

@ -79,10 +79,8 @@ Player::Player(IGameDef *gamedef):
physics_override_jump = 1; physics_override_jump = 1;
physics_override_gravity = 1; physics_override_gravity = 1;
hud_flags = HUD_DRAW_HOTBAR hud_flags = HUD_FLAG_HOTBAR_VISIBLE | HUD_FLAG_HEALTHBAR_VISIBLE |
| HUD_DRAW_HEALTHBAR HUD_FLAG_CROSSHAIR_VISIBLE | HUD_FLAG_WIELDITEM_VISIBLE;
| HUD_DRAW_CROSSHAIR
| HUD_DRAW_WIELDITEM;
} }
Player::~Player() Player::~Player()

@ -54,10 +54,10 @@ struct EnumString es_HudElementStat[] =
struct EnumString es_HudBuiltinElement[] = struct EnumString es_HudBuiltinElement[] =
{ {
{HUD_BUILTIN_HOTBAR, "hotbar"}, {HUD_FLAG_HOTBAR_VISIBLE, "hotbar"},
{HUD_BUILTIN_HEALTHBAR, "healthbar"}, {HUD_FLAG_HEALTHBAR_VISIBLE, "healthbar"},
{HUD_BUILTIN_CROSSHAIR, "crosshair"}, {HUD_FLAG_CROSSHAIR_VISIBLE, "crosshair"},
{HUD_BUILTIN_WIELDITEM, "wielditem"}, {HUD_FLAG_WIELDITEM_VISIBLE, "wielditem"},
{0, NULL}, {0, NULL},
}; };
@ -911,30 +911,29 @@ int ObjectRef::l_hud_get(lua_State *L)
return 1; return 1;
} }
// hud_builtin_enable(self, id, flag) // hud_set_flags(self, flags)
int ObjectRef::l_hud_builtin_enable(lua_State *L) int ObjectRef::l_hud_set_flags(lua_State *L)
{ {
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); Player *player = getplayer(ref);
if (player == NULL) if (player == NULL)
return 0; return 0;
HudBuiltinElement id; u32 flags = 0;
int id_i; u32 mask = 0;
bool flag;
std::string s(lua_tostring(L, 2)); const EnumString *esp = es_HudBuiltinElement;
for (int i = 0; esp[i].str; i++) {
// Return nil if component is not in enum if (getboolfield(L, 2, esp[i].str, flag)) {
if (!string_to_enum(es_HudBuiltinElement, id_i, s)) flags |= esp[i].num * flag;
mask |= esp[i].num;
}
}
if (!get_server(L)->hudSetFlags(player, flags, mask))
return 0; return 0;
id = (HudBuiltinElement)id_i; lua_pushboolean(L, true);
bool flag = (bool)lua_toboolean(L, 3);
bool ok = get_server(L)->hudBuiltinEnable(player, id, flag);
lua_pushboolean(L, (int)ok);
return 1; return 1;
} }
@ -1048,9 +1047,7 @@ const luaL_reg ObjectRef::methods[] = {
luamethod(ObjectRef, hud_remove), luamethod(ObjectRef, hud_remove),
luamethod(ObjectRef, hud_change), luamethod(ObjectRef, hud_change),
luamethod(ObjectRef, hud_get), luamethod(ObjectRef, hud_get),
luamethod(ObjectRef, hud_builtin_enable), luamethod(ObjectRef, hud_set_flags),
//luamethod(ObjectRef, hud_lock_next_bar),
//luamethod(ObjectRef, hud_unlock_bar),
{0,0} {0,0}
}; };

@ -202,8 +202,8 @@ private:
// hud_get(self, id) // hud_get(self, id)
static int l_hud_get(lua_State *L); static int l_hud_get(lua_State *L);
// hud_builtin_enable(self, id, flag) // hud_set_flags(self, flags)
static int l_hud_builtin_enable(lua_State *L); static int l_hud_set_flags(lua_State *L);
public: public:
ObjectRef(ServerActiveObject *object); ObjectRef(ServerActiveObject *object);

@ -3675,14 +3675,14 @@ void Server::SendHUDChange(u16 peer_id, u32 id, HudElementStat stat, void *value
m_con.Send(peer_id, 0, data, true); m_con.Send(peer_id, 0, data, true);
} }
void Server::SendHUDBuiltinEnable(u16 peer_id, u32 id, bool flag) void Server::SendHUDSetFlags(u16 peer_id, u32 flags, u32 mask)
{ {
std::ostringstream os(std::ios_base::binary); std::ostringstream os(std::ios_base::binary);
// Write command // Write command
writeU16(os, TOCLIENT_HUD_BUILTIN_ENABLE); writeU16(os, TOCLIENT_HUD_SET_FLAGS);
writeU8(os, id); writeU32(os, flags);
writeU8(os, (flag ? 1 : 0)); writeU32(os, mask);
// Make data buffer // Make data buffer
std::string s = os.str(); std::string s = os.str();
@ -4680,11 +4680,11 @@ bool Server::hudChange(Player *player, u32 id, HudElementStat stat, void *data)
return true; return true;
} }
bool Server::hudBuiltinEnable(Player *player, u32 id, bool flag) { bool Server::hudSetFlags(Player *player, u32 flags, u32 mask) {
if (!player) if (!player)
return false; return false;
SendHUDBuiltinEnable(player->peer_id, id, flag); SendHUDSetFlags(player->peer_id, flags, mask);
return true; return true;
} }

@ -540,7 +540,7 @@ public:
u32 hudAdd(Player *player, HudElement *element); u32 hudAdd(Player *player, HudElement *element);
bool hudRemove(Player *player, u32 id); bool hudRemove(Player *player, u32 id);
bool hudChange(Player *player, u32 id, HudElementStat stat, void *value); bool hudChange(Player *player, u32 id, HudElementStat stat, void *value);
bool hudBuiltinEnable(Player *player, u32 id, bool flag); bool hudSetFlags(Player *player, u32 flags, u32 mask);
private: private:
@ -584,7 +584,8 @@ private:
void SendHUDAdd(u16 peer_id, u32 id, HudElement *form); void SendHUDAdd(u16 peer_id, u32 id, HudElement *form);
void SendHUDRemove(u16 peer_id, u32 id); void SendHUDRemove(u16 peer_id, u32 id);
void SendHUDChange(u16 peer_id, u32 id, HudElementStat stat, void *value); void SendHUDChange(u16 peer_id, u32 id, HudElementStat stat, void *value);
void SendHUDBuiltinEnable(u16 peer_id, u32 id, bool flag); void SendHUDSetFlags(u16 peer_id, u32 flags, u32 mask);
/* /*
Send a node removal/addition event to all clients except ignore_id. Send a node removal/addition event to all clients except ignore_id.
Additionally, if far_players!=NULL, players further away than Additionally, if far_players!=NULL, players further away than