From 50928c2d12bf145711df877bb86aadfb1306860b Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Sun, 8 Feb 2009 17:12:38 -0500 Subject: Add choice choosing to ChoiceWindow ChoiceWindow has also been modified around to rely on a Window class which makes the process more elegant (not elegant, just more so than before). TitleScreenGameState has also been modified to close the game when "End" is selected. --- .../fourpuzzle/gamestate/TitleScreenGameState.java | 72 +++++++++---- src/com/fourisland/fourpuzzle/util/PauseTimer.java | 41 ++++++++ .../fourisland/fourpuzzle/window/ChoiceWindow.java | 68 ++++++------ .../fourpuzzle/window/SystemChoiceArea.java | 56 ---------- .../fourpuzzle/window/SystemGraphic.java | 10 +- src/com/fourisland/fourpuzzle/window/Window.java | 116 +++++++++++++++++++++ 6 files changed, 253 insertions(+), 110 deletions(-) create mode 100644 src/com/fourisland/fourpuzzle/util/PauseTimer.java delete mode 100755 src/com/fourisland/fourpuzzle/window/SystemChoiceArea.java create mode 100644 src/com/fourisland/fourpuzzle/window/Window.java (limited to 'src/com') diff --git a/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java b/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java index 2f5dc97..998d33a 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java @@ -10,6 +10,7 @@ import com.fourisland.fourpuzzle.gamestate.mapview.MapViewGameState; import com.fourisland.fourpuzzle.transition.SquareTransition; import com.fourisland.fourpuzzle.transition.TransitionDirection; import com.fourisland.fourpuzzle.util.ObjectLoader; +import com.fourisland.fourpuzzle.util.PauseTimer; import com.fourisland.fourpuzzle.window.ChoiceWindow; import java.awt.Graphics2D; import java.awt.event.KeyEvent; @@ -38,29 +39,62 @@ public class TitleScreenGameState implements GameState { Audio.stopMusic(); } + PauseTimer pt = new PauseTimer(0); public void processInput() { - if (Game.getKey().getKeyCode() == KeyEvent.VK_ENTER) + if (pt.isElapsed()) { - Game.setSaveFile(new SaveFile()); - - new Thread(new Runnable() { - public void run() { - try { - Display.transition(new SquareTransition(TransitionDirection.Out)); - } catch (InterruptedException ex) { - Thread.currentThread().interrupt(); - } - - Game.setGameState(new MapViewGameState("TestMap", 1, 2)); - - try { - Display.transition(new SquareTransition(TransitionDirection.In)); - } catch (InterruptedException ex) { - Thread.currentThread().interrupt(); - } + if (Game.getKey().getKeyCode() == KeyEvent.VK_ENTER) + { + if (choices.getSelected().equals(Database.getVocab("NewGame"))) + { + Game.setSaveFile(new SaveFile()); + + new Thread(new Runnable() { + public void run() { + try { + Display.transition(new SquareTransition(TransitionDirection.Out)); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + + Game.setGameState(new MapViewGameState("TestMap", 1, 2)); + + try { + Display.transition(new SquareTransition(TransitionDirection.In)); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + } + }).start(); + } else if (choices.getSelected().equals(Database.getVocab("LoadGame"))) + { + // Do nothing, yet + } else if (choices.getSelected().equals(Database.getVocab("EndGame"))) + { + new Thread(new Runnable() { + public void run() { + try { + Display.transition(new SquareTransition(TransitionDirection.Out)); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + + System.exit(0); + } + }).start(); } - }).start(); + } else if (Game.getKey().getKeyCode() == KeyEvent.VK_UP) + { + choices.moveUp(); + + pt.setTimer(1); + } else if (Game.getKey().getKeyCode() == KeyEvent.VK_DOWN) + { + choices.moveDown(); + + pt.setTimer(1); + } } } diff --git a/src/com/fourisland/fourpuzzle/util/PauseTimer.java b/src/com/fourisland/fourpuzzle/util/PauseTimer.java new file mode 100644 index 0000000..0e52b0f --- /dev/null +++ b/src/com/fourisland/fourpuzzle/util/PauseTimer.java @@ -0,0 +1,41 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.fourpuzzle.util; + +/** + * + * @author hatkirby + */ +public class PauseTimer { + + private int ticks; + public PauseTimer(int ticks) + { + this.ticks = ticks; + } + + Interval in = Interval.createTickInterval(1); + public boolean isElapsed() + { + if (in.isElapsed()) + { + if (ticks == 0) + { + return true; + } else { + ticks--; + } + } + + return false; + } + + public void setTimer(int ticks) + { + this.ticks = ticks; + } + +} diff --git a/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java b/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java index 40fff30..3b30acc 100755 --- a/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java +++ b/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java @@ -5,18 +5,13 @@ package com.fourisland.fourpuzzle.window; -import com.fourisland.fourpuzzle.util.TransparentPixelFilter; +import com.fourisland.fourpuzzle.Audio; import java.awt.Font; -import java.awt.Toolkit; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.TexturePaint; import java.awt.image.BufferedImage; -import java.awt.image.FilteredImageSource; import java.util.List; -import static com.fourisland.fourpuzzle.window.SystemChoiceArea.*; - -/* TODO Find a more elegant way to implement window, it looks terrible now */ /** * @@ -35,7 +30,7 @@ public class ChoiceWindow { numChoices = choices.size(); this.center = center; - createGraphic(new BufferedImage(TopLeft.getWidth()+getWidth()+TopRight.getWidth(), TopLeft.getHeight()+getHeight()+BottomLeft.getHeight(), BufferedImage.TYPE_INT_ARGB).createGraphics()); + createGraphic(new BufferedImage(Window.Default.getFullWidth(width), Window.Default.getFullHeight(height), BufferedImage.TYPE_INT_ARGB).createGraphics()); } private int width; @@ -57,36 +52,38 @@ public class ChoiceWindow { width += SPACER*2; height -= SPACER; - BufferedImage temp = new BufferedImage(TopLeft.getWidth()+getWidth()+TopRight.getWidth(), TopLeft.getHeight()+getHeight()+BottomLeft.getHeight(), BufferedImage.TYPE_INT_ARGB); - Graphics2D g = temp.createGraphics(); - - g.drawImage(SystemGraphic.getChoiceArea(TopLeft), 0, 0, null); - g.drawImage(SystemGraphic.getChoiceArea(Top), TopLeft.getWidth(), 0, getWidth(),Top.getHeight(), null); - g.drawImage(SystemGraphic.getChoiceArea(TopRight), TopLeft.getWidth()+getWidth(), 0, null); - g.drawImage(SystemGraphic.getChoiceArea(Left), 0, TopLeft.getHeight(), Left.getWidth(),getHeight(), null); - g.drawImage(SystemGraphic.getChoiceArea(BottomLeft), 0, TopLeft.getHeight()+getHeight(), null); - g.drawImage(SystemGraphic.getChoiceArea(Bottom), BottomLeft.getWidth(), (getHeight()+TopLeft.getHeight()+BottomLeft.getHeight())-Bottom.getHeight(), getWidth(),Bottom.getHeight(), null); - g.drawImage(SystemGraphic.getChoiceArea(BottomRight), BottomRight.getWidth()+getWidth(), TopRight.getHeight()+getHeight(), null); - g.drawImage(SystemGraphic.getChoiceArea(Right), (getWidth()+TopLeft.getWidth()+TopRight.getWidth())-Right.getWidth(), TopRight.getHeight(), Right.getWidth(),getHeight(), null); - - cacheBase = new BufferedImage(TopLeft.getWidth()+getWidth()+TopRight.getWidth(), TopLeft.getHeight()+getHeight()+BottomLeft.getHeight(), BufferedImage.TYPE_INT_ARGB); + cacheBase = new BufferedImage(Window.Default.getFullWidth(width), Window.Default.getFullHeight(height), BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = cacheBase.createGraphics(); - g2.drawImage(SystemGraphic.getMessageBackground(), 1, 1, TopLeft.getWidth()+getWidth()+TopRight.getWidth()-2, TopLeft.getHeight()+getHeight()+BottomLeft.getHeight()-2, null); - g2.drawImage(Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(temp.getSource(), new TransparentPixelFilter(SystemGraphic.getTransparentColor().getRGB()))), 0, 0, null); + g2.drawImage(SystemGraphic.getMessageBackground(), 1, 1, Window.Default.getFullWidth(width)-2, Window.Default.getFullHeight(height)-2, null); + g2.drawImage(Window.Default.getImage(width, height), 0, 0, null); + } + + public void render(Graphics2D g2, int x, int y) + { + g2.drawImage(cacheBase, x, y, null); + g2.setFont(g2.getFont().deriveFont(Font.BOLD)); - int ty = TopLeft.getHeight()+g2.getFontMetrics().getHeight()-SPACER; + int fh = g2.getFontMetrics().getHeight(); + int ty = Window.Default.getTopY()+fh-SPACER+y; for (String choice : choices) { - int tx = TopLeft.getWidth(); + int fw = g2.getFontMetrics().stringWidth(choice); + int tx = Window.Default.getLeftX()+x; if (center) { - tx += ((width/2)-(g2.getFontMetrics().stringWidth(choice)/2)); + tx += ((width/2)-(fw/2)); + } + + if (getSelected().equals(choice)) + { + g2.drawImage(SystemGraphic.getSelectionBackground(), tx, ty-fh, fw+SPACER-2, fh+SPACER-2, null); + g2.drawImage(Window.Selector.getImage(fw-Window.Selector.getLeftX(), fh-Window.Selector.getTopY()), tx-SPACER, ty-fh, null); } - g2.setPaint(new TexturePaint(SystemGraphic.getTextColor(), new Rectangle(tx, ty, g2.getFontMetrics().stringWidth(choice),g2.getFontMetrics().getHeight()))); + g2.setPaint(new TexturePaint(SystemGraphic.getTextColor(), new Rectangle(tx, ty, fw, fh))); g2.drawString(choice, tx, ty); ty+=(SPACER+g2.getFontMetrics().getHeight()); @@ -95,11 +92,6 @@ public class ChoiceWindow { g2.setFont(g2.getFont().deriveFont(Font.PLAIN)); } - public void render(Graphics2D g2, int x, int y) - { - g2.drawImage(cacheBase, x, y, null); - } - public int getWidth() { return width; @@ -113,12 +105,22 @@ public class ChoiceWindow { int selected = 0; public void moveUp() { - selected--; + if (selected > 0) + { + Audio.playSound("Cursor1"); + + selected--; + } } public void moveDown() { - selected++; + if (selected < (choices.size()-1)) + { + Audio.playSound("Cursor1"); + + selected++; + } } public String getSelected() diff --git a/src/com/fourisland/fourpuzzle/window/SystemChoiceArea.java b/src/com/fourisland/fourpuzzle/window/SystemChoiceArea.java deleted file mode 100755 index 5fc115f..0000000 --- a/src/com/fourisland/fourpuzzle/window/SystemChoiceArea.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ - -package com.fourisland.fourpuzzle.window; - -/** - * - * @author hatkirby - */ -public enum SystemChoiceArea -{ - Top(47, 0, 1, 9), - TopRight(56, 0, 8, 9), - Right(56, 15, 8, 1), - BottomRight(56, 25, 8, 9), - Bottom(47, 24, 1, 9), - BottomLeft(32, 24, 11, 9), - Left(32, 15, 11, 1), - TopLeft(32, 0, 8, 8), - UpArrow(43, 9, 10, 6), - DownArrow(43, 17, 10, 6); - - private final int x; - private final int y; - private final int width; - private final int height; - private SystemChoiceArea(int x, int y, int width, int height) - { - this.x = x; - this.y = y; - this.width = width; - this.height = height; - } - - public int getX() - { - return x; - } - - public int getY() - { - return y; - } - - public int getWidth() - { - return width; - } - - public int getHeight() - { - return height; - } -} \ No newline at end of file diff --git a/src/com/fourisland/fourpuzzle/window/SystemGraphic.java b/src/com/fourisland/fourpuzzle/window/SystemGraphic.java index ea67f41..24e0104 100755 --- a/src/com/fourisland/fourpuzzle/window/SystemGraphic.java +++ b/src/com/fourisland/fourpuzzle/window/SystemGraphic.java @@ -7,6 +7,7 @@ package com.fourisland.fourpuzzle.window; import com.fourisland.fourpuzzle.util.ObjectLoader; import java.awt.Color; +import java.awt.Rectangle; import java.awt.image.BufferedImage; /** @@ -32,9 +33,14 @@ public class SystemGraphic { return systemGraphic.getSubimage(0, 0, 32, 32); } - public static BufferedImage getChoiceArea(SystemChoiceArea sca) + public static BufferedImage getSelectionBackground() { - return systemGraphic.getSubimage(sca.getX(), sca.getY(), sca.getWidth(), sca.getHeight()); + return systemGraphic.getSubimage(79, 15, 1, 1); + } + + public static BufferedImage getChoiceArea(Rectangle sca) + { + return systemGraphic.getSubimage(sca.x, sca.y, sca.width, sca.height); } public static BufferedImage getTextColor() diff --git a/src/com/fourisland/fourpuzzle/window/Window.java b/src/com/fourisland/fourpuzzle/window/Window.java new file mode 100644 index 0000000..fa40cba --- /dev/null +++ b/src/com/fourisland/fourpuzzle/window/Window.java @@ -0,0 +1,116 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.fourpuzzle.window; + +import com.fourisland.fourpuzzle.util.TransparentPixelFilter; +import java.awt.Graphics2D; +import java.awt.Image; +import java.awt.Rectangle; +import java.awt.Toolkit; +import java.awt.image.BufferedImage; +import java.awt.image.FilteredImageSource; + +/** + * + * @author hatkirby + */ +public enum Window +{ + Default(32), + Selector(64); + + private enum SystemArea + { + TOP(15, 0, 1, 9), + TOP_RIGHT(24, 0, 8, 9), + RIGHT(24, 15, 8, 1), + BOTTOM_RIGHT(24, 25, 8, 9), + BOTTOM(15, 24, 1, 9), + BOTTOM_LEFT(0, 24, 11, 9), + LEFT(0, 15, 11, 1), + TOP_LEFT(0, 0, 8, 8); + + private final int x; + private final int y; + private final int width; + private final int height; + private SystemArea(int x, int y, int width, int height) + { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + } + } + + private int x; + private Window(int x) + { + this.x = x; + } + + private int getX(SystemArea sa) + { + return x+sa.x; + } + + private int getY(SystemArea sa) + { + return sa.y; + } + + private int getWidth(SystemArea sa) + { + return sa.width; + } + + private int getHeight(SystemArea sa) + { + return sa.height; + } + + private Rectangle getBounds(SystemArea sa) + { + return new Rectangle(getX(sa), getY(sa), getWidth(sa), getHeight(sa)); + } + + public int getFullWidth(int width) + { + return getWidth(SystemArea.TOP_LEFT) + getWidth(SystemArea.TOP_RIGHT) + width; + } + + public int getFullHeight(int height) + { + return getHeight(SystemArea.TOP_LEFT) + getHeight(SystemArea.BOTTOM_LEFT) + height; + } + + public int getLeftX() + { + return getWidth(SystemArea.TOP_LEFT); + } + + public int getTopY() + { + return getHeight(SystemArea.TOP_LEFT); + } + + public Image getImage(int width, int height) + { + BufferedImage temp = new BufferedImage(getFullWidth(width), getFullHeight(height), BufferedImage.TYPE_INT_ARGB); + Graphics2D g = temp.createGraphics(); + + g.drawImage(SystemGraphic.getChoiceArea(getBounds(SystemArea.TOP_LEFT)), 0, 0, null); + g.drawImage(SystemGraphic.getChoiceArea(getBounds(SystemArea.TOP)), getWidth(SystemArea.TOP_LEFT), 0, width, getHeight(SystemArea.TOP), null); + g.drawImage(SystemGraphic.getChoiceArea(getBounds(SystemArea.TOP_RIGHT)), getWidth(SystemArea.TOP_LEFT)+width, 0, null); + g.drawImage(SystemGraphic.getChoiceArea(getBounds(SystemArea.LEFT)), 0, getHeight(SystemArea.TOP_LEFT), getWidth(SystemArea.LEFT),height, null); + g.drawImage(SystemGraphic.getChoiceArea(getBounds(SystemArea.BOTTOM_LEFT)), 0, getHeight(SystemArea.TOP_LEFT)+height, null); + g.drawImage(SystemGraphic.getChoiceArea(getBounds(SystemArea.BOTTOM)), getWidth(SystemArea.BOTTOM_LEFT), (height+getHeight(SystemArea.TOP_LEFT)+getHeight(SystemArea.BOTTOM_LEFT))-getHeight(SystemArea.BOTTOM), width, getHeight(SystemArea.BOTTOM), null); + g.drawImage(SystemGraphic.getChoiceArea(getBounds(SystemArea.BOTTOM_RIGHT)), getWidth(SystemArea.BOTTOM_RIGHT)+width, getHeight(SystemArea.TOP_RIGHT)+height, null); + g.drawImage(SystemGraphic.getChoiceArea(getBounds(SystemArea.RIGHT)), (width+getWidth(SystemArea.TOP_LEFT)+getWidth(SystemArea.TOP_RIGHT))-getWidth(SystemArea.RIGHT), getHeight(SystemArea.TOP_RIGHT), getWidth(SystemArea.RIGHT), height, null); + + return Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(temp.getSource(), new TransparentPixelFilter(SystemGraphic.getTransparentColor().getRGB()))); + } +} \ No newline at end of file -- cgit 1.4.1