Fix background[] pos-offset lower-right-corner being at least (1,1) (#13320)

IGUIElement has a MinSize for the RelativeRect, which is at least (1,1).
This means a pos offset of (0,0) will cause a seemingly off-by-1 error at the
lower right corner, and (0.1,0.1) for example will just not work on the lower
right corner.
Ergo, we can't use the AbsoluteRect for storing the pos offset.
This commit is contained in:
DS 2023-04-14 21:05:09 +02:00 committed by GitHub
parent d49d80a4a0
commit ae7271b725
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 10 deletions

@ -22,9 +22,10 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
GUIBackgroundImage::GUIBackgroundImage(gui::IGUIEnvironment *env, GUIBackgroundImage::GUIBackgroundImage(gui::IGUIEnvironment *env,
gui::IGUIElement *parent, s32 id, const core::rect<s32> &rectangle, gui::IGUIElement *parent, s32 id, const core::rect<s32> &rectangle,
const std::string &name, const core::rect<s32> &middle, const std::string &name, const core::rect<s32> &middle,
ISimpleTextureSource *tsrc, bool autoclip) : ISimpleTextureSource *tsrc, bool autoclip, v2s32 autoclip_offset) :
gui::IGUIElement(gui::EGUIET_ELEMENT, env, parent, id, rectangle), gui::IGUIElement(gui::EGUIET_ELEMENT, env, parent, id, rectangle),
m_name(name), m_middle(middle), m_tsrc(tsrc), m_autoclip(autoclip) m_name(name), m_middle(middle), m_tsrc(tsrc), m_autoclip(autoclip),
m_autoclip_offset(autoclip_offset)
{ {
} }
@ -42,9 +43,14 @@ void GUIBackgroundImage::draw()
return; return;
} }
core::rect<s32> rect = AbsoluteRect; core::rect<s32> rect;
if (m_autoclip) if (m_autoclip) {
rect.LowerRightCorner += Parent->getAbsoluteClippingRect().getSize(); rect = Parent->getAbsoluteClippingRect();
rect.UpperLeftCorner -= m_autoclip_offset;
rect.LowerRightCorner += m_autoclip_offset;
} else {
rect = AbsoluteRect;
}
video::IVideoDriver *driver = Environment->getVideoDriver(); video::IVideoDriver *driver = Environment->getVideoDriver();

@ -26,7 +26,8 @@ class GUIBackgroundImage : public gui::IGUIElement
public: public:
GUIBackgroundImage(gui::IGUIEnvironment *env, gui::IGUIElement *parent, s32 id, GUIBackgroundImage(gui::IGUIEnvironment *env, gui::IGUIElement *parent, s32 id,
const core::rect<s32> &rectangle, const std::string &name, const core::rect<s32> &rectangle, const std::string &name,
const core::rect<s32> &middle, ISimpleTextureSource *tsrc, bool autoclip); const core::rect<s32> &middle, ISimpleTextureSource *tsrc, bool autoclip,
v2s32 autoclip_offset);
virtual void draw() override; virtual void draw() override;
@ -35,4 +36,5 @@ private:
core::rect<s32> m_middle; core::rect<s32> m_middle;
ISimpleTextureSource *m_tsrc; ISimpleTextureSource *m_tsrc;
bool m_autoclip; bool m_autoclip;
v2s32 m_autoclip_offset;
}; };

@ -1101,17 +1101,18 @@ void GUIFormSpecMenu::parseBackground(parserData* data, const std::string &eleme
258 + m_fields.size() 258 + m_fields.size()
); );
core::rect<s32> rect; core::rect<s32> rect{};
v2s32 autoclip_offset{};
if (!clip) { if (!clip) {
// no auto_clip => position like normal image // no auto_clip => position like normal image
rect = core::rect<s32>(pos, pos + geom); rect = core::rect<s32>(pos, pos + geom);
} else { } else {
// it will be auto-clipped when drawing // element will be auto-clipped when drawing
rect = core::rect<s32>(-pos, pos); autoclip_offset = pos;
} }
GUIBackgroundImage *e = new GUIBackgroundImage(Environment, data->background_parent.get(), GUIBackgroundImage *e = new GUIBackgroundImage(Environment, data->background_parent.get(),
spec.fid, rect, name, middle, m_tsrc, clip); spec.fid, rect, name, middle, m_tsrc, clip, autoclip_offset);
FATAL_ERROR_IF(!e, "Failed to create background formspec element"); FATAL_ERROR_IF(!e, "Failed to create background formspec element");