summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/tankontank/ui/OkCancel.java
blob: 3ed64d66496f79daa7be53a0e1635597c388288b (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
116
117
118
119
120
121
122
package ch.asynk.tankontank.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 Action action;

    public enum Action
    {
        EXIT_BOARD,
        ABORT_TURN,
        END_TURN,
        END_DEPLOYMENT,
    }

    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, Action action)
    {
        show(msg, action, Position.MIDDLE_CENTER);
    }

    public void show(String msg, Action action, Position position)
    {
        this.action = action;

        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);

        okBtn.setPosition((x + width - okBtn.getWidth() - PADDING), (y + PADDING));
        cancelBtn.setPosition((okBtn.getX() - cancelBtn.getWidth() - HSPACING), okBtn.getY());
        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 (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);
    }
}