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

Author Topic: Should I re-position a graphical text in the dead center again  (Read 1154 times)

0 Members and 1 Guest are viewing this topic.

datpham2

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Should I re-position a graphical text in the dead center again, which I previously do, after editing the text of the graphical text, recalculate its new center origin of the text? I did see that with a new origin, the text automatically applied to whatever the previous x and y were. Does it a wasteful thing to add another duplicate line of code to set it in the dead center?

// Draw some text
Text messageText;

// Assign the actual message
messageText.setString("Press Enter to start!");

// Position the text
FloatRect textRect = messageText.getLocalBounds();

messageText.setOrigin(textRect.left +
    textRect.width / 2.0f,
    textRect.top +
    textRect.height / 2.0f);
   
messageText.setPosition(1920 / 2.0f, 1080 / 2.0f);

/ has the player been squished by a branch?
if (branchPositions[5] == playerSide)
{
    // Change the text of the message
    messageText.setString("SQUISHED!!");

    // Center it on the screen
    FloatRect textRect = messageText.getLocalBounds();
    messageText.setOrigin(textRect.left +
        textRect.width / 2.0f,
        textRect.top +
        textRect.height / 2.0f);

    messageText.setPosition(1920 / 2.0f, 1080 / 2.0f);  // Should I add do this line of code?
}
« Last Edit: July 18, 2022, 03:40:12 pm by eXpl0it3r »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Should I re-position a graphical text in the dead center again
« Reply #1 on: July 16, 2022, 12:51:53 pm »
That's opinion really.

That said, you should consider that after a text's string has changed, you may want to re-evaluate its position.

If your text is 'anchored' to the top left, changing its position is likely unnecessary. And, in fact, changing it could make it jump around a bit.

If you text is anchored elsewhere - such as the centre - adjusting its position is very likely required. For example, if it start at 3 characters and is centred but then changes to 30 characters, it will no longer be centred and its 2nd character (approximately) will still be in centre of where the centre of the text should be.

It's worth noting, though, that baselines and maximum heights can change depending on the characters used in the text so for a "centred text line", you may wish to only adjust the x position and leave the y position in what you consider correct.
For example, imagine centring "aaayyy" and then changing it "aaaAAA" and centring that. It would be quite noticeable that the 3 lower case as have their vertical position move.



tl:dr;
Yes, if you're centring text, adjust its position every time you change its string. ;D
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything