1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.vectomatic.svg.edu.client.menu;
19
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.vectomatic.dom.svg.OMSVGTSpanElement;
27 import org.vectomatic.dom.svg.OMText;
28 import org.vectomatic.dom.svg.ui.SVGButtonBase.SVGFace;
29 import org.vectomatic.dom.svg.ui.SVGButtonBase.SVGFaceName;
30 import org.vectomatic.dom.svg.ui.SVGButtonBase.SVGStyleChange;
31 import org.vectomatic.dom.svg.ui.SVGImage;
32 import org.vectomatic.dom.svg.ui.SVGPushButton;
33 import org.vectomatic.dom.svg.ui.SVGResource;
34 import org.vectomatic.dom.svg.utils.DOMHelper;
35 import org.vectomatic.dom.svg.utils.SVGPrefixResolver;
36 import org.vectomatic.svg.edu.client.commons.CommonBundle;
37 import org.vectomatic.svg.edu.client.commons.CommonConstants;
38 import org.vectomatic.svg.edu.client.commons.ConfirmBox;
39 import org.vectomatic.svg.edu.client.commons.LicenseBox;
40 import org.vectomatic.svg.edu.client.commons.Utils;
41 import org.vectomatic.svg.edu.client.dots.DotsMain;
42 import org.vectomatic.svg.edu.client.maze.MazeMain;
43 import org.vectomatic.svg.edu.client.push.PushMain;
44 import org.vectomatic.svg.edu.client.puzzle.PuzzleMain;
45
46 import com.google.gwt.core.client.EntryPoint;
47 import com.google.gwt.core.client.GWT;
48 import com.google.gwt.core.client.RunAsyncCallback;
49 import com.google.gwt.core.client.Scheduler;
50 import com.google.gwt.dom.client.Style;
51 import com.google.gwt.event.dom.client.ClickEvent;
52 import com.google.gwt.event.dom.client.ClickHandler;
53 import com.google.gwt.user.client.Command;
54 import com.google.gwt.user.client.Window;
55 import com.google.gwt.user.client.ui.Anchor;
56 import com.google.gwt.user.client.ui.DialogBox;
57 import com.google.gwt.user.client.ui.FlowPanel;
58 import com.google.gwt.user.client.ui.Label;
59 import com.google.gwt.user.client.ui.RootPanel;
60
61
62
63
64
65 public class Menu implements EntryPoint {
66
67
68
69
70 private static abstract class GameCallback implements RunAsyncCallback {
71 public void onFailure(Throwable reason) {
72 if (Utils.isDebug()) {
73 Utils.handleFatalError(reason);
74 } else {
75 Window.alert(CommonConstants.INSTANCE.loadError());
76 }
77 }
78 public abstract void onSuccess();
79 }
80
81
82
83
84
85 private static class GamePrefixResolver extends SVGPrefixResolver {
86 public GamePrefixResolver() {
87 prefixToUri.put("v", VECTOMATIC_NS_URI);
88 }
89 }
90 private static final String VECTOMATIC_NS_URI = "http://www.vectomatic.org";
91 private static GamePrefixResolver resolver = new GamePrefixResolver();
92 private static MenuBundle bundle = MenuBundle.INSTANCE;
93 private static MenuCss css = bundle.css();
94 private static MenuConstants menuConstants = MenuConstants.INSTANCE;
95
96
97
98 private static DialogBox confirmBox;
99
100 private static final int COL_COUNT = 2;
101 private static final float WIDTH = 100f;
102 private static final float HEIGHT = 100f;
103
104
105
106
107 enum Game {
108 DOTS {
109 @Override
110 protected RunAsyncCallback getCallback() {
111 return new GameCallback() {
112 public void onSuccess() {
113 RootPanel root = RootPanel.get(CommonConstants.ID_UIROOT);
114 root.remove(root.getWidget(0));
115 DotsMain main = new DotsMain(Game.createHomeButton());
116 main.onModuleLoad();
117 }
118 };
119 }
120 @Override
121 protected SVGImage getImage() {
122 return createImage(
123 bundle.connectdots(),
124 menuConstants.connectDotsTitle(),
125 getCallback());
126 }
127 @Override
128 protected Label getRule() {
129 return new Label(menuConstants.connectDotsRule());
130 }
131 },
132 MAZE {
133 @Override
134 protected RunAsyncCallback getCallback() {
135 return new GameCallback() {
136 public void onSuccess() {
137 RootPanel root = RootPanel.get(CommonConstants.ID_UIROOT);
138 root.remove(root.getWidget(0));
139 MazeMain main = new MazeMain(Game.createHomeButton());
140 main.onModuleLoad();
141 }
142 };
143 }
144 @Override
145 protected SVGImage getImage() {
146 return createImage(
147 bundle.maze(),
148 menuConstants.mazeTitle(),
149 getCallback());
150 }
151 @Override
152 protected Label getRule() {
153 return new Label(menuConstants.mazeRule());
154 }
155 },
156 PUSH {
157 @Override
158 protected RunAsyncCallback getCallback() {
159 return new GameCallback() {
160 public void onSuccess() {
161 RootPanel root = RootPanel.get(CommonConstants.ID_UIROOT);
162 root.remove(root.getWidget(0));
163 PushMain main = new PushMain(Game.createHomeButton());
164 main.onModuleLoad();
165 }
166 };
167 }
168 @Override
169 protected SVGImage getImage() {
170 return createImage(
171 bundle.push(),
172 menuConstants.pushTitle(),
173 getCallback());
174 }
175 @Override
176 protected Label getRule() {
177 return new Label(menuConstants.pushRule());
178 }
179 },
180 PUZZLE {
181 @Override
182 protected RunAsyncCallback getCallback() {
183 return new GameCallback() {
184 public void onSuccess() {
185 RootPanel root = RootPanel.get(CommonConstants.ID_UIROOT);
186 root.remove(root.getWidget(0));
187 PuzzleMain main = new PuzzleMain(Game.createHomeButton());
188 main.onModuleLoad();
189 }
190 };
191 }
192 @Override
193 protected SVGImage getImage() {
194 return createImage(
195 bundle.puzzle(),
196 menuConstants.puzzleTitle(),
197 getCallback());
198 }
199 @Override
200 protected Label getRule() {
201 return new Label(menuConstants.puzzleRule());
202 }
203 };
204 protected abstract SVGImage getImage();
205 protected abstract Label getRule();
206 protected abstract RunAsyncCallback getCallback();
207
208 public FlowPanel getPanel() {
209 FlowPanel panel = new FlowPanel();
210 FlowPanel imagePanel = new FlowPanel();
211 imagePanel.add(getImage());
212 imagePanel.setStyleName(css.gameLogo());
213 panel.add(imagePanel);
214 Label rule = getRule();
215 rule.setStyleName(css.gameRule());
216 panel.add(rule);
217 return panel;
218 }
219
220 private static SVGImage createImage(
221 SVGResource gameLogo,
222 String gameTitle,
223 final RunAsyncCallback callback) {
224 final SVGImage svgImage = new SVGImage(gameLogo);
225 svgImage.addClickHandler(new ClickHandler() {
226
227 @Override
228 public void onClick(ClickEvent event) {
229 GWT.runAsync(callback);
230 }
231
232 });
233
234 List<OMSVGTSpanElement> tspans = new ArrayList<OMSVGTSpanElement>();
235 Iterator<OMSVGTSpanElement> iterator = DOMHelper.evaluateXPath(svgImage.getSvgElement(), ".//svg:tspan[@v:title]", resolver);
236 while(iterator.hasNext()) {
237
238 tspans.add(iterator.next());
239 }
240 for (OMSVGTSpanElement tspan : tspans) {
241 tspan.appendChild(new OMText(gameTitle));
242 }
243 return svgImage;
244 }
245
246 private static SVGPushButton createHomeButton() {
247 Map<SVGFaceName, SVGFace> faces = new HashMap<SVGFaceName, SVGFace>();
248 faces.put(SVGFaceName.UP, new SVGFace(new SVGStyleChange[] { new SVGStyleChange( new String[] { CommonBundle.INSTANCE.css().navigationUp()} )}));
249 faces.put(SVGFaceName.UP_HOVERING, new SVGFace(new SVGStyleChange[] { new SVGStyleChange( new String[] { CommonBundle.INSTANCE.css().navigationUpHovering()} )}));
250 SVGPushButton homeButton = new SVGPushButton(bundle.home().getSvg(), faces);
251 homeButton.setClassNameBaseVal(CommonBundle.INSTANCE.mediaQueries().navigationPanelMenuButton());
252 homeButton.addClickHandler(new ClickHandler() {
253 @Override
254 public void onClick(ClickEvent event) {
255 confirmBox.center();
256 confirmBox.show();
257 }
258 });
259 return homeButton;
260 }
261 };
262
263 @Override
264 public void onModuleLoad() {
265 GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {
266 public void onUncaughtException(Throwable throwable) {
267 Utils.handleFatalError(throwable);
268 }
269 });
270
271
272
273 Scheduler.get().scheduleDeferred(new Command() {
274 @Override
275 public void execute() {
276 onModuleLoad2();
277 }
278 });
279 }
280
281
282 public void onModuleLoad2() {
283 MenuBundle.INSTANCE.css().ensureInjected();
284
285 FlowPanel gamePanel = new FlowPanel();
286 FlowPanel menuPanel = new FlowPanel();
287 menuPanel.setStyleName(css.gamePanel());
288 Game[] games = Game.values();
289 int rowCount = (games.length + 1) / 2;
290 float rowHeight = HEIGHT / rowCount;
291 float colWidth = WIDTH / COL_COUNT;
292 for (int i = 0; i < games.length; i++) {
293 int row = i / 2;
294 int col = i % 2;
295 float left = col * colWidth;
296 float top = row * rowHeight;
297 FlowPanel panel = games[i].getPanel();
298 panel.getElement().getStyle().setPosition(Style.Position.ABSOLUTE);
299 panel.getElement().getStyle().setLeft(left, Style.Unit.PCT);
300 panel.getElement().getStyle().setTop(top, Style.Unit.PCT);
301 panel.getElement().getStyle().setWidth(colWidth, Style.Unit.PCT);
302 panel.getElement().getStyle().setHeight(rowHeight, Style.Unit.PCT);
303 menuPanel.add(panel);
304 }
305
306
307 final LicenseBox licenseBox = new LicenseBox();
308 Anchor licenseAnchor = new Anchor();
309 licenseAnchor.setStyleName(css.anchor());
310 licenseAnchor.setText(CommonConstants.INSTANCE.license());
311 licenseAnchor.addClickHandler(new ClickHandler() {
312
313 @Override
314 public void onClick(ClickEvent event) {
315 licenseBox.getBox().center();
316 licenseBox.getBox().show();
317 }
318
319 });
320
321 gamePanel.add(menuPanel);
322 gamePanel.add(licenseAnchor);
323
324 RootPanel.get(CommonConstants.ID_UIROOT).add(gamePanel);
325 confirmBox = ConfirmBox.createConfirmBox(gamePanel);
326
327 String gameParam = Window.Location.getParameter("game");
328 Game game = null;
329 if (gameParam != null) {
330 try {
331 game = Game.valueOf(gameParam.toUpperCase());
332 } catch (IllegalArgumentException e) {
333 }
334 }
335 if (game != null) {
336 GWT.runAsync(game.getCallback());
337 }
338 }
339 }