summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/ui/Msg.java
blob: 5e5f4b6a66e16b8c5a6b40066af1fb621837f10c (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
72
73
74
75
76
77
78
79
80
81
package ch.asynk.rustanddust.ui;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

public class Msg extends Patch
{
    private LabelStack label;

    public Msg(BitmapFont font, NinePatch patch, float fontSize)
    {
        super(patch);
        label = new LabelStack(font, fontSize);
    }

    @Override
    public void dispose()
    {
        super.dispose();
        label.dispose();
    }

    public void updatePosition()
    {
        if (!visible) return;
        float dx = (position.getX(rect.width) - rect.x);
        float dy = (position.getY(rect.height) - rect.y);
        translate(dx, dy);
        label.translate(dx, dy);
    }

    public void write(String text, float duration)
    {
        label.write(text, duration);
        resize();
    }

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

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

    private void resize()
    {
        setPosition(label.getX(), label.getY(), label.getWidth(), label.getHeight());
    }

    public boolean animate(float delta)
    {
        if (label.animate(delta))
            resize();
        return true;
    }

    @Override
    public void draw(Batch batch)
    {
        if (!label.visible) return;
        super.draw(batch);
        label.draw(batch);
    }

    @Override
    public void drawDebug(ShapeRenderer shapes)
    {
        if (!label.visible) return;
        super.drawDebug(shapes);
        label.drawDebug(shapes);
    }
}