Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: origin relative to the Transformable  (Read 5746 times)

0 Members and 2 Guests are viewing this topic.

MickeyKnox

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
origin relative to the Transformable
« on: August 26, 2024, 01:08:43 am »
I would like to set the origin of a Transformable to its center. So far I'm doing it like this:

text.setOrigin(text.getLocalBounds().width / 2, text.getLocalBounds().height / 2);

That's quite verbose I think. Moreover, once I set a new string to be displayed, I also need to calculate the origin again.

Instead, I would like to do something simple like: setRelativeOrigin(.5f, .5f) (with 0, 0 being top left; 1, 1 being lower right, and thus the center at 0.5, 0.5.)

Is something like this already possible and I've missed it? Otherwise I'd like to request it as a feature :)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: origin relative to the Transformable
« Reply #1 on: August 26, 2024, 10:52:48 am »
text.setOrigin(text.getLocalBounds().getSize() / 2) should work with SFML 2.6

Otherwise there isn't another option to change the origin and I don't see us adding setRelativeOrigin.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

MickeyKnox

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: origin relative to the Transformable
« Reply #2 on: August 26, 2024, 09:01:56 pm »
Quote
I don't see us adding setRelativeOrigin

What do you mean by that? You're not willing to invest into the developement of that function? Or you wouldn't want that to be part of the API?

I'm asking as I might consider developing this function myself at some point; and, if that's appreciated, contribute it to SFML.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: origin relative to the Transformable
« Reply #3 on: August 27, 2024, 12:02:27 am »
All the function would do is wrap a single vector operation, which is very simplistic, hard to understand the meaning/usage, and wouldn't fit into the current API landscape.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

MickeyKnox

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: origin relative to the Transformable
« Reply #4 on: August 27, 2024, 07:42:14 pm »
I agree that the function itself would be trivial. However, when the Transformable is meant to preserve the relative origin (as in my example with setting a new string to the Text) it gets a bit trickier.

But if it doesn't fit with the rest of the API I have to accept that.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: origin relative to the Transformable
« Reply #5 on: August 29, 2024, 09:19:54 pm »
The origin of a drawable should always be updated whenever the drawable is manipulated unless you can be sure that it doesn't need it for some reason. This is particularly important with texts that change string, fonts, styles, sizes etc..

Also remember that the centre of an actual visible bit of text, the origin should also take into account the position of the local bounds (as an sf::Text's bounds can be non-zero) so it's likely that you should use (presuming SFML 2.6+) something like:
text.setOrigin(text.getLocalBounds().getPosition() + (text.getLocalBounds().getSize() / 2.f));

If you are interested in using a library that calculates different parts of a drawable automatically (or understand how to calculate it for yourself), then please feel free to have a look at Plinth, in particular "SFML Anchor" (the code - similar to above - that calculates the centre, for example, is here). This also works with SFML pre-v2.6. Note that is still requires manually updating of the origin when needed; it just simplifies the code (and tends to make it more readable).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

MickeyKnox

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: origin relative to the Transformable
« Reply #6 on: September 06, 2024, 12:50:26 am »
Quote
The origin of a drawable should always be updated whenever the drawable is manipulated unless you can be sure that it doesn't need it for some reason. This is particularly important with texts that change string, fonts, styles, sizes etc..

I was proposing for SFML to handle this. Thus, when I set the origin of a text to its center, then update the string in the text, I wouldn't have to worry about updating the origin as well. Not a trivial thing, if this functionality is to be introduced to all Drawables...

Garwin

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
Re: origin relative to the Transformable
« Reply #7 on: September 06, 2024, 09:05:26 am »
I cannot speak of SFML team. But you can easily add template that handles that for each Transformable.

#include <SFML/Graphics/Transformable.hpp>
#include <type_traits>

template <typename T>
concept SfTransformable = std::is_base_of_v<sf::Transformable, T>;

void setOriginToCenter (SfTransformable auto& sfTransformableObject)
{
    sfTransformableObject.setOrigin(sfTransformableObject.getLocalBounds().getPosition()
        + (sfTransformableObject.getLocalBounds().getSize() / 2.f));
}
 

Now you can try if it works. It would even work for your graphical entities based on sf::Transformable.

#include <SFML/Graphics.hpp>

int main ()
{
    sf::RectangleShape shape;
    setOriginToCenter(shape);

    sf::Text text;
    setOriginToCenter(text);

    int a;
    //setOriginToCenter(a); // error - do not pass concept SfTransformable

    sf::Vector2f vec2f;
    //setOriginToCenter(vec2f) // error - do not pass concept SfTransformable

    return 0;
}
 

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: origin relative to the Transformable
« Reply #8 on: September 14, 2024, 06:14:16 pm »
Quote
The origin of a drawable should always be updated
I was proposing for SFML to handle this. Thus, when I set the origin of a text to its center, then update the string in the text, I wouldn't have to worry about updating the origin as well.
The problem with this is that the required result of this can be dependent on its use, especially with text.
For example, when you change a character in all lower case string to its capital, the text would move downwards. Most often, it's best to find the "best" centre for the text and then keep that when changing the string. Note that I'm, here, talking about the y value only; the x still needs updating. Also, this is more complicated for multi-line text.

My main point really is that sometimes you may want to allow it to move the text so that the true centre is where you position and sometimes you may want to text a centre based on a 'usual baseline' and centralise the x, meaning that, since that this decision is made by the user, the user should probably centre the text manually.

Of course, you can add simple functions to do this automatically in the specific way you want it, if required. For example:
void setTextString(sf::Text& text, const sf::String& string, const bool autoCenter = true)
{
    text.setString(string);
    if (autoCenter)
        text.setOrigin( /* you know this bit ;) */ );
}

// ...

sf::Text text;
// set up the text including font and character size first!
setTextString(text, "Some string");
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*