summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/gdx/boardgame/ui/Menu.java
blob: b7540839ad616fe9442eeb057e953bc942381193 (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
package ch.asynk.gdx.boardgame.ui;

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.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;

public class Menu extends Patch
{
    private Label title;
    private Label[] entries;
    private int touchedItem;

    private int entriesOffset;      // horizontal offset
    private int titleSpacing;       // between title and entries
    private int entriesSpacing;     // between entries

    public Menu(BitmapFont font, NinePatch patch, String title, String[] entries)
    {
        super(patch);
        this.touchedItem = -1;
        setTitle(font, title);
        setEntries(font, entries);
    }

    public void setTitle(BitmapFont font, String title)
    {
        this.title = new Label(font);
        this.title.write(title);
        this.title.setParent(this);
        taint();
    }

    public void setEntries(BitmapFont font, String[] entries)
    {
        this.entries = new Label[entries.length];
        for (int i = 0; i < entries.length; i++) {
            Label l = new Label(font);
            l.write(entries[i]);
            l.setParent(this);
            this.entries[i] = l;
        }
        taint();
    }

    public void setLabelsOffset(int entriesOffset)
    {
        this.entriesOffset = entriesOffset;
        taint();
    }

    public void setPaddings(int titlePadding, int labelPadding)
    {
        this.title.setPadding(titlePadding);
        for (Label label : entries) {
            label.setPadding(labelPadding);
        }
        taint();
    }

    public void setSpacings(int titleSpacing, int entriesSpacing)
    {
        this.titleSpacing = titleSpacing;
        this.entriesSpacing = entriesSpacing;
        taint();
    }

    @Override public void computeGeometry(Rectangle area, boolean resized)
    {
        title.computeGeometry(area, resized);
        float h = title.getHeight();
        float w = title.getWidth();
        for (Label label : entries) {
            label.computeGeometry(area, resized);
            h += label.getHeight();
            float t = label.getWidth() + entriesOffset;
            if (t > w)
                w = t;
        }
        rect.width = w + (2 * padding);
        rect.height = h + (titleSpacing + (entriesSpacing * (entries.length - 1)) + (2 * padding));

        super.computeGeometry(area, resized);

        float y = getInnerHeight() - title.getHeight();
        title.setPositionClear(0, y, innerRect);
        y -= titleSpacing;
        for (Label l : entries) {
            l.setPositionClear(x + entriesOffset, y - l.getHeight(), innerRect);
            y -= (l.getHeight() + entriesSpacing);
        }
    }

    public int touched()
    {
        return touchedItem;
    }

    @Override public Element touch(float x, float y)
    {
        touchedItem = -1;
        if (super.touch(x, y) != null) {
            for (int i = 0; i < entries.length; i++) {
                final Element touched = entries[i].touch(x, y);
                if (touched != null) {
                    touchedItem = i;
                    return touched;
                }
            }
        }
        return null;
    }

    @Override public void drawReal(Batch batch)
    {
        super.drawReal(batch);
        title.draw(batch);
        for (Label label : entries) {
            label.draw(batch);
        }
    }

    @Override public void drawDebug(ShapeRenderer shapeRenderer)
    {
        super.drawDebug(shapeRenderer);
        title.drawDebug(shapeRenderer);
        for (Label label : entries) {
            label.drawDebug(shapeRenderer);
        }
    }
}