diff options
author | Starla Insigna <hatkirby@fourisland.com> | 2009-02-12 16:36:07 -0500 |
---|---|---|
committer | Starla Insigna <hatkirby@fourisland.com> | 2009-02-12 16:36:07 -0500 |
commit | bbf294dbf6b552751e5d9f3fb66188bd1bee724b (patch) | |
tree | 1e29d91a6e8a3b9d26ec874169de85928d4ec56f /src/com/fourisland/fourpuzzle/window/MessageWindow.java | |
parent | 3cd96daaf22236e4eb15c6422f772abf08351023 (diff) | |
download | fourpuzzle-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.java | 99 |
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 | ||
8 | import com.fourisland.fourpuzzle.Display; | 8 | import com.fourisland.fourpuzzle.Display; |
9 | import com.fourisland.fourpuzzle.Game; | 9 | import com.fourisland.fourpuzzle.Game; |
10 | import com.fourisland.fourpuzzle.PuzzleApplication; | ||
11 | import com.fourisland.fourpuzzle.util.Interval; | ||
12 | import com.fourisland.fourpuzzle.util.Renderable; | ||
10 | import java.awt.Graphics2D; | 13 | import java.awt.Graphics2D; |
11 | import java.awt.Rectangle; | 14 | import java.awt.Rectangle; |
12 | import java.awt.TexturePaint; | 15 | import java.awt.TexturePaint; |
16 | import java.awt.event.KeyAdapter; | ||
17 | import java.awt.event.KeyEvent; | ||
13 | import java.awt.image.BufferedImage; | 18 | import java.awt.image.BufferedImage; |
14 | import java.util.ArrayList; | 19 | import java.util.ArrayList; |
15 | import java.util.List; | 20 | import java.util.List; |
21 | import java.util.concurrent.CountDownLatch; | ||
16 | 22 | ||
17 | /** | 23 | /** |
18 | * | 24 | * |
19 | * @author hatkirby | 25 | * @author hatkirby |
20 | */ | 26 | */ |
21 | public class MessageWindow { | 27 | public 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 |