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

Inconsistent events ordering #21

Open mgsx-dev opened 3 years ago

mgsx-dev commented 3 years ago

I have several issues using both events and delay (WAIT=X).

With following test code :

long timeBase = System.currentTimeMillis();
TypingLabel tl = new TypingLabel("Hello{WAIT=5}{EVENT=on}{WAIT=5}{EVENT=off}bye", skin);
tl.setTypingListener(new TypingAdapter(){
    @Override
    public void onChar(Character ch) {
        System.out.println((System.currentTimeMillis() - timeBase) + " " + ch);
    }
    @Override
    public void event(String event) {
        System.out.println((System.currentTimeMillis() - timeBase) + " " + event);
    }
});

It produces following trace:

109 e
143 l
176 l
209 on
209 off
209 o
243 b
10343 y
10376 e

As you can see :

idogsayswhat commented 3 years ago

Can confirm that I can reproduce this.

We can look in the code for this snippet here:

            // Notify listener about char progression
            int nextIndex = rawCharIndex == 0 ? 0 : MathUtils.clamp(rawCharIndex, 0, getText().length - 1);
            Character nextChar = nextIndex == 0 ? null : getText().charAt(nextIndex);
            if(nextChar != null && listener != null) {
                listener.onChar(nextChar);
            }

https://github.com/rafaskb/typing-label/blob/381131096cc13f8c4a0514d951e5608f0412d2a4/src/main/java/com/rafaskoberg/gdx/typinglabel/TypingLabel.java#L535-L540

It actually seems 'intended' - the onChar is fired whenever the index is incremented (nextIndex instead of something current). If we assume we start at 0, and only pass in the next character, we are indeed not receiving the event for index 0.

I was able to obtain:

133 H 166 e 198 l 233 on 233 off 233 l 266 o 10366 b 10416 y

With a change to listener.onChar(nextChar);. of: listener.onChar(getText().charAt(rawCharIndex-1));

Also, the last character is missing, due to my change (previously, it was missing the first 😺 )

tommyettinger commented 2 years ago

I believe I have fixed this in TextraTypist, but it was a beast to rewrite the various involved code, and I still am not sure I handled everything 100% correctly in all cases. I remember going back and forth between missing the first character and missing the last character... I also remember effects being especially tricky to line up with where their tags were. My solution involved, to some extent, tracking raw character position in the String with tag markup, and handling priority relative to those positions, while the displayed glyphs were essentially separate.