diff options
author | Starla Insigna <hatkirby@fourisland.com> | 2009-02-15 14:54:15 -0500 |
---|---|---|
committer | Starla Insigna <hatkirby@fourisland.com> | 2009-02-15 14:54:15 -0500 |
commit | 1fb7799ff91729285bf145b2c78f9233a61ac35c (patch) | |
tree | e6779a3409f76975b067dd11ce24cfa556583705 /src/com | |
parent | d242cfcf9a62bed5158c33c061e47fa393e2301a (diff) | |
download | fourpuzzle-1fb7799ff91729285bf145b2c78f9233a61ac35c.tar.gz fourpuzzle-1fb7799ff91729285bf145b2c78f9233a61ac35c.tar.bz2 fourpuzzle-1fb7799ff91729285bf145b2c78f9233a61ac35c.zip |
Engine: Fixed MessageWindow bug
Previously, MessageWindow would, on occasion, throw out an ArrayIndexOutOfBoundsException. This may have been because of some keyboard input. Because of this, the keyboard input system has been re-written to be anologous to the Display system (Renderable). Now, only one input processor will run at a time because they are executed in order by KeyboardInput, rather than all at once using AWT event handlers.
Diffstat (limited to 'src/com')
10 files changed, 211 insertions, 110 deletions
diff --git a/src/com/fourisland/fourpuzzle/Game.java b/src/com/fourisland/fourpuzzle/Game.java index 432d8ff..b3d5190 100755 --- a/src/com/fourisland/fourpuzzle/Game.java +++ b/src/com/fourisland/fourpuzzle/Game.java | |||
@@ -7,7 +7,6 @@ package com.fourisland.fourpuzzle; | |||
7 | 7 | ||
8 | import com.fourisland.fourpuzzle.gamestate.mapview.event.HeroEvent; | 8 | import com.fourisland.fourpuzzle.gamestate.mapview.event.HeroEvent; |
9 | import com.fourisland.fourpuzzle.gamestate.GameState; | 9 | import com.fourisland.fourpuzzle.gamestate.GameState; |
10 | import java.awt.event.KeyEvent; | ||
11 | 10 | ||
12 | /** | 11 | /** |
13 | * | 12 | * |
@@ -45,16 +44,6 @@ public class Game { | |||
45 | Game.gameState.initalize(); | 44 | Game.gameState.initalize(); |
46 | } | 45 | } |
47 | 46 | ||
48 | private static KeyEvent key; | ||
49 | public static KeyEvent getKey() | ||
50 | { | ||
51 | return key; | ||
52 | } | ||
53 | public static void setKey(KeyEvent key) | ||
54 | { | ||
55 | Game.key = key; | ||
56 | } | ||
57 | |||
58 | public static HeroEvent getHeroEvent() | 47 | public static HeroEvent getHeroEvent() |
59 | { | 48 | { |
60 | return getSaveFile().getHero(); | 49 | return getSaveFile().getHero(); |
diff --git a/src/com/fourisland/fourpuzzle/KeyInput.java b/src/com/fourisland/fourpuzzle/KeyInput.java new file mode 100644 index 0000000..37eda77 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/KeyInput.java | |||
@@ -0,0 +1,60 @@ | |||
1 | /* | ||
2 | * To change this template, choose Tools | Templates | ||
3 | * and open the template in the editor. | ||
4 | */ | ||
5 | |||
6 | package com.fourisland.fourpuzzle; | ||
7 | |||
8 | import java.awt.event.KeyEvent; | ||
9 | |||
10 | /** | ||
11 | * | ||
12 | * @author hatkirby | ||
13 | */ | ||
14 | public class KeyInput | ||
15 | { | ||
16 | private boolean ctrl = false; | ||
17 | private boolean alt = false; | ||
18 | private boolean shift = false; | ||
19 | private int key = 0; | ||
20 | public void keyInput(KeyEvent ev) | ||
21 | { | ||
22 | ctrl = ev.isControlDown(); | ||
23 | alt = ev.isAltDown(); | ||
24 | shift = ev.isShiftDown(); | ||
25 | key = ev.getKeyCode(); | ||
26 | } | ||
27 | |||
28 | public void letGo() | ||
29 | { | ||
30 | ctrl = false; | ||
31 | alt = false; | ||
32 | shift = false; | ||
33 | key = 0; | ||
34 | } | ||
35 | |||
36 | public boolean isCtrlDown() | ||
37 | { | ||
38 | return ctrl; | ||
39 | } | ||
40 | |||
41 | public boolean isAltDown() | ||
42 | { | ||
43 | return alt; | ||
44 | } | ||
45 | |||
46 | public boolean isShiftDown() | ||
47 | { | ||
48 | return shift; | ||
49 | } | ||
50 | |||
51 | public boolean isKeyDown() | ||
52 | { | ||
53 | return (key == 0 ? false : true); | ||
54 | } | ||
55 | |||
56 | public int getKey() | ||
57 | { | ||
58 | return key; | ||
59 | } | ||
60 | } \ No newline at end of file | ||
diff --git a/src/com/fourisland/fourpuzzle/KeyboardInput.java b/src/com/fourisland/fourpuzzle/KeyboardInput.java new file mode 100644 index 0000000..a3e849d --- /dev/null +++ b/src/com/fourisland/fourpuzzle/KeyboardInput.java | |||
@@ -0,0 +1,45 @@ | |||
1 | /* | ||
2 | * To change this template, choose Tools | Templates | ||
3 | * and open the template in the editor. | ||
4 | */ | ||
5 | |||
6 | package com.fourisland.fourpuzzle; | ||
7 | |||
8 | import com.fourisland.fourpuzzle.util.Inputable; | ||
9 | import java.util.concurrent.CopyOnWriteArrayList; | ||
10 | |||
11 | /** | ||
12 | * | ||
13 | * @author hatkirby | ||
14 | */ | ||
15 | public class KeyboardInput { | ||
16 | |||
17 | private static CopyOnWriteArrayList<Inputable> inputables = new CopyOnWriteArrayList<Inputable>(); | ||
18 | private static KeyInput key = new KeyInput(); | ||
19 | |||
20 | public static synchronized void registerInputable(Inputable inputable) | ||
21 | { | ||
22 | inputables.add(inputable); | ||
23 | } | ||
24 | |||
25 | public static synchronized void unregisterInputable(Inputable inputable) | ||
26 | { | ||
27 | inputables.remove(inputable); | ||
28 | } | ||
29 | |||
30 | public static void processInput() | ||
31 | { | ||
32 | Game.getGameState().processInput(key); | ||
33 | |||
34 | for (Inputable inputable : inputables) | ||
35 | { | ||
36 | inputable.processInput(key); | ||
37 | } | ||
38 | } | ||
39 | |||
40 | static synchronized KeyInput getKey() | ||
41 | { | ||
42 | return key; | ||
43 | } | ||
44 | |||
45 | } \ No newline at end of file | ||
diff --git a/src/com/fourisland/fourpuzzle/PuzzleApplication.java b/src/com/fourisland/fourpuzzle/PuzzleApplication.java index cc9af03..069b126 100755 --- a/src/com/fourisland/fourpuzzle/PuzzleApplication.java +++ b/src/com/fourisland/fourpuzzle/PuzzleApplication.java | |||
@@ -7,7 +7,6 @@ package com.fourisland.fourpuzzle; | |||
7 | import com.fourisland.fourpuzzle.database.Database; | 7 | import com.fourisland.fourpuzzle.database.Database; |
8 | import com.fourisland.fourpuzzle.database.Vocabulary; | 8 | import com.fourisland.fourpuzzle.database.Vocabulary; |
9 | import com.fourisland.fourpuzzle.gamestate.TitleScreenGameState; | 9 | import com.fourisland.fourpuzzle.gamestate.TitleScreenGameState; |
10 | import com.fourisland.fourpuzzle.gamestate.mapview.ChipSet; | ||
11 | import com.fourisland.fourpuzzle.util.Interval; | 10 | import com.fourisland.fourpuzzle.util.Interval; |
12 | import com.fourisland.fourpuzzle.window.SystemGraphic; | 11 | import com.fourisland.fourpuzzle.window.SystemGraphic; |
13 | import java.awt.GraphicsEnvironment; | 12 | import java.awt.GraphicsEnvironment; |
@@ -87,7 +86,7 @@ public class PuzzleApplication extends Application { | |||
87 | debugSpeed = true; | 86 | debugSpeed = true; |
88 | } | 87 | } |
89 | } else { | 88 | } else { |
90 | Game.setKey(e); | 89 | KeyboardInput.getKey().keyInput(e); |
91 | } | 90 | } |
92 | } | 91 | } |
93 | 92 | ||
@@ -98,8 +97,7 @@ public class PuzzleApplication extends Application { | |||
98 | { | 97 | { |
99 | debugSpeed = false; | 98 | debugSpeed = false; |
100 | } else { | 99 | } else { |
101 | e.setKeyCode(KeyEvent.VK_UNDEFINED); | 100 | KeyboardInput.getKey().letGo(); |
102 | Game.setKey(e); | ||
103 | } | 101 | } |
104 | } | 102 | } |
105 | }); | 103 | }); |
@@ -120,10 +118,7 @@ public class PuzzleApplication extends Application { | |||
120 | { | 118 | { |
121 | if (!Display.isTransitionRunning()) | 119 | if (!Display.isTransitionRunning()) |
122 | { | 120 | { |
123 | if (Game.getKey() != null) | 121 | KeyboardInput.processInput(); |
124 | { | ||
125 | Game.getGameState().processInput(); | ||
126 | } | ||
127 | 122 | ||
128 | Game.getGameState().doGameCycle(); | 123 | Game.getGameState().doGameCycle(); |
129 | } | 124 | } |
diff --git a/src/com/fourisland/fourpuzzle/gamestate/GameOverGameState.java b/src/com/fourisland/fourpuzzle/gamestate/GameOverGameState.java index ab1b9bb..d9b9ea1 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/GameOverGameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/GameOverGameState.java | |||
@@ -9,11 +9,10 @@ import com.fourisland.fourpuzzle.Audio; | |||
9 | import com.fourisland.fourpuzzle.database.Database; | 9 | import com.fourisland.fourpuzzle.database.Database; |
10 | import com.fourisland.fourpuzzle.Display; | 10 | import com.fourisland.fourpuzzle.Display; |
11 | import com.fourisland.fourpuzzle.Game; | 11 | import com.fourisland.fourpuzzle.Game; |
12 | import com.fourisland.fourpuzzle.KeyInput; | ||
12 | import com.fourisland.fourpuzzle.SaveFile; | 13 | import com.fourisland.fourpuzzle.SaveFile; |
13 | import com.fourisland.fourpuzzle.database.Music; | 14 | import com.fourisland.fourpuzzle.database.Music; |
14 | import com.fourisland.fourpuzzle.database.Transitions; | 15 | import com.fourisland.fourpuzzle.database.Transitions; |
15 | import com.fourisland.fourpuzzle.transition.SquareTransition; | ||
16 | import com.fourisland.fourpuzzle.transition.TransitionDirection; | ||
17 | import com.fourisland.fourpuzzle.util.ObjectLoader; | 16 | import com.fourisland.fourpuzzle.util.ObjectLoader; |
18 | import java.awt.Graphics2D; | 17 | import java.awt.Graphics2D; |
19 | import java.awt.event.KeyEvent; | 18 | import java.awt.event.KeyEvent; |
@@ -34,9 +33,9 @@ public class GameOverGameState implements GameState { | |||
34 | Audio.stopMusic(); | 33 | Audio.stopMusic(); |
35 | } | 34 | } |
36 | 35 | ||
37 | public void processInput() | 36 | public void processInput(KeyInput key) |
38 | { | 37 | { |
39 | if ((Game.getKey().getKeyCode() == KeyEvent.VK_ENTER) || (Game.getKey().getKeyCode() == KeyEvent.VK_SPACE)) | 38 | if ((key.getKey() == KeyEvent.VK_ENTER) || (key.getKey() == KeyEvent.VK_SPACE)) |
40 | { | 39 | { |
41 | Game.setSaveFile(new SaveFile()); | 40 | Game.setSaveFile(new SaveFile()); |
42 | 41 | ||
diff --git a/src/com/fourisland/fourpuzzle/gamestate/GameState.java b/src/com/fourisland/fourpuzzle/gamestate/GameState.java index e7bd783..ff5b656 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/GameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/GameState.java | |||
@@ -5,18 +5,18 @@ | |||
5 | 5 | ||
6 | package com.fourisland.fourpuzzle.gamestate; | 6 | package com.fourisland.fourpuzzle.gamestate; |
7 | 7 | ||
8 | import com.fourisland.fourpuzzle.util.Inputable; | ||
8 | import com.fourisland.fourpuzzle.util.Renderable; | 9 | import com.fourisland.fourpuzzle.util.Renderable; |
9 | 10 | ||
10 | /** | 11 | /** |
11 | * | 12 | * |
12 | * @author hatkirby | 13 | * @author hatkirby |
13 | */ | 14 | */ |
14 | public interface GameState extends Renderable { | 15 | public interface GameState extends Renderable, Inputable { |
15 | 16 | ||
16 | public void initalize(); | 17 | public void initalize(); |
17 | public void deinitalize(); | 18 | public void deinitalize(); |
18 | 19 | ||
19 | public void processInput(); | ||
20 | public void doGameCycle(); | 20 | public void doGameCycle(); |
21 | 21 | ||
22 | } \ No newline at end of file | 22 | } \ No newline at end of file |
diff --git a/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java b/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java index fe8b1ed..432d245 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java | |||
@@ -7,6 +7,7 @@ package com.fourisland.fourpuzzle.gamestate; | |||
7 | 7 | ||
8 | import com.fourisland.fourpuzzle.database.Database; | 8 | import com.fourisland.fourpuzzle.database.Database; |
9 | import com.fourisland.fourpuzzle.*; | 9 | import com.fourisland.fourpuzzle.*; |
10 | import com.fourisland.fourpuzzle.KeyInput; | ||
10 | import com.fourisland.fourpuzzle.database.Music; | 11 | import com.fourisland.fourpuzzle.database.Music; |
11 | import com.fourisland.fourpuzzle.database.Sound; | 12 | import com.fourisland.fourpuzzle.database.Sound; |
12 | import com.fourisland.fourpuzzle.database.Transitions; | 13 | import com.fourisland.fourpuzzle.database.Transitions; |
@@ -43,11 +44,11 @@ public class TitleScreenGameState implements GameState { | |||
43 | } | 44 | } |
44 | 45 | ||
45 | PauseTimer pt = new PauseTimer(0); | 46 | PauseTimer pt = new PauseTimer(0); |
46 | public void processInput() | 47 | public void processInput(KeyInput key) |
47 | { | 48 | { |
48 | if (pt.isElapsed()) | 49 | if (pt.isElapsed()) |
49 | { | 50 | { |
50 | if (Game.getKey().getKeyCode() == KeyEvent.VK_ENTER) | 51 | if (key.getKey() == KeyEvent.VK_ENTER) |
51 | { | 52 | { |
52 | Audio.playSound(Database.getSound(Sound.Selection)); | 53 | Audio.playSound(Database.getSound(Sound.Selection)); |
53 | 54 | ||
@@ -89,12 +90,12 @@ public class TitleScreenGameState implements GameState { | |||
89 | } | 90 | } |
90 | }).start(); | 91 | }).start(); |
91 | } | 92 | } |
92 | } else if (Game.getKey().getKeyCode() == KeyEvent.VK_UP) | 93 | } else if (key.getKey() == KeyEvent.VK_UP) |
93 | { | 94 | { |
94 | choices.moveUp(); | 95 | choices.moveUp(); |
95 | 96 | ||
96 | pt.setTimer(1); | 97 | pt.setTimer(1); |
97 | } else if (Game.getKey().getKeyCode() == KeyEvent.VK_DOWN) | 98 | } else if (key.getKey() == KeyEvent.VK_DOWN) |
98 | { | 99 | { |
99 | choices.moveDown(); | 100 | choices.moveDown(); |
100 | 101 | ||
diff --git a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java index a20062d..1213b0c 100755 --- a/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/mapview/MapViewGameState.java | |||
@@ -11,6 +11,7 @@ import com.fourisland.fourpuzzle.Direction; | |||
11 | import com.fourisland.fourpuzzle.Display; | 11 | import com.fourisland.fourpuzzle.Display; |
12 | import com.fourisland.fourpuzzle.gamestate.mapview.event.HeroEvent; | 12 | import com.fourisland.fourpuzzle.gamestate.mapview.event.HeroEvent; |
13 | import com.fourisland.fourpuzzle.Game; | 13 | import com.fourisland.fourpuzzle.Game; |
14 | import com.fourisland.fourpuzzle.KeyInput; | ||
14 | import com.fourisland.fourpuzzle.Layer; | 15 | import com.fourisland.fourpuzzle.Layer; |
15 | import com.fourisland.fourpuzzle.PuzzleApplication; | 16 | import com.fourisland.fourpuzzle.PuzzleApplication; |
16 | import com.fourisland.fourpuzzle.database.Database; | 17 | import com.fourisland.fourpuzzle.database.Database; |
@@ -34,7 +35,6 @@ import java.awt.image.BufferedImage; | |||
34 | public class MapViewGameState implements GameState { | 35 | public class MapViewGameState implements GameState { |
35 | 36 | ||
36 | public boolean debugWalkthrough = false; | 37 | public boolean debugWalkthrough = false; |
37 | boolean processInput = true; | ||
38 | Map currentMap; | 38 | Map currentMap; |
39 | Viewpoint currentViewpoint = null; | 39 | Viewpoint currentViewpoint = null; |
40 | 40 | ||
@@ -61,90 +61,85 @@ public class MapViewGameState implements GameState { | |||
61 | // Do nothing, yet | 61 | // Do nothing, yet |
62 | } | 62 | } |
63 | 63 | ||
64 | public void processInput() | 64 | public void processInput(KeyInput key) |
65 | { | 65 | { |
66 | if (processInput) | 66 | HeroEvent hero = Game.getSaveFile().getHero(); |
67 | { | ||
68 | HeroEvent hero = Game.getSaveFile().getHero(); | ||
69 | 67 | ||
70 | if (Game.getKey().isControlDown() && !debugWalkthrough) | 68 | if (key.isCtrlDown() && !debugWalkthrough) |
69 | { | ||
70 | if (PuzzleApplication.INSTANCE.getContext().getResourceMap().getBoolean("debugMode")) | ||
71 | { | 71 | { |
72 | if (PuzzleApplication.INSTANCE.getContext().getResourceMap().getBoolean("debugMode")) | 72 | debugWalkthrough = true; |
73 | { | ||
74 | debugWalkthrough = true; | ||
75 | } | ||
76 | } else { | ||
77 | debugWalkthrough = false; | ||
78 | } | 73 | } |
74 | } else { | ||
75 | debugWalkthrough = false; | ||
76 | } | ||
79 | 77 | ||
80 | if (!hero.isMoving() && !MoveEventThread.isHeroActive() && !EventHandler.isRunningEvent()) | 78 | if (!hero.isMoving() && !MoveEventThread.isHeroActive() && !EventHandler.isRunningEvent()) |
81 | { | 79 | { |
82 | Direction toMove = null; | 80 | Direction toMove = null; |
83 | Boolean letsMove = false; | 81 | Boolean letsMove = false; |
84 | 82 | ||
85 | switch (Game.getKey().getKeyCode()) | 83 | switch (key.getKey()) |
86 | { | 84 | { |
87 | case KeyEvent.VK_UP: | 85 | case KeyEvent.VK_UP: |
88 | toMove = Direction.North; | 86 | toMove = Direction.North; |
89 | letsMove = true; | 87 | letsMove = true; |
90 | break; | 88 | break; |
91 | case KeyEvent.VK_RIGHT: | 89 | case KeyEvent.VK_RIGHT: |
92 | toMove = Direction.East; | 90 | toMove = Direction.East; |
93 | letsMove = true; | 91 | letsMove = true; |
94 | break; | 92 | break; |
95 | case KeyEvent.VK_DOWN: | 93 | case KeyEvent.VK_DOWN: |
96 | toMove = Direction.South; | 94 | toMove = Direction.South; |
97 | letsMove = true; | 95 | letsMove = true; |
98 | break; | 96 | break; |
99 | case KeyEvent.VK_LEFT: | 97 | case KeyEvent.VK_LEFT: |
100 | toMove = Direction.West; | 98 | toMove = Direction.West; |
101 | letsMove = true; | 99 | letsMove = true; |
102 | break; | 100 | break; |
103 | } | 101 | } |
104 | 102 | ||
105 | if (letsMove) | 103 | if (letsMove) |
104 | { | ||
105 | if (!hero.startMoving(toMove)) | ||
106 | { | 106 | { |
107 | if (!hero.startMoving(toMove)) | 107 | for (LayerEvent ev : currentMap.getEvents()) |
108 | { | 108 | { |
109 | for (LayerEvent ev : currentMap.getEvents()) | 109 | if (ev.getCalltime() == EventCallTime.OnHeroTouch) |
110 | { | 110 | { |
111 | if (ev.getCalltime() == EventCallTime.OnHeroTouch) | 111 | if (ev.getLayer() == Layer.Middle) |
112 | { | 112 | { |
113 | if (ev.getLayer() == Layer.Middle) | 113 | if (Functions.isFacing(hero, ev)) |
114 | { | 114 | { |
115 | if (Functions.isFacing(hero, ev)) | 115 | ev.getCallback().activate(ev.getCalltime()); |
116 | { | ||
117 | ev.getCallback().activate(ev.getCalltime()); | ||
118 | } | ||
119 | } | 116 | } |
120 | } | 117 | } |
121 | } | 118 | } |
122 | } | 119 | } |
123 | } | 120 | } |
121 | } | ||
124 | 122 | ||
125 | if ((Game.getKey().getKeyCode() == KeyEvent.VK_ENTER) || (Game.getKey().getKeyCode() == KeyEvent.VK_SPACE)) | 123 | if ((key.getKey() == KeyEvent.VK_ENTER) || (key.getKey() == KeyEvent.VK_SPACE)) |
124 | { | ||
125 | for (LayerEvent ev : currentMap.getEvents()) | ||
126 | { | 126 | { |
127 | for (LayerEvent ev : currentMap.getEvents()) | 127 | if (ev.getCalltime() == EventCallTime.PushKey) |
128 | { | 128 | { |
129 | if (ev.getCalltime() == EventCallTime.PushKey) | 129 | if (ev.getLayer() == Layer.Middle) |
130 | { | 130 | { |
131 | if (ev.getLayer() == Layer.Middle) | 131 | if (Functions.isFacing(hero, ev)) |
132 | { | 132 | { |
133 | if (Functions.isFacing(hero, ev)) | 133 | ev.setDirection(hero.getDirection().opposite()); |
134 | { | 134 | ev.getCallback().activate(ev.getCalltime()); |
135 | ev.setDirection(hero.getDirection().opposite()); | 135 | } |
136 | ev.getCallback().activate(ev.getCalltime()); | 136 | } else { |
137 | } | 137 | if (ev.getLocation().equals(hero.getLocation())) |
138 | } else { | 138 | { |
139 | if (ev.getLocation().equals(hero.getLocation())) | 139 | ev.getCallback().activate(ev.getCalltime()); |
140 | { | ||
141 | ev.getCallback().activate(ev.getCalltime()); | ||
142 | } | ||
143 | } | 140 | } |
144 | } | 141 | } |
145 | } | 142 | } |
146 | |||
147 | Game.getKey().setKeyCode(KeyEvent.VK_UNDEFINED); | ||
148 | } | 143 | } |
149 | } | 144 | } |
150 | } | 145 | } |
diff --git a/src/com/fourisland/fourpuzzle/util/Inputable.java b/src/com/fourisland/fourpuzzle/util/Inputable.java new file mode 100644 index 0000000..9b108b9 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/util/Inputable.java | |||
@@ -0,0 +1,17 @@ | |||
1 | /* | ||
2 | * To change this template, choose Tools | Templates | ||
3 | * and open the template in the editor. | ||
4 | */ | ||
5 | |||
6 | package com.fourisland.fourpuzzle.util; | ||
7 | |||
8 | import com.fourisland.fourpuzzle.KeyInput; | ||
9 | |||
10 | /** | ||
11 | * | ||
12 | * @author hatkirby | ||
13 | */ | ||
14 | public interface Inputable { | ||
15 | |||
16 | public void processInput(KeyInput key); | ||
17 | } | ||
diff --git a/src/com/fourisland/fourpuzzle/window/MessageWindow.java b/src/com/fourisland/fourpuzzle/window/MessageWindow.java index fb90f04..9ddd379 100644 --- a/src/com/fourisland/fourpuzzle/window/MessageWindow.java +++ b/src/com/fourisland/fourpuzzle/window/MessageWindow.java | |||
@@ -7,14 +7,15 @@ 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; | 10 | import com.fourisland.fourpuzzle.KeyboardInput; |
11 | import com.fourisland.fourpuzzle.KeyInput; | ||
11 | import com.fourisland.fourpuzzle.gamestate.mapview.FaceSet; | 12 | import com.fourisland.fourpuzzle.gamestate.mapview.FaceSet; |
13 | import com.fourisland.fourpuzzle.util.Inputable; | ||
12 | import com.fourisland.fourpuzzle.util.Interval; | 14 | import com.fourisland.fourpuzzle.util.Interval; |
13 | import com.fourisland.fourpuzzle.util.Renderable; | 15 | import com.fourisland.fourpuzzle.util.Renderable; |
14 | import java.awt.Graphics2D; | 16 | import java.awt.Graphics2D; |
15 | import java.awt.Rectangle; | 17 | import java.awt.Rectangle; |
16 | import java.awt.TexturePaint; | 18 | import java.awt.TexturePaint; |
17 | import java.awt.event.KeyAdapter; | ||
18 | import java.awt.event.KeyEvent; | 19 | import java.awt.event.KeyEvent; |
19 | import java.awt.image.BufferedImage; | 20 | import java.awt.image.BufferedImage; |
20 | import java.util.ArrayList; | 21 | import java.util.ArrayList; |
@@ -59,48 +60,46 @@ public class MessageWindow implements Renderable { | |||
59 | private static void displayMessage(final MessageWindow mw) throws InterruptedException | 60 | private static void displayMessage(final MessageWindow mw) throws InterruptedException |
60 | { | 61 | { |
61 | final CountDownLatch cdl = new CountDownLatch(1); | 62 | final CountDownLatch cdl = new CountDownLatch(1); |
62 | 63 | Inputable in = new Inputable() { | |
63 | Display.registerRenderable(mw); | 64 | public void processInput(KeyInput key) |
64 | 65 | { | |
65 | KeyAdapter ka = new KeyAdapter() { | 66 | if ((key.getKey() == KeyEvent.VK_ENTER) || (key.getKey() == KeyEvent.VK_SPACE)) |
66 | @Override | ||
67 | public void keyPressed(KeyEvent e) { | ||
68 | if ((e.getKeyCode() == KeyEvent.VK_ENTER) || (e.getKeyCode() == KeyEvent.VK_SPACE)) | ||
69 | { | 67 | { |
70 | if (mw.pushEnter()) | 68 | if (mw.pushEnter()) |
71 | { | 69 | { |
72 | cdl.countDown(); | 70 | cdl.countDown(); |
73 | } | 71 | } |
74 | } | 72 | } |
75 | |||
76 | Game.setKey(null); | ||
77 | } | 73 | } |
78 | }; | 74 | }; |
79 | 75 | ||
80 | PuzzleApplication.gameFrame.addKeyListener(ka); | 76 | Display.registerRenderable(mw); |
81 | 77 | KeyboardInput.registerInputable(in); | |
78 | |||
82 | cdl.await(); | 79 | cdl.await(); |
83 | 80 | ||
84 | PuzzleApplication.gameFrame.removeKeyListener(ka); | ||
85 | Display.unregisterRenderable(mw); | 81 | Display.unregisterRenderable(mw); |
82 | KeyboardInput.unregisterInputable(in); | ||
86 | } | 83 | } |
87 | 84 | ||
88 | public static void displayMessage(String message) throws InterruptedException | 85 | public static void displayMessage(String message) throws InterruptedException |
89 | { | 86 | { |
90 | displayMessage(new MessageWindow(message)); | 87 | MessageWindow temp = new MessageWindow(message); |
88 | temp.initalizeMessages(message); | ||
89 | displayMessage(temp); | ||
91 | } | 90 | } |
92 | 91 | ||
93 | public static void displayMessage(String message, String faceSet, int face) throws InterruptedException | 92 | public static void displayMessage(String message, String faceSet, int face) throws InterruptedException |
94 | { | 93 | { |
95 | displayMessage(new MessageWindow(message, faceSet, face)); | 94 | MessageWindow temp = new MessageWindow(message, faceSet, face); |
95 | temp.initalizeMessages(message); | ||
96 | displayMessage(temp); | ||
96 | } | 97 | } |
97 | 98 | ||
98 | private void initalizeMessages(String message, Graphics2D g) | 99 | private void initalizeMessages(String message) |
99 | { | 100 | { |
100 | messages = new ArrayList<String>(); | 101 | messages = new ArrayList<String>(); |
101 | 102 | ||
102 | Display.setFont(g); | ||
103 | |||
104 | int length = width - SPACER; | 103 | int length = width - SPACER; |
105 | if (hasFace) | 104 | if (hasFace) |
106 | { | 105 | { |
@@ -111,7 +110,7 @@ public class MessageWindow implements Renderable { | |||
111 | int len = 0; | 110 | int len = 0; |
112 | while (!temp.isEmpty()) | 111 | while (!temp.isEmpty()) |
113 | { | 112 | { |
114 | while ((g.getFontMetrics().stringWidth(temp.substring(0, len)) < length) && (len < temp.length())) | 113 | while ((Display.getFontMetrics().stringWidth(temp.substring(0, len)) < length) && (len < temp.length())) |
115 | { | 114 | { |
116 | len++; | 115 | len++; |
117 | } | 116 | } |
@@ -153,7 +152,7 @@ public class MessageWindow implements Renderable { | |||
153 | { | 152 | { |
154 | if (messages == null) | 153 | if (messages == null) |
155 | { | 154 | { |
156 | initalizeMessages(message, g2); | 155 | initalizeMessages(message); |
157 | } | 156 | } |
158 | 157 | ||
159 | int y = MessageWindowLocation.Bottom.getY(); | 158 | int y = MessageWindowLocation.Bottom.getY(); |
@@ -240,4 +239,5 @@ public class MessageWindow implements Renderable { | |||
240 | return y; | 239 | return y; |
241 | } | 240 | } |
242 | } | 241 | } |
242 | |||
243 | } | 243 | } |