Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

[FIXED] Refactor MojamComponent, added MenuStack #849

Merged
merged 3 commits into from

4 participants

@kylecbrodie

I fixed my previous error of putting add instead of super.add (StackOverflowException)
I tested it and there are no exceptions generated.
Please test it yourself (ves) and let me know before you pull it

@master-lincoln
Collaborator

There is also some more work going on by fierydrake in this area. I propose to leave this open for a while...

kylecbrodie added some commits
@kylecbrodie kylecbrodie [FIXED] Refactor MojamComponent, added MenuStack
I fixed my previous error of putting add instead of super.add (StackOverflowException)
I tested it and there are no exceptions generated
c068b3a
@kylecbrodie kylecbrodie cast correction c0e7581
@kylecbrodie kylecbrodie corrected menuStack not getting keys 8d4e571
@kylecbrodie

Me gusta MenuStack

@Maescool
Owner

any objections?
@fierydrake ?
@Flet
@master-lincoln

@fierydrake

Don't hold up changes on my account. It's only my fault if my merge is painful.

@Maescool Maescool merged commit ac23ea5 into Maescool:develop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 11, 2012
  1. @kylecbrodie

    [FIXED] Refactor MojamComponent, added MenuStack

    kylecbrodie authored
    I fixed my previous error of putting add instead of super.add (StackOverflowException)
    I tested it and there are no exceptions generated
  2. @kylecbrodie

    cast correction

    kylecbrodie authored
  3. @kylecbrodie
This page is out of date. Refresh to see the latest.
View
110 src/com/mojang/mojam/MojamComponent.java
@@ -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;
@@ -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;
@@ -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";
@@ -122,7 +121,7 @@
private LatencyCache latencyCache = new LatencyCache();
- private Stack<GuiMenu> menuStack = new Stack<GuiMenu>();
+ private MenuStack menuStack = new MenuStack();
private InputHandler inputHandler;
private int lastX = 0;
@@ -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);
@@ -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();
@@ -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));
}
}
@@ -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) {
@@ -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;
}
@@ -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);
@@ -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();
}
}
}
@@ -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();
@@ -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();
@@ -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;
@@ -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:
@@ -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");
View
79 src/com/mojang/mojam/gui/MenuStack.java
@@ -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);
+ }
+}
Something went wrong with that request. Please try again.