summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/menu/MenuCtrl.java
blob: 960ada3c79698a792f7c8921bf449e23a9e4afd7 (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
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;
import ch.asynk.rustanddust.ui.OkCancel;

public class MenuCtrl implements Disposable, Drawable
{
    enum MenuType
    {
        MAIN(0),
        OPTIONS(1),
        TUTORIALS(2),
        PLAY(3),
        NONE(4),
        OK(64),
        OKKO(65),
        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 String getAsk();
        public void postAnswer(boolean ok);
    }

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

    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.okCancel = new OkCancel(game.font, game.bgPatch, game.getUiRegion(game.UI_OK), game.getUiRegion(game.UI_CANCEL));

        this.current = MenuType.MAIN;

        this.okCancel.visible = false;
        this.visible = true;
    }

    public boolean touch(float x, float y)
    {
        if (okCancel.hit(x, y)) {
            visible = true;
            okCancel.visible = false;
            panels[current.i].postAnswer(okCancel.ok);
            return false;
        }

        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.OK) || (next == MenuType.OKKO)) {
            okCancel.visible = true;
            okCancel.show(panels[current.i].getAsk());
            if (next == MenuType.OK)
                okCancel.noCancel();
            visible = false;
            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();
        okCancel.dispose();
    }

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

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