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