src/gameui/GameUIDemo2.java
author Matthias Mann
Fri Feb 03 06:35:38 2012 +0100 (3 months ago)
changeset 260 42064e048ff4
parent 231 c4d3e5eac85f
permissions -rw-r--r--
LoginDemo: added initial keyboard focus, edit fields respond to return key
Matthias@106
     1
/*
Matthias@106
     2
 * Copyright (c) 2008-2010, Matthias Mann
Matthias@106
     3
 *
Matthias@106
     4
 * All rights reserved.
Matthias@106
     5
 *
Matthias@106
     6
 * Redistribution and use in source and binary forms, with or without
Matthias@106
     7
 * modification, are permitted provided that the following conditions are met:
Matthias@106
     8
 *
Matthias@106
     9
 *     * Redistributions of source code must retain the above copyright notice,
Matthias@106
    10
 *       this list of conditions and the following disclaimer.
Matthias@106
    11
 *     * Redistributions in binary form must reproduce the above copyright
Matthias@106
    12
 *       notice, this list of conditions and the following disclaimer in the
Matthias@106
    13
 *       documentation and/or other materials provided with the distribution.
Matthias@106
    14
 *     * Neither the name of Matthias Mann nor the names of its contributors may
Matthias@106
    15
 *       be used to endorse or promote products derived from this software
Matthias@106
    16
 *       without specific prior written permission.
Matthias@106
    17
 *
Matthias@106
    18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Matthias@106
    19
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Matthias@106
    20
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Matthias@106
    21
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
Matthias@106
    22
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Matthias@106
    23
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Matthias@106
    24
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Matthias@106
    25
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
Matthias@106
    26
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Matthias@106
    27
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Matthias@106
    28
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Matthias@106
    29
 */
Matthias@106
    30
package gameui;
Matthias@106
    31
Matthias@106
    32
import de.matthiasmann.twl.DialogLayout;
Matthias@106
    33
import de.matthiasmann.twl.Event;
Matthias@106
    34
import de.matthiasmann.twl.FPSCounter;
Matthias@106
    35
import de.matthiasmann.twl.GUI;
Matthias@231
    36
import de.matthiasmann.twl.Label;
Matthias@231
    37
import de.matthiasmann.twl.RadialPopupMenu;
Matthias@106
    38
import de.matthiasmann.twl.ToggleButton;
Matthias@106
    39
import de.matthiasmann.twl.renderer.lwjgl.LWJGLRenderer;
Matthias@106
    40
import de.matthiasmann.twl.theme.ThemeManager;
Matthias@106
    41
import org.lwjgl.opengl.Display;
Matthias@106
    42
import org.lwjgl.opengl.DisplayMode;
Matthias@106
    43
import org.lwjgl.opengl.GL11;
Matthias@253
    44
import test.TestUtils;
Matthias@106
    45
Matthias@106
    46
/**
Matthias@106
    47
 * A simple game UI demo using DialogLayout
Matthias@106
    48
 *
Matthias@106
    49
 * @author Matthias Mann
Matthias@106
    50
 */
Matthias@106
    51
