summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/menu/MenuCtrl.java
blob: 56015fd921fc9be6c4106b17188d98c3a17f3d2f (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
package ch.asynk.rustanddust.menu;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

import ch.asynk.rustanddust.RustAndDust;
import ch.asynk.rustanddust.engine.gfx.Drawable;

public class MenuCtrl implements Disposable, Drawable
{
    enum MenuType
    {
        MAIN(0),
        OPTIONS(1),
        TUTORIALS(2),
        PLAY(3),
        NONE(4),
        BEGIN(66),
        EXIT(666);
        public int i;
        MenuType(int i) { this.i = i; }
    }

    interface Panel extends Disposable, Drawable
    {
        public MenuType prepare();
        public void computePosition();
        public MenuType touch(float x, float y);
    }

    public boolean visible;
    private Panel []panels;
    private MenuType current;

    public MenuCtrl(final RustAndDust game)
    {
        this.panels = new Panel[MenuType.NONE.i];
        this.panels[MenuType.MAIN.i] = new MainMenu(game);
        this.panels[MenuType.OPTIONS.i] = new OptionsMenu(game);
        this.panels[MenuType.TUTORIALS.i] = new TutorialsMenu(game);
        this.panels[MenuType.PLAY.i] = new PlayMenu(game);

        this.current = MenuType.MAIN;

        this.visible = true;
    }

    public boolean touch(float x, float y)
    {
        MenuType next = panels[current.i].touch(x, y);

        if (next == MenuType.BEGIN) return true;

        if (next == MenuType.EXIT) {
            // TODO clean shutdown
            Gdx.app.exit();
            return false;
        }

        if (next != MenuType.NONE) {
            while(current != next) {
                current = next;
                next = panels[next.i].prepare();
            }
        }

        return false;
    }

    public void computePosition()
    {
        for (int i = 0; i < MenuType.NONE.i; i++)
            this.panels[i].computePosition();
    }

    @Override
    public void dispose()
    {
        for (int i = 0; i < MenuType.NONE.i; i++)
            panels[i].dispose();
    }

    @Override
    public void draw(Batch batch)
    {
        if (visible)
            panels[current.i].draw(batch);
    }

    @Override
    public void drawDebug(ShapeRenderer debugShapes)
    {
        if (visible)
            panels[current.i].drawDebug(debugShapes);
    }
}