Package ponkOut.Menu

Source Code of ponkOut.Menu.MenuState

/* Copyright 2010-2013 Christian Matt
*
* This file is part of PonkOut.
*
* PonkOut is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PonkOut is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PonkOut.  If not, see <http://www.gnu.org/licenses/>.
*/

package ponkOut.Menu;

import java.util.ArrayList;
import java.util.LinkedList;

import org.lwjgl.input.Controllers;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector3f;

import ponkOut.GameState;
import ponkOut.PonkOut;
import ponkOut.Settings;
import ponkOut.State;
import ponkOut.StateChangeListener;
import ponkOut.Menu.InputSelectionItem.InputType;
import ponkOut.Menu.MenuItem.Action;
import ponkOut.Menu.MenuItem.Place;
import ponkOut.graphics.Camera;
import ponkOut.graphics.Capabilities;
import ponkOut.graphics.Color;
import ponkOut.graphics.Cursor;
import ponkOut.graphics.Font;
import ponkOut.graphics.GameGraphicsObjectsManager;
import ponkOut.graphics.GraphicsObjectsManager;
import ponkOut.graphics.Lighting;
import ponkOut.graphics.MenuGraphicsObjectsManager;
import ponkOut.graphics.Skybox;
import ponkOut.graphics.resources.ShaderManager;
import ponkOut.graphics.resources.TextureManager;
import ponkOut.input.InputManager;

public class MenuState extends State {
  private enum RotationDirection {
    NONE, LEFT, RIGHT, UP, DOWN
  };

  public enum FlyingDirection {
    NONE, IN, OUT
  };

  private enum Menu {
    MAIN, SETTINGS, GAME, GRAPHICS, CONTROLS, NEW_GAME, CREDITS, EXIT
  };

  private GraphicsObjectsManager goManager;
  private InputManager inputManager;
  private Settings settings;

  private Cursor cursor;
  private float mouseSpeed;

  private LinkedList<MenuItem> menuItems;
  private LinkedList<MenuItem> gameItems;
  private LinkedList<MenuItem> graphicsItems;
  private LinkedList<MenuItem> controlsItems;
  private ArrayList<LinkedList<MenuItem>> keyboardItems = new ArrayList<LinkedList<MenuItem>>(2);
  private ArrayList<LinkedList<MenuItem>> mouseItems = new ArrayList<LinkedList<MenuItem>>(2);
  private ArrayList<LinkedList<MenuItem>> controllerItems = new ArrayList<LinkedList<MenuItem>>(2);

  private InputDeviceSpinner inputDeviceSpinner[] = new InputDeviceSpinner[2];

  private RotationDirection rotating;
  private FlyingDirection flying;
  private float rotX;
  private float rotY;
  private float destRotX;
  private float destRotY;
  private float posZ;
  private float destPosZ;

  private Menu currentMenu;
  private State nextGameState;
  private InputSelectionItem waitForInput;

