Skip to content

Commit 7860097

Browse files
committedSep 4, 2013
Use player:set_hotbar_image() instead of hardcoded hotbar.png
1 parent 5b518ed commit 7860097

10 files changed

+98
-10
lines changed
 

‎doc/lua_api.txt

+4
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,10 @@ Player-only: (no-op for other objects)
16181618
^ if a flag is nil, the flag is not modified
16191619
- hud_set_hotbar_itemcount(count): sets number of items in builtin hotbar
16201620
^ count: number of items, must be between 1 and 23
1621+
- hud_set_hotbar_image(texturename)
1622+
^ sets background image for hotbar
1623+
- hud_set_hotbar_selected_image(texturename)
1624+
^ sets image for selected item of hotbar
16211625

16221626
InvRef: Reference to an inventory
16231627
methods:

‎src/client.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -2175,6 +2175,10 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id)
21752175
s32 hotbar_itemcount = readS32((u8*) value.c_str());
21762176
if(hotbar_itemcount > 0 && hotbar_itemcount <= HUD_HOTBAR_ITEMCOUNT_MAX)
21772177
player->hud_hotbar_itemcount = hotbar_itemcount;
2178+
} else if (param == HUD_PARAM_HOTBAR_IMAGE) {
2179+
((LocalPlayer *) player)->hotbar_image = value;
2180+
} else if (param == HUD_PARAM_HOTBAR_SELECTED_IMAGE) {
2181+
((LocalPlayer *) player)->hotbar_selected_image = value;
21782182
}
21792183
}
21802184
else

‎src/hud.cpp

+26-7
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,11 @@ Hud::Hud(video::IVideoDriver *driver, gui::IGUIEnvironment* guienv,
6464
selectionbox_argb = video::SColor(255, sbox_r, sbox_g, sbox_b);
6565

6666
use_crosshair_image = tsrc->isKnownSourceImage("crosshair.png");
67-
use_hotbar_bg_img = tsrc->isKnownSourceImage("hotbar.png");
68-
use_hotbar_border_img = tsrc->isKnownSourceImage("hotbar_selected.png");
67+
68+
hotbar_image = "";
69+
use_hotbar_image = false;
70+
hotbar_selected_image = "";
71+
use_hotbar_selected_image = false;
6972
}
7073

7174

