IGUIElement::getAlignment() suggestion

Discuss about anything related to the Irrlicht Engine, or read announcements about any significant features or usage changes.
Post Reply
chronologicaldot
Competition winner
Posts: 688
Joined: Mon Sep 10, 2012 8:51 am

IGUIElement::getAlignment() suggestion

Post by chronologicaldot »

Not very important, but I ran across a case where I needed the alignment of a GUI element: It happens when you have one element that you want to add "transparently" (in a metaphorical sense) by having it wrap another element. To do that, it's easy enough to just grab the position of the inner element and make it that of the outer element while expanding the inner element to be glued to the outer one. Unfortunately, for the whole thing to work on resize, I need to get the alignment as well. I came up with my own little fix, which was to add a simple struct called AlignmentInfo to EGUIAlignment.h and add a setter and getter to IGUIElement.

EGUIAlignment.h

Code: Select all

 
// Preceded by GUIAlignmentNames [] ...
 
//! Convenient packaging of alignment info
struct AlignmentInfo
{
    EGUI_ALIGNMENT left;
    EGUI_ALIGNMENT right;
    EGUI_ALIGNMENT top;
    EGUI_ALIGNMENT bottom;
 
    AlignmentInfo() : left( EGUIA_UPPERLEFT ), right( EGUIA_UPPERLEFT ), top( EGUIA_UPPERLEFT ), bottom( EGUIA_UPPERLEFT ) {}
    AlignmentInfo( EGUI_ALIGNMENT l, EGUI_ALIGNMENT r, EGUI_ALIGNMENT t, EGUI_ALIGNMENT b ) : left(l), right(r), top(t), bottom(b) {}
};
 
IGUIElement.h

Code: Select all

 
// Preceded by setAlignment() {} ...
 
    //! Set the alignment with an info struct
    void setAlignment( AlignmentInfo info )
    {
        setAlignment( info.left, info.right, info.top, info.bottom );
    }
 
 
    //! Retrieve the alignment.
    AlignmentInfo  getAlignment() const
    {
        return AlignmentInfo( AlignLeft, AlignRight, AlignTop, AlignBottom );
    }
 
I considered using a single function that took an index, but it was more confusing and the above solution was just as clean.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: IGUIElement::getAlignment() suggestion

Post by CuteAlien »

Hm, indeed no getter so far. We could add 4 functions getAlignmentLeft, getAlignmentRight, getAlignmentTop, getAlignmentBottom.
Setter/getter are then a bit asymmetric, but avoids adding a new struct.

Nicest c++ solution would probably be to use an inner class (or struct) in IGUIElement, but not sure how well those work with language wrappers.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply