Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIXED] Refactor MojamComponent, added MenuStack #849

Merged
merged 3 commits into from
Mar 17, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 36 additions & 74 deletions src/com/mojang/mojam/MojamComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import java.awt.Point;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
Expand Down Expand Up @@ -54,6 +52,7 @@
import com.mojang.mojam.gui.LevelEditorMenu;
import com.mojang.mojam.gui.LevelSelect;
import com.mojang.mojam.gui.LocaleMenu;
import com.mojang.mojam.gui.MenuStack;
import com.mojang.mojam.gui.OptionsMenu;
import com.mojang.mojam.gui.PauseMenu;
import com.mojang.mojam.gui.TitleMenu;
Expand Down Expand Up @@ -96,7 +95,7 @@
import com.mojang.mojam.sound.NoSoundPlayer;
import com.mojang.mojam.sound.SoundPlayer;

public class MojamComponent extends Canvas implements Runnable, MouseMotionListener, CommandListener, PacketListener, MouseListener, ButtonListener, KeyListener {
public class MojamComponent extends Canvas implements Runnable, MouseMotionListener, CommandListener, PacketListener, MouseListener, ButtonListener {

public static final String GAME_TITLE = "Catacomb Snatch";
public static final String GAME_VERSION = "1.0.0-SNAPSHOT";
Expand All @@ -122,7 +121,7 @@ public class MojamComponent extends Canvas implements Runnable, MouseMotionListe

private LatencyCache latencyCache = new LatencyCache();

private Stack<GuiMenu> menuStack = new Stack<GuiMenu>();
private MenuStack menuStack = new MenuStack();

private InputHandler inputHandler;
private int lastX = 0;
Expand Down Expand Up @@ -173,9 +172,10 @@ public MojamComponent() {
String localeString = Options.get(Options.LOCALE, "en");
setLocale(new Locale(localeString));

menuStack.setStackButtonListener(this);
menu = new TitleMenu(GAME_WIDTH, GAME_HEIGHT);
addMenu(menu);
addKeyListener(this);
menuStack.add(menu);
addKeyListener(menuStack);
addKeyListener(chat);
addKeyListener(console);

Expand All @@ -197,7 +197,7 @@ public void setLocale(Locale locale) {

@SuppressWarnings("unchecked")
public void notifyLocaleChange(){
Stack<GuiMenu> menuClone = (Stack<GuiMenu>) menuStack.clone();
MenuStack menuClone = (MenuStack) menuStack.clone();

while (!menuClone.isEmpty()) {
menuClone.pop().changeLocale();
Expand Down Expand Up @@ -272,7 +272,7 @@ public void stop(boolean exit) {
soundPlayer.shutdown();
System.exit(0);
} else {
addMenu(new ExitMenu(GAME_WIDTH, GAME_HEIGHT));
menuStack.add(new ExitMenu(GAME_WIDTH, GAME_HEIGHT));
}
}

Expand Down Expand Up @@ -305,14 +305,14 @@ private void initInput() {

private void initCharacters(){
if(!Options.isCharacterIDset()){
addMenu(new CharacterSelectionMenu());
menuStack.add(new CharacterSelectionMenu());
}
playerCharacter = GameCharacter.values()[Options.getCharacterID()];
}

public void showError(String s) {
handleAction(TitleMenu.RETURN_TO_TITLESCREEN);
addMenu(new GuiError(s));
menuStack.add(new GuiError(s));
}

private synchronized void createLevel(String levelPath, GameMode mode, GameCharacter character) {
Expand Down Expand Up @@ -655,7 +655,7 @@ private void tick() {
int winner = level.victoryConditions.playerVictorious();
GameCharacter winningCharacter = winner == players[0].getTeam() ? players[0].getCharacter()
: players[1].getCharacter();
addMenu(new WinMenu(GAME_WIDTH, GAME_HEIGHT, winner, winningCharacter));
menuStack.add(new WinMenu(GAME_WIDTH, GAME_HEIGHT, winner, winningCharacter));
level = null;
return;
}
Expand Down Expand Up @@ -756,7 +756,7 @@ private void tick() {

synchronizer = new TurnSynchronizer(MojamComponent.this, packetLink, localId, 2);

clearMenus();
menuStack.clear();
createLevel(TitleMenu.level, TitleMenu.defaultGameMode, playerCharacter);

synchronizer.setStarted(true);
Expand Down Expand Up @@ -876,9 +876,9 @@ public void handle(int playerId, NetworkCommand packet) {
PauseCommand pc = (PauseCommand) packet;
paused = pc.isPause();
if (paused) {
addMenu(new PauseMenu(GAME_WIDTH, GAME_HEIGHT));
menuStack.add(new PauseMenu(GAME_WIDTH, GAME_HEIGHT));
} else {
popMenu();
menuStack.safePop();
}
}
}
Expand Down Expand Up @@ -968,17 +968,17 @@ public void handleAction(int id) {
setLocale("af");
break;
case TitleMenu.RETURN_TO_TITLESCREEN:
clearMenus();
menuStack.clear();
level = null;
TitleMenu menu = new TitleMenu(GAME_WIDTH, GAME_HEIGHT);
addMenu(menu);
menuStack.add(menu);
this.nextMusicInterval = 0;
soundPlayer.stopBackgroundMusic();
soundPlayer.startTitleMusic();
break;

case TitleMenu.START_GAME_ID:
clearMenus();
menuStack.clear();
isMultiplayer = false;
chat.clear();

Expand All @@ -992,20 +992,20 @@ public void handleAction(int id) {
break;

case TitleMenu.SELECT_LEVEL_ID:
addMenu(new LevelSelect(false));
menuStack.add(new LevelSelect(false));
break;

case TitleMenu.SELECT_HOST_LEVEL_ID:
addMenu(new LevelSelect(true));
menuStack.add(new LevelSelect(true));
break;
/*
* case TitleMenu.UPDATE_LEVELS: GuiMenu menu = menuStack.pop(); if
* (menu instanceof LevelSelect) { addMenu(new
* LevelSelect(((LevelSelect) menu).bHosting)); } else { addMenu(new
* (menu instanceof LevelSelect) { menuStack.add(new
* LevelSelect(((LevelSelect) menu).bHosting)); } else { menuStack.add(new
* LevelSelect(false)); } }
*/
case TitleMenu.HOST_GAME_ID:
addMenu(new HostingWaitMenu());
menuStack.add(new HostingWaitMenu());
isMultiplayer = true;
isServer = true;
chat.clear();
Expand Down Expand Up @@ -1057,11 +1057,11 @@ public void run() {
break;

case TitleMenu.JOIN_GAME_ID:
addMenu(new JoinGameMenu());
menuStack.add(new JoinGameMenu());
break;

case TitleMenu.CANCEL_JOIN_ID:
popMenu();
menuStack.safePop();
if (hostThread != null) {
hostThread.interrupt();
hostThread = null;
Expand All @@ -1087,32 +1087,32 @@ public void run() {
} catch (Exception e) {
e.printStackTrace();
// System.exit(1);
addMenu(new TitleMenu(GAME_WIDTH, GAME_HEIGHT));
menuStack.add(new TitleMenu(GAME_WIDTH, GAME_HEIGHT));
}
break;

case TitleMenu.HOW_TO_PLAY:
addMenu(new HowToPlayMenu(level != null));
menuStack.add(new HowToPlayMenu(level != null));
break;

case TitleMenu.OPTIONS_ID:
addMenu(new OptionsMenu(level != null));
menuStack.add(new OptionsMenu(level != null));
break;

case TitleMenu.SELECT_DIFFICULTY_ID:
addMenu(new DifficultySelect(false));
menuStack.add(new DifficultySelect(false));
break;

case TitleMenu.SELECT_DIFFICULTY_HOSTING_ID:
addMenu(new DifficultySelect(true));
menuStack.add(new DifficultySelect(true));
break;

case TitleMenu.KEY_BINDINGS_ID:
addMenu(new KeyBindingsMenu(keys, inputHandler));
menuStack.add(new KeyBindingsMenu(keys, inputHandler));
break;

case TitleMenu.LEVEL_EDITOR_ID:
addMenu(new LevelEditorMenu());
menuStack.add(new LevelEditorMenu());
break;

case TitleMenu.EXIT_GAME_ID:
Expand All @@ -1129,66 +1129,28 @@ public void run() {
break;

case TitleMenu.BACK_ID:
popMenu();
menuStack.safePop();
break;

case TitleMenu.CREDITS_ID:
addMenu(new CreditsScreen(GAME_WIDTH, GAME_HEIGHT));
menuStack.add(new CreditsScreen(GAME_WIDTH, GAME_HEIGHT));
break;

case TitleMenu.CHARACTER_ID:
addMenu(new CharacterSelectionMenu());
menuStack.add(new CharacterSelectionMenu());
break;

case TitleMenu.AUDIO_VIDEO_ID:
addMenu(new AudioVideoMenu(level != null));
menuStack.add(new AudioVideoMenu(level != null));
break;

case TitleMenu.LOCALE_ID:
localemenu = new LocaleMenu(level != null);
addMenu(localemenu);
menuStack.add(localemenu);
break;
}
}

private void clearMenus() {
while (!menuStack.isEmpty()) {
menuStack.pop();
}
}

private void addMenu(GuiMenu menu) {
menuStack.add(menu);
menu.addButtonListener(this);
}

private void popMenu() {
if (!menuStack.isEmpty()) {
menuStack.pop();
}
}

@Override
public void keyPressed(KeyEvent e) {
if (!menuStack.isEmpty()) {
menuStack.peek().keyPressed(e);
}
}

@Override
public void keyReleased(KeyEvent e) {
if (!menuStack.isEmpty()) {
menuStack.peek().keyReleased(e);
}
}

@Override
public void keyTyped(KeyEvent e) {
if (!menuStack.isEmpty()) {
menuStack.peek().keyTyped(e);
}
}

public static File getMojamDir() {
if (mojamDir == null) {
mojamDir = getAppDir("mojam");
Expand Down
79 changes: 79 additions & 0 deletions src/com/mojang/mojam/gui/MenuStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.mojang.mojam.gui;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Stack;

import com.mojang.mojam.gui.components.ButtonListener;

public class MenuStack extends Stack<GuiMenu> implements KeyListener {

private static final long serialVersionUID = 1L;
private ButtonListener stackButtonListener;

/***
* Creates an empty MenuStack
*/
public MenuStack() {
super();
}

/***
* sets the ButtonListener that will be applied to all
* menus in the stack after this call.
* *WARNING* only use the add method when added menus
* to the stack, else they won't have this ButtonListener
* added.
*
* @param bl the ButtonListener to be added to all menus.
*/
public void setStackButtonListener(ButtonListener bl) {
stackButtonListener = bl;
}

/***
* adds a menu to the stack and automatically adds
* the stack button listener as a button listener
*
* @return true if it succeeded, false otherwise
*/
public boolean add(GuiMenu menu) {
try {
super.add(menu);
menu.addButtonListener(stackButtonListener);
} catch(Exception e) {
e.printStackTrace();
return false;
}
return true;
}

/***
* attempts to pop the top menu off the stack
*
* @return the menu that was popped or null otherwise
*/
public GuiMenu safePop() {
try {
return pop();
} catch(Exception e) {
e.printStackTrace();
return null;
}
}

public void keyPressed(KeyEvent e) {
if(!empty())
peek().keyPressed(e);
}

public void keyReleased(KeyEvent e) {
if(!empty())
peek().keyReleased(e);
}

public void keyTyped(KeyEvent e) {
if(!empty())
peek().keyTyped(e);
}
}