small drawItemStack cleanup
authorest31 <MTest31@outlook.com>
Sun, 7 Feb 2016 19:27:50 +0000 (20:27 +0100)
committerest31 <MTest31@outlook.com>
Sun, 7 Feb 2016 23:35:24 +0000 (00:35 +0100)
-> 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
src/hud.cpp
src/hud.h

index 4784d73145a4daba18114d1e41b26c60a0c6b1ac..116688e956f86d85317e8681f86d3b809c2b1349 100644 (file)
@@ -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<s32>(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<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()
@@ -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);
        }
 
        /*
@@ -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 */
index 2d22f963c02482890b6222e6e5a05e0c8be07fcb..1c144b021a2119f31c92be599aae890bdb47d8eb 100644 (file)
@@ -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
@@ -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;
        }
@@ -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();
index 88e7181d6f67079152b7573e1d89daf1415dac1a..f373d4fe2703221dd11b04e92071d1506a6903a3 100644 (file)
--- 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<s32> &rect,
                const core::rect<s32> *clip,
                IGameDef *gamedef,
-               bool selected,
-               bool hovered,
-               bool dragged);
+               ItemRotationKind rotation_kind);
 
 #endif