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

Author Topic: Text position and height  (Read 1208 times)

0 Members and 1 Guest are viewing this topic.

Noctiphobia

  • Newbie
  • *
  • Posts: 5
    • View Profile
Text position and height
« on: March 19, 2015, 01:25:33 am »
Hello,
I've ran into an issue I have no idea how to resolve.
I have a scoreboard, which is made up of a RectangleShape (back) and three Texts (caption (bold, with bigger font size), names and scores (parallel to names)). The caption is supposed to be centered on top of the rectangle, with names and scores directly below it, and the back is supposed to be 200 pixels wide and exactly as tall as the sum of caption's and names' height (of course I'm going to add some indentation after I resolve this problem).
The problem is that after using the following code (well, the following code is stripped down to the essential parts), nothing besides back's position is what I imagined it to be on the y axis (positions on the x axis work fine). The behaviour caused by this code is pictured in the attachment.
   // Some initialization code I skipped (loading texts and back, tested to work as intended)
   back.setSize(Vector2f(200.f, names.getLocalBounds().height+caption.getLocalBounds().height));
   back.setPosition(pwindow->mapPixelToCoords(Vector2i((int)(pwindow->getSize().x-back.getSize().x), 0)));
   caption.setPosition(back.getPosition().x+(back.getSize().x-caption.getLocalBounds().width)/2, back.getPosition().y); //this text should start at the top of back
   float y=caption.getPosition().y+caption.getLocalBounds().height;
   names.setPosition(back.getPosition().x+indentH, y); //this should move names directly under caption
   points.setPosition(back.getPosition().x+back.getSize().x-indentH, y); //points has origin in upper right corner, in case you were wondering
 
So, is there something I don't understand about the way Text behaves (particularly setPosition() and getLocalBounds())?
If this makes a difference, I haven't moved the project from SFML 2.1 yet, but according to the changelog, nothing changed in this aspect.
Thank you for your attention, hopefully the code is readable.

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Text position and height
« Reply #1 on: March 19, 2015, 07:39:17 pm »
The problem I see in your picture is that "Score" overlaps with "Player 1". Is that basically the problem you are trying to solve? This is just a guess because I don't have a way to test it right now, but have you checked to see if the "top" field is non-zero when calling getLocalBounds on the text? So maybe you need something like this:
float y=caption.getPosition().y+caption.getLocalBounds().height+caption.getLocalBounds().top;
 

Noctiphobia

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Text position and height
« Reply #2 on: March 19, 2015, 08:10:54 pm »
Thank you, this has fixed my problem. I assumed LocalBounds().top would always be 0, which isn't the case there.