From eca79b20cdb258768668c2ecf1b7333de62c7d7f Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Sun, 8 Feb 2009 12:09:25 -0500 Subject: Implemented a basic ChoiceWindow This window should be used when there are a few choices available (such as in TitleScreenGameState and MenuGameState) and one needs to be chosen. The implementation currently is bad, the code is messy and it could be optimized. Plus, there are a few bugs in it and no choice can currently be made, only the rendering has been done. --- src/com/fourisland/fourpuzzle/Database.java | 6 ++ .../fourpuzzle/gamestate/TitleScreenGameState.java | 11 +++ .../fourpuzzle/util/TransparentPixelFilter.java | 35 +++++++ .../fourisland/fourpuzzle/window/ChoiceWindow.java | 109 +++++++++++++++++++++ .../fourpuzzle/window/SystemChoiceArea.java | 56 +++++++++++ .../fourpuzzle/window/SystemGraphic.java | 43 ++++++++ 6 files changed, 260 insertions(+) create mode 100644 src/com/fourisland/fourpuzzle/util/TransparentPixelFilter.java create mode 100644 src/com/fourisland/fourpuzzle/window/ChoiceWindow.java create mode 100644 src/com/fourisland/fourpuzzle/window/SystemChoiceArea.java create mode 100644 src/com/fourisland/fourpuzzle/window/SystemGraphic.java (limited to 'src/com') diff --git a/src/com/fourisland/fourpuzzle/Database.java b/src/com/fourisland/fourpuzzle/Database.java index 8d51f9a..b6e97cf 100644 --- a/src/com/fourisland/fourpuzzle/Database.java +++ b/src/com/fourisland/fourpuzzle/Database.java @@ -31,7 +31,13 @@ public class Database { private static void loadDefaultVocabulary() { + /* Global */ vocabulary.put("Title", "Untitled Game"); + + /* TitleScreen */ + vocabulary.put("NewGame", "New Game"); + vocabulary.put("LoadGame", "Load Game"); + vocabulary.put("EndGame", "End"); } public static String getVocab(String key) diff --git a/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java b/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java index 9247282..621ec0d 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java @@ -10,8 +10,10 @@ 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.window.ChoiceWindow; import java.awt.Graphics2D; import java.awt.event.KeyEvent; +import java.util.Arrays; /** * @@ -19,9 +21,17 @@ import java.awt.event.KeyEvent; */ public class TitleScreenGameState implements GameState { + ChoiceWindow choices; + int wx, wy; + public void initalize() { Audio.playMusic(Database.getMusic("Title")); + + choices = new ChoiceWindow(Arrays.asList(Database.getVocab("NewGame"), Database.getVocab("LoadGame"), Database.getVocab("EndGame"))); + + wx = (Game.WIDTH/2)-(choices.getWidth()/2); + wy = (Game.HEIGHT/4*3)-(choices.getHeight()/2); } public void deinitalize() @@ -63,6 +73,7 @@ public class TitleScreenGameState implements GameState { public void render(Graphics2D g) { g.drawImage(ObjectLoader.getImage("Picture", "Title"), 0, 0, null); + choices.render(g, wx, wy); } } diff --git a/src/com/fourisland/fourpuzzle/util/TransparentPixelFilter.java b/src/com/fourisland/fourpuzzle/util/TransparentPixelFilter.java new file mode 100644 index 0000000..ed1c49c --- /dev/null +++ b/src/com/fourisland/fourpuzzle/util/TransparentPixelFilter.java @@ -0,0 +1,35 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.fourpuzzle.util; + +import java.awt.image.RGBImageFilter; + +/** + * + * @author hatkirby + */ +public class TransparentPixelFilter extends RGBImageFilter { + + private int trans; + public TransparentPixelFilter(int i) + { + trans = i; + + canFilterIndexColorModel = true; + } + + @Override + public int filterRGB(int x, int y, int rgb) + { + if (rgb == trans) + { + return 0; + } else { + return rgb; + } + } + +} diff --git a/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java b/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java new file mode 100644 index 0000000..ed20ab0 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java @@ -0,0 +1,109 @@ +/* + * 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.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 */ + +/** + * + * @author hatkirby + */ +public class ChoiceWindow { + + private final int SPACER = 4; + + private List choices; + int numChoices; + public ChoiceWindow(List choices) + { + this.choices = choices; + numChoices = choices.size(); + + createGraphic(new BufferedImage(TopLeft.getWidth()+getWidth()+TopRight.getWidth(), TopLeft.getHeight()+getHeight()+BottomLeft.getHeight(), BufferedImage.TYPE_INT_ARGB).createGraphics()); + } + + private int width; + private int height; + BufferedImage cacheBase; + private void createGraphic(Graphics2D g3) + { + for (String choice : choices) + { + int l = g3.getFontMetrics().stringWidth(choice); + if (l > getWidth()) + { + width = l; + } + + height += g3.getFontMetrics().getHeight() + SPACER; + } + + 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); + 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(-25600))), 0, 0, null); + g2.setFont(g2.getFont().deriveFont(Font.BOLD)); + + int tx = TopLeft.getWidth(); + int ty = TopLeft.getHeight()+g2.getFontMetrics().getHeight()-SPACER; + for (String choice : choices) + { + /* TODO The following code paints the text onto the window. However, + * when it paints the lowercase 'y', the tail is white, not the + * correct gradient. */ + + g2.setPaint(new TexturePaint(SystemGraphic.getTextColor(), new Rectangle(tx, ty, g2.getFontMetrics().stringWidth(choice),g2.getFontMetrics().getHeight()))); + g2.drawString(choice, tx, ty); + + ty+=(SPACER+g2.getFontMetrics().getHeight()); + } + + 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; + } + + public int getHeight() + { + return height; + } + +} diff --git a/src/com/fourisland/fourpuzzle/window/SystemChoiceArea.java b/src/com/fourisland/fourpuzzle/window/SystemChoiceArea.java new file mode 100644 index 0000000..8908c10 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/window/SystemChoiceArea.java @@ -0,0 +1,56 @@ +/* + * 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, 4), + TopRight(56, 0, 8, 8), + Right(60, 15, 4, 1), + BottomRight(56, 24, 8, 8), + Bottom(47, 28, 1, 4), + BottomLeft(32, 24, 8, 8), + Left(32, 15, 4, 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 new file mode 100644 index 0000000..70fc7fe --- /dev/null +++ b/src/com/fourisland/fourpuzzle/window/SystemGraphic.java @@ -0,0 +1,43 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.fourpuzzle.window; + +import com.fourisland.fourpuzzle.util.ObjectLoader; +import java.awt.image.BufferedImage; + +/** + * + * @author hatkirby + */ +public class SystemGraphic { + + private static BufferedImage systemGraphic; + static + { + initalize("System"); + } + + public static void initalize(String graphic) + { + systemGraphic = ObjectLoader.getImage("Picture", graphic); + } + + public static BufferedImage getMessageBackground() + { + return systemGraphic.getSubimage(0, 0, 32, 32); + } + + public static BufferedImage getChoiceArea(SystemChoiceArea sca) + { + return systemGraphic.getSubimage(sca.getX(), sca.getY(), sca.getWidth(), sca.getHeight()); + } + + public static BufferedImage getTextColor() + { + return systemGraphic.getSubimage(0, 48, 16, 16); + } + +} -- cgit 1.4.1