Skip to content

Commit

Permalink
small drawItemStack cleanup
Browse files Browse the repository at this point in the history
-> Replace the three bool params with an enum
-> Add struct for the static content, leads to less repetition
-> cache enable_animations setting
  • Loading branch information
est31 committed Feb 7, 2016
1 parent 6cd2b3b commit 16c7008
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 44 deletions.
12 changes: 7 additions & 5 deletions src/guiFormSpecMenu.cpp
Expand Up @@ -2196,6 +2196,8 @@ void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase,
&& m_selected_item->listname == s.listname
&& m_selected_item->i == item_i;
bool hovering = rect.isPointInside(m_pointer);
ItemRotationKind rotation_kind = selected ? IT_ROT_SELECTED :
(hovering ? IT_ROT_HOVERED : IT_ROT_NONE);

if (phase == 0) {
if (hovering) {
Expand Down Expand Up @@ -2238,7 +2240,7 @@ void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase,
{
drawItemStack(driver, m_font, item,
rect, &AbsoluteClippingRect, m_gamedef,
selected, hovering, false);
rotation_kind);
}

// Draw tooltip
Expand Down Expand Up @@ -2284,7 +2286,7 @@ void GUIFormSpecMenu::drawSelectedItem()
if (!m_selected_item) {
drawItemStack(driver, m_font, ItemStack(),
core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
NULL, m_gamedef, false, false, true);
NULL, m_gamedef, IT_ROT_DRAGGED);
return;
}

Expand All @@ -2297,7 +2299,7 @@ void GUIFormSpecMenu::drawSelectedItem()

core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
drawItemStack(driver, m_font, stack, rect, NULL, m_gamedef, false, false, true);
drawItemStack(driver, m_font, stack, rect, NULL, m_gamedef, IT_ROT_DRAGGED);
}

void GUIFormSpecMenu::drawMenu()
Expand Down Expand Up @@ -2434,7 +2436,7 @@ void GUIFormSpecMenu::drawMenu()
// Viewport rectangle on screen
core::rect<s32> rect = imgrect + spec.pos;
drawItemStack(driver, m_font, item, rect, &AbsoluteClippingRect,
m_gamedef, false, false, false);
m_gamedef, IT_ROT_NONE);
}

/*
Expand All @@ -2452,7 +2454,7 @@ void GUIFormSpecMenu::drawMenu()
if (!item_hovered) {
drawItemStack(driver, m_font, ItemStack(),
core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
NULL, m_gamedef, false, true, false);
NULL, m_gamedef, IT_ROT_HOVERED);
}

/* TODO find way to show tooltips on touchscreen */
Expand Down
53 changes: 17 additions & 36 deletions src/hud.cpp
Expand Up @@ -156,7 +156,7 @@ void Hud::drawItem(const ItemStack &item, const core::rect<s32>& rect,
if (!use_hotbar_image)
driver->draw2DRectangle(bgcolor2, rect, NULL);
drawItemStack(driver, g_fontengine->getFont(), item, rect, NULL,
gamedef, selected, false, false);
gamedef, selected ? IT_ROT_SELECTED : IT_ROT_NONE);
}

//NOTE: selectitem = 0 -> no selected; selectitem 1-based
Expand Down Expand Up @@ -486,32 +486,26 @@ void Hud::resizeHotbar() {
}
}

struct MeshTimeInfo {
s32 time;
scene::IMesh *mesh;
};

void drawItemStack(video::IVideoDriver *driver,
gui::IGUIFont *font,
const ItemStack &item,
const core::rect<s32> &rect,
const core::rect<s32> *clip,
IGameDef *gamedef,
bool selected,
bool hovered,
bool dragged)
ItemRotationKind rotation_kind)
{
static s32 hovered_time;
static s32 selected_time;
static s32 dragged_time;
static scene::IMesh *hovered_mesh;
static scene::IMesh *selected_mesh;
static scene::IMesh *dragged_mesh;
bool enable_animations =
static MeshTimeInfo rotation_time_infos[IT_ROT_NONE];
static bool enable_animations =
g_settings->getBool("inventory_items_animations");

if (item.empty()) {
if (selected) {
selected_mesh = NULL;
} else if (hovered) {
hovered_mesh = NULL;
} else if (dragged) {
dragged_mesh = NULL;
if (rotation_kind < IT_ROT_NONE) {
rotation_time_infos[rotation_kind].mesh = NULL;
}
return;
}
Expand All @@ -522,26 +516,13 @@ void drawItemStack(video::IVideoDriver *driver,
if (mesh) {
driver->clearZBuffer();
s32 delta = 0;
if (selected) {
if (mesh != selected_mesh) {
selected_mesh = mesh;
selected_time = getTimeMs();
} else {
delta = porting::getDeltaMs(selected_time, getTimeMs()) % 100000;
}
} else if (hovered) {
if (mesh != hovered_mesh) {
hovered_mesh = mesh;
hovered_time = getTimeMs();
} else {
delta = porting::getDeltaMs(hovered_time, getTimeMs()) % 100000;
}
} else if (dragged) {
if (mesh != dragged_mesh) {
dragged_mesh = mesh;
dragged_time = getTimeMs();
if (rotation_kind < IT_ROT_NONE) {
MeshTimeInfo &ti = rotation_time_infos[rotation_kind];
if (mesh != ti.mesh) {
ti.mesh = mesh;
ti.time = getTimeMs();
} else {
delta = porting::getDeltaMs(dragged_time, getTimeMs()) % 100000;
delta = porting::getDeltaMs(ti.time, getTimeMs()) % 100000;
}
}
core::rect<s32> oldViewPort = driver->getViewPort();
Expand Down
11 changes: 8 additions & 3 deletions src/hud.h
Expand Up @@ -147,15 +147,20 @@ class Hud {
video::SColor hbar_colors[4];
};

enum ItemRotationKind {
IT_ROT_SELECTED,
IT_ROT_HOVERED,
IT_ROT_DRAGGED,
IT_ROT_NONE, // Must be last, also serves as number
};

void drawItemStack(video::IVideoDriver *driver,
gui::IGUIFont *font,
const ItemStack &item,
const core::rect<s32> &rect,
const core::rect<s32> *clip,
IGameDef *gamedef,
bool selected,
bool hovered,
bool dragged);
ItemRotationKind rotation_kind);

#endif

Expand Down

0 comments on commit 16c7008

Please sign in to comment.