From 16c70087711db38ac40a428c64623355b8e7691b Mon Sep 17 00:00:00 2001 From: est31 Date: Sun, 7 Feb 2016 20:27:50 +0100 Subject: [PATCH] small drawItemStack cleanup -> Replace the three bool params with an enum -> Add struct for the static content, leads to less repetition -> cache enable_animations setting --- src/guiFormSpecMenu.cpp | 12 ++++++---- src/hud.cpp | 53 +++++++++++++---------------------------- src/hud.h | 11 ++++++--- 3 files changed, 32 insertions(+), 44 deletions(-) diff --git a/src/guiFormSpecMenu.cpp b/src/guiFormSpecMenu.cpp index 4784d7314..116688e95 100644 --- a/src/guiFormSpecMenu.cpp +++ b/src/guiFormSpecMenu.cpp @@ -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) { @@ -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 @@ -2284,7 +2286,7 @@ void GUIFormSpecMenu::drawSelectedItem() if (!m_selected_item) { drawItemStack(driver, m_font, ItemStack(), core::rect(v2s32(0, 0), v2s32(0, 0)), - NULL, m_gamedef, false, false, true); + NULL, m_gamedef, IT_ROT_DRAGGED); return; } @@ -2297,7 +2299,7 @@ void GUIFormSpecMenu::drawSelectedItem() core::rect imgrect(0,0,imgsize.X,imgsize.Y); core::rect 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() @@ -2434,7 +2436,7 @@ void GUIFormSpecMenu::drawMenu() // Viewport rectangle on screen core::rect rect = imgrect + spec.pos; drawItemStack(driver, m_font, item, rect, &AbsoluteClippingRect, - m_gamedef, false, false, false); + m_gamedef, IT_ROT_NONE); } /* @@ -2452,7 +2454,7 @@ void GUIFormSpecMenu::drawMenu() if (!item_hovered) { drawItemStack(driver, m_font, ItemStack(), core::rect(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 */ diff --git a/src/hud.cpp b/src/hud.cpp index 2d22f963c..1c144b021 100644 --- a/src/hud.cpp +++ b/src/hud.cpp @@ -156,7 +156,7 @@ void Hud::drawItem(const ItemStack &item, const core::rect& 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 @@ -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 &rect, const core::rect *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; } @@ -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(); + 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(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(); - } else { - delta = porting::getDeltaMs(dragged_time, getTimeMs()) % 100000; + delta = porting::getDeltaMs(ti.time, getTimeMs()) % 100000; } } core::rect oldViewPort = driver->getViewPort(); diff --git a/src/hud.h b/src/hud.h index 88e7181d6..f373d4fe2 100644 --- a/src/hud.h +++ b/src/hud.h @@ -147,15 +147,20 @@ private: 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 &rect, const core::rect *clip, IGameDef *gamedef, - bool selected, - bool hovered, - bool dragged); + ItemRotationKind rotation_kind); #endif