summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/ui/OkCancel.java
blob: c0641028a82c90c07a61a0e04849133f68b99804 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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.TextureAtlas;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

public class OkCancel extends Patch
{
    public static int PADDING = 20;
    public static int VSPACING = 10;
    public static int HSPACING = 10;

    public boolean ok;
    protected Label label;
    protected Bg okBtn;
    protected Bg cancelBtn;

    public OkCancel(BitmapFont font, TextureAtlas atlas)
    {
        super(atlas.createPatch("typewriter"));
        this.label = new Label(font);
        this.okBtn = new Bg(atlas.findRegion("ok"));
        this.cancelBtn = new Bg(atlas.findRegion("cancel"));
        this.visible = false;
    }

    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);
        okBtn.translate(dx, dy);
        cancelBtn.translate(dx, dy);
    }

    public void show(String msg)
    {
        show(msg, Position.MIDDLE_CENTER);
    }

    public void show(String msg, Position position)
    {
        label.write(msg);

        float height = (label.getHeight() + okBtn.getHeight() + (2 * PADDING) + (2 * VSPACING));
        float width = (label.getWidth() + (2 * PADDING));
        float w2 = (okBtn.getWidth() + cancelBtn.getWidth() + (2 * PADDING) + (1 * HSPACING));
        if (w2 > width)
            width = w2;
        float x = position.getX(width);
        float y = position.getY(height);
        setPosition(x, y, width, height);

        setBtnRight(okBtn);
        setBtnLeft(cancelBtn);
        label.setPosition((x + PADDING), (y + PADDING + okBtn.getHeight() + VSPACING));
        cancelBtn.visible = true;
        visible = true;
        ok = false;
    }

    public void noCancel()
    {
        cancelBtn.visible = false;
    }

    @Override
    public boolean hit(float x, float y)
    {
        if (!cancelBtn.visible && super.hit(x, y)) {
            ok = true;
            return true;
        }
        if (okBtn.hit(x, y)) {
            ok = true;
            return true;
        } else if (cancelBtn.hit(x, y)) {
            ok = false;
            return true;
        }
        return false;
    }

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

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

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