summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/ui/OkCancel.java
blob: 8bbceeff84b023091626bbe40806c030e946e48d (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.rustanddust.ui;

import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
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;
    protected Sound snd;

    public OkCancel(BitmapFont font, NinePatch patch, AtlasRegion okRegion, AtlasRegion cancelRegion, Sound snd)
    {
        super(patch);
        this.label = new Label(font);
        this.okBtn = new Bg(okRegion);
        this.cancelBtn = new Bg(cancelRegion);
        this.snd = snd;
        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);

        setBottomRight(okBtn);
        setBottomLeft(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)) {
            snd.play();
            ok = true;
            return true;
        }
        if (okBtn.hit(x, y)) {
            snd.play();
            ok = true;
            return true;
        } else if (cancelBtn.hit(x, y)) {
            snd.play();
            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);
    }
}