setOrigin function for widgets

Started by heaven31415, 31 May 2016, 16:24:43

heaven31415

Hello, I was wondering whether I can find something similar to SFML setOrigin method inside of TGUI library. As far as I can see in TGUI by default the origin is on the top left corner of widget (same as in SFML), however it is very nice sometimes to change it. For example to center point of widget, how should I approach this problem?

texus

TGUI currently does not support scaling and rotating widgets, so there is little reason for setting an origin.

Instead of the following
Code (cpp) Select
setOrigin(widget->getSize() / 2.f);
setPosition(x, y);


you will just have to do set the position like this:
Code (cpp) Select
setPosition(x - widget->getSize().x / 2.f, y - widget->getSize().y / 2.f);

I will consider a setOrigin function for v0.8 where I plan to add scaling, but don't expect it to be added in v0.7.

heaven31415

In that case, if it is not yet implemented I would like to propose something.
Normally setOrigin function is just taking x and y coordinates in local coordinate system, however in my opinion it would be very handy to have another overload.
This overload should take two std::string arguments which would determine the position of the origin.

First std::string should take only 3 possible values: "Left", "Middle", "Right", second one should take another 3 values: "Top", "Middle", "Bottom". This gives you 9 very easy to set origin points which are very often used (from my experience).

Code (cpp) Select
Widget widget;
widget->setOrigin("Middle", "Middle");


What do you think about it?

texus

It is a good idea, but I'll look into the exact implementation when the time comes (e.g. maybe go for one parameter so that you don't have to write "Middle" twice and have e.g. "TopLeft" for others).
But one thing that bothers me a bit is that it will probably never be used. I can come up with use cases for having the origin as the center of the widget, but I don't think the other options would be used. I might just as well just have a function that takes a boolean which determines whether it is top left of centered.