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

Author Topic: [SOLVED] Text Input Field - Ideas for wrapping  (Read 336 times)

0 Members and 1 Guest are viewing this topic.

Athenian Hoplite

  • Newbie
  • *
  • Posts: 19
  • Nenikikamen !
    • View Profile
[SOLVED] Text Input Field - Ideas for wrapping
« on: May 19, 2020, 03:19:11 pm »
Hi there,

First of all big praise to the dev team for this awesome library !

I am developing a game engine of sorts on top of SFML. With that comes the development of a UI system for it. I have already set up a sort of UI event system for dealing with events and distributing them to subscribers.

In implementing a text input field class I come to wonder how to deal with text wrapping on the end of the input field. Most fields (in most UIs I've used) usually simply continue to draw to the right as more characters are inserted and characters on the left are "lost" off the view.

I have already implemented this behaviour by using a RenderTexture to draw the sf::Text to and then drawing the RenderTexture to a final sprite. When input wraps I simply push the sprites' texture rect to the right (by adding on X) thus revealing the new text and stop rendering text on the left end.

This is all good but this forces me to create a rather big render texture so that I can continue to write on it. Lets say I have a 300x30 text field, the texture I use has to be at least 900x30 so that I can allow the user to input at least as much text as 3 times the text field size (for an example). This seems wasteful of memory (and texture binding/unbinding cost) ...

I tried simply erasing characters on the left of the text as the user reaches the limit on the right but as the characters have different sizes this sometimes results in a dance where 1 character removed on the left leaves space for 2 new ones on the right (which is annoying).

Any ideas ?

Thanks in advance
« Last Edit: May 19, 2020, 03:51:12 pm by Athenian Hoplite »
"By will of the Athenian People be it resolved:
If anyone rises up against the people for tyranny or join in establishing the tyranny or overthrow the People of the Athenians and the democracy in Athens, whoever kills him who does these things shall be blameless." - Athenian Law 337 BC

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 9507
    • View Profile
    • development blog
    • Email
Re: Text Input Field - Ideas for wrapping
« Reply #1 on: May 19, 2020, 03:32:17 pm »
Your RenderTexture only needs to be as big as your input field.
You can just move the sf::Text object on the render texture to the left and display whatever remains on the render texture as your input field.
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/

Athenian Hoplite

  • Newbie
  • *
  • Posts: 19
  • Nenikikamen !
    • View Profile
Re: Text Input Field - Ideas for wrapping
« Reply #2 on: May 19, 2020, 03:49:34 pm »
Ahah ! What a dummie I was ! Thanks man, that's exactly it !
Cheers !
"By will of the Athenian People be it resolved:
If anyone rises up against the people for tyranny or join in establishing the tyranny or overthrow the People of the Athenians and the democracy in Athens, whoever kills him who does these things shall be blameless." - Athenian Law 337 BC

 

anything