summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/ui/Label.java
blob: 1bff1baf3cdfc0d443967531bcabb5bb69c38e21 (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
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.GlyphLayout;

public class Label extends Widget
{
    private BitmapFont font;
    private GlyphLayout layout;
    private float dx;
    private float dy;
    protected String text;

    public Label(BitmapFont font)
    {
        this(font, 0f);
    }

    public Label(BitmapFont font, float padding)
    {
        this(font, padding, Position.MIDDLE_CENTER);
    }

    public Label(BitmapFont font, float padding, Position position)
    {
        super();
        this.font = font;
        this.padding = padding;
        this.position = position;
        this.layout = new GlyphLayout();
    }

    @Override
    public void dispose()
    {
    }

    @Override
    public void translate(float dx, float dy)
    {
        setPosition((rect.x + dx), (rect.y + dy));
    }

    @Override
    public void setPosition(float x, float y)
    {
        this.layout.setText(font, (text == null) ? "" : text);
        setPosition(x, y, (layout.width + (2 * padding)), (layout.height + (2 * padding)));
        this.dx = (x + padding);
        this.dy = (y + padding + layout.height);
    }

    public void write(String text)
    {
        this.text = text;
        compute();
        setPosition(position);
    }

    public void write(String text, float x, float y)
    {
        this.text = text;
        setPosition(x, y);
    }

    private void compute()
    {
        this.layout.setText(font, (text == null) ? "" : text);
        this.rect.width = (layout.width + (2 * padding));
        this.rect.height = (layout.height + (2 * padding));
    }

    @Override
    public void draw(Batch batch)
    {
        if (!visible) return;
        font.draw(batch, layout, dx, dy);
    }
}