From f22951ab2b63c691f81d11267ff3e63e70457a14 Mon Sep 17 00:00:00 2001 From: luk3yx Date: Wed, 7 Jun 2023 10:28:41 +1200 Subject: [PATCH] Add pressed inventory slot background image --- doc/lua_api.txt | 1 + src/gui/guiFormSpecMenu.cpp | 3 ++- src/gui/guiInventoryList.cpp | 20 +++++++++++++++++--- src/gui/guiInventoryList.h | 5 +++++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/doc/lua_api.txt b/doc/lua_api.txt index c67c7562a..92bfb1fa1 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -2913,6 +2913,7 @@ Some types may inherit styles from parent types. * spacing - 2d vector, sets the space between inventory slots in coordinates. * bgimg - slot background image. Defaults to none. * bgimg_hovered - hovered slot background image. + * bgimg_pressed - pressed slot background image. * bgimg_middle - Makes the bgimg textures render in 9-sliced mode and defines the middle rect. * image_button (additional properties) * fgimg - standard image. Defaults to none. diff --git a/src/gui/guiFormSpecMenu.cpp b/src/gui/guiFormSpecMenu.cpp index 88eccbbb8..81d8b5d4e 100644 --- a/src/gui/guiFormSpecMenu.cpp +++ b/src/gui/guiFormSpecMenu.cpp @@ -540,12 +540,13 @@ void GUIFormSpecMenu::parseList(parserData *data, const std::string &element) video::ITexture *bgimg = style.getTexture(StyleSpec::BGIMG, m_tsrc, nullptr); video::ITexture *bgimg_hovered = style.getTexture(StyleSpec::BGIMG_HOVERED, m_tsrc, nullptr); + video::ITexture *bgimg_pressed = style.getTexture(StyleSpec::BGIMG_PRESSED, m_tsrc, nullptr); core::rect bgimg_middle = style.getRect(StyleSpec::BGIMG_MIDDLE, core::rect()); GUIInventoryList *e = new GUIInventoryList(Environment, data->current_parent, spec.fid, rect, m_invmgr, loc, listname, geom, start_i, v2s32(slot_size.X, slot_size.Y), slot_spacing, bgimg, bgimg_hovered, - bgimg_middle, this, data->inventorylist_options, m_font); + bgimg_pressed, bgimg_middle, this, data->inventorylist_options, m_font); e->setNotClipped(style.getBool(StyleSpec::NOCLIP, false)); diff --git a/src/gui/guiInventoryList.cpp b/src/gui/guiInventoryList.cpp index fb59ce76a..a9c91d4f2 100644 --- a/src/gui/guiInventoryList.cpp +++ b/src/gui/guiInventoryList.cpp @@ -36,6 +36,7 @@ GUIInventoryList::GUIInventoryList(gui::IGUIEnvironment *env, const v2f32 &slot_spacing, video::ITexture* slotbg_n_texture, video::ITexture* slotbg_h_texture, + video::ITexture* slotbg_p_texture, const core::rect &slotbg_middle, GUIFormSpecMenu *fs_menu, const Options &options, @@ -50,11 +51,13 @@ GUIInventoryList::GUIInventoryList(gui::IGUIEnvironment *env, m_slot_spacing(slot_spacing), m_slotbg_n_texture(slotbg_n_texture), m_slotbg_h_texture(slotbg_h_texture), + m_slotbg_p_texture(slotbg_p_texture), m_slotbg_middle(slotbg_middle), m_fs_menu(fs_menu), m_options(options), m_font(font), m_hovered_i(-1), + m_pressed(false), m_already_warned(false) { } @@ -118,8 +121,12 @@ void GUIInventoryList::draw() // layer 0 if (m_slotbg_n_texture) { video::ITexture *texture = m_slotbg_n_texture; - if (hovering && m_slotbg_h_texture) - texture = m_slotbg_h_texture; + if (hovering) { + if (m_pressed && m_slotbg_p_texture) + texture = m_slotbg_p_texture; + else if (m_slotbg_h_texture) + texture = m_slotbg_h_texture; + } core::rect srcrect(core::position2d(0, 0), core::dimension2di(texture->getOriginalSize())); @@ -198,8 +205,15 @@ bool GUIInventoryList::OnEvent(const SEvent &event) m_hovered_i = getItemIndexAtPos(v2s32(event.MouseInput.X, event.MouseInput.Y)); - if (m_hovered_i != -1) + if (m_hovered_i != -1) { + if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) + m_pressed = true; + else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) + m_pressed = false; return IGUIElement::OnEvent(event); + } + + m_pressed = false; // no item slot at pos of mouse event => allow clicking through // find the element that would be hovered if this inventorylist was invisible diff --git a/src/gui/guiInventoryList.h b/src/gui/guiInventoryList.h index 712c6905c..d576730a5 100644 --- a/src/gui/guiInventoryList.h +++ b/src/gui/guiInventoryList.h @@ -71,6 +71,7 @@ public: const v2f32 &slot_spacing, video::ITexture* slotbg_n_texture, video::ITexture* slotbg_h_texture, + video::ITexture* slotbg_p_texture, const core::rect &slotbg_middle, GUIFormSpecMenu *fs_menu, const Options &options, @@ -123,6 +124,7 @@ private: // Slot textures video::ITexture* m_slotbg_n_texture; video::ITexture* m_slotbg_h_texture; + video::ITexture* m_slotbg_p_texture; core::rect m_slotbg_middle; // the GUIFormSpecMenu can have an item selected and co. @@ -136,6 +138,9 @@ private: // the index of the hovered item; -1 if no item is hovered s32 m_hovered_i; + // Whether the hovered item is being pressed + bool m_pressed; + // we do not want to write a warning on every draw bool m_already_warned; };