mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-31 18:31:04 +00:00
Simplify HUD handling in Player class
This commit is contained in:
parent
bb74b9d488
commit
36b5374715
6 changed files with 35 additions and 65 deletions
|
@ -307,11 +307,8 @@ void Hud::drawItems(v2s32 screen_pos, v2s32 screen_offset, s32 itemcount, v2f al
|
|||
|
||||
bool Hud::hasElementOfType(HudElementType type)
|
||||
{
|
||||
for (size_t i = 0; i != player->maxHudId(); i++) {
|
||||
HudElement *e = player->getHud(i);
|
||||
if (!e)
|
||||
continue;
|
||||
if (e->type == type)
|
||||
for (HudElement *e : player->getHudElements()) {
|
||||
if (e && e->type == type)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
@ -342,9 +339,13 @@ void Hud::drawLuaElements(const v3s16 &camera_offset)
|
|||
const u32 text_height = g_fontengine->getTextHeight();
|
||||
gui::IGUIFont *const font = g_fontengine->getFont();
|
||||
|
||||
// Reorder elements by z_index
|
||||
std::vector<HudElement*> elems;
|
||||
elems.reserve(player->maxHudId());
|
||||
|
||||
elems.reserve(player->getHudElements().size());
|
||||
for (HudElement *e : player->getHudElements()) {
|
||||
if (e)
|
||||
elems.push_back(e);
|
||||
}
|
||||
|
||||
// Add builtin elements if the server doesn't send them.
|
||||
// Declared here such that they have the same lifetime as the elems vector
|
||||
|
@ -361,17 +362,10 @@ void Hud::drawLuaElements(const v3s16 &camera_offset)
|
|||
elems.push_back(&hotbar);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i != player->maxHudId(); i++) {
|
||||
HudElement *e = player->getHud(i);
|
||||
if (!e)
|
||||
continue;
|
||||
|
||||
auto it = elems.begin();
|
||||
while (it != elems.end() && (*it)->z_index <= e->z_index)
|
||||
++it;
|
||||
|
||||
elems.insert(it, e);
|
||||
}
|
||||
// Reorder by Z-index for rendering
|
||||
std::sort(elems.begin(), elems.end(), [] (HudElement *l, HudElement *r) {
|
||||
return l->z_index < r->z_index;
|
||||
});
|
||||
|
||||
for (HudElement *e : elems) {
|
||||
|
||||
|
|
|
@ -115,8 +115,6 @@ public:
|
|||
m_cao = toset;
|
||||
}
|
||||
|
||||
u32 maxHudId() const { return hud.size(); }
|
||||
|
||||
u16 getBreath() const { return m_breath; }
|
||||
void setBreath(u16 breath) { m_breath = breath; }
|
||||
|
||||
|
|
|
@ -110,8 +110,6 @@ ItemStack &Player::getWieldedItem(ItemStack *selected, ItemStack *hand) const
|
|||
|
||||
u32 Player::addHud(HudElement *toadd)
|
||||
{
|
||||
MutexAutoLock lock(m_mutex);
|
||||
|
||||
u32 id = getFreeHudID();
|
||||
|
||||
if (id < hud.size())
|
||||
|
@ -124,37 +122,27 @@ u32 Player::addHud(HudElement *toadd)
|
|||
|
||||
HudElement* Player::getHud(u32 id)
|
||||
{
|
||||
MutexAutoLock lock(m_mutex);
|
||||
|
||||
if (id < hud.size())
|
||||
return hud[id];
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void Player::hudApply(std::function<void(const std::vector<HudElement*>&)> f)
|
||||
{
|
||||
MutexAutoLock lock(m_mutex);
|
||||
f(hud);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
HudElement* Player::removeHud(u32 id)
|
||||
{
|
||||
MutexAutoLock lock(m_mutex);
|
||||
|
||||
HudElement* retval = NULL;
|
||||
HudElement* retval = nullptr;
|
||||
if (id < hud.size()) {
|
||||
retval = hud[id];
|
||||
hud[id] = NULL;
|
||||
hud[id] = nullptr;
|
||||
}
|
||||
// shrink list if possible
|
||||
while (!hud.empty() && hud.back() == nullptr)
|
||||
hud.pop_back();
|
||||
return retval;
|
||||
}
|
||||
|
||||
void Player::clearHud()
|
||||
{
|
||||
MutexAutoLock lock(m_mutex);
|
||||
|
||||
while(!hud.empty()) {
|
||||
while (!hud.empty()) {
|
||||
delete hud.back();
|
||||
hud.pop_back();
|
||||
}
|
||||
|
|
12
src/player.h
12
src/player.h
|
@ -9,8 +9,6 @@
|
|||
#include "constants.h"
|
||||
#include "util/basic_macros.h"
|
||||
#include "util/string.h"
|
||||
#include <mutex>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
#define PLAYERNAME_SIZE 20
|
||||
|
@ -219,8 +217,8 @@ public:
|
|||
return m_fov_override_spec;
|
||||
}
|
||||
|
||||
const auto &getHudElements() const { return hud; }
|
||||
HudElement* getHud(u32 id);
|
||||
void hudApply(std::function<void(const std::vector<HudElement*>&)> f);
|
||||
u32 addHud(HudElement* hud);
|
||||
HudElement* removeHud(u32 id);
|
||||
void clearHud();
|
||||
|
@ -237,12 +235,6 @@ protected:
|
|||
u16 m_wield_index = 0;
|
||||
PlayerFovSpec m_fov_override_spec = { 0.0f, false, 0.0f };
|
||||
|
||||
std::vector<HudElement *> hud;
|
||||
|
||||
private:
|
||||
// Protect some critical areas
|
||||
// hud for example can be modified by EmergeThread
|
||||
// and ServerThread
|
||||
// FIXME: ^ this sounds like nonsense. should be checked.
|
||||
std::mutex m_mutex;
|
||||
std::vector<HudElement *> hud;
|
||||
};
|
||||
|
|
|
@ -425,15 +425,14 @@ int LuaLocalPlayer::l_hud_get_all(lua_State *L)
|
|||
return 0;
|
||||
|
||||
lua_newtable(L);
|
||||
player->hudApply([&](const std::vector<HudElement*>& hud) {
|
||||
for (std::size_t id = 0; id < hud.size(); ++id) {
|
||||
HudElement *elem = hud[id];
|
||||
if (elem != nullptr) {
|
||||
push_hud_element(L, elem);
|
||||
lua_rawseti(L, -2, id);
|
||||
}
|
||||
u32 id = 0;
|
||||
for (HudElement *elem : player->getHudElements()) {
|
||||
if (elem != nullptr) {
|
||||
push_hud_element(L, elem);
|
||||
lua_rawseti(L, -2, id);
|
||||
}
|
||||
});
|
||||
++id;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
|
@ -1903,15 +1903,14 @@ int ObjectRef::l_hud_get_all(lua_State *L)
|
|||
return 0;
|
||||
|
||||
lua_newtable(L);
|
||||
player->hudApply([&](const std::vector<HudElement*>& hud) {
|
||||
for (std::size_t id = 0; id < hud.size(); ++id) {
|
||||
HudElement *elem = hud[id];
|
||||
if (elem != nullptr) {
|
||||
push_hud_element(L, elem);
|
||||
lua_rawseti(L, -2, id);
|
||||
}
|
||||
u32 id = 0;
|
||||
for (HudElement *elem : player->getHudElements()) {
|
||||
if (elem != nullptr) {
|
||||
push_hud_element(L, elem);
|
||||
lua_rawseti(L, -2, id);
|
||||
}
|
||||
});
|
||||
++id;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue