summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/game/hud/ActionButtons.java
blob: e992867188212a7a70d216f513210ab6743f6513 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package ch.asynk.rustanddust.game.hud;

import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

import ch.asynk.rustanddust.RustAndDust;
import ch.asynk.rustanddust.game.Ctrl;
import ch.asynk.rustanddust.game.Ctrl.MsgType;
import ch.asynk.rustanddust.ui.Widget;
import ch.asynk.rustanddust.ui.Bg;
import ch.asynk.rustanddust.ui.Position;

public class ActionButtons extends Widget
{
    public static int PADDING = 5;

    private final Ctrl ctrl;

    public enum Buttons {
        NONE(-1, 0),
        PROMOTE(0, 1),
        DONE(1, 2),
        ABORT(2, 4),
        LAST(3, 0);

        Buttons(int i, int b)
        {
            this.i = i;
            this.b = b;
        }

        public int i;
        public int b;
    }

    private Sprite bg;
    private Bg buttons [];
    private MsgType msgs [];

    public ActionButtons(RustAndDust game)
    {
        this.bg = new Sprite(game.factory.getHudRegion(game.factory.DISABLED));
        this.ctrl = game.ctrl;
        this.visible = false;
        this.position = Position.BOTTOM_RIGHT;

        this.buttons = new Bg[Buttons.LAST.i];
        this.buttons[Buttons.DONE.i] = new Bg(game.factory.getHudRegion(game.factory.ACT_DONE));
        this.buttons[Buttons.ABORT.i] = new Bg(game.factory.getHudRegion(game.factory.ACT_ABORT));
        this.buttons[Buttons.PROMOTE.i] = new Bg(game.factory.getHudRegion(game.factory.ACT_PROMOTE));

        this.msgs = new MsgType[Buttons.LAST.i];
        this.msgs[Buttons.DONE.i] = MsgType.OK;
        this.msgs[Buttons.ABORT.i] = MsgType.CANCEL;
        this.msgs[Buttons.PROMOTE.i] = MsgType.PROMOTE;
    }

    @Override
    public void dispose()
    {
        for (int i = 0; i < Buttons.LAST.i; i++)
            buttons[i].dispose();
    }

    public void update(Position position)
    {
        setPosition(position);
        updatePosition();
    }

    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);
        for (int i = 0; i < Buttons.LAST.i; i++)
            buttons[i].translate(dx, dy);
    }

    public void hide()
    {
        for (int i = 0; i < Buttons.LAST.i; i++)
            buttons[i].visible = false;
        this.visible = false;
    }

    private float setButton(Bg btn, float x, float y)
    {
        btn.visible = true;
        btn.setPosition(x, y);
        return (y + btn.getHeight() + PADDING);
    }

    public void show(int bits)
    {
        int b = bits;
        int count = 0;
        while (b > 0) {
            if ((b & 0x01) == 1)
                count += 1;
            b /= 2;
        }

        if (count == 0) {
            this.visible = false;
            return;
        }

        rect.width = (buttons[0].getWidth() + (2 * PADDING));
        rect.height = ((buttons[0].getHeight() * count) + ((count + 1) * PADDING));
        rect.x =  position.getX(rect.width);
        rect.y =  position.getY(rect.height);

        float x = (rect.x + PADDING);
        float y = (rect.y + PADDING);

        b = 1;
        for (int i = 0; i < Buttons.LAST.i; i++) {
            if ((bits & b) == b)
                y = setButton(buttons[i], x, y);
            else
                buttons[i].visible = false;
            b *= 2;
        }

        this.visible = true;
    }

    public boolean hit(float x, float y)
    {
        if (!super.hit(x,y))
            return false;

        for (int i = 0; i < Buttons.LAST.i; i++) {
            if (buttons[i].hit(x, y)) {
                ctrl.sendMsg(msgs[i]);
                return true;
            }
        }

        return false;
    }

    @Override
    public void draw(Batch batch)
    {
        if (!visible) return;
        batch.draw(bg, rect.x, rect.y, rect.width, rect.height);
        for (int i = 0; i < Buttons.LAST.i; i++)
            buttons[i].draw(batch);
    }

    @Override
    public void drawDebug(ShapeRenderer shapes)
    {
        if (!visible) return;
        super.drawDebug(shapes);
        for (int i = 0; i < Buttons.LAST.i; i++)
            buttons[i].drawDebug(shapes);
    }
}