src/gameui/GameUIDemo2.java
author Matthias Mann
Sun Feb 06 17:23:04 2011 +0100 (15 months ago)
changeset 231 c4d3e5eac85f
parent 197 939d8ee43216
child 253 25120c47f789
permissions -rw-r--r--
added radial menu also to GameUIDemo2
     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.DialogLayout;
    33 import de.matthiasmann.twl.Event;
    34 import de.matthiasmann.twl.FPSCounter;
    35 import de.matthiasmann.twl.GUI;
    36 import de.matthiasmann.twl.Label;
    37 import de.matthiasmann.twl.RadialPopupMenu;
    38 import de.matthiasmann.twl.ToggleButton;
    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.SimpleTest;
    45 
    46 /**
    47  * A simple game UI demo using DialogLayout
    48  *
    49  * @author Matthias Mann
    50  */
    51 public class GameUIDemo2 extends DialogLayout {
    52 
    53     public static void main(String[] args) {
    54         try {
    55             Display.setDisplayMode(new DisplayMode(800, 600));
    56             Display.create();
    57             Display.setTitle("TWL Game UI Demo");
    58             Display.setVSyncEnabled(true);
    59 
    60             LWJGLRenderer renderer = new LWJGLRenderer();
    61             GameUIDemo2 gameUI = new GameUIDemo2();
    62             GUI gui = new GUI(gameUI, renderer);
    63 
    64             ThemeManager theme = ThemeManager.createThemeManager(
    65                     GameUIDemo2.class.getResource("gameui.xml"), renderer);
    66             gui.applyTheme(theme);
    67 
    68             while(!Display.isCloseRequested() && !gameUI.quit) {
    69                 GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
    70 
    71                 gui.update();
    72                 Display.update();
    73                 SimpleTest.reduceInputLag();
    74             }
    75 
    76             gui.destroy();
    77             theme.destroy();
    78         } catch (Exception ex) {
    79             SimpleTest.showErrMsg(ex);
    80         }
    81         Display.destroy();
    82     }
    83 
    84     private final ToggleButton[] actionButtons;
    85     private final ToggleButton btnPause;
    86     private final ToggleButton btnArmageddon;
    87     private final FPSCounter fpsCounter;
    88     private final Label lastSelectedRadialEntry;
    89 
    90     public boolean quit;
    91 
    92     private static final String[] ACTION_NAMES = {
    93         "pingu-digger",
    94         "pingu-miner",
    95         "pingu-basher",
    96         "pingu-climber",
    97         "pingu-floater",
    98         "pingu-bomber",
    99         "pingu-blocker",
   100         "pingu-bridger",
   101     };
   102 
   103     public GameUIDemo2() {
   104         actionButtons = new ToggleButton[ACTION_NAMES.length];
   105         for(int i=0 ; i<ACTION_NAMES.length ; i++) {
   106             actionButtons[i] = new ToggleButton();
   107             actionButtons[i].setTheme(ACTION_NAMES[i]);
   108         }
   109 
   110         btnPause = new ToggleButton();
   111         btnPause.setTheme("pause");
   112 
   113         btnArmageddon = new ToggleButton();
   114         btnArmageddon.setTheme("armageddon");
   115 
   116         fpsCounter = new FPSCounter();
   117 
   118         lastSelectedRadialEntry = new Label();
   119         lastSelectedRadialEntry.setText("Right click on the background");
   120         add(lastSelectedRadialEntry);
   121 
   122         // create the groups for the action buttons (aligned top left)
   123         Group actionButtonsH = createSequentialGroup()
   124                 .addGap("actionButtonsLeft")
   125                 .addGroup(createParallelGroup(actionButtons))
   126                 .addGap();
   127         Group actionButtonsV = createSequentialGroup()
   128                 .addGap("actionButtonsTop")
   129                 .addWidgets(actionButtons)
   130                 .addGap();
   131 
   132         // create the groups for the game control buttons (aligned top right)
   133         Group gameCtrlH = createSequentialGroup()
   134                 .addGap()
   135                 .addWidget(btnArmageddon)
   136                 .addWidget(btnPause)
   137                 .addGap("gameCtrlRight");
   138         Group gameCtrlV = createSequentialGroup()
   139                 .addGap("gameCtrlTop")
   140                 .addGroup(createParallelGroup(btnArmageddon, btnPause))
   141                 .addGap();
   142         
   143         // create the groups for the status display (aligned bottom right)
   144         Group statusH = createSequentialGroup()
   145                 .addGap()
   146                 .addWidget(fpsCounter)
   147                 .addGap("statusRight");
   148         Group statusV = createSequentialGroup()
   149                 .addGap()
   150                 .addWidget(fpsCounter)
   151                 .addGap("statusBottom");
   152 
   153         // create the groups for the radial menu message display (aligned bottom center)
   154         Group radialMenuMessageH = createSequentialGroup()
   155                 .addGap()
   156                 .addWidget(lastSelectedRadialEntry)
   157                 .addGap();
   158         Group radialMenuMessageV = createSequentialGroup()
   159                 .addGap()
   160                 .addWidget(lastSelectedRadialEntry)
   161                 .addGap("statusBottom");
   162 
   163         // now overlay all groups
   164         setHorizontalGroup(createParallelGroup(actionButtonsH, gameCtrlH, statusH, radialMenuMessageH));
   165         setVerticalGroup(createParallelGroup(actionButtonsV, gameCtrlV, statusV, radialMenuMessageV));
   166     }
   167 
   168     @Override
   169     protected boolean handleEvent(Event evt) {
   170         if(super.handleEvent(evt)) {
   171             return true;
   172         }
   173         switch (evt.getType()) {
   174             case KEY_PRESSED:
   175                 switch (evt.getKeyCode()) {
   176                     case Event.KEY_ESCAPE:
   177                         quit = true;
   178                         return true;
   179                 }
   180                 break;
   181             case MOUSE_BTNDOWN:
   182                 if(evt.getMouseButton() == Event.MOUSE_RBUTTON) {
   183                     return createRadialMenu().openPopup(evt);
   184                 }
   185                 break;
   186         }
   187         return evt.isMouseEventNoWheel();
   188     }
   189 
   190     RadialPopupMenu createRadialMenu() {
   191         RadialPopupMenu rpm = new RadialPopupMenu(this);
   192         for(int i=0 ; i<10 ; i++) {
   193             final int idx = i;
   194             rpm.addButton("star", new Runnable() {
   195                 public void run() {
   196                     lastSelectedRadialEntry.setText("Selected " + idx);
   197                 }
   198             });
   199         }
   200         return rpm;
   201     }
   202 
   203 }