summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/ui/Menu.java
blob: 0ffc5f10f0a8b929348773c6e2d5d97694bd4a6f (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
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.NinePatch;

public class Menu extends Patch
{
    public static int PADDING = 40;
    public static int VSPACING = 8;

    protected Label []labels;

    public interface MenuItem
    {
        public int last();
        public int i();
        public String s();
        public MenuItem get(int i);
    };

    protected MenuItem menuItem;

    public Menu(MenuItem menuItem, BitmapFont font, NinePatch ninePatch)
    {
        super(ninePatch);
        this.menuItem = menuItem;
        this.labels = new Label[menuItem.last()];
        for (int i = 0; i< menuItem.last(); i ++) {
            labels[i] = new Label(font, 10);
            labels[i].write(menuItem.get(i).s());
        }
    }

    protected Label label(MenuItem m)
    {
        return labels[m.i()];
    }

    protected float widestLabel()
    {
        float w = 0f;
        for (int i = 0; i< menuItem.last(); i ++) {
            float t = labels[i].getWidth();
            if (t> w)
                w = t;
        }
        return w;
    }

    protected float highestLabel()
    {
        float h = 0f;
        for (int i = 0; i< menuItem.last(); i ++) {
            float t = labels[i].getHeight();
            if (t> h)
                h = t;
        }
        return h;
    }

    public void setPosition()
    {
        float lh = highestLabel();
        float h = ((menuItem.last() * lh) + (2 * PADDING) + ((menuItem.last() - 1) * VSPACING));
        float w = (widestLabel() + (2 * PADDING));
        float x = position.getX(w);
        float y = position.getY(h);
        setPosition(x, y, w, h);

        y += PADDING;
        x += PADDING;
        float dy = (VSPACING + lh);

        for (int i = 0; i< menuItem.last(); i ++) {
            labels[i].setPosition(x, y);
            y += dy;
        }
    }

    @Override
    public void dispose()
    {
        super.dispose();
        for (int i = 0; i < menuItem.last(); i ++)
            labels[i].dispose();
    }

    @Override
    public boolean hit(float x, float y)
    {
        for (int i = 0; i< menuItem.last(); i ++) {
            if (labels[i].hit(x, y)) {
                menuItem = menuItem.get(i);
                return true;
            }
        }
        return false;
    }

    @Override
    public void draw(Batch batch)
    {
        if (!visible) return;
        super.draw(batch);
        for (int i = 0; i < menuItem.last(); i ++)
            labels[i].draw(batch);
    }
}