From 3cecb76e6a2240f052eb14a374b7a75950cbbc3d Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Sun, 8 Feb 2009 19:40:41 -0500 Subject: Started MessageWindow Currently, MessageWindow splits a string into four lines and displays it when necessary. However, there is currently no way to close the window, it does not animate upon opening and more. --- .../gamestate/mapview/MapViewGameState.java | 12 ++ .../gamestate/mapview/event/SpecialEvent.java | 2 +- .../fourisland/fourpuzzle/window/ChoiceWindow.java | 2 +- .../fourpuzzle/window/MessageWindow.java | 139 +++++++++++++++++++++ 4 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 src/com/fourisland/fourpuzzle/window/MessageWindow.java (limited to 'src') diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java index 3acfff4..9924c9e 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java @@ -22,6 +22,7 @@ import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.AutomaticViewpoint; import com.fourisland.fourpuzzle.gamestate.mapview.viewpoint.Viewpoint; import com.fourisland.fourpuzzle.util.Functions; import com.fourisland.fourpuzzle.util.ResourceNotFoundException; +import com.fourisland.fourpuzzle.window.MessageWindow; import java.awt.Graphics2D; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; @@ -230,6 +231,11 @@ public class MapViewGameState implements GameState { g.drawImage(eventLayer, 0, 0, Game.WIDTH, Game.HEIGHT, x, y, x+Game.WIDTH, y+Game.HEIGHT, null); g.drawImage(currentMap.renderUpper(), 0, 0, Game.WIDTH, Game.HEIGHT, x, y, x+Game.WIDTH, y+Game.HEIGHT, null); + + if (mw != null) + { + mw.render(g); + } } public void initCurrentMap(String mapName) @@ -268,5 +274,11 @@ public class MapViewGameState implements GameState { { currentViewpoint = viewpoint; } + + volatile MessageWindow mw = null; + public void displayMessage(String message) + { + mw = new MessageWindow(message); + } } diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java index c48b312..5bd312a 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/event/SpecialEvent.java @@ -51,7 +51,7 @@ public class SpecialEvent { */ public void DisplayMessage(String message) { - throw new UnsupportedOperationException("Not yet implemented"); + mapView.displayMessage(message); } /** diff --git a/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java b/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java index 19d53ac..e466e63 100755 --- a/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java +++ b/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java @@ -19,7 +19,7 @@ import java.util.List; */ public class ChoiceWindow { - private final int SPACER = 4; + private static final int SPACER = 4; private List choices; int numChoices; diff --git a/src/com/fourisland/fourpuzzle/window/MessageWindow.java b/src/com/fourisland/fourpuzzle/window/MessageWindow.java new file mode 100644 index 0000000..9e482ba --- /dev/null +++ b/src/com/fourisland/fourpuzzle/window/MessageWindow.java @@ -0,0 +1,139 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.fourisland.fourpuzzle.window; + +import com.fourisland.fourpuzzle.Game; +import java.awt.Font; +import java.awt.FontFormatException; +import java.awt.Graphics2D; +import java.awt.Rectangle; +import java.awt.TexturePaint; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author hatkirby + */ +public class MessageWindow { + + private static final int SPACER = 4; + private static final int HEIGHT = 4*(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB).createGraphics().getFontMetrics().getHeight()+SPACER); + + private List messages; + int width; + BufferedImage cacheBase; + public MessageWindow(String message) + { + width = Game.WIDTH - Window.Default.getFullWidth(0); + messages = new ArrayList(); + + initalizeMessages(message, new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB).createGraphics()); + + cacheBase = new BufferedImage(Game.WIDTH, Window.Default.getFullHeight(HEIGHT), BufferedImage.TYPE_INT_ARGB); + Graphics2D g2 = cacheBase.createGraphics(); + + g2.drawImage(SystemGraphic.getMessageBackground(), 1, 1, Game.WIDTH-2, Window.Default.getFullHeight(HEIGHT)-2, null); + g2.drawImage(Window.Default.getImage(width, HEIGHT), 0, 0, null); + } + + private void initalizeMessages(String message, Graphics2D g) + { + setFont(g); + + String temp = message; + int len = 0; + while (!temp.isEmpty()) + { + while ((g.getFontMetrics().stringWidth(temp.substring(0, len)) < (width - SPACER)) && (len < temp.length())) + { + len++; + } + + if (len != temp.length()) + { + while ((!temp.substring(len, len+1).equals(" ")) && (len > 0)) + { + len--; + } + } + + messages.add(temp.substring(0, len)); + + if (len != temp.length()) + { + temp = temp.substring(len+1); + } else { + temp = ""; + } + + len = 0; + } + } + + public void render(Graphics2D g2) + { + int y = MessageWindowLocation.Bottom.getY(); + + g2.drawImage(cacheBase, 0, y, null); + + setFont(g2); + + int fh = g2.getFontMetrics().getHeight(); + int ty = Window.Default.getTopY()+fh-(SPACER/2)+y; + int msgs = Math.min(messages.size(), 4); + for (int i=0;i