@@ -95,10 +98,26 @@ void Hud::drawItem(v2s32 upperleftpos, s32 imgsize, s32 itemcount,
9598
const video::SColor hbar_color(255, 255, 255, 255);
9699
const video::SColor hbar_colors[] = {hbar_color, hbar_color, hbar_color, hbar_color};
97100

98-
if (use_hotbar_bg_img) {
101+
if (hotbar_image != player->hotbar_image) {
102+
hotbar_image = player->hotbar_image;
103+
if (hotbar_image != "")
104+
use_hotbar_image = tsrc->isKnownSourceImage(hotbar_image);
105+
else
106+
use_hotbar_image = false;
107+
}
108+
109+
if (hotbar_selected_image != player->hotbar_selected_image) {
110+
hotbar_selected_image = player->hotbar_selected_image;
111+
if (hotbar_selected_image != "")
112+
use_hotbar_selected_image = tsrc->isKnownSourceImage(hotbar_selected_image);
113+
else
114+
use_hotbar_selected_image = false;
115+
}
116+
117+
if (use_hotbar_image) {
99118
core::rect<s32> imgrect2(-padding/2, -padding/2, width+padding/2, height+padding/2);
100119
core::rect<s32> rect2 = imgrect2 + pos;
101-
video::ITexture *texture = tsrc->getTexture("hotbar.png");
120+
video::ITexture *texture = tsrc->getTexture(hotbar_image);
102121
core::dimension2di imgsize(texture->getOriginalSize());
103122
driver->draw2DImage(texture, rect2,
104123
core::rect<s32>(core::position2d<s32>(0,0), imgsize),
@@ -127,10 +146,10 @@ void Hud::drawItem(v2s32 upperleftpos, s32 imgsize, s32 itemcount,
127146
core::rect<s32> rect = imgrect + pos + steppos;
128147

129148
if (selectitem == i + 1) {
130-
if (use_hotbar_border_img) {
149+
if (use_hotbar_selected_image) {
131150
core::rect<s32> imgrect2(-padding*2, -padding*2, height, height);
132151
rect = imgrect2 + pos + steppos;
133-
video::ITexture *texture = tsrc->getTexture("hotbar_selected.png");
152+
video::ITexture *texture = tsrc->getTexture(hotbar_selected_image);
134153
core::dimension2di imgsize(texture->getOriginalSize());
135154
driver->draw2DImage(texture, rect,
136155
core::rect<s32>(core::position2d<s32>(0,0), imgsize),
@@ -192,7 +211,7 @@ void Hud::drawItem(v2s32 upperleftpos, s32 imgsize, s32 itemcount,
192211
}
193212

194213
video::SColor bgcolor2(128, 0, 0, 0);
195-
if (!use_hotbar_bg_img)
214+
if (!use_hotbar_image)
196215
driver->draw2DRectangle(bgcolor2, rect, NULL);
197216
drawItemStack(driver, font, item, rect, NULL, gamedef);
198217
}

‎src/hud.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3939
#define HUD_FLAG_BREATHBAR_VISIBLE (1 << 4)
4040

4141
#define HUD_PARAM_HOTBAR_ITEMCOUNT 1
42+
#define HUD_PARAM_HOTBAR_IMAGE 2
43+
#define HUD_PARAM_HOTBAR_SELECTED_IMAGE 3
4244

4345
#define HUD_HOTBAR_ITEMCOUNT_DEFAULT 8
4446
#define HUD_HOTBAR_ITEMCOUNT_MAX 23
@@ -106,8 +108,10 @@ class Hud {
106108
video::SColor crosshair_argb;
107109
video::SColor selectionbox_argb;
108110
bool use_crosshair_image;
109-
bool use_hotbar_border_img;
110-
bool use_hotbar_bg_img;
111+
std::string hotbar_image;
112+
bool use_hotbar_image;
113+
std::string hotbar_selected_image;
114+
bool use_hotbar_selected_image;
111115

112116
Hud(video::IVideoDriver *driver, gui::IGUIEnvironment* guienv,
113117
gui::IGUIFont *font, u32 text_height, IGameDef *gamedef,

‎src/localplayer.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ LocalPlayer::LocalPlayer(IGameDef *gamedef):
4343
last_pitch(0),
4444
last_yaw(0),
4545
last_keyPressed(0),
46+
hotbar_image(""),
47+
hotbar_selected_image(""),
4648
m_sneak_node(32767,32767,32767),
4749
m_sneak_node_exists(false),
4850
m_old_node_below(32767,32767,32767),

‎src/localplayer.h

+3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ class LocalPlayer : public Player
6161

6262
float camera_impact;
6363

64+
std::string hotbar_image;
65+
std::string hotbar_selected_image;
66+
6467
private:
6568
// This is used for determining the sneaking range
6669
v3s16 m_sneak_node;

‎src/script/lua_api/l_object.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,34 @@ int ObjectRef::l_hud_set_hotbar_itemcount(lua_State *L)
10221022
return 1;
10231023
}
10241024

1025+
// hud_set_hotbar_image(self, name)
1026+
int ObjectRef::l_hud_set_hotbar_image(lua_State *L)
1027+
{
1028+
ObjectRef *ref = checkobject(L, 1);
1029+
Player *player = getplayer(ref);
1030+
if (player == NULL)
1031+
return 0;
1032+
1033+
std::string name = lua_tostring(L, 2);
1034+
1035+
getServer(L)->hudSetHotbarImage(player, name);
1036+
return 1;
1037+
}
1038+
1039+
// hud_set_hotbar_selected_image(self, name)
1040+
int ObjectRef::l_hud_set_hotbar_selected_image(lua_State *L)
1041+
{
1042+
ObjectRef *ref = checkobject(L, 1);
1043+
Player *player = getplayer(ref);
1044+
if (player == NULL)
1045+
return 0;
1046+
1047+
std::string name = lua_tostring(L, 2);
1048+
1049+
getServer(L)->hudSetHotbarSelectedImage(player, name);
1050+
return 1;
1051+
}
1052+
10251053
ObjectRef::ObjectRef(ServerActiveObject *object):
10261054
m_object(object)
10271055
{
@@ -1136,5 +1164,7 @@ const luaL_reg ObjectRef::methods[] = {
11361164
luamethod(ObjectRef, hud_get),
11371165
luamethod(ObjectRef, hud_set_flags),
11381166
luamethod(ObjectRef, hud_set_hotbar_itemcount),
1167+
luamethod(ObjectRef, hud_set_hotbar_image),
1168+
luamethod(ObjectRef, hud_set_hotbar_selected_image),
11391169
{0,0}
11401170
};

‎src/script/lua_api/l_object.h

+6
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,12 @@ class ObjectRef : public ModApiBase {
215215
// hud_set_hotbar_itemcount(self, hotbar_itemcount)
216216
static int l_hud_set_hotbar_itemcount(lua_State *L);
217217

218+
// hud_set_hotbar_image(self, name)
219+
static int l_hud_set_hotbar_image(lua_State *L);
220+
221+
// hud_set_hotbar_selected_image(self, name)
222+
static int l_hud_set_hotbar_selected_image(lua_State *L);
223+
218224
public:
219225
ObjectRef(ServerActiveObject *object);
220226

‎src/server.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -4999,6 +4999,20 @@ bool Server::hudSetHotbarItemcount(Player *player, s32 hotbar_itemcount) {
49994999
return true;
50005000
}
50015001

5002+
void Server::hudSetHotbarImage(Player *player, std::string name) {
5003+
if (!player)
5004+
return;
5005+
5006+
SendHUDSetParam(player->peer_id, HUD_PARAM_HOTBAR_IMAGE, name);
5007+
}
5008+
5009+
void Server::hudSetHotbarSelectedImage(Player *player, std::string name) {
5010+
if (!player)
5011+
return;
5012+
5013+
SendHUDSetParam(player->peer_id, HUD_PARAM_HOTBAR_SELECTED_IMAGE, name);
5014+
}
5015+
50025016
void Server::notifyPlayers(const std::wstring msg)
50035017
{
50045018
BroadcastChatMessage(msg);

‎src/server.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,9 @@ class Server : public con::PeerHandler, public MapEventReceiver,
493493
bool hudChange(Player *player, u32 id, HudElementStat stat, void *value);
494494
bool hudSetFlags(Player *player, u32 flags, u32 mask);
495495
bool hudSetHotbarItemcount(Player *player, s32 hotbar_itemcount);
496-
496+
void hudSetHotbarImage(Player *player, std::string name);
497+
void hudSetHotbarSelectedImage(Player *player, std::string name);
498+
497499
private:
498500

499501
// con::PeerHandler implementation.

0 commit comments

Comments
 (0)
Please sign in to comment.