Use player:set_hotbar_image() instead of hardcoded hotbar.png

This commit is contained in:
PilzAdam 2013-09-03 19:51:40 +02:00
parent 5b518ed2fe
commit 7860097eda
10 changed files with 98 additions and 10 deletions

View File

@ -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:

View File

@ -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

View File

@ -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<s32> imgrect2(-padding/2, -padding/2, width+padding/2, height+padding/2);
core::rect<s32> 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<s32>(core::position2d<s32>(0,0), imgsize),
@ -127,10 +146,10 @@ void Hud::drawItem(v2s32 upperleftpos, s32 imgsize, s32 itemcount,
core::rect<s32> rect = imgrect + pos + steppos;
if (selectitem == i + 1) {
if (use_hotbar_border_img) {
if (use_hotbar_selected_image) {
core::rect<s32> 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<s32>(core::position2d<s32>(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);
}

View File

@ -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,

View File

@ -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),

View File

@ -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;

View File

@ -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}
};

View File

@ -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);

View File

@ -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);

View File

@ -493,6 +493,8 @@ 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: