summary refs log tree commit diff stats
path: root/src/com/fourisland/fourpuzzle/window/MessageWindow.java
diff options
context:
space:
mode:
authorStarla Insigna <hatkirby@fourisland.com>2009-02-12 16:36:07 -0500
committerStarla Insigna <hatkirby@fourisland.com>2009-02-12 16:36:07 -0500
commitbbf294dbf6b552751e5d9f3fb66188bd1bee724b (patch)
tree1e29d91a6e8a3b9d26ec874169de85928d4ec56f /src/com/fourisland/fourpuzzle/window/MessageWindow.java
parent3cd96daaf22236e4eb15c6422f772abf08351023 (diff)
downloadfourpuzzle-bbf294dbf6b552751e5d9f3fb66188bd1bee724b.tar.gz
fourpuzzle-bbf294dbf6b552751e5d9f3fb66188bd1bee724b.tar.bz2
fourpuzzle-bbf294dbf6b552751e5d9f3fb66188bd1bee724b.zip
Engine: Wrote Message System
MessageWindow now has a static method run by SpecialEvent that triggers the message box. This method blocks until the message is complete and renders via Display's new feature. The message box also now features the "next" arrow and the letters gradually appear.

Display has also been re-worked to have a list of a new interface called Renderable, which is any object that can be rendered. Such objects (such as MessageWindow) can register to Display, which will render them onto the game frame after the GameState has been rendered.

Closes #5.
Diffstat (limited to 'src/com/fourisland/fourpuzzle/window/MessageWindow.java')
-rw-r--r--src/com/fourisland/fourpuzzle/window/MessageWindow.java99
1 files changed, 94 insertions, 5 deletions
diff --git a/src/com/fourisland/fourpuzzle/window/MessageWindow.java b/src/com/fourisland/fourpuzzle/window/MessageWindow.java index deab252..3272fcc 100644 --- a/src/com/fourisland/fourpuzzle/window/MessageWindow.java +++ b/src/com/fourisland/fourpuzzle/window/MessageWindow.java
@@ -7,26 +7,36 @@ package com.fourisland.fourpuzzle.window;
7 7
8import com.fourisland.fourpuzzle.Display; 8import com.fourisland.fourpuzzle.Display;
9import com.fourisland.fourpuzzle.Game; 9import com.fourisland.fourpuzzle.Game;
10import com.fourisland.fourpuzzle.PuzzleApplication;
11import com.fourisland.fourpuzzle.util.Interval;
12import com.fourisland.fourpuzzle.util.Renderable;
10import java.awt.Graphics2D; 13import java.awt.Graphics2D;
11import java.awt.Rectangle; 14import java.awt.Rectangle;
12import java.awt.TexturePaint; 15import java.awt.TexturePaint;
16import java.awt.event.KeyAdapter;
17import java.awt.event.KeyEvent;
13import java.awt.image.BufferedImage; 18import java.awt.image.BufferedImage;
14import java.util.ArrayList; 19import java.util.ArrayList;
15import java.util.List; 20import java.util.List;
21import java.util.concurrent.CountDownLatch;
16 22
17/** 23/**
18 * 24 *
19 * @author hatkirby 25 * @author hatkirby
20 */ 26 */
21public class MessageWindow { 27public class MessageWindow implements Renderable {
22 28
23 private static final int SPACER = 4; 29 private static final int SPACER = 4;
24 private static final int HEIGHT = 4*(Display.createCanvas(1, 1).createGraphics().getFontMetrics().getHeight()+SPACER); 30 private static final int HEIGHT = 4*(Display.createCanvas(1, 1).createGraphics().getFontMetrics().getHeight()+SPACER);
25 31
26 private List<String> messages; 32 private volatile List<String> messages;
27 int width; 33 int width;
28 BufferedImage cacheBase; 34 BufferedImage cacheBase;
29 public MessageWindow(String message) 35 int num = 0;
36 int upTo = 0;
37 boolean bounceArrow = false;
38 Interval in = Interval.createTickInterval(4);
39 private MessageWindow(String message)
30 { 40 {
31 width = Game.WIDTH - Window.Default.getFullWidth(0); 41 width = Game.WIDTH - Window.Default.getFullWidth(0);
32 messages = new ArrayList<String>(); 42 messages = new ArrayList<String>();
@@ -36,6 +46,36 @@ public class MessageWindow {
36 cacheBase = Window.Default.getImage(width, HEIGHT); 46 cacheBase = Window.Default.getImage(width, HEIGHT);
37 } 47 }
38 48
49 public static void displayMessage(String message) throws InterruptedException
50 {
51 final MessageWindow mw = new MessageWindow(message);
52 final CountDownLatch cdl = new CountDownLatch(1);
53
54 Display.registerRenderable(mw);
55
56 KeyAdapter ka = new KeyAdapter() {
57 @Override
58 public void keyPressed(KeyEvent e) {
59 if ((e.getKeyCode() == KeyEvent.VK_ENTER) || (e.getKeyCode() == KeyEvent.VK_SPACE))
60 {
61 if (mw.pushEnter())
62 {
63 cdl.countDown();
64 }
65 }
66
67 Game.setKey(null);
68 }
69 };
70
71 PuzzleApplication.gameFrame.addKeyListener(ka);
72
73 cdl.await();
74
75 PuzzleApplication.gameFrame.removeKeyListener(ka);
76 Display.unregisterRenderable(mw);
77 }
78
39 private void initalizeMessages(String message, Graphics2D g) 79 private void initalizeMessages(String message, Graphics2D g)
40 { 80 {
41 Display.setFont(g); 81 Display.setFont(g);
@@ -68,12 +108,24 @@ public class MessageWindow {
68 108
69 len = 0; 109 len = 0;
70 } 110 }
111
112 setLength();
113 }
114
115 private void setLength()
116 {
117 num = 0;
118
119 for (int i=0;i<messages.size();i++)
120 {
121 num += messages.get(i).length();
122 }
71 } 123 }
72 124
73 public void render(Graphics2D g2) 125 public void render(Graphics2D g2)
74 { 126 {
75 int y = MessageWindowLocation.Bottom.getY(); 127 int y = MessageWindowLocation.Bottom.getY();
76 128
77 Display.setFont(g2); 129 Display.setFont(g2);
78 130
79 g2.drawImage(cacheBase, 0, y, null); 131 g2.drawImage(cacheBase, 0, y, null);
@@ -81,6 +133,7 @@ public class MessageWindow {
81 int fh = g2.getFontMetrics().getHeight(); 133 int fh = g2.getFontMetrics().getHeight();
82 int ty = Window.Default.getTopY()+fh-(SPACER/2)+y; 134 int ty = Window.Default.getTopY()+fh-(SPACER/2)+y;
83 int msgs = Math.min(messages.size(), 4); 135 int msgs = Math.min(messages.size(), 4);
136 int toPrint = upTo;
84 for (int i=0;i<msgs;i++) 137 for (int i=0;i<msgs;i++)
85 { 138 {
86 String message = messages.get(i); 139 String message = messages.get(i);
@@ -88,10 +141,46 @@ public class MessageWindow {
88 int tx = Window.Default.getLeftX(); 141 int tx = Window.Default.getLeftX();
89 142
90 g2.setPaint(new TexturePaint(SystemGraphic.getTextColor(), new Rectangle(tx, ty, fw, fh))); 143 g2.setPaint(new TexturePaint(SystemGraphic.getTextColor(), new Rectangle(tx, ty, fw, fh)));
91 g2.drawString(message, tx, ty); 144 g2.drawString(message.substring(0, Math.min(toPrint, message.length())), tx, ty);
92 145
93 ty+=(SPACER+g2.getFontMetrics().getHeight()); 146 ty+=(SPACER+g2.getFontMetrics().getHeight());
147
148 toPrint -= Math.min(toPrint, message.length());
149 }
150
151 if (upTo < num)
152 {
153 upTo+=3;
154 } else {
155 g2.drawImage(SystemGraphic.getDownArrow(), (Window.Default.getFullWidth(width)/2)-5, y+HEIGHT-SPACER+(bounceArrow ? 1 : 0), null);
156
157 if (in.isElapsed())
158 {
159 bounceArrow = !bounceArrow;
160 }
161 }
162 }
163
164 private synchronized boolean pushEnter()
165 {
166 if (upTo >= num)
167 {
168 int msgs = messages.size();
169 for (int i=0;i<Math.min(4, msgs);i++)
170 {
171 messages.remove(0);
172 }
173
174 if (messages.size() > 0)
175 {
176 upTo = 0;
177 setLength();
178 } else {
179 return true;
180 }
94 } 181 }
182
183 return false;
95 } 184 }
96 185
97 public static enum MessageWindowLocation 186 public static enum MessageWindowLocation