2 * Copyright (c) 2008-2010, Matthias Mann
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of Matthias Mann nor the names of its contributors may
15 * be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 import de.matthiasmann.twl.DialogLayout;
33 import de.matthiasmann.twl.Event;
34 import de.matthiasmann.twl.FPSCounter;
35 import de.matthiasmann.twl.GUI;
36 import de.matthiasmann.twl.Label;
37 import de.matthiasmann.twl.RadialPopupMenu;
38 import de.matthiasmann.twl.ToggleButton;
39 import de.matthiasmann.twl.renderer.lwjgl.LWJGLRenderer;
40 import de.matthiasmann.twl.theme.ThemeManager;
41 import org.lwjgl.opengl.Display;
42 import org.lwjgl.opengl.DisplayMode;
43 import org.lwjgl.opengl.GL11;
44 import test.SimpleTest;
47 * A simple game UI demo using DialogLayout
49 * @author Matthias Mann
51 public class GameUIDemo2 extends DialogLayout {
53 public static void main(String[] args) {
55 Display.setDisplayMode(new DisplayMode(800, 600));
57 Display.setTitle("TWL Game UI Demo");
58 Display.setVSyncEnabled(true);
60 LWJGLRenderer renderer = new LWJGLRenderer();
61 GameUIDemo2 gameUI = new GameUIDemo2();
62 GUI gui = new GUI(gameUI, renderer);
64 ThemeManager theme = ThemeManager.createThemeManager(
65 GameUIDemo2.class.getResource("gameui.xml"), renderer);
66 gui.applyTheme(theme);
68 while(!Display.isCloseRequested() && !gameUI.quit) {
69 GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
73 SimpleTest.reduceInputLag();
78 } catch (Exception ex) {
79 SimpleTest.showErrMsg(ex);
84 private final ToggleButton[] actionButtons;
85 private final ToggleButton btnPause;
86 private final ToggleButton btnArmageddon;
87 private final FPSCounter fpsCounter;
88 private final Label lastSelectedRadialEntry;
92 private static final String[] ACTION_NAMES = {
103 public GameUIDemo2() {
104 actionButtons = new ToggleButton[ACTION_NAMES.length];
105 for(int i=0 ; i<ACTION_NAMES.length ; i++) {
106 actionButtons[i] = new ToggleButton();
107 actionButtons[i].setTheme(ACTION_NAMES[i]);
110 btnPause = new ToggleButton();
111 btnPause.setTheme("pause");
113 btnArmageddon = new ToggleButton();
114 btnArmageddon.setTheme("armageddon");
116 fpsCounter = new FPSCounter();
118 lastSelectedRadialEntry = new Label();
119 lastSelectedRadialEntry.setText("Right click on the background");
120 add(lastSelectedRadialEntry);
122 // create the groups for the action buttons (aligned top left)
123 Group actionButtonsH = createSequentialGroup()
124 .addGap("actionButtonsLeft")
125 .addGroup(createParallelGroup(actionButtons))
127 Group actionButtonsV = createSequentialGroup()
128 .addGap("actionButtonsTop")
129 .addWidgets(actionButtons)
132 // create the groups for the game control buttons (aligned top right)
133 Group gameCtrlH = createSequentialGroup()
135 .addWidget(btnArmageddon)
137 .addGap("gameCtrlRight");
138 Group gameCtrlV = createSequentialGroup()
139 .addGap("gameCtrlTop")
140 .addGroup(createParallelGroup(btnArmageddon, btnPause))
143 // create the groups for the status display (aligned bottom right)
144 Group statusH = createSequentialGroup()
146 .addWidget(fpsCounter)
147 .addGap("statusRight");
148 Group statusV = createSequentialGroup()
150 .addWidget(fpsCounter)
151 .addGap("statusBottom");
153 // create the groups for the radial menu message display (aligned bottom center)
154 Group radialMenuMessageH = createSequentialGroup()
156 .addWidget(lastSelectedRadialEntry)
158 Group radialMenuMessageV = createSequentialGroup()
160 .addWidget(lastSelectedRadialEntry)
161 .addGap("statusBottom");
163 // now overlay all groups
164 setHorizontalGroup(createParallelGroup(actionButtonsH, gameCtrlH, statusH, radialMenuMessageH));
165 setVerticalGroup(createParallelGroup(actionButtonsV, gameCtrlV, statusV, radialMenuMessageV));
169 protected boolean handleEvent(Event evt) {
170 if(super.handleEvent(evt)) {
173 switch (evt.getType()) {
175 switch (evt.getKeyCode()) {
176 case Event.KEY_ESCAPE:
182 if(evt.getMouseButton() == Event.MOUSE_RBUTTON) {
183 return createRadialMenu().openPopup(evt);
187 return evt.isMouseEventNoWheel();
190 RadialPopupMenu createRadialMenu() {
191 RadialPopupMenu rpm = new RadialPopupMenu(this);
192 for(int i=0 ; i<10 ; i++) {
194 rpm.addButton("star", new Runnable() {
196 lastSelectedRadialEntry.setText("Selected " + idx);