2 * Copyright (c) 2008-2011, 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.Event;
33 import de.matthiasmann.twl.FPSCounter;
34 import de.matthiasmann.twl.GUI;
35 import de.matthiasmann.twl.Label;
36 import de.matthiasmann.twl.RadialPopupMenu;
37 import de.matthiasmann.twl.ToggleButton;
38 import de.matthiasmann.twl.WheelWidget;
39 import de.matthiasmann.twl.Widget;
40 import de.matthiasmann.twl.model.SimpleChangableListModel;
41 import de.matthiasmann.twl.renderer.lwjgl.LWJGLRenderer;
42 import de.matthiasmann.twl.theme.ThemeManager;
43 import java.util.ArrayList;
44 import org.lwjgl.opengl.Display;
45 import org.lwjgl.opengl.DisplayMode;
46 import org.lwjgl.opengl.GL11;
47 import test.TestUtils;
51 * @author Matthias Mann
53 public class GameUIDemo extends Widget {
55 public static void main(String[] args) {
57 Display.setDisplayMode(new DisplayMode(800, 600));
59 Display.setTitle("TWL Game UI Demo");
60 Display.setVSyncEnabled(true);
62 LWJGLRenderer renderer = new LWJGLRenderer();
63 GameUIDemo gameUI = new GameUIDemo();
64 GUI gui = new GUI(gameUI, renderer);
66 ThemeManager theme = ThemeManager.createThemeManager(
67 GameUIDemo.class.getResource("gameui.xml"), renderer);
68 gui.applyTheme(theme);
70 while(!Display.isCloseRequested() && !gameUI.quit) {
71 GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
75 TestUtils.reduceInputLag();
80 } catch (Exception ex) {
81 TestUtils.showErrMsg(ex);
86 private final ToggleButton[] actionButtons;
87 private final ToggleButton btnPause;
88 private final ToggleButton btnArmageddon;
89 private final FPSCounter fpsCounter;
90 private final Label lastSelectedRadialEntry;
92 private final SimpleChangableListModel<String> digits;
93 private final ArrayList<WheelWidget<String>> wheels;
97 private static final String[] ACTION_NAMES = {
108 public GameUIDemo() {
109 actionButtons = new ToggleButton[ACTION_NAMES.length];
110 for(int i=0 ; i<ACTION_NAMES.length ; i++) {
111 actionButtons[i] = new ToggleButton();
112 actionButtons[i].setTheme(ACTION_NAMES[i]);
113 add(actionButtons[i]);
116 btnPause = new ToggleButton();
117 btnPause.setTheme("pause");
120 btnArmageddon = new ToggleButton();
121 btnArmageddon.setTheme("armageddon");
124 fpsCounter = new FPSCounter();
127 lastSelectedRadialEntry = new Label();
128 lastSelectedRadialEntry.setText("Right click on the background");
129 add(lastSelectedRadialEntry);
131 digits = new SimpleChangableListModel<String>("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
132 wheels = new ArrayList<WheelWidget<String>>();
133 for(int i=0 ; i<4 ; i++) {
134 WheelWidget<String> wheel = new WheelWidget<String>(digits);
136 wheel.setCyclic(true);
142 protected void layout() {
146 for(ToggleButton b : actionButtons) {
149 y += b.getHeight() + 5;
152 x = getInnerWidth() - 10;
155 btnPause.adjustSize();
156 x -= btnPause.getWidth() + 5;
157 btnPause.setPosition(x, y);
159 btnArmageddon.adjustSize();
160 x -= btnArmageddon.getWidth() + 5;
161 btnArmageddon.setPosition(x, y);
163 fpsCounter.adjustSize();
164 fpsCounter.setPosition(
165 getInnerWidth() - fpsCounter.getWidth(),
166 getInnerHeight() - fpsCounter.getHeight());
168 lastSelectedRadialEntry.adjustSize();
169 lastSelectedRadialEntry.setPosition(
170 getInnerWidth()/2 - lastSelectedRadialEntry.getWidth()/2,
171 getInnerBottom() - lastSelectedRadialEntry.getHeight());
174 for(WheelWidget<String> wheel : wheels) {
176 wheelsWidth += wheel.getWidth();
178 x = getInnerX() + (getInnerWidth()-wheelsWidth)/2;
179 y = getInnerY() + (getInnerHeight()-wheels.get(0).getHeight())/2;
180 for(WheelWidget<String> wheel : wheels) {
181 wheel.setPosition(x, y);
182 x += wheel.getWidth();
187 protected boolean handleEvent(Event evt) {
188 if(super.handleEvent(evt)) {
191 switch (evt.getType()) {
193 switch (evt.getKeyCode()) {
194 case Event.KEY_ESCAPE:
200 if(evt.getMouseButton() == Event.MOUSE_RBUTTON) {
201 return createRadialMenu().openPopup(evt);
205 return evt.isMouseEventNoWheel();
208 RadialPopupMenu createRadialMenu() {
209 RadialPopupMenu rpm = new RadialPopupMenu(this);
210 for(int i=0 ; i<10 ; i++) {
212 rpm.addButton("star", new Runnable() {
214 lastSelectedRadialEntry.setText("Selected " + idx);