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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
package ch.asynk.rustanddust.menu;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Batch;
import ch.asynk.rustanddust.ui.Bg;
import ch.asynk.rustanddust.ui.Label;
import ch.asynk.rustanddust.ui.Button;
import ch.asynk.rustanddust.ui.List;
import ch.asynk.rustanddust.ui.Patch;
import ch.asynk.rustanddust.ui.Scrollable;
import ch.asynk.rustanddust.ui.Position;
import ch.asynk.rustanddust.RustAndDust;
import ch.asynk.rustanddust.util.GameRecord;
public class PlayMenu extends Patch implements MenuCtrl.Panel
{
public static int PADDING = 60;
public static int TITLE_PADDING = 30;
private final RustAndDust game;
private Label title;
private Label gameMode;
private Label gameModeValue;
private float gameModeWidth;
private boolean notImplemented;
private Scrollable list;
protected Bg cancelBtn;
protected Button newBtn;
protected Button playBtn;
protected Button deleteBtn;
public PlayMenu(RustAndDust game)
{
super(game.bgPatch);
this.game = game;
this.gameMode = new Label(game.font);
this.gameMode.write("Game mode : ");
this.gameModeValue = new Label(game.font, 0f, Position.ABSOLUTE);
this.cancelBtn = new Bg(game.getUiRegion(game.UI_CANCEL));
this.newBtn = new Button("New", game.font, game.bgPatch, 20f);
this.playBtn = new Button("Play", game.font, game.bgPatch, 20f);
this.deleteBtn = new Button("Delete", game.font, game.bgPatch, 20f);
this.title = new Label(game.font);
this.title.write("- Play");
this.list = new Scrollable(new List(game, 10f), game.framePatch);
this.padding = PADDING;
this.gameModeValue.write(game.config.gameMode.getLongest().s);
this.gameModeWidth = gameModeValue.getWidth();
}
private List getList()
{
return (List) this.list.getChild();
}
@Override
public MenuCtrl.MenuType postAnswer(boolean ok)
{
if (!notImplemented && ok) {
GameRecord r = GameRecord.remove(getList().getIdx());
game.db.deleteGame(r.id);
r.dispose();
getList().unselect();
showBtns(false);
}
return MenuCtrl.MenuType.NONE;
}
@Override
public String getAsk()
{
if (notImplemented)
return String.format("'%s' Game Mode not implemented yet.", game.config.gameMode.s);
else
return "Permanently delete this game ?";
}
@Override
public MenuCtrl.MenuType prepare()
{
loadGames();
computePosition();
return MenuCtrl.MenuType.PLAY;
}
@Override
public void computePosition()
{
float h = (title.getHeight() + TITLE_PADDING);
h += list.getBestHeight();
h += (2 * padding);
h = Math.min(h, Gdx.graphics.getHeight() - 60);
float w = title.getWidth() + PADDING + gameModeWidth + 5 + gameMode.getWidth();
if (list.getWidth() > w) w = list.getWidth();
if (list.getBestWidth() > w) w = list.getBestWidth();
w += (2 * padding);
w = Math.min(w, Gdx.graphics.getWidth() - 60);
float x = position.getX(w);
float y = position.getY(h);
setPosition(x, y, w, h);
setBottomLeft(cancelBtn);
setBottomRight(newBtn);
playBtn.setPosition(newBtn.getX() - playBtn.getWidth() - 5, newBtn.getY());
deleteBtn.setPosition(playBtn.getX() - deleteBtn.getWidth() - 5, newBtn.getY());
showBtns(false);
y += padding;
x += padding;
list.setPosition(x, y, (getWidth() - 2 * padding), (getHeight() - 2 * padding - TITLE_PADDING - title.getHeight()));
y += list.getHeight() + TITLE_PADDING;
title.setPosition(x, y);
this.gameModeValue.write(game.config.gameMode.s);
gameModeValue.setPosition(getX() + w - PADDING - gameModeWidth, y);
gameMode.setPosition(gameModeValue.getX() - 5 - gameMode.getWidth(), y);
}
@Override
public boolean drag(float x, float y, int dx, int dy)
{
if (!list.hit(x, y)) return false;
return list.drag(x, y, dx, dy);
}
@Override
public MenuCtrl.MenuType touch(float x, float y)
{
Integer i = getList().getIdx();
if (gameMode.hit(x, y) || gameModeValue.hit(x, y)) {
game.playType();
cycleGameMode();
} else if (newBtn.hit(x, y)) {
game.playEnter();
return tryNew();
} else if (cancelBtn.hit(x, y)) {
game.playType();
return MenuCtrl.MenuType.MAIN;
} else if (newBtn.hit(x, y)) {
game.playEnter();
return MenuCtrl.MenuType.NEW_GAME;
} else if (deleteBtn.hit(x, y)) {
game.playType();
return MenuCtrl.MenuType.OKKO;
} else if (playBtn.hit(x, y)) {
setConfig();
return MenuCtrl.MenuType.RESUME;
} else if (list.hit(x, y)) {
if (i != getList().getIdx()) {
game.playType();
showBtns(getList().getIdx() != null);
}
// show buttons only if can play
// if (i != getList().getIdx()) {
// game.playType();
// GameRecord r = (GameRecord) getList().getSelected();
// showBtns((r != null) && (r.canPlay()));
// }
return MenuCtrl.MenuType.NONE;
}
return MenuCtrl.MenuType.NONE;
}
private void loadGames()
{
game.db.loadGames(game.config.gameMode.i);
game.config.gameId = game.db.NO_RECORD;
getList().setItems(4, GameRecord.list);
}
private void cycleGameMode()
{
game.config.gameMode = game.config.gameMode.next();
float fx = gameModeValue.getX();
float fy = gameModeValue.getY();
gameModeValue.write(game.config.gameMode.s);
gameModeValue.setPosition(fx, fy);
showBtns(false);
loadGames();
}
private void setConfig()
{
GameRecord g = GameRecord.get(getList().getIdx());
game.config.gameId = g.id;
game.config.battle = game.factory.getBattle(g.battle);
}
private MenuCtrl.MenuType tryNew()
{
notImplemented = !game.config.gameModeImplemented();
if (!notImplemented)
return MenuCtrl.MenuType.NEW_GAME;
return MenuCtrl.MenuType.OK;
}
private void showBtns(boolean show)
{
deleteBtn.visible = show;
playBtn.visible = show;
}
@Override
public void dispose()
{
super.dispose();
list.dispose();
title.dispose();
gameMode.dispose();
gameModeValue.dispose();
newBtn.dispose();
playBtn.dispose();
deleteBtn.dispose();
cancelBtn.dispose();
GameRecord.clearList();
}
@Override
public void draw(Batch batch)
{
super.draw(batch);
list.draw(batch);
title.draw(batch);
gameMode.draw(batch);
gameModeValue.draw(batch);
newBtn.draw(batch);
playBtn.draw(batch);
deleteBtn.draw(batch);
cancelBtn.draw(batch);
}
}
|