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

Author Topic: Designing Button  (Read 1230 times)

0 Members and 2 Guests are viewing this topic.

master_clown

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Designing Button
« on: January 01, 2017, 04:24:54 am »
I wrote a Button class, which contains its outer form — rectangle (RectangleShape)—  and text (sf::Text) inside it. I've faced with problem with managing text position and size inside the rect. I want the text to be centered inside the rect, and as long as there is possibility for moving and resizing a button, it turns complicated to find out how to move and resize text along with the button.

What can you offer? I suppose there are lots of games which use GUI, so there are checked solutions.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Designing Button
« Reply #1 on: January 02, 2017, 12:01:54 am »
When positioning text, you must take into account that the origin isn't automatically where you might think it is. Retrieve the local bounding rectangle and use the left and right members to find the top-left corner and the width and height members to offset from the top-left corner to the centre by setting the text's origin.

Since you now have the width and height of the text, you can set the text's scale to match the size of the area you wish it to fill. It's probably better to start with a larger text and scale down than with a smaller text and scale up.

You can change the character size of the text too but note that this might be slow so may be better to only do this when resizing is complete. You probably still might want to scale to fit exactly, if required.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything