From 7860097eda449f0bb99971a037967bd338441133 Mon Sep 17 00:00:00 2001 From: PilzAdam Date: Tue, 3 Sep 2013 19:51:40 +0200 Subject: [PATCH] Use player:set_hotbar_image() instead of hardcoded hotbar.png --- doc/lua_api.txt | 4 ++++ src/client.cpp | 4 ++++ src/hud.cpp | 33 ++++++++++++++++++++++++++------- src/hud.h | 8 ++++++-- src/localplayer.cpp | 2 ++ src/localplayer.h | 3 +++ src/script/lua_api/l_object.cpp | 30 ++++++++++++++++++++++++++++++ src/script/lua_api/l_object.h | 6 ++++++ src/server.cpp | 14 ++++++++++++++ src/server.h | 4 +++- 10 files changed, 98 insertions(+), 10 deletions(-) diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 21cbcb822..e65392c17 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -1618,6 +1618,10 @@ Player-only: (no-op for other objects) ^ if a flag is nil, the flag is not modified - hud_set_hotbar_itemcount(count): sets number of items in builtin hotbar ^ count: number of items, must be between 1 and 23 +- hud_set_hotbar_image(texturename) + ^ sets background image for hotbar +- hud_set_hotbar_selected_image(texturename) + ^ sets image for selected item of hotbar InvRef: Reference to an inventory methods: diff --git a/src/client.cpp b/src/client.cpp index ecbb32dd2..063dc4158 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -2175,6 +2175,10 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id) s32 hotbar_itemcount = readS32((u8*) value.c_str()); if(hotbar_itemcount > 0 && hotbar_itemcount <= HUD_HOTBAR_ITEMCOUNT_MAX) player->hud_hotbar_itemcount = hotbar_itemcount; + } else if (param == HUD_PARAM_HOTBAR_IMAGE) { + ((LocalPlayer *) player)->hotbar_image = value; + } else if (param == HUD_PARAM_HOTBAR_SELECTED_IMAGE) { + ((LocalPlayer *) player)->hotbar_selected_image = value; } } else diff --git a/src/hud.cpp b/src/hud.cpp index 273da9c92..de34b9d64 100644 --- a/src/hud.cpp +++ b/src/hud.cpp @@ -64,8 +64,11 @@ Hud::Hud(video::IVideoDriver *driver, gui::IGUIEnvironment* guienv, selectionbox_argb = video::SColor(255, sbox_r, sbox_g, sbox_b); use_crosshair_image = tsrc->isKnownSourceImage("crosshair.png"); - use_hotbar_bg_img = tsrc->isKnownSourceImage("hotbar.png"); - use_hotbar_border_img = tsrc->isKnownSourceImage("hotbar_selected.png"); + + hotbar_image = ""; + use_hotbar_image = false; + hotbar_selected_image = ""; + use_hotbar_selected_image = false; } @@ -95,10 +98,26 @@ void Hud::drawItem(v2s32 upperleftpos, s32 imgsize, s32 itemcount, const video::SColor hbar_color(255, 255, 255, 255); const video::SColor hbar_colors[] = {hbar_color, hbar_color, hbar_color, hbar_color}; - if (use_hotbar_bg_img) { + if (hotbar_image != player->hotbar_image) { + hotbar_image = player->hotbar_image; + if (hotbar_image != "") + use_hotbar_image = tsrc->isKnownSourceImage(hotbar_image); + else + use_hotbar_image = false; + } + + if (hotbar_selected_image != player->hotbar_selected_image) { + hotbar_selected_image = player->hotbar_selected_image; + if (hotbar_selected_image != "") + use_hotbar_selected_image = tsrc->isKnownSourceImage(hotbar_selected_image); + else + use_hotbar_selected_image = false; + } + + if (use_hotbar_image) { core::rect imgrect2(-padding/2, -padding/2, width+padding/2, height+padding/2); core::rect rect2 = imgrect2 + pos; - video::ITexture *texture = tsrc->getTexture("hotbar.png"); + video::ITexture *texture = tsrc->getTexture(hotbar_image); core::dimension2di imgsize(texture->getOriginalSize()); driver->draw2DImage(texture, rect2, core::rect(core::position2d(0,0), imgsize), @@ -127,10 +146,10 @@ void Hud::drawItem(v2s32 upperleftpos, s32 imgsize, s32 itemcount, core::rect rect = imgrect + pos + steppos; if (selectitem == i + 1) { - if (use_hotbar_border_img) { + if (use_hotbar_selected_image) { core::rect imgrect2(-padding*2, -padding*2, height, height); rect = imgrect2 + pos + steppos; - video::ITexture *texture = tsrc->getTexture("hotbar_selected.png"); + video::ITexture *texture = tsrc->getTexture(hotbar_selected_image); core::dimension2di imgsize(texture->getOriginalSize()); driver->draw2DImage(texture, rect, core::rect(core::position2d(0,0), imgsize), @@ -192,7 +211,7 @@ void Hud::drawItem(v2s32 upperleftpos, s32 imgsize, s32 itemcount, } video::SColor bgcolor2(128, 0, 0, 0); - if (!use_hotbar_bg_img) + if (!use_hotbar_image) driver->draw2DRectangle(bgcolor2, rect, NULL); drawItemStack(driver, font, item, rect, NULL, gamedef); } diff --git a/src/hud.h b/src/hud.h index 92ee9a62c..c69867a23 100644 --- a/src/hud.h +++ b/src/hud.h @@ -39,6 +39,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #define HUD_FLAG_BREATHBAR_VISIBLE (1 << 4) #define HUD_PARAM_HOTBAR_ITEMCOUNT 1 +#define HUD_PARAM_HOTBAR_IMAGE 2 +#define HUD_PARAM_HOTBAR_SELECTED_IMAGE 3 #define HUD_HOTBAR_ITEMCOUNT_DEFAULT 8 #define HUD_HOTBAR_ITEMCOUNT_MAX 23 @@ -106,8 +108,10 @@ public: video::SColor crosshair_argb; video::SColor selectionbox_argb; bool use_crosshair_image; - bool use_hotbar_border_img; - bool use_hotbar_bg_img; + std::string hotbar_image; + bool use_hotbar_image; + std::string hotbar_selected_image; + bool use_hotbar_selected_image; Hud(video::IVideoDriver *driver, gui::IGUIEnvironment* guienv, gui::IGUIFont *font, u32 text_height, IGameDef *gamedef, diff --git a/src/localplayer.cpp b/src/localplayer.cpp index f8dfca050..48e6592c1 100644 --- a/src/localplayer.cpp +++ b/src/localplayer.cpp @@ -43,6 +43,8 @@ LocalPlayer::LocalPlayer(IGameDef *gamedef): last_pitch(0), last_yaw(0), last_keyPressed(0), + hotbar_image(""), + hotbar_selected_image(""), m_sneak_node(32767,32767,32767), m_sneak_node_exists(false), m_old_node_below(32767,32767,32767), diff --git a/src/localplayer.h b/src/localplayer.h index 8c3041c04..b6a3ed1f6 100644 --- a/src/localplayer.h +++ b/src/localplayer.h @@ -61,6 +61,9 @@ public: float camera_impact; + std::string hotbar_image; + std::string hotbar_selected_image; + private: // This is used for determining the sneaking range v3s16 m_sneak_node; diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index c0da79c29..6a800f15d 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -1022,6 +1022,34 @@ int ObjectRef::l_hud_set_hotbar_itemcount(lua_State *L) return 1; } +// hud_set_hotbar_image(self, name) +int ObjectRef::l_hud_set_hotbar_image(lua_State *L) +{ + ObjectRef *ref = checkobject(L, 1); + Player *player = getplayer(ref); + if (player == NULL) + return 0; + + std::string name = lua_tostring(L, 2); + + getServer(L)->hudSetHotbarImage(player, name); + return 1; +} + +// hud_set_hotbar_selected_image(self, name) +int ObjectRef::l_hud_set_hotbar_selected_image(lua_State *L) +{ + ObjectRef *ref = checkobject(L, 1); + Player *player = getplayer(ref); + if (player == NULL) + return 0; + + std::string name = lua_tostring(L, 2); + + getServer(L)->hudSetHotbarSelectedImage(player, name); + return 1; +} + ObjectRef::ObjectRef(ServerActiveObject *object): m_object(object) { @@ -1136,5 +1164,7 @@ const luaL_reg ObjectRef::methods[] = { luamethod(ObjectRef, hud_get), luamethod(ObjectRef, hud_set_flags), luamethod(ObjectRef, hud_set_hotbar_itemcount), + luamethod(ObjectRef, hud_set_hotbar_image), + luamethod(ObjectRef, hud_set_hotbar_selected_image), {0,0} }; diff --git a/src/script/lua_api/l_object.h b/src/script/lua_api/l_object.h index b6f5cd06f..8fd6c8e71 100644 --- a/src/script/lua_api/l_object.h +++ b/src/script/lua_api/l_object.h @@ -215,6 +215,12 @@ private: // hud_set_hotbar_itemcount(self, hotbar_itemcount) static int l_hud_set_hotbar_itemcount(lua_State *L); + // hud_set_hotbar_image(self, name) + static int l_hud_set_hotbar_image(lua_State *L); + + // hud_set_hotbar_selected_image(self, name) + static int l_hud_set_hotbar_selected_image(lua_State *L); + public: ObjectRef(ServerActiveObject *object); diff --git a/src/server.cpp b/src/server.cpp index 8ce8df349..3bdf9c23d 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -4999,6 +4999,20 @@ bool Server::hudSetHotbarItemcount(Player *player, s32 hotbar_itemcount) { return true; } +void Server::hudSetHotbarImage(Player *player, std::string name) { + if (!player) + return; + + SendHUDSetParam(player->peer_id, HUD_PARAM_HOTBAR_IMAGE, name); +} + +void Server::hudSetHotbarSelectedImage(Player *player, std::string name) { + if (!player) + return; + + SendHUDSetParam(player->peer_id, HUD_PARAM_HOTBAR_SELECTED_IMAGE, name); +} + void Server::notifyPlayers(const std::wstring msg) { BroadcastChatMessage(msg); diff --git a/src/server.h b/src/server.h index 4e7675ecb..12520a1d7 100644 --- a/src/server.h +++ b/src/server.h @@ -493,7 +493,9 @@ public: bool hudChange(Player *player, u32 id, HudElementStat stat, void *value); bool hudSetFlags(Player *player, u32 flags, u32 mask); bool hudSetHotbarItemcount(Player *player, s32 hotbar_itemcount); - + void hudSetHotbarImage(Player *player, std::string name); + void hudSetHotbarSelectedImage(Player *player, std::string name); + private: // con::PeerHandler implementation. -- 2.25.1