summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/gdx/boardgame/ui/Button.java
blob: dc7e6240d32a21f14452db44c85d3c8b7601bf69 (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
package ch.asynk.gdx.boardgame.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;
import com.badlogic.gdx.math.Rectangle;

public class Button extends Patch
{
    protected Label label;
    protected float spacing;    // label is aligned within spacing frame;

    public Button(BitmapFont font, NinePatch patch)
    {
        this(font, patch, 0);
    }

    public Button(BitmapFont font, NinePatch patch, float padding)
    {
        this(font, patch, padding, 0);
    }

    public Button(BitmapFont font, NinePatch patch, float padding, float spacing)
    {
        super(patch);
        this.padding = padding;
        this.spacing = spacing;
        this.label = new Label(font);
        this.label.setParent(this, Alignment.MIDDLE_CENTER);
    }

    public void write(String text)
    {
        label.write(text);
        taint();
    }

    public void setLabelAlignment(Alignment alignment)
    {
        label.setAlignment(alignment);
    }

    @Override public void computeDimensions()
    {
        label.computeDimensions();
        float dd = 2 * (padding + spacing);
        rect.width = label.getWidth() + dd;
        rect.height = label.getHeight() + dd;
    }

    @Override public void computeGeometry(Rectangle area, boolean resized)
    {
        super.computeGeometry(area, resized);
        label.computeGeometry(innerRect, resized);
    }

    @Override public void drawReal(Batch batch)
    {
        super.drawReal(batch);
        label.draw(batch);
    }

    @Override public void drawDebug(ShapeRenderer shapeRenderer)
    {
        super.drawDebug(shapeRenderer);
        label.drawDebug(shapeRenderer);
    }
}