rafaskb / typing-label

A libGDX Label that appears as if it was being typed in real time.
MIT License
151 stars 20 forks source link

setText method not working as intended #18

Closed Cigeea closed 4 years ago

Cigeea commented 5 years ago

The settext method does not work as intended: after a first call to draw has been done on the label with a given text A of n characters, the setText(Text B) method will only print the first n characters of Text B.

To reproduce this bug easily, I modified the TypingLabelTest class by removing all the text.append calls in the createTypingLabel method. I then added two calls to text.append("12"); and text.append("34");

In the render method, under the stage.draw() call, add if(Gdx.input.isKeyJustPressed(Keys.ENTER)) { // label.restart(); label.setText("This is a dummy text that will be cut"); }

Now if you launch you will see an expected "1234" appearing normally on the screen, but after pressing the enter key, only the "This" will be printed. If you click on restart the whole text appears normally.

My conclusion is that every call to the setText method must be accompanied by a label.restart call.

I don't think this is an intended feature since switching to a regular Label label object doesn't reproduce this.

rafaskb commented 5 years ago

Hi, thanks for reporting the issue!

As the setText docs say, restart() should be used instead, but I understand this doesn't feel like the right behavior. That means we have to change how setText works in a way that doesn't hurt games using the current behavior, and works when label is yet not typed, is being typed, and is completely typed, both for longer and shorter strings.

Here's a few ideas:

  1. Always restart the label once setText is used, as you suggested.
  2. Keep the typing at the same index but fix longer strings not showing up until the end, and if the typing is already finished just skip to the end and show everything.
  3. Always skip to the end when using setText and ask developers to use restart instead, if they want the "typing" effect, but that would break most games using it I think.

I'm not in love with any of them yet so I'll keep thinking about the issue.

rafaskb commented 4 years ago

I ended up taking a hybrid approach here. If setText is called after the char progression is already running, then the label is restarted. However if the label's char progression is already ended (see hasEnded()), then the label skips to the end automatically.