Basic widget functions

This tutorial is about the functions that you find in the Widget base class. These functions are available for all widgets.

Position and size

Every widget can be given a certain position. This position is relative to the position of the parent widget. If you really need the absolute position on the screen you can use the getAbsolutePosition function.

The size is also a property that all widgets are supposed to have. Important to know is that borders are never part of the size, they are drawn around the widget. To get the size including the borders (and including the title bar in child window) you can use the getFullSize function.

widget->setPosition(50, 50);
sf::Vector2f position = widget->getPosition();
sf::Vector2f absolutePosition = widget->getAbsolutePosition();

widget->setSize(200, 50);
sf::Vector2f size = widget->getSize();
sf::Vector2f fullSize = widget->getFullSize();

Instead of constants, the setPosition and setSize functions can also be given layouts as argument so that you can give your widget a relative position or size. If you tell widget B to have the same size as widget A then widget B will automatically change size when you give widget A a new size.

Visible and enabled

The enabled property determines if a widget receives events. If a widget is disabled, it will still be drawn but it will be unresponsive to your mouse clicks.

The visibility property determines if a widget is drawn. A hidden widget also no longer receives events, it is no longer there. The only way to have an invisible yet still functioning widget is to use ClickableWidget. Hiding and showing widgets is similar to adding and removing them, its just easier and faster. It can be very handy to put different screens into different Panel widgets and just hide all the panels except for one.

Every widget is visible and enabled by default.

widget->hide();
widget->show();
widget->isVisible();

widget->enable();
widget->disable();
widget->isEnabled();

Overlapping widgets

What happens when widgets overlap? Which is the topmost widget? The answer is simple: the last widget that you added to the parent. You can manipulate the order in which widgets are drawn by using the moveToFront and moveToBack functions though.

widget->moveToFront();
widget->moveToBack();

Transparency

You can make widgets semi-transparent with the setOpacity functions. It takes a float between 0 and 1 where 0 is completely transparent and 1 is fully opaque.

widget->setOpacity(0.8f);