public class GameUIDemo2 extends DialogLayout {
Matthias@106
    52
Matthias@106
    53
    public static void main(String[] args) {
Matthias@106
    54
        try {
Matthias@106
    55
            Display.setDisplayMode(new DisplayMode(800, 600));
Matthias@106
    56
            Display.create();
Matthias@106
    57
            Display.setTitle("TWL Game UI Demo");
Matthias@106
    58
            Display.setVSyncEnabled(true);
Matthias@106
    59
Matthias@106
    60
            LWJGLRenderer renderer = new LWJGLRenderer();
Matthias@106
    61
            GameUIDemo2 gameUI = new GameUIDemo2();
Matthias@106
    62
            GUI gui = new GUI(gameUI, renderer);
Matthias@106
    63
Matthias@106
    64
            ThemeManager theme = ThemeManager.createThemeManager(
Matthias@106
    65
                    GameUIDemo2.class.getResource("gameui.xml"), renderer);
Matthias@106
    66
            gui.applyTheme(theme);
Matthias@106
    67
Matthias@106
    68
            while(!Display.isCloseRequested() && !gameUI.quit) {
Matthias@106
    69
                GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
Matthias@106
    70
Matthias@106
    71
                gui.update();
Matthias@106
    72
                Display.update();
Matthias@253
    73
                TestUtils.reduceInputLag();
Matthias@106
    74
            }
Matthias@106
    75
Matthias@106
    76
            gui.destroy();
Matthias@106
    77
            theme.destroy();
Matthias@106
    78
        } catch (Exception ex) {
Matthias@253
    79
            TestUtils.showErrMsg(ex);
Matthias@106
    80
        }
Matthias@106
    81
        Display.destroy();
Matthias@106
    82
    }
Matthias@106
    83
Matthias@106
    84
    private final ToggleButton[] actionButtons;
Matthias@106
    85
    private final ToggleButton btnPause;
Matthias@106
    86
    private final ToggleButton btnArmageddon;
Matthias@106
    87
    private final FPSCounter fpsCounter;
Matthias@231
    88
    private final Label lastSelectedRadialEntry;
Matthias@106
    89
Matthias@106
    90
    public boolean quit;
Matthias@106
    91
Matthias@106
    92
    private static final String[] ACTION_NAMES = {
Matthias@106
    93
        "pingu-digger",
Matthias@106
    94
        "pingu-miner",
Matthias@106
    95
        "pingu-basher",
Matthias@106
    96
        "pingu-climber",
Matthias@106
    97
        "pingu-floater",
Matthias@106
    98
        "pingu-bomber",
Matthias@106
    99
        "pingu-blocker",
Matthias@106
   100
        "pingu-bridger",
Matthias@106
   101
    };
Matthias@106
   102
Matthias@106
   103
    public GameUIDemo2() {
Matthias@106
   104
        actionButtons = new ToggleButton[ACTION_NAMES.length];
Matthias@106
   105
        for(int i=0 ; i<ACTION_NAMES.length ; i++) {
Matthias@106
   106
            actionButtons[i] = new ToggleButton();
Matthias@106
   107
            actionButtons[i].setTheme(ACTION_NAMES[i]);
Matthias@106
   108
        }
Matthias@106
   109
Matthias@106
   110
        btnPause = new ToggleButton();
Matthias@106
   111
        btnPause.setTheme("pause");
Matthias@106
   112
Matthias@106
   113
        btnArmageddon = new ToggleButton();
Matthias@106
   114
        btnArmageddon.setTheme("armageddon");
Matthias@106
   115
Matthias@106
   116
        fpsCounter = new FPSCounter();
Matthias@106
   117
Matthias@231
   118
        lastSelectedRadialEntry = new Label();
Matthias@231
   119
        lastSelectedRadialEntry.setText("Right click on the background");
Matthias@231
   120
        add(lastSelectedRadialEntry);
Matthias@231
   121
Matthias@106
   122
        // create the groups for the action buttons (aligned top left)
Matthias@106
   123
        Group actionButtonsH = createSequentialGroup()
Matthias@106
   124
                .addGap("actionButtonsLeft")
Matthias@106
   125
                .addGroup(createParallelGroup(actionButtons))
Matthias@106
   126
                .addGap();
Matthias@106
   127
        Group actionButtonsV = createSequentialGroup()
Matthias@106
   128
                .addGap("actionButtonsTop")
Matthias@106
   129
                .addWidgets(actionButtons)
Matthias@106
   130
                .addGap();
Matthias@106
   131
Matthias@106
   132
        // create the groups for the game control buttons (aligned top right)
Matthias@106
   133
        Group gameCtrlH = createSequentialGroup()
Matthias@106
   134
                .addGap()
Matthias@106
   135
                .addWidget(btnArmageddon)
Matthias@106
   136
                .addWidget(btnPause)
Matthias@106
   137
                .addGap("gameCtrlRight");
Matthias@106
   138
        Group gameCtrlV = createSequentialGroup()
Matthias@106
   139
                .addGap("gameCtrlTop")
Matthias@106
   140
                .addGroup(createParallelGroup(btnArmageddon, btnPause))
Matthias@106
   141
                .addGap();
Matthias@106
   142
        
Matthias@106
   143
        // create the groups for the status display (aligned bottom right)
Matthias@106
   144
        Group statusH = createSequentialGroup()
Matthias@106
   145
                .addGap()
Matthias@106
   146
                .addWidget(fpsCounter)
Matthias@106
   147
                .addGap("statusRight");
Matthias@106
   148
        Group statusV = createSequentialGroup()
Matthias@106
   149
                .addGap()
Matthias@106
   150
                .addWidget(fpsCounter)
Matthias@106
   151
                .addGap("statusBottom");
Matthias@106
   152
Matthias@231
   153
        // create the groups for the radial menu message display (aligned bottom center)
Matthias@231
   154
        Group radialMenuMessageH = createSequentialGroup()
Matthias@231
   155
                .addGap()
Matthias@231
   156
                .addWidget(lastSelectedRadialEntry)
Matthias@231
   157
                .addGap();
Matthias@231
   158
        Group radialMenuMessageV = createSequentialGroup()
Matthias@231
   159
                .addGap()
Matthias@231
   160
                .addWidget(lastSelectedRadialEntry)
Matthias@231
   161
                .addGap("statusBottom");
Matthias@231
   162
Matthias@106
   163
        // now overlay all groups
Matthias@231
   164
        setHorizontalGroup(createParallelGroup(actionButtonsH, gameCtrlH, statusH, radialMenuMessageH));
Matthias@231
   165
        setVerticalGroup(createParallelGroup(actionButtonsV, gameCtrlV, statusV, radialMenuMessageV));
Matthias@106
   166
    }
Matthias@106
   167
Matthias@106
   168
    @Override
Matthias@106
   169
    protected boolean handleEvent(Event evt) {
Matthias@136
   170
        if(super.handleEvent(evt)) {
Matthias@136
   171
            return true;
Matthias@136
   172
        }
Matthias@136
   173
        switch (evt.getType()) {
Matthias@136
   174
            case KEY_PRESSED:
Matthias@136
   175
                switch (evt.getKeyCode()) {
Matthias@197
   176
                    case Event.KEY_ESCAPE:
Matthias@136
   177
                        quit = true;
Matthias@136
   178
                        return true;
Matthias@136
   179
                }
Matthias@231
   180
                break;
Matthias@231
   181
            case MOUSE_BTNDOWN:
Matthias@231
   182
                if(evt.getMouseButton() == Event.MOUSE_RBUTTON) {
Matthias@231
   183
                    return createRadialMenu().openPopup(evt);
Matthias@231
   184
                }
Matthias@231
   185
                break;
Matthias@106
   186
        }
Matthias@231
   187
        return evt.isMouseEventNoWheel();
Matthias@231
   188
    }
Matthias@231
   189
Matthias@231
   190
    RadialPopupMenu createRadialMenu() {
Matthias@231
   191
        RadialPopupMenu rpm = new RadialPopupMenu(this);
Matthias@231
   192
        for(int i=0 ; i<10 ; i++) {
Matthias@231
   193
            final int idx = i;
Matthias@231
   194
            rpm.addButton("star", new Runnable() {
Matthias@231
   195
                public void run() {
Matthias@231
   196
                    lastSelectedRadialEntry.setText("Selected " + idx);
Matthias@231
   197
                }
Matthias@231
   198
            });
Matthias@231
   199
        }
Matthias@231
   200
        return rpm;
Matthias@106
   201
    }
Matthias@106
   202
Matthias@106
   203
}