/* Copyright 2010, 2012-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 org.lwjgl.util.vector.Vector3f;
import ponkOut.graphics.Cursor;
import ponkOut.graphics.GraphicsObjectsManager;
import ponkOut.graphics.Material;
import ponkOut.graphics.WorldText;
public class MenuItem extends WorldText {
public enum Place {
LEFT, RIGHT, TOP, BOTTOM, FRONT, BACK
};
public enum Action {
NONE, ROTATE_LEFT, ROTATE_RIGHT, ROTATE_UP, ROTATE_DOWN, APPLY_GAME, APPLY_GRAPHICS, APPLY_CONTROLS, DISCARD_GAME, DISCARD_GRAPHICS, DISCARD_CONTROLS, RESTORE_GAME, RESTORE_GRAPHICS, RESTORE_CONTROLS, START_GAME, SHOW_CREDITS, EXIT
};
private MenuState menu;
private Vector3f position;
private Place place;
private Action action;
private boolean enabled;
private static Material enabledMaterial = new Material(0.4f, 0.4f, 0.7f, 0.5f, 0.55f, 0.7f, 0.1f, 0.1f, 0.2f, 1.0f,
5.0f);
private static Material disabledMaterial = new Material(0.4f, 0.4f, 0.4f, 0.5f, 0.55f, 0.6f, 0.1f, 0.1f, 0.2f,
0.7f, 5.0f);
public MenuItem(String string, float size, Vector3f position, float rotX, float rotY, float rotZ, Place place,
Action action, boolean hCenter, boolean vCenter, boolean enabled, MenuState menu,
GraphicsObjectsManager goManager) {
super(string, size, enabled ? enabledMaterial : disabledMaterial, position, rotX, rotY, rotZ, goManager);
this.menu = menu;
this.position = position;
this.place = place;
this.action = action;
this.enabled = enabled;
if (hCenter)
centerH();
if (vCenter)
centerV();
}
public MenuItem(String string, float size, Vector3f position, float rotX, float rotY, float rotZ, Place place,
Action action, boolean hCenter, boolean vCenter, MenuState menu, GraphicsObjectsManager goManager) {
this(string, size, position, rotX, rotY, rotZ, place, action, hCenter, vCenter, true, menu, goManager);
}
public void translateInTextDir(float d) {
switch (place) {
case LEFT:
translate(0.0f, 0.0f, -d);
break;
case RIGHT:
translate(0.0f, 0.0f, d);
break;
case TOP:
translate(0.0f, 0.0f, d);
break;
case BOTTOM:
translate(0.0f, 0.0f, d);
break;
case FRONT:
translate(d, 0.0f, 0.0f);
break;
case BACK:
translate(-d, 0.0f, 0.0f);
break;
}
}
public void centerH() {
// undo previous translations in text-direction
Vector3f p = getPosition();
if (place == Place.FRONT || place == Place.BACK)
p.x = position.x;
else
p.z = position.z;
setPosition(p);
// centre
translateInTextDir(-0.5f * getWidth());
}
public void centerV() {
// undo previous translations in vertical direction
Vector3f p = getPosition();
if (place == Place.TOP || place == Place.BOTTOM)
p.x = position.x;
else
p.y = position.y;
setPosition(p);
switch (place) {
case LEFT:
translate(0.0f, 0.5f * getHeight(), 0.0f);
break;
case RIGHT:
translate(0.0f, 0.5f * getHeight(), 0.0f);
break;
case TOP:
translate(-0.5f * getHeight(), 0.0f, 0.0f);
break;
case BOTTOM:
translate(0.5f * getHeight(), 0.0f, 0.0f);
break;
case FRONT:
translate(0.0f, 0.5f * getHeight(), 0.0f);
break;
case BACK:
translate(0.0f, 0.5f * getHeight(), 0.0f);
break;
}
}
/**
* @param cursor
* cursor
* @param eps
* positive value
* @return whether distance of cursor from text is less than eps if enabled,
* false if disabled
*/
public boolean isInBoundingBox(Cursor cursor, float eps) {
if (!isEnabled() || !isVisible())
return false;
Vector3f position = getPosition();
float left = 0, right = 0, top = 0, bottom = 0, front = 0, back = 0;
switch (place) {
case LEFT:
left = position.x - eps;
right = position.x + eps;
top = position.y + eps;
bottom = position.y - getHeight() - eps;
front = position.z + eps;
back = position.z - getWidth() - eps;
break;
case RIGHT:
left = position.x - eps;
right = position.x + eps;
top = position.y + eps;
bottom = position.y - getHeight() - eps;
front = position.z + getWidth() + eps;
back = position.z - eps;
break;
case TOP:
left = position.x - eps;
right = position.x + getHeight() + eps;
top = position.y + eps;
bottom = position.y - eps;
front = position.z + getWidth() + eps;
back = position.z - eps;
break;
case BOTTOM:
left = position.x - getHeight() - eps;
right = position.x + eps;
top = position.y + eps;
bottom = position.y - eps;
front = position.z + getWidth() + eps;
back = position.z - eps;
break;
case FRONT:
left = position.x - eps;
right = position.x + getWidth() + eps;
top = position.y + eps;
bottom = position.y - getHeight() - eps;
front = position.z + eps;
back = position.z - eps;
break;
case BACK:
left = position.x - getWidth() - eps;
right = position.x + eps;
top = position.y + eps;
bottom = position.y - getHeight() - eps;
front = position.z + eps;
back = position.z - eps;
break;
}
Vector3f cp = cursor.getTextPosition();
return (cp.x > left && cp.x < right && cp.y > bottom && cp.y < top && cp.z < front && cp.z > back);
}
public void performAction() {
switch (action) {
case ROTATE_LEFT:
menu.rotateLeft();
break;
case ROTATE_RIGHT:
menu.rotateRight();
break;
case ROTATE_UP:
menu.rotateUp();
break;
case ROTATE_DOWN:
menu.rotateDown();
break;
case APPLY_GAME:
menu.applyGame();
break;
case DISCARD_GAME:
menu.discardGame();
break;
case RESTORE_GAME:
menu.restoreGame();
break;
case APPLY_GRAPHICS:
menu.applyGraphics();
break;
case DISCARD_GRAPHICS:
menu.discardGraphics();
break;
case RESTORE_GRAPHICS:
menu.restoreGraphics();
break;
case APPLY_CONTROLS:
menu.applyControls();
break;
case DISCARD_CONTROLS:
menu.discardControls();
break;
case RESTORE_CONTROLS:
menu.restoreControls();
break;
case START_GAME:
menu.startGame();
break;
case SHOW_CREDITS:
menu.showCredits();
break;
case EXIT:
menu.exit();
break;
case NONE:
break;
}
}
public boolean isApply() {
switch (action) {
case APPLY_GAME:
case APPLY_GRAPHICS:
case APPLY_CONTROLS:
return true;
default:
return false;
}
}
public boolean isDiscard() {
switch (action) {
case DISCARD_GAME:
case DISCARD_GRAPHICS:
case DISCARD_CONTROLS:
case EXIT:
return true;
default:
return false;
}
}
public void apply() {
}
public void discard() {
}
public void restoreDefault() {
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
appearance = enabled ? enabledMaterial : disabledMaterial;
}
public boolean isEnabled() {
return enabled;
}
}