  public MenuState(StateChangeListener listener, State previous) {
    super(listener, previous);

    goManager = new MenuGraphicsObjectsManager();
    inputManager = InputManager.getInstance();
    settings = Settings.getInstance();

    updateMouseSpeed();

    rotating = RotationDirection.NONE;
    flying = FlyingDirection.NONE;
    rotX = 0.0f;
    rotY = 0.0f;
    destRotX = 0.0f;
    destRotY = 0.0f;
    posZ = 0.0f;
    destPosZ = 0.0f;

    currentMenu = Menu.MAIN;
    waitForInput = null;

    menuItems = new LinkedList<MenuItem>();
    gameItems = new LinkedList<MenuItem>();
    graphicsItems = new LinkedList<MenuItem>();
    controlsItems = new LinkedList<MenuItem>();

    for (int player = 0; player < 2; ++player) {
      keyboardItems.add(new LinkedList<MenuItem>());
      mouseItems.add(new LinkedList<MenuItem>());
      controllerItems.add(new LinkedList<MenuItem>());
    }

    menuItems.add(new MenuItem("New Game", 1.0f, new Vector3f(0.0f, 4.5f, -20.0f), 0.0f, 0.0f, 0.0f, Place.FRONT,
        Action.START_GAME, true, true, this, goManager));
    menuItems.add(new MenuItem("Settings", 1.0f, new Vector3f(0.0f, 1.5f, -20.0f), 0.0f, 0.0f, 0.0f, Place.FRONT,
        Action.ROTATE_RIGHT, true, true, this, goManager));
    menuItems.add(new MenuItem("Credits", 1.0f, new Vector3f(0.0f, -1.5f, -20.0f), 0.0f, 0.0f, 0.0f, Place.FRONT,
        Action.SHOW_CREDITS, true, true, this, goManager));
    menuItems.add(new MenuItem("Exit", 1.0f, new Vector3f(0.0f, -4.5f, -20.0f), 0.0f, 0.0f, 0.0f, Place.FRONT,
        Action.EXIT, true, true, this, goManager));

    menuItems.add(new MenuItem("Game", 1.0f, new Vector3f(20.0f, 4.5f, 0.0f), 0.0f, -90.0f, 0.0f, Place.RIGHT,
        Action.ROTATE_UP, true, true, this, goManager));
    menuItems.add(new MenuItem("Graphics", 1.0f, new Vector3f(20.0f, 1.5f, 0.0f), 0.0f, -90.0f, 0.0f, Place.RIGHT,
        Action.ROTATE_RIGHT, true, true, this, goManager));
    menuItems.add(new MenuItem("Controls", 1.0f, new Vector3f(20.0f, -1.5f, 0.0f), 0.0f, -90.0f, 0.0f, Place.RIGHT,
        Action.ROTATE_DOWN, true, true, this, goManager));
    menuItems.add(new MenuItem("Back", 1.0f, new Vector3f(20.0f, -4.5f, 0.0f), 0.0f, -90.0f, 0.0f, Place.RIGHT,
        Action.ROTATE_LEFT, true, true, this, goManager));

    new MenuItem("Player 1 Paddle:", 0.5f, new Vector3f(-7.5f, 20.0f, -10.0f), 90.0f, -90.0f, 0.0f, Place.TOP,
        Action.NONE, false, false, this, goManager);
    gameItems.add(new PaddleSpinner(4, 0.5f, new Vector3f(-7.5f, 20.0f, -2.0f), 90.0f, -90.0f, 0.0f, Place.TOP,
        false, false, true, new SpinnerMediator(Spinner.SpinnerType.PADDLE1), this, goManager));
    new MenuItem("Player 2 Paddle:", 0.5f, new Vector3f(-6.0f, 20.0f, -10.0f), 90.0f, -90.0f, 0.0f, Place.TOP,
        Action.NONE, false, false, this, goManager);
    gameItems.add(new PaddleSpinner(4, 0.5f, new Vector3f(-6.0f, 20.0f, -2.0f), 90.0f, -90.0f, 0.0f, Place.TOP,
        false, false, true, new SpinnerMediator(Spinner.SpinnerType.PADDLE2), this, goManager));
    menuItems.add(new MenuItem("Apply", 0.5f, new Vector3f(6.5f, 20.0f, -10.0f), 90.0f, -90.0f, 0.0f, Place.TOP,
        Action.APPLY_GAME, false, false, this, goManager));
    menuItems.add(new MenuItem("Discard", 0.5f, new Vector3f(6.5f, 20.0f, 6.5f), 90.0f, -90.0f, 0.0f, Place.TOP,
        Action.DISCARD_GAME, false, false, this, goManager));
    menuItems.add(new MenuItem("Restore Defaults", 0.5f, new Vector3f(6.5f, 20.0f, 0.0f), 90.0f, -90.0f, 0.0f,
        Place.TOP, Action.RESTORE_GAME, true, false, this, goManager));
    menuItems.addAll(gameItems);

    new MenuItem("Display Mode:", 0.5f, new Vector3f(10.0f, 7.5f, 20.0f), 0.0f, -180.0f, 0.0f, Place.BACK,
        Action.NONE, false, false, this, goManager);
    graphicsItems.add(new Spinner(19, 0.5f, new Vector3f(2.5f, 7.5f, 20.0f), 0.0f, -180.0f, 0.0f, Place.BACK,
        false, false, true, new DisplayModeSpinnerMediator(), this, goManager));
    new MenuItem("Anti-Aliasing:", 0.5f, new Vector3f(10.0f, 6.0f, 20.0f), 0.0f, -180.0f, 0.0f, Place.BACK,
        Action.NONE, false, false, this, goManager);
    graphicsItems.add(new Spinner(3, 0.5f, new Vector3f(2.5f, 6.0f, 20.0f), 0.0f, -180.0f, 0.0f, Place.BACK, false,
        false, Capabilities.getMultisampling(), new AntiAliasingSpinnerMediator(), this, goManager));
    graphicsItems
        .add(new Checkbox("Fullscreen", 0.5f, new Vector3f(10.0f, 4.5f, 20.0f), 0.0f, -180.0f, 0.0f,
            Place.BACK, false, false, true, new CheckboxMediator(Settings.BooleanValue.FULLSCREEN), this,
            goManager));
    graphicsItems.add(new Checkbox("VSync", 0.5f, new Vector3f(10.0f, 3.0f, 20.0f), 0.0f, -180.0f, 0.0f,
        Place.BACK, false, false, true, new CheckboxMediator(Settings.BooleanValue.VSYNC), this, goManager));
    graphicsItems.add(new Checkbox("Anisotropic Filtering", 0.5f, new Vector3f(10.0f, 1.5f, 20.0f), 0.0f, -180.0f,
        0.0f, Place.BACK, false, false, Capabilities.getAnisotropic(), new CheckboxMediator(
            Settings.BooleanValue.ANISOTROPIC), this, goManager));
    menuItems.add(new MenuItem("Apply", 0.5f, new Vector3f(10.0f, -6.5f, 20.0f), 0.0f, -180.0f, 0.0f, Place.BACK,
        Action.APPLY_GRAPHICS, false, false, this, goManager));
    menuItems.add(new MenuItem("Discard", 0.5f, new Vector3f(-6.5f, -6.5f, 20.0f), 0.0f, -180.0f, 0.0f, Place.BACK,
        Action.DISCARD_GRAPHICS, false, false, this, goManager));
    menuItems.add(new MenuItem("Restore Defaults", 0.5f, new Vector3f(0.0f, -6.5f, 20.0f), 0.0f, -180.0f, 0.0f,
        Place.BACK, Action.RESTORE_GRAPHICS, true, false, this, goManager));
    menuItems.addAll(graphicsItems);

    new MenuItem("Menu Mouse Speed:", 0.5f, new Vector3f(7.5f, -20.0f, -10.0f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
        Action.NONE, false, false, this, goManager);
    controlsItems.add(new Spinner(11, 0.5f, new Vector3f(7.5f, -20.0f, 0.0f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
        false, false, true, new SpinnerMediator(Spinner.SpinnerType.MENU_MOUSE_SPEED), this, goManager));
    new MenuItem("Player 1", 0.5f, new Vector3f(6.0f, -20.0f, -4.25f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
        Action.NONE, false, false, this, goManager);
    new MenuItem("Player 2", 0.5f, new Vector3f(6.0f, -20.0f, 4.25f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
        Action.NONE, false, false, this, goManager);
    new MenuItem("Device:", 0.5f, new Vector3f(4.5f, -20.0f, -10.0f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
        Action.NONE, false, false, this, goManager);
    inputDeviceSpinner[0] = new InputDeviceSpinner(11, 0.5f, new Vector3f(4.5f, -20.0f, -6.0f), -90.0f, -90.0f,
        0.0f, Place.BOTTOM, false, false, true, 0, new InputDeviceSpinnerMediator(0), this, goManager);
    inputDeviceSpinner[1] = new InputDeviceSpinner(11, 0.5f, new Vector3f(4.5f, -20.0f, 2.5f), -90.0f, -90.0f,
        0.0f, Place.BOTTOM, false, false, true, 1, new InputDeviceSpinnerMediator(1), this, goManager);
    controlsItems.add(inputDeviceSpinner[0]);
    controlsItems.add(inputDeviceSpinner[1]);
    new MenuItem("Speed:", 0.5f, new Vector3f(3.0f, -20.0f, -10.0f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
        Action.NONE, false, false, this, goManager);
    keyboardItems.get(0).add(
        new Spinner(11, 0.5f, new Vector3f(3.0f, -20.0f, -6.0f), -90.0f, -90.0f, 0.0f, Place.BOTTOM, false,
            false, true, new SpinnerMediator(Spinner.SpinnerType.KEYBOARD_SPEED1), this, goManager));
    keyboardItems.get(1).add(
        new Spinner(11, 0.5f, new Vector3f(3.0f, -20.0f, 2.5f), -90.0f, -90.0f, 0.0f, Place.BOTTOM, false,
            false, true, new SpinnerMediator(Spinner.SpinnerType.KEYBOARD_SPEED2), this, goManager));
    mouseItems.get(0).add(
        new Spinner(11, 0.5f, new Vector3f(3.0f, -20.0f, -6.0f), -90.0f, -90.0f, 0.0f, Place.BOTTOM, false,
            false, true, new SpinnerMediator(Spinner.SpinnerType.MOUSE_SPEED1), this, goManager));
    mouseItems.get(1).add(
        new Spinner(11, 0.5f, new Vector3f(3.0f, -20.0f, 2.5f), -90.0f, -90.0f, 0.0f, Place.BOTTOM, false,
            false, true, new SpinnerMediator(Spinner.SpinnerType.MOUSE_SPEED2), this, goManager));
    controllerItems.get(0).add(
        new Spinner(11, 0.5f, new Vector3f(3.0f, -20.0f, -6.0f), -90.0f, -90.0f, 0.0f, Place.BOTTOM, false,
            false, true, new SpinnerMediator(Spinner.SpinnerType.CONTROLLER_SPEED1), this, goManager));
    controllerItems.get(1).add(
        new Spinner(11, 0.5f, new Vector3f(3.0f, -20.0f, 2.5f), -90.0f, -90.0f, 0.0f, Place.BOTTOM, false,
            false, true, new SpinnerMediator(Spinner.SpinnerType.CONTROLLER_SPEED2), this, goManager));
    new MenuItem("Up:", 0.5f, new Vector3f(1.5f, -20.0f, -10.0f), -90.0f, -90.0f, 0.0f, Place.BOTTOM, Action.NONE,
        false, false, this, goManager);
    keyboardItems.get(0).add(
        new KeyboardSelectionItem(0.5f, new Vector3f(1.5f, -20.0f, -6.0f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
            Action.NONE, false, false, 0, new InputSelectionItemMediator(InputType.KEYBOARD_UP, 0), this,
            goManager));
    keyboardItems.get(1).add(
        new KeyboardSelectionItem(0.5f, new Vector3f(1.5f, -20.0f, 2.5f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
            Action.NONE, false, false, 1, new InputSelectionItemMediator(InputType.KEYBOARD_UP, 1), this,
            goManager));
    mouseItems.get(0).add(
        new MenuItem("Mouse Up", 0.5f, new Vector3f(1.5f, -20.0f, -6.0f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
            Action.NONE, false, false, false, this, goManager));
    mouseItems.get(1).add(
        new MenuItem("Mouse Up", 0.5f, new Vector3f(1.5f, -20.0f, 2.5f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
            Action.NONE, false, false, false, this, goManager));
    controllerItems.get(0).add(
        new ControllerSelectionItem(0.5f, new Vector3f(1.5f, -20.0f, -6.0f), -90.0f, -90.0f, 0.0f,
            Place.BOTTOM, Action.NONE, false, false, 0, new InputSelectionItemMediator(
                InputType.CONTROLLER_UP, 0), this, goManager));
    controllerItems.get(1).add(
        new ControllerSelectionItem(0.5f, new Vector3f(1.5f, -20.0f, 2.5f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
            Action.NONE, false, false, 1, new InputSelectionItemMediator(InputType.CONTROLLER_UP, 1), this,
            goManager));
    new MenuItem("Down:", 0.5f, new Vector3f(0.0f, -20.0f, -10.0f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
        Action.NONE, false, false, this, goManager);
    keyboardItems.get(0).add(
        new KeyboardSelectionItem(0.5f, new Vector3f(0.0f, -20.0f, -6.0f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
            Action.NONE, false, false, 0, new InputSelectionItemMediator(InputType.KEYBOARD_DOWN, 0), this,
            goManager));
    keyboardItems.get(1).add(
        new KeyboardSelectionItem(0.5f, new Vector3f(0.0f, -20.0f, 2.5f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
            Action.NONE, false, false, 1, new InputSelectionItemMediator(InputType.KEYBOARD_DOWN, 1), this,
            goManager));
    mouseItems.get(0).add(
        new MenuItem("Mouse Down", 0.5f, new Vector3f(0.0f, -20.0f, -6.0f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
            Action.NONE, false, false, false, this, goManager));
    mouseItems.get(1).add(
        new MenuItem("Mouse Down", 0.5f, new Vector3f(0.0f, -20.0f, 2.5f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
            Action.NONE, false, false, false, this, goManager));
    controllerItems.get(0).add(
        new ControllerSelectionItem(0.5f, new Vector3f(0.0f, -20.0f, -6.0f), -90.0f, -90.0f, 0.0f,
            Place.BOTTOM, Action.NONE, false, false, 0, new InputSelectionItemMediator(
                InputType.CONTROLLER_DOWN, 0), this, goManager));
    controllerItems.get(1).add(
        new ControllerSelectionItem(0.5f, new Vector3f(0.0f, -20.0f, 2.5f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
            Action.NONE, false, false, 1, new InputSelectionItemMediator(InputType.CONTROLLER_DOWN, 1),
            this, goManager));
    new MenuItem("Left:", 0.5f, new Vector3f(-1.5f, -20.0f, -10.0f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
        Action.NONE, false, false, this, goManager);
    keyboardItems.get(0).add(
        new KeyboardSelectionItem(0.5f, new Vector3f(-1.5f, -20.0f, -6.0f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
            Action.NONE, false, false, 0, new InputSelectionItemMediator(InputType.KEYBOARD_LEFT, 0), this,
            goManager));
    keyboardItems.get(1).add(
        new KeyboardSelectionItem(0.5f, new Vector3f(-1.5f, -20.0f, 2.5f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
            Action.NONE, false, false, 1, new InputSelectionItemMediator(InputType.KEYBOARD_LEFT, 1), this,
            goManager));
    mouseItems.get(0).add(
        new MenuItem("Mouse Left", 0.5f, new Vector3f(-1.5f, -20.0f, -6.0f), -90.0f, -90.0f, 0.0f,
            Place.BOTTOM, Action.NONE, false, false, false, this, goManager));
    mouseItems.get(1).add(
        new MenuItem("Mouse Left", 0.5f, new Vector3f(-1.5f, -20.0f, 2.5f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
            Action.NONE, false, false, false, this, goManager));
    controllerItems.get(0).add(
        new ControllerSelectionItem(0.5f, new Vector3f(-1.5f, -20.0f, -6.0f), -90.0f, -90.0f, 0.0f,
            Place.BOTTOM, Action.NONE, false, false, 0, new InputSelectionItemMediator(
                InputType.CONTROLLER_LEFT, 0), this, goManager));
    controllerItems.get(1).add(
        new ControllerSelectionItem(0.5f, new Vector3f(-1.5f, -20.0f, 2.5f), -90.0f, -90.0f, 0.0f,
            Place.BOTTOM, Action.NONE, false, false, 1, new InputSelectionItemMediator(
                InputType.CONTROLLER_LEFT, 1), this, goManager));
    new MenuItem("Right:", 0.5f, new Vector3f(-3.0f, -20.0f, -10.0f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
        Action.NONE, false, false, this, goManager);
    keyboardItems.get(0).add(
        new KeyboardSelectionItem(0.5f, new Vector3f(-3.0f, -20.0f, -6.0f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
            Action.NONE, false, false, 0, new InputSelectionItemMediator(InputType.KEYBOARD_RIGHT, 0),
            this, goManager));
    keyboardItems.get(1).add(
        new KeyboardSelectionItem(0.5f, new Vector3f(-3.0f, -20.0f, 2.5f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
            Action.NONE, false, false, 1, new InputSelectionItemMediator(InputType.KEYBOARD_RIGHT, 1),
            this, goManager));
    mouseItems.get(0).add(
        new MenuItem("Mouse Right", 0.5f, new Vector3f(-3.0f, -20.0f, -6.0f), -90.0f, -90.0f, 0.0f,
            Place.BOTTOM, Action.NONE, false, false, false, this, goManager));
    mouseItems.get(1).add(
        new MenuItem("Mouse Right", 0.5f, new Vector3f(-3.0f, -20.0f, 2.5f), -90.0f, -90.0f, 0.0f,
            Place.BOTTOM, Action.NONE, false, false, false, this, goManager));
    controllerItems.get(0).add(
        new ControllerSelectionItem(0.5f, new Vector3f(-3.0f, -20.0f, -6.0f), -90.0f, -90.0f, 0.0f,
            Place.BOTTOM, Action.NONE, false, false, 0, new InputSelectionItemMediator(
                InputType.CONTROLLER_RIGHT, 0), this, goManager));
    controllerItems.get(1).add(
        new ControllerSelectionItem(0.5f, new Vector3f(-3.0f, -20.0f, 2.5f), -90.0f, -90.0f, 0.0f,
            Place.BOTTOM, Action.NONE, false, false, 1, new InputSelectionItemMediator(
                InputType.CONTROLLER_RIGHT, 1), this, goManager));
    new MenuItem("Fire:", 0.5f, new Vector3f(-4.5f, -20.0f, -10.0f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
        Action.NONE, false, false, this, goManager);
    keyboardItems.get(0).add(
        new KeyboardSelectionItem(0.5f, new Vector3f(-4.5f, -20.0f, -6.0f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
            Action.NONE, false, false, 0, new InputSelectionItemMediator(InputType.KEYBOARD_FIRE, 0), this,
            goManager));
    keyboardItems.get(1).add(
        new KeyboardSelectionItem(0.5f, new Vector3f(-4.5f, -20.0f, 2.5f), -90.0f, -90.0f, 0.0f, Place.BOTTOM,
            Action.NONE, false, false, 1, new InputSelectionItemMediator(InputType.KEYBOARD_FIRE, 1), this,
            goManager));
    mouseItems.get(0).add(
        new MenuItem("Mouse Button 1", 0.5f, new Vector3f(-4.5f, -20.0f, -6.0f), -90.0f, -90.0f, 0.0f,
            Place.BOTTOM, Action.NONE, false, false, false, this, goManager));
    mouseItems.get(1).add(
        new MenuItem("Mouse Button 1", 0.5f, new Vector3f(-4.5f, -20.0f, 2.5f), -90.0f, -90.0f, 0.0f,
            Place.BOTTOM, Action.NONE, false, false, false, this, goManager));
    controllerItems.get(0).add(
        new ControllerSelectionItem(0.5f, new Vector3f(-4.5f, -20.0f, -6.0f), -90.0f, -90.0f, 0.0f,
            Place.BOTTOM, Action.NONE, false, false, 0, new InputSelectionItemMediator(
                InputType.CONTROLLER_FIRE, 0), this, goManager));
    controllerItems.get(1).add(
        new ControllerSelectionItem(0.5f, new Vector3f(-4.5f, -20.0f, 2.5f), -90.0f, -90.0f, 0.0f,
            Place.BOTTOM, Action.NONE, false, false, 1, new InputSelectionItemMediator(
                InputType.CONTROLLER_FIRE, 1), this, goManager));
    menuItems.add(new MenuItem("Apply", 0.5f, new Vector3f(-6.5f, -20.0f, -10.0f), -90.0f, -90.0f, 0.0f,
        Place.BOTTOM, Action.APPLY_CONTROLS, false, false, this, goManager));
    menuItems.add(new MenuItem("Discard", 0.5f, new Vector3f(-6.5f, -20.0f, 6.5f), -90.0f, -90.0f, 0.0f,
        Place.BOTTOM, Action.DISCARD_CONTROLS, false, false, this, goManager));
    menuItems.add(new MenuItem("Restore Defaults", 0.5f, new Vector3f(-6.5f, -20.0f, 0.0f), -90.0f, -90.0f, 0.0f,
        Place.BOTTOM, Action.RESTORE_CONTROLS, true, false, this, goManager));
    controlsItems.addAll(keyboardItems.get(0));
    controlsItems.addAll(keyboardItems.get(1));
    controlsItems.addAll(mouseItems.get(0));
    controlsItems.addAll(mouseItems.get(1));
    controlsItems.addAll(controllerItems.get(0));
    controlsItems.addAll(controllerItems.get(1));
    menuItems.addAll(controlsItems);

    for (int player = 0; player < 2; ++player) {
      switch (settings.getInputDeviceType(player)) {
      case KEYBOARD:
        showKeyboardSettings(player);
        break;

      case MOUSE:
        showMouseSettings(player);
        break;

      case CONTROLLER:
        showControllerSettings(player);
        break;
      }
    }
  }

  @Override
  public void init() {
    Lighting.init();
    Camera.init(new Vector3f(0.0f, 0.0f, posZ));

    cursor = new Cursor(new Color(0.4f, 0.5f, 1.0f), new Color(0.7f, 0.6f, 1.0f), 0.0f, 4.5f, -19.5f, 0.5f,
        goManager);
  }

  @Override
  public void update(float elapsedTime) {
    if (waitForInput != null) {
      // consume all mouse events to prevent them from firing after
      // waiting
      while (Mouse.next()) {
      }

      int player = waitForInput.getPlayer();
      int selectedDevice = inputDeviceSpinner[player].getValue();
      if (selectedDevice > 1)
        waitForInput.controllerEvent(inputManager.getNextControllerEvent(selectedDevice - 2));
    } else {
      // do not multiply by elaplsedTime, because getMouseMovement returns
      // position difference from last frame
      cursor.translate(mouseSpeed * inputManager.getMouseMovementX(),
          mouseSpeed * inputManager.getMouseMovementY());

      // rotate camera
      switch (rotating) {
      case LEFT:
        if (rotY < destRotY)
          rotY += getRotationSpeed(destRotY - rotY) * elapsedTime;
        if (rotY >= destRotY) {
          rotY = destRotY;
          rotating = RotationDirection.NONE;
        }
        Camera.setRotation(rotX, rotY, 0.0f);
        cursor.setRotation(rotX, rotY);
        break;

      case RIGHT:
        if (rotY > destRotY)
          rotY -= getRotationSpeed(rotY - destRotY) * elapsedTime;
        if (rotY <= destRotY) {
          rotY = destRotY;
          rotating = RotationDirection.NONE;
        }
        Camera.setRotation(rotX, rotY, 0.0f);
        cursor.setRotation(rotX, rotY);
        break;

      case UP:
        if (rotX < destRotX)
          rotX += getRotationSpeed(destRotX - rotX) * elapsedTime;
        if (rotX >= destRotX) {
          rotX = destRotX;
          rotating = RotationDirection.NONE;
        }
        Camera.setRotation(rotX, rotY, 0.0f);
        cursor.setRotation(rotX, rotY);
        break;

      case DOWN:
        if (rotX > destRotX)
          rotX -= getRotationSpeed(rotX - destRotX) * elapsedTime;
        if (rotX <= destRotX) {
          rotX = destRotX;
          rotating = RotationDirection.NONE;
        }
        Camera.setRotation(rotX, rotY, 0.0f);
        cursor.setRotation(rotX, rotY);
        break;

      case NONE:
        break;
      }

      switch (flying) {
      case IN:
        if (posZ > destPosZ) {
          posZ -= getFlySpeed(posZ - destPosZ, currentMenu == Menu.NEW_GAME ? 50.0f : 400.0f,
              currentMenu == Menu.MAIN) * elapsedTime;
        }
        if (posZ <= destPosZ) {
          posZ = destPosZ;
          flying = FlyingDirection.NONE;

          switch (currentMenu) {
          case NEW_GAME:
            flyOut(Menu.MAIN, 50.0f);
            getListener().changeState(nextGameState);
            return;

          default:
            break;
          }
        }
        Camera.setPosition(new Vector3f(0.0f, 0.0f, posZ));
        break;

      case OUT:
        if (posZ < destPosZ) {
          switch (currentMenu) {
          case CREDITS:
            posZ += getFlySpeed(destPosZ - posZ, 200.0f, false) * elapsedTime;
            break;

          case MAIN:
            posZ += getFlySpeed(destPosZ - posZ, 100.0f, true) * elapsedTime;
            break;

          default:
            posZ += getFlySpeed(destPosZ - posZ, 400.0f, false) * elapsedTime;
            break;
          }
        }
        if (posZ >= destPosZ) {
          posZ = destPosZ;
          flying = FlyingDirection.NONE;

          switch (currentMenu) {
          case CREDITS:
            flyIn(Menu.MAIN, 200.0f);
            getListener().changeState(new CreditsState(getListener(), this));
            return;

          case EXIT:
            getListener().exit();
            break;

          default:
            break;
          }
        }

        Camera.setPosition(new Vector3f(0.0f, 0.0f, posZ));
        break;

      case NONE:
        break;
      }

      // find item under mouse cursor
      MenuItem hoveredItem = null;

      if (rotating == RotationDirection.NONE && flying == FlyingDirection.NONE) {
        for (MenuItem item : menuItems)
          if (item.isInBoundingBox(cursor, 0.1f))
            hoveredItem = item;
      }

      if (hoveredItem != null) {
        if (hoveredItem.isApply())
          cursor.setState(Cursor.State.APPLY_HOVER);
        else if (hoveredItem.isDiscard())
          cursor.setState(Cursor.State.DISCARD_HOVER);
        else
          cursor.setState(Cursor.State.HOVER);
      } else
        cursor.setState(Cursor.State.NORMAL);

      while (Mouse.next()) {
        if (Mouse.getEventButtonState() && Mouse.getEventButton() == 0)
          if (hoveredItem != null)
            hoveredItem.performAction();
      }
    }
  }

  @Override
  public void render() {
    // clear screen
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);

    // update camera position
    GL11.glLoadIdentity();
    Camera.applyTransformation();

    // draw objects
    goManager.drawScene();
  }

  @Override
  public void cleanup() {
    goManager.removeWorldObject(cursor);
  }

  @Override
  public void keyDown(int key) {
    if (waitForInput != null) {
      waitForInput.keyDown(key);
    } else if (key == Keyboard.KEY_ESCAPE && rotating == RotationDirection.NONE && flying == FlyingDirection.NONE) {
      switch (currentMenu) {
      case MAIN:
        exit();
        break;

      case SETTINGS:
        rotateLeft();
        break;

      case GAME:
        discardGame();
        break;

      case GRAPHICS:
        discardGraphics();
        break;

      case CONTROLS:
        discardControls();
        break;

      default:
        break;
      }
    }
  }

  private void updateMouseSpeed() {
    mouseSpeed = Settings.getInstance().getMenuMouseSpeed() * 0.005f;
  }

  /**
   * Calculates speed after travelling s with constant acceleration
   *
   * @param v_0
   *            initial speed
   * @param a
   *            acceleration
   * @param s
   *            distance travelled so far
   * @return current speed
   */
  private static float getSpeed(float v_0, float a, float s) {
    // we have v(t) = at + v_0, s(t) = 1/2 * at^2 + v_0*t
    // hence t(s) = (sqrt(v_0^2 + 2as) - v_0) / a
    // and v(s) = sqrt(v_0^2 + 2as)
    return (float) (Math.sqrt(v_0 * v_0 + 2.0f * a * s));
  }

  private static float getRotationSpeed(float dr) {
    // start symmetrically decelerating halfway
    if (dr < 45.0f)
      dr = 90.0f - dr;

    return getSpeed(25.0f, 1000.0f, 90.0f - dr);
  }

  public static float getFlySpeed(float dz, float dist, boolean brake) {
    if (brake) {
      // start symmetrically decelerating halfway
      if (dz < dist / 2.0f)
        dz = dist - dz;
    }

    return getSpeed(25.0f, 4000.0f, dist - dz);
  }

  public void rotateLeft() {
    rotating = RotationDirection.LEFT;
    destRotY += 90.0f;

    if (currentMenu == Menu.SETTINGS)
      currentMenu = Menu.MAIN;
    else if (currentMenu == Menu.GRAPHICS)
      currentMenu = Menu.SETTINGS;
  }

  public void rotateRight() {
    rotating = RotationDirection.RIGHT;
    destRotY -= 90.0f;

    if (currentMenu == Menu.MAIN)
      currentMenu = Menu.SETTINGS;
    else if (currentMenu == Menu.SETTINGS)
      currentMenu = Menu.GRAPHICS;
  }

  public void rotateUp() {
    rotating = RotationDirection.UP;
    destRotX += 90.0f;

    if (currentMenu == Menu.SETTINGS)
      currentMenu = Menu.GAME;
    else if (currentMenu == Menu.CONTROLS)
      currentMenu = Menu.SETTINGS;
  }

  public void rotateDown() {
    rotating = RotationDirection.DOWN;
    destRotX -= 90.0f;

    if (currentMenu == Menu.GAME)
      currentMenu = Menu.SETTINGS;
    else if (currentMenu == Menu.SETTINGS)
      currentMenu = Menu.CONTROLS;
  }

  public void applyGame() {
    for (MenuItem item : gameItems)
      item.apply();

    rotateDown();
  }

  public void discardGame() {
    for (MenuItem item : gameItems)
      item.discard();

    rotateDown();
  }

  public void restoreGame() {
    for (MenuItem item : gameItems)
      item.restoreDefault();
  }

  public void applyGraphics() {
    for (MenuItem item : graphicsItems)
      item.apply();

    cleanup();
    TextureManager.getInstance().clear();
    ShaderManager.getInstance().clear();

    Display.destroy();

    PonkOut.initDisplay();
    PonkOut.initGL();

    MenuGraphicsObjectsManager.init();
    GameGraphicsObjectsManager.init();
    Font.init();
    Skybox.init();
    Lighting.init();

    cursor = new Cursor(new Color(0.4f, 0.5f, 1.0f), new Color(0.7f, 0.6f, 1.0f), -8.0f, -6.5f, -19.5f, 0.5f,
        goManager);

    rotateLeft();
  }

  public void discardGraphics() {
    for (MenuItem item : graphicsItems)
      item.discard();

    rotateLeft();
  }

  public void restoreGraphics() {
    for (MenuItem item : graphicsItems)
      item.restoreDefault();
  }

  public void applyControls() {
    for (MenuItem item : controlsItems)
      item.apply();

    updateMouseSpeed();

    rotateUp();
  }

  public void discardControls() {
    for (MenuItem item : controlsItems)
      item.discard();

    rotateUp();
  }

  public void restoreControls() {
    for (MenuItem item : controlsItems)
      item.restoreDefault();
  }

  public void startGame() {
    nextGameState = new GameState(getListener(), this);
    flyIn(Menu.NEW_GAME, 50.0f);
  }

  public void showCredits() {
    flyOut(Menu.CREDITS, 200.0f);
  }

  public void exit() {
    flyOut(Menu.EXIT, 400.0f);
  }

  private void flyOut(Menu newMenu, float dist) {
    flying = FlyingDirection.OUT;
    destPosZ += dist;
    currentMenu = newMenu;
  }

  private void flyIn(Menu newMenu, float dist) {
    flying = FlyingDirection.IN;
    destPosZ -= dist;
    currentMenu = newMenu;
  }

  public void showKeyboardSettings(int player) {
    for (MenuItem item : keyboardItems.get(player)) {
      item.setVisibility(true);
    }
    for (MenuItem item : mouseItems.get(player)) {
      item.setVisibility(false);
    }
    for (MenuItem item : controllerItems.get(player)) {
      item.setVisibility(false);
    }
  }

  public void showMouseSettings(int player) {
    for (MenuItem item : keyboardItems.get(player)) {
      item.setVisibility(false);
    }
    for (MenuItem item : mouseItems.get(player)) {
      item.setVisibility(true);
    }
    for (MenuItem item : controllerItems.get(player)) {
      item.setVisibility(false);
    }
  }

  public void showControllerSettings(int player) {
    for (MenuItem item : keyboardItems.get(player)) {
      item.setVisibility(false);
    }
    for (MenuItem item : mouseItems.get(player)) {
      item.setVisibility(false);
    }
    for (MenuItem item : controllerItems.get(player)) {
      item.setVisibility(true);
    }
  }

  public void waitForInput(InputSelectionItem item) {
    waitForInput = item;

    // clear all old events
    Controllers.clearEvents();

    // hide cursor while waiting for input
    cursor.setVisibility(item == null);
  }
}
TOP

Related Classes of ponkOut.Menu.MenuState

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.