summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/ui/LabelStack.java
blob: 9ab0768563dbbbb4c8c0399e5c6f262f16ff4618 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package ch.asynk.rustanddust.ui;

import com.badlogic.gdx.graphics.g2d.BitmapFont;

import ch.asynk.rustanddust.engine.util.IterableQueue;
import ch.asynk.rustanddust.engine.gfx.Animation;

public class LabelStack extends Label implements Animation
{
    class MsgInfo
    {
        String text;
        float duration;
        Position position;
        MsgInfo(String text, float duration, Position position)
        {
            this.text = text;
            this.duration = duration;
            this.position = position;
        }
    }

    private float duration;
    private float elapsed;
    private IterableQueue<MsgInfo> queue;

    public LabelStack(BitmapFont font, float padding)
    {
        super(font, padding);
        this.visible = false;
        this.queue = new IterableQueue<MsgInfo>(3);
    }

    public void pushWrite(String text, float duration, Position position)
    {
        if (visible)
            queue.enqueue(new MsgInfo(text, duration, position));
        else
            write(text, duration, position);
    }

    public void write(String text, float duration, Position position)
    {
        this.position = position;
        write(text, duration);
    }

    public void write(String text, float duration)
    {
        this.duration = duration;
        this.visible = true;
        this.elapsed = 0f;
        write(text);
    }

    @Override
    public boolean animate(float delta)
    {
        if (!visible) return false;
        elapsed += delta;
        if (elapsed >= duration) {
           visible = false;
           if (queue.size() > 0) {
               MsgInfo info = queue.dequeue();
               write(info.text, info.duration, info.position);
               return true;
           }
        }
        return false;
    }
}