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

Author Topic: sf::Text Sub Rectangle  (Read 4319 times)

0 Members and 1 Guest are viewing this topic.

ief015

  • Newbie
  • *
  • Posts: 7
    • View Profile
sf::Text Sub Rectangle
« on: February 08, 2012, 04:33:13 am »
Is there a way to do something similar to sf::Sprite::SetSubRect but for sf::Text?

For example:

I'm making a textbox for my game. There's a problem though;



As you can see, the sf::Text is drawn past the textbox.


I guess this also goes as a request. Any ideas?
Thanks

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
sf::Text Sub Rectangle
« Reply #1 on: February 08, 2012, 05:16:08 am »
First of all, brohoof to my fellow Dashite!

Now, could you possibly post the code for the textbox? I may be able to work out a solution if I see the code.

EDIT: Reread your post. Looking for such a function now.

Second EDIT: No, there does not appear to be, but I do have a solution. Render the text to a render texture which is the size of the textbox. It'll automatically clip the text that goes out of bounds. Then you can render that render texture where the textbox belongs.
I use the latest build of SFML2

ief015

  • Newbie
  • *
  • Posts: 7
    • View Profile
sf::Text Sub Rectangle
« Reply #2 on: February 08, 2012, 05:30:09 am »
Eh. That's what I was thinking of doing in the first place.
I was hoping to find a much more efficient way of doing this.

The drawing code is basically just drawing sf::Text on top of a rectangle. Nothing crazy :P

Perhaps some fancy OpenGL functions would do the trick?

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
sf::Text Sub Rectangle
« Reply #3 on: February 08, 2012, 05:31:39 am »
I'm not too experienced with OpenGL, but I seem to remember some sort of GL Scissors function that might work. But I'm not an expert.
I use the latest build of SFML2

coolhome

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
sf::Text Sub Rectangle
« Reply #4 on: February 10, 2012, 04:26:52 am »
This is what I would do or something similar.

On the update when input is triggered do something along the lines of this

Code: [Select]

//Add the character inputted

//Lets check the size of this thing now
if(someText.GetLocalBounsd().width > inputBox.width)
{
sf::String str = someText.GetString();

str.Erase(str.GetSize() -1, 1); //Remove that character that we just added.

someText.SetString(str);
}
CoderZilla - Everything Programming

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
sf::Text Sub Rectangle
« Reply #5 on: February 10, 2012, 10:32:30 pm »
What version of SFML 2.0 are you using?
Because sf::Sprite::SetSubrect() got replaced by SetTexuterRect() and in SFML 1.6 there is no sf::Text.

I also assume that you're using a class to handle the textbox. What I now would do is storing the real text as a std::string and only assign the text you can see to sf::Text.

You could then check whether the rectagle of sf::Text is smaller as the textbox rect.
Although the scrolling within the box you'd also have to implement if needed. And watch out what should happen if the text height gets bigger than the textbox height.
GUI elements are not always that trivial.  :wink:
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/