setOrigin function for widgets
« on: 31 May 2016, 16:24:43 »
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

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Re: setOrigin function for widgets
« Reply #1 on: 31 May 2016, 16:32:37 »
TGUI currently does not support scaling and rotating widgets, so there is little reason for setting an origin.

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

you will just have to do set the position like this:
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.

Re: setOrigin function for widgets
« Reply #2 on: 31 May 2016, 16:52:03 »
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).

Widget widget;
widget->setOrigin("Middle", "Middle");

What do you think about it?

*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Re: setOrigin function for widgets
« Reply #3 on: 31 May 2016, 16:59:53 »
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.