summaryrefslogtreecommitdiff
path: root/src/script
diff options
context:
space:
mode:
authorred-001 <red-001@outlook.ie>2018-01-20 13:09:58 +0000
committerLoïc Blot <nerzhul@users.noreply.github.com>2018-01-20 14:09:58 +0100
commit9649e4721467dab348011633c814a63a184bd018 (patch)
treeb5c381d8b597406aa21889a1c40c2689adaa39a9 /src/script
parentd45e5da8ca808e552123bcd94e76b0b435a6ea79 (diff)
downloadminetest-9649e4721467dab348011633c814a63a184bd018.tar.gz
minetest-9649e4721467dab348011633c814a63a184bd018.tar.bz2
minetest-9649e4721467dab348011633c814a63a184bd018.zip
[CSM] Add basic HUD manipulation. (#6067)
* [CSM] Add basic HUD manipulation. Workaround for on_connect not working right now.
Diffstat (limited to 'src/script')
-rw-r--r--src/script/common/c_content.cpp139
-rw-r--r--src/script/common/c_content.h7
-rw-r--r--src/script/lua_api/l_localplayer.cpp73
-rw-r--r--src/script/lua_api/l_localplayer.h11
-rw-r--r--src/script/lua_api/l_object.cpp171
5 files changed, 233 insertions, 168 deletions
diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp
index 507fd58b4..cac48d488 100644
--- a/src/script/common/c_content.cpp
+++ b/src/script/common/c_content.cpp
@@ -1792,3 +1792,142 @@ void push_objectRef(lua_State *L, const u16 id)
lua_remove(L, -2); // object_refs
lua_remove(L, -2); // core
}
+
+void read_hud_element(lua_State *L, HudElement *elem)
+{
+ elem->type = (HudElementType)getenumfield(L, 2, "hud_elem_type",
+ es_HudElementType, HUD_ELEM_TEXT);
+
+ lua_getfield(L, 2, "position");
+ elem->pos = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
+ lua_pop(L, 1);
+
+ lua_getfield(L, 2, "scale");
+ elem->scale = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
+ lua_pop(L, 1);
+
+ lua_getfield(L, 2, "size");
+ elem->size = lua_istable(L, -1) ? read_v2s32(L, -1) : v2s32();
+ lua_pop(L, 1);
+
+ elem->name = getstringfield_default(L, 2, "name", "");
+ elem->text = getstringfield_default(L, 2, "text", "");
+ elem->number = getintfield_default(L, 2, "number", 0);
+ elem->item = getintfield_default(L, 2, "item", 0);
+ elem->dir = getintfield_default(L, 2, "direction", 0);
+
+ // Deprecated, only for compatibility's sake
+ if (elem->dir == 0)
+ elem->dir = getintfield_default(L, 2, "dir", 0);
+
+ lua_getfield(L, 2, "alignment");
+ elem->align = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
+ lua_pop(L, 1);
+
+ lua_getfield(L, 2, "offset");
+ elem->offset = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
+ lua_pop(L, 1);
+
+ lua_getfield(L, 2, "world_pos");
+ elem->world_pos = lua_istable(L, -1) ? read_v3f(L, -1) : v3f();
+ lua_pop(L, 1);
+
+ /* check for known deprecated element usage */
+ if ((elem->type == HUD_ELEM_STATBAR) && (elem->size == v2s32()))
+ log_deprecated(L,"Deprecated usage of statbar without size!");
+}
+
+void push_hud_element(lua_State *L, HudElement *elem)
+{
+ lua_newtable(L);
+
+ lua_pushstring(L, es_HudElementType[(u8)elem->type].str);
+ lua_setfield(L, -2, "type");
+
+ push_v2f(L, elem->pos);
+ lua_setfield(L, -2, "position");
+
+ lua_pushstring(L, elem->name.c_str());
+ lua_setfield(L, -2, "name");
+
+ push_v2f(L, elem->scale);
+ lua_setfield(L, -2, "scale");
+
+ lua_pushstring(L, elem->text.c_str());
+ lua_setfield(L, -2, "text");
+
+ lua_pushnumber(L, elem->number);
+ lua_setfield(L, -2, "number");
+
+ lua_pushnumber(L, elem->item);
+ lua_setfield(L, -2, "item");
+
+ lua_pushnumber(L, elem->dir);
+ lua_setfield(L, -2, "direction");
+
+ // Deprecated, only for compatibility's sake
+ lua_pushnumber(L, elem->dir);
+ lua_setfield(L, -2, "dir");
+
+ push_v3f(L, elem->world_pos);
+ lua_setfield(L, -2, "world_pos");
+}
+
+HudElementStat read_hud_change(lua_State *L, HudElement *elem, void **value)
+{
+ HudElementStat stat = HUD_STAT_NUMBER;
+ if (lua_isstring(L, 3)) {
+ int statint;
+ std::string statstr = lua_tostring(L, 3);
+ stat = string_to_enum(es_HudElementStat, statint, statstr) ?
+ (HudElementStat)statint : stat;
+ }
+
+ switch (stat) {
+ case HUD_STAT_POS:
+ elem->pos = read_v2f(L, 4);
+ *value = &elem->pos;
+ break;
+ case HUD_STAT_NAME:
+ elem->name = luaL_checkstring(L, 4);
+ *value = &elem->name;
+ break;
+ case HUD_STAT_SCALE:
+ elem->scale = read_v2f(L, 4);
+ *value = &elem->scale;
+ break;
+ case HUD_STAT_TEXT:
+ elem->text = luaL_checkstring(L, 4);
+ *value = &elem->text;
+ break;
+ case HUD_STAT_NUMBER:
+ elem->number = luaL_checknumber(L, 4);
+ *value = &elem->number;
+ break;
+ case HUD_STAT_ITEM:
+ elem->item = luaL_checknumber(L, 4);
+ *value = &elem->item;
+ break;
+ case HUD_STAT_DIR:
+ elem->dir = luaL_checknumber(L, 4);
+ *value = &elem->dir;
+ break;
+ case HUD_STAT_ALIGN:
+ elem->align = read_v2f(L, 4);
+ *value = &elem->align;
+ break;
+ case HUD_STAT_OFFSET:
+ elem->offset = read_v2f(L, 4);
+ *value = &elem->offset;
+ break;
+ case HUD_STAT_WORLD_POS:
+ elem->world_pos = read_v3f(L, 4);
+ *value = &elem->world_pos;
+ break;
+ case HUD_STAT_SIZE:
+ elem->size = read_v2s32(L, 4);
+ *value = &elem->size;
+ break;
+ }
+ return stat;
+}
diff --git a/src/script/common/c_content.h b/src/script/common/c_content.h
index 80a96e327..d5c375a8f 100644
--- a/src/script/common/c_content.h
+++ b/src/script/common/c_content.h
@@ -39,6 +39,7 @@ extern "C" {
#include "itemgroup.h"
#include "itemdef.h"
#include "c_types.h"
+#include "hud.h"
namespace Json { class Value; }
@@ -181,4 +182,10 @@ void push_pointed_thing (lua_State *L, const PointedThing &
void push_objectRef (lua_State *L, const u16 id);
+void read_hud_element (lua_State *L, HudElement *elem);
+
+void push_hud_element (lua_State *L, HudElement *elem);
+
+HudElementStat read_hud_change (lua_State *L, HudElement *elem, void **value);
+
extern struct EnumString es_TileAnimationType[];
diff --git a/src/script/lua_api/l_localplayer.cpp b/src/script/lua_api/l_localplayer.cpp
index da560c3ac..492422d92 100644
--- a/src/script/lua_api/l_localplayer.cpp
+++ b/src/script/lua_api/l_localplayer.cpp
@@ -21,6 +21,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "l_internal.h"
#include "script/common/c_converter.h"
#include "localplayer.h"
+#include "hud.h"
+#include "common/c_content.h"
LuaLocalPlayer::LuaLocalPlayer(LocalPlayer *m) : m_localplayer(m)
{
@@ -272,6 +274,73 @@ int LuaLocalPlayer::l_get_movement(lua_State *L)
return 1;
}
+
+// hud_add(self, form)
+int LuaLocalPlayer::l_hud_add(lua_State *L)
+{
+ LocalPlayer *player = getobject(L, 1);
+
+ HudElement *elem = new HudElement;
+ read_hud_element(L, elem);
+
+ u32 id = player->addHud(elem);
+ if (id == U32_MAX) {
+ delete elem;
+ return 0;
+ }
+ lua_pushnumber(L, id);
+ return 1;
+}
+
+// hud_remove(self, id)
+int LuaLocalPlayer::l_hud_remove(lua_State *L)
+{
+ LocalPlayer *player = getobject(L, 1);
+ u32 id = luaL_checkinteger(L, 2);
+ HudElement *element = player->removeHud(id);
+ if (!element)
+ lua_pushboolean(L, false);
+ else
+ lua_pushboolean(L, true);
+ delete element;
+ return 1;
+}
+
+// hud_change(self, id, stat, data)
+int LuaLocalPlayer::l_hud_change(lua_State *L)
+{
+ LocalPlayer *player = getobject(L, 1);
+
+ u32 id = luaL_checkinteger(L, 2);
+
+ HudElement *element = player->getHud(id);
+ if (!element)
+ return 0;
+
+ void *unused;
+ read_hud_change(L, element, &unused);
+
+ lua_pushboolean(L, true);
+ return 1;
+}
+
+// hud_get(self, id)
+int LuaLocalPlayer::l_hud_get(lua_State *L)
+{
+ LocalPlayer *player = getobject(L, 1);
+
+ u32 id = luaL_checkinteger(L, -1);
+
+ HudElement *e = player->getHud(id);
+ if (!e) {
+ lua_pushnil(L);
+ return 1;
+ }
+
+ push_hud_element(L, e);
+ return 1;
+}
+
LuaLocalPlayer *LuaLocalPlayer::checkobject(lua_State *L, int narg)
{
luaL_checktype(L, narg, LUA_TUSERDATA);
@@ -353,6 +422,10 @@ const luaL_Reg LuaLocalPlayer::methods[] = {
luamethod(LuaLocalPlayer, get_movement_acceleration),
luamethod(LuaLocalPlayer, get_movement_speed),
luamethod(LuaLocalPlayer, get_movement),
+ luamethod(LuaLocalPlayer, hud_add),
+ luamethod(LuaLocalPlayer, hud_remove),
+ luamethod(LuaLocalPlayer, hud_change),
+ luamethod(LuaLocalPlayer, hud_get),
{0, 0}
};
diff --git a/src/script/lua_api/l_localplayer.h b/src/script/lua_api/l_localplayer.h
index d30fe1d64..01de2ed4e 100644
--- a/src/script/lua_api/l_localplayer.h
+++ b/src/script/lua_api/l_localplayer.h
@@ -66,6 +66,17 @@ private:
static int l_get_movement(lua_State *L);
+ // hud_add(self, id, form)
+ static int l_hud_add(lua_State *L);
+
+ // hud_rm(self, id)
+ static int l_hud_remove(lua_State *L);
+
+ // hud_change(self, id, stat, data)
+ static int l_hud_change(lua_State *L);
+ // hud_get(self, id)
+ static int l_hud_get(lua_State *L);
+
LocalPlayer *m_localplayer = nullptr;
public:
diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp
index bc150d70f..3afd21ec3 100644
--- a/src/script/lua_api/l_object.cpp
+++ b/src/script/lua_api/l_object.cpp
@@ -32,44 +32,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "hud.h"
#include "scripting_server.h"
-struct EnumString es_HudElementType[] =
-{
- {HUD_ELEM_IMAGE, "image"},
- {HUD_ELEM_TEXT, "text"},
- {HUD_ELEM_STATBAR, "statbar"},
- {HUD_ELEM_INVENTORY, "inventory"},
- {HUD_ELEM_WAYPOINT, "waypoint"},
-{0, NULL},
-};
-
-struct EnumString es_HudElementStat[] =
-{
- {HUD_STAT_POS, "position"},
- {HUD_STAT_POS, "pos"}, /* Deprecated, only for compatibility's sake */
- {HUD_STAT_NAME, "name"},
- {HUD_STAT_SCALE, "scale"},
- {HUD_STAT_TEXT, "text"},
- {HUD_STAT_NUMBER, "number"},
- {HUD_STAT_ITEM, "item"},
- {HUD_STAT_DIR, "direction"},
- {HUD_STAT_ALIGN, "alignment"},
- {HUD_STAT_OFFSET, "offset"},
- {HUD_STAT_WORLD_POS, "world_pos"},
- {0, NULL},
-};
-
-struct EnumString es_HudBuiltinElement[] =
-{
- {HUD_FLAG_HOTBAR_VISIBLE, "hotbar"},
- {HUD_FLAG_HEALTHBAR_VISIBLE, "healthbar"},
- {HUD_FLAG_CROSSHAIR_VISIBLE, "crosshair"},
- {HUD_FLAG_WIELDITEM_VISIBLE, "wielditem"},
- {HUD_FLAG_BREATHBAR_VISIBLE, "breathbar"},
- {HUD_FLAG_MINIMAP_VISIBLE, "minimap"},
- {HUD_FLAG_MINIMAP_RADAR_VISIBLE, "minimap_radar"},
- {0, NULL},
-};
-
/*
ObjectRef
*/
@@ -1345,48 +1307,7 @@ int ObjectRef::l_hud_add(lua_State *L)
return 0;
HudElement *elem = new HudElement;
-
- elem->type = (HudElementType)getenumfield(L, 2, "hud_elem_type",
- es_HudElementType, HUD_ELEM_TEXT);
-
- lua_getfield(L, 2, "position");
- elem->pos = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
- lua_pop(L, 1);
-
- lua_getfield(L, 2, "scale");
- elem->scale = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
- lua_pop(L, 1);
-
- lua_getfield(L, 2, "size");
- elem->size = lua_istable(L, -1) ? read_v2s32(L, -1) : v2s32();
- lua_pop(L, 1);
-
- elem->name = getstringfield_default(L, 2, "name", "");
- elem->text = getstringfield_default(L, 2, "text", "");
- elem->number = getintfield_default(L, 2, "number", 0);
- elem->item = getintfield_default(L, 2, "item", 0);
- elem->dir = getintfield_default(L, 2, "direction", 0);
-
- // Deprecated, only for compatibility's sake
- if (elem->dir == 0)
- elem->dir = getintfield_default(L, 2, "dir", 0);
-
- lua_getfield(L, 2, "alignment");
- elem->align = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
- lua_pop(L, 1);
-
- lua_getfield(L, 2, "offset");
- elem->offset = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
- lua_pop(L, 1);
-
- lua_getfield(L, 2, "world_pos");
- elem->world_pos = lua_istable(L, -1) ? read_v3f(L, -1) : v3f();
- lua_pop(L, 1);
-
- /* check for known deprecated element usage */
- if ((elem->type == HUD_ELEM_STATBAR) && (elem->size == v2s32())) {
- log_deprecated(L,"Deprecated usage of statbar without size!");
- }
+ read_hud_element(L, elem);
u32 id = getServer(L)->hudAdd(player, elem);
if (id == U32_MAX) {
@@ -1433,61 +1354,8 @@ int ObjectRef::l_hud_change(lua_State *L)
if (!e)
return 0;
- HudElementStat stat = HUD_STAT_NUMBER;
- if (lua_isstring(L, 3)) {
- int statint;
- std::string statstr = lua_tostring(L, 3);
- stat = string_to_enum(es_HudElementStat, statint, statstr) ?
- (HudElementStat)statint : HUD_STAT_NUMBER;
- }
-
void *value = NULL;
- switch (stat) {
- case HUD_STAT_POS:
- e->pos = read_v2f(L, 4);
- value = &e->pos;
- break;
- case HUD_STAT_NAME:
- e->name = luaL_checkstring(L, 4);
- value = &e->name;
- break;
- case HUD_STAT_SCALE:
- e->scale = read_v2f(L, 4);
- value = &e->scale;
- break;
- case HUD_STAT_TEXT:
- e->text = luaL_checkstring(L, 4);
- value = &e->text;
- break;
- case HUD_STAT_NUMBER:
- e->number = luaL_checknumber(L, 4);
- value = &e->number;
- break;
- case HUD_STAT_ITEM:
- e->item = luaL_checknumber(L, 4);
- value = &e->item;
- break;
- case HUD_STAT_DIR:
- e->dir = luaL_checknumber(L, 4);
- value = &e->dir;
- break;
- case HUD_STAT_ALIGN:
- e->align = read_v2f(L, 4);
- value = &e->align;
- break;
- case HUD_STAT_OFFSET:
- e->offset = read_v2f(L, 4);
- value = &e->offset;
- break;
- case HUD_STAT_WORLD_POS:
- e->world_pos = read_v3f(L, 4);
- value = &e->world_pos;
- break;
- case HUD_STAT_SIZE:
- e->size = read_v2s32(L, 4);
- value = &e->size;
- break;
- }
+ HudElementStat stat = read_hud_change(L, e, &value);
getServer(L)->hudChange(player, id, stat, value);
@@ -1509,40 +1377,7 @@ int ObjectRef::l_hud_get(lua_State *L)
HudElement *e = player->getHud(id);
if (!e)
return 0;
-
- lua_newtable(L);
-
- lua_pushstring(L, es_HudElementType[(u8)e->type].str);
- lua_setfield(L, -2, "type");
-
- push_v2f(L, e->pos);
- lua_setfield(L, -2, "position");
-
- lua_pushstring(L, e->name.c_str());
- lua_setfield(L, -2, "name");
-
- push_v2f(L, e->scale);
- lua_setfield(L, -2, "scale");
-
- lua_pushstring(L, e->text.c_str());
- lua_setfield(L, -2, "text");
-
- lua_pushnumber(L, e->number);
- lua_setfield(L, -2, "number");
-
- lua_pushnumber(L, e->item);
- lua_setfield(L, -2, "item");
-
- lua_pushnumber(L, e->dir);
- lua_setfield(L, -2, "direction");
-
- // Deprecated, only for compatibility's sake
- lua_pushnumber(L, e->dir);
- lua_setfield(L, -2, "dir");
-
- push_v3f(L, e->world_pos);
- lua_setfield(L, -2, "world_pos");
-
+ push_hud_element(L, e);
return 1;
}