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.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.Widget;
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.TestUtils;
48 * @author Matthias Mann
50 public class GameUIDemo extends Widget {
52 public static void main(String[] args) {
54 Display.setDisplayMode(new DisplayMode(800, 600));
56 Display.setTitle("TWL Game UI Demo");
57 Display.setVSyncEnabled(true);
59 LWJGLRenderer renderer = new LWJGLRenderer();
60 GameUIDemo gameUI = new GameUIDemo();
61 GUI gui = new GUI(gameUI, renderer);
63 ThemeManager theme = ThemeManager.createThemeManager(
64 GameUIDemo.class.getResource("gameui.xml"), renderer);
65 gui.applyTheme(theme);
67 while(!Display.isCloseRequested() && !gameUI.quit) {
68 GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
72 TestUtils.reduceInputLag();
77 } catch (Exception ex) {
78 TestUtils.showErrMsg(ex);
83 private final ToggleButton[] actionButtons;
84 private final ToggleButton btnPause;
85 private final ToggleButton btnArmageddon;
86 private final FPSCounter fpsCounter;
87 private final Label lastSelectedRadialEntry;
91 private static final String[] ACTION_NAMES = {
102 public GameUIDemo() {
103 actionButtons = new ToggleButton[ACTION_NAMES.length];
104 for(int i=0 ; i<ACTION_NAMES.length ; i++) {
105 actionButtons[i] = new ToggleButton();
106 actionButtons[i].setTheme(ACTION_NAMES[i]);
107 add(actionButtons[i]);
110 btnPause = new ToggleButton();
111 btnPause.setTheme("pause");
114 btnArmageddon = new ToggleButton();
115 btnArmageddon.setTheme("armageddon");
118 fpsCounter = new FPSCounter();
121 lastSelectedRadialEntry = new Label();
122 lastSelectedRadialEntry.setText("Right click on the background");
123 add(lastSelectedRadialEntry);
127 protected void layout() {
131 for(ToggleButton b : actionButtons) {
134 y += b.getHeight() + 5;
137 x = getInnerWidth() - 10;
140 btnPause.adjustSize();
141 x -= btnPause.getWidth() + 5;
142 btnPause.setPosition(x, y);
144 btnArmageddon.adjustSize();
145 x -= btnArmageddon.getWidth() + 5;
146 btnArmageddon.setPosition(x, y);
148 fpsCounter.adjustSize();
149 fpsCounter.setPosition(
150 getInnerWidth() - fpsCounter.getWidth(),
151 getInnerHeight() - fpsCounter.getHeight());
153 lastSelectedRadialEntry.adjustSize();
154 lastSelectedRadialEntry.setPosition(
155 getInnerWidth()/2 - lastSelectedRadialEntry.getWidth()/2,
156 getInnerBottom() - lastSelectedRadialEntry.getHeight());
160 protected boolean handleEvent(Event evt) {
161 if(super.handleEvent(evt)) {
164 switch (evt.getType()) {
166 switch (evt.getKeyCode()) {
167 case Event.KEY_ESCAPE:
173 if(evt.getMouseButton() == Event.MOUSE_RBUTTON) {
174 return createRadialMenu().openPopup(evt);
178 return evt.isMouseEventNoWheel();
181 RadialPopupMenu createRadialMenu() {
182 RadialPopupMenu rpm = new RadialPopupMenu(this);
183 for(int i=0 ; i<10 ; i++) {
185 rpm.addButton("star", new Runnable() {
187 lastSelectedRadialEntry.setText("Selected " + idx);