Skip to content

Commit

Permalink
Fix offset being ignored by inventory bar
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenwardy committed Jan 9, 2015
1 parent 3c8e372 commit 027e404
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 40 deletions.
75 changes: 41 additions & 34 deletions src/hud.cpp
Expand Up @@ -58,22 +58,22 @@ Hud::Hud(video::IVideoDriver *driver, scene::ISceneManager* smgr,
for (unsigned int i=0; i < 4; i++ ){
hbar_colors[i] = hbar_color;
}

tsrc = gamedef->getTextureSource();

v3f crosshair_color = g_settings->getV3F("crosshair_color");
u32 cross_r = rangelim(myround(crosshair_color.X), 0, 255);
u32 cross_g = rangelim(myround(crosshair_color.Y), 0, 255);
u32 cross_b = rangelim(myround(crosshair_color.Z), 0, 255);
u32 cross_a = rangelim(g_settings->getS32("crosshair_alpha"), 0, 255);
crosshair_argb = video::SColor(cross_a, cross_r, cross_g, cross_b);

v3f selectionbox_color = g_settings->getV3F("selectionbox_color");
u32 sbox_r = rangelim(myround(selectionbox_color.X), 0, 255);
u32 sbox_g = rangelim(myround(selectionbox_color.Y), 0, 255);
u32 sbox_b = rangelim(myround(selectionbox_color.Z), 0, 255);
selectionbox_argb = video::SColor(255, sbox_r, sbox_g, sbox_b);

use_crosshair_image = tsrc->isKnownSourceImage("crosshair.png");

hotbar_image = "";
Expand Down Expand Up @@ -158,24 +158,30 @@ void Hud::drawItem(const ItemStack &item, const core::rect<s32>& rect, bool sele
}

//NOTE: selectitem = 0 -> no selected; selectitem 1-based
void Hud::drawItems(v2s32 upperleftpos, s32 itemcount, s32 offset,
InventoryList *mainlist, u16 selectitem, u16 direction)
void Hud::drawItems(v2s32 upperleftpos, s32 itemcount, s32 inv_offset,
InventoryList *mainlist, u16 selectitem, u16 direction, v2s32 offset)
{
#ifdef HAVE_TOUCHSCREENGUI
if ( (g_touchscreengui) && (offset == 0))
if ( (g_touchscreengui) && (inv_offset == 0))
g_touchscreengui->resetHud();
#endif

s32 height = m_hotbar_imagesize + m_padding * 2;
s32 width = (itemcount - offset) * (m_hotbar_imagesize + m_padding * 2);
s32 width = (itemcount - inv_offset) * (m_hotbar_imagesize + m_padding * 2);

if (direction == HUD_DIR_TOP_BOTTOM || direction == HUD_DIR_BOTTOM_TOP) {
width = m_hotbar_imagesize + m_padding * 2;
height = (itemcount - offset) * (m_hotbar_imagesize + m_padding * 2);
height = (itemcount - inv_offset) * (m_hotbar_imagesize + m_padding * 2);
}

offset.X *= g_settings->getFloat("hud_scaling") *
porting::getDisplayDensity();

offset.Y *= g_settings->getFloat("hud_scaling") *
porting::getDisplayDensity();

// Position of upper left corner of bar
v2s32 pos = upperleftpos;
v2s32 pos = upperleftpos + offset;

if (hotbar_image != player->hotbar_image) {
hotbar_image = player->hotbar_image;
Expand Down Expand Up @@ -205,26 +211,25 @@ void Hud::drawItems(v2s32 upperleftpos, s32 itemcount, s32 offset,
NULL, hbar_colors, true);
}

for (s32 i = offset; i < itemcount && (size_t)i < mainlist->getSize(); i++)
{
for (s32 i = inv_offset; i < itemcount && (size_t)i < mainlist->getSize(); i++) {
v2s32 steppos;
s32 fullimglen = m_hotbar_imagesize + m_padding * 2;

core::rect<s32> imgrect(0, 0, m_hotbar_imagesize, m_hotbar_imagesize);

switch (direction) {
case HUD_DIR_RIGHT_LEFT:
steppos = v2s32(-(m_padding + (i - offset) * fullimglen), m_padding);
break;
case HUD_DIR_TOP_BOTTOM:
steppos = v2s32(m_padding, m_padding + (i - offset) * fullimglen);
break;
case HUD_DIR_BOTTOM_TOP:
steppos = v2s32(m_padding, -(m_padding + (i - offset) * fullimglen));
break;
default:
steppos = v2s32(m_padding + (i - offset) * fullimglen, m_padding);
break;
case HUD_DIR_RIGHT_LEFT:
steppos = v2s32(-(m_padding + (i - inv_offset) * fullimglen), m_padding);
break;
case HUD_DIR_TOP_BOTTOM:
steppos = v2s32(m_padding, m_padding + (i - inv_offset) * fullimglen);
break;
case HUD_DIR_BOTTOM_TOP:
steppos = v2s32(m_padding, -(m_padding + (i - inv_offset) * fullimglen));
break;
default:
steppos = v2s32(m_padding + (i - inv_offset) * fullimglen, m_padding);
break;
}

drawItem(mainlist->getItem(i), (imgrect + pos + steppos), (i +1) == selectitem );
Expand All @@ -237,14 +242,15 @@ void Hud::drawItems(v2s32 upperleftpos, s32 itemcount, s32 offset,
}


void Hud::drawLuaElements(v3s16 camera_offset) {
void Hud::drawLuaElements(v3s16 camera_offset)
{
u32 text_height = g_fontengine->getTextHeight();
irr::gui::IGUIFont* font = g_fontengine->getFont();
for (size_t i = 0; i != player->maxHudId(); i++) {
HudElement *e = player->getHud(i);
if (!e)
continue;

v2s32 pos(floor(e->pos.X * (float) m_screensize.X + 0.5),
floor(e->pos.Y * (float) m_screensize.Y + 0.5));
switch (e->type) {
Expand Down Expand Up @@ -288,7 +294,8 @@ void Hud::drawLuaElements(v3s16 camera_offset) {
break; }
case HUD_ELEM_INVENTORY: {
InventoryList *inv = inventory->getList(e->text);
drawItems(pos, e->number, 0, inv, e->item, e->dir);
v2s32 offs(e->offset.X, e->offset.Y);
drawItems(pos, e->number, 0, inv, e->item, e->dir, offs);
break; }
case HUD_ELEM_WAYPOINT: {
v3f p_pos = player->getPosition() / BS;
Expand Down Expand Up @@ -331,11 +338,11 @@ void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture,
{
const video::SColor color(255, 255, 255, 255);
const video::SColor colors[] = {color, color, color, color};

video::ITexture *stat_texture = tsrc->getTexture(texture);
if (!stat_texture)
return;

core::dimension2di srcd(stat_texture->getOriginalSize());
core::dimension2di dstd;
if (size == v2s32()) {
Expand Down Expand Up @@ -375,7 +382,7 @@ void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture,
}
steppos.X *= dstd.Width;
steppos.Y *= dstd.Height;

for (s32 i = 0; i < count / 2; i++)
{
core::rect<s32> srcrect(0, 0, srcd.Width, srcd.Height);
Expand All @@ -385,7 +392,7 @@ void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture,
driver->draw2DImage(stat_texture, dstrect, srcrect, NULL, colors, true);
p += steppos;
}

if (count % 2 == 1)
{
core::rect<s32> srcrect(0, 0, srcd.Width / 2, srcd.Height);
Expand All @@ -406,7 +413,7 @@ void Hud::drawHotbar(u16 playeritem) {
//silently ignore this we may not be initialized completely
return;
}

s32 hotbar_itemcount = player->hud_hotbar_itemcount;
s32 width = hotbar_itemcount * (m_hotbar_imagesize + m_padding * 2);
v2s32 pos = centerlowerpos - v2s32(width / 2, m_hotbar_imagesize + m_padding * 3);
Expand Down Expand Up @@ -449,7 +456,7 @@ void Hud::drawHotbar(u16 playeritem) {


void Hud::drawCrosshair() {

if (use_crosshair_image) {
video::ITexture *crosshair = tsrc->getTexture("crosshair.png");
v2u32 size = crosshair->getOriginalSize();
Expand Down Expand Up @@ -495,7 +502,7 @@ void drawItemStack(video::IVideoDriver *driver,
{
if(item.empty())
return;

const ItemDefinition &def = item.getDefinition(gamedef->idef());
video::ITexture *texture = gamedef->idef()->getInventoryTexture(def.name, gamedef);

Expand Down
12 changes: 6 additions & 6 deletions src/hud.h
Expand Up @@ -116,11 +116,11 @@ class Hud {
std::string hotbar_selected_image;
bool use_hotbar_selected_image;
v3s16 camera_offset;

Hud(video::IVideoDriver *driver,scene::ISceneManager* smgr,
gui::IGUIEnvironment* guienv, IGameDef *gamedef, LocalPlayer *player,
Inventory *inventory);

void drawHotbar(u16 playeritem);
void resizeHotbar();
void drawCrosshair();
Expand All @@ -129,12 +129,12 @@ class Hud {
private:
void drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture,
s32 count, v2s32 offset, v2s32 size=v2s32());
void drawItems(v2s32 upperleftpos, s32 itemcount, s32 offset,
InventoryList *mainlist, u16 selectitem, u16 direction);

void drawItems(v2s32 upperleftpos, s32 itemcount, s32 inv_offset,
InventoryList *mainlist, u16 selectitem, u16 direction, v2s32 offset=v2s32(0, 0));

void drawItem(const ItemStack &item, const core::rect<s32>& rect, bool selected);

v2u32 m_screensize;
v2s32 m_displaycenter;
s32 m_hotbar_imagesize;
Expand Down

0 comments on commit 027e404

Please sign in to comment.