src/gameui/GameUIDemo.java
author Matthias Mann
Thu Jun 23 22:43:25 2011 +0200 (11 months ago)
changeset 253 25120c47f789
parent 231 c4d3e5eac85f
child 257 d00c2426a444
permissions -rw-r--r--
refactored utility methods into TestUtils
     1 /*
     2  * Copyright (c) 2008-2010, Matthias Mann
     3  *
     4  * All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     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.
    17  *
    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.
    29  */
    30 package gameui;
    31 
    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;
    45 
    46 /**
    47  *
    48  * @author Matthias Mann
    49  */
    50 public class GameUIDemo extends Widget {
    51 
    52     public static void main(String[] args) {
    53         try {
    54             Display.setDisplayMode(new DisplayMode(800, 600));
    55             Display.create();
    56             Display.setTitle("TWL Game UI Demo");
    57             Display.setVSyncEnabled(true);
    58 
    59             LWJGLRenderer renderer = new LWJGLRenderer();
    60             GameUIDemo gameUI = new GameUIDemo();
    61             GUI gui = new GUI(gameUI, renderer);
    62 
    63             ThemeManager theme = ThemeManager.createThemeManager(
    64                     GameUIDemo.class.getResource("gameui.xml"), renderer);
    65             gui.applyTheme(theme);
    66 
    67             while(!Display.isCloseRequested() && !gameUI.quit) {
    68                 GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
    69 
    70                 gui.update();
    71                 Display.update();
    72                 TestUtils.reduceInputLag();
    73             }
    74 
    75             gui.destroy();
    76             theme.destroy();
    77         } catch (Exception ex) {
    78             TestUtils.showErrMsg(ex);
    79         }
    80         Display.destroy();
    81     }
    82 
    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;
    88 
    89     public boolean quit;
    90 
    91     private static final String[] ACTION_NAMES = {
    92         "pingu-digger",
    93         "pingu-miner",
    94         "pingu-basher",
    95         "pingu-climber",
    96         "pingu-floater",
    97         "pingu-bomber",
    98         "pingu-blocker",
    99         "pingu-bridger",
   100     };
   101 
   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]);
   108         }
   109 
   110         btnPause = new ToggleButton();
   111         btnPause.setTheme("pause");
   112         add(btnPause);
   113 
   114         btnArmageddon = new ToggleButton();
   115         btnArmageddon.setTheme("armageddon");
   116         add(btnArmageddon);
   117 
   118         fpsCounter = new FPSCounter();
   119         add(fpsCounter);
   120 
   121         lastSelectedRadialEntry = new Label();
   122         lastSelectedRadialEntry.setText("Right click on the background");
   123         add(lastSelectedRadialEntry);
   124     }
   125 
   126     @Override
   127     protected void layout() {
   128         int x = 10;
   129         int y = 40;
   130         
   131         for(ToggleButton b : actionButtons) {
   132             b.setPosition(x, y);
   133             b.adjustSize();
   134             y += b.getHeight() + 5;
   135         }
   136 
   137         x = getInnerWidth() - 10;
   138         y = 10;
   139 
   140         btnPause.adjustSize();
   141         x -= btnPause.getWidth() + 5;
   142         btnPause.setPosition(x, y);
   143 
   144         btnArmageddon.adjustSize();
   145         x -= btnArmageddon.getWidth() + 5;
   146         btnArmageddon.setPosition(x, y);
   147 
   148         fpsCounter.adjustSize();
   149         fpsCounter.setPosition(
   150                 getInnerWidth() - fpsCounter.getWidth(),
   151                 getInnerHeight() - fpsCounter.getHeight());
   152 
   153         lastSelectedRadialEntry.adjustSize();
   154         lastSelectedRadialEntry.setPosition(
   155                 getInnerWidth()/2 - lastSelectedRadialEntry.getWidth()/2,
   156                 getInnerBottom() - lastSelectedRadialEntry.getHeight());
   157     }
   158 
   159     @Override
   160     protected boolean handleEvent(Event evt) {
   161         if(super.handleEvent(evt)) {
   162             return true;
   163         }
   164         switch (evt.getType()) {
   165             case KEY_PRESSED:
   166                 switch (evt.getKeyCode()) {
   167                     case Event.KEY_ESCAPE:
   168                         quit = true;
   169                         return true;
   170                 }
   171                 break;
   172             case MOUSE_BTNDOWN:
   173                 if(evt.getMouseButton() == Event.MOUSE_RBUTTON) {
   174                     return createRadialMenu().openPopup(evt);
   175                 }
   176                 break;
   177         }
   178         return evt.isMouseEventNoWheel();
   179     }
   180 
   181     RadialPopupMenu createRadialMenu() {
   182         RadialPopupMenu rpm = new RadialPopupMenu(this);
   183         for(int i=0 ; i<10 ; i++) {
   184             final int idx = i;
   185             rpm.addButton("star", new Runnable() {
   186                 public void run() {
   187                     lastSelectedRadialEntry.setText("Selected " + idx);
   188                 }
   189             });
   190         }
   191         return rpm;
   192     }
   193 }