diff options
Diffstat (limited to 'src/com/fourisland/fourpuzzle/window/ChoiceWindow.java')
| -rwxr-xr-x | src/com/fourisland/fourpuzzle/window/ChoiceWindow.java | 99 |
1 files changed, 89 insertions, 10 deletions
| diff --git a/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java b/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java index 90f2d96..8dce753 100755 --- a/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java +++ b/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java | |||
| @@ -8,12 +8,16 @@ package com.fourisland.fourpuzzle.window; | |||
| 8 | import com.fourisland.fourpuzzle.Audio; | 8 | import com.fourisland.fourpuzzle.Audio; |
| 9 | import com.fourisland.fourpuzzle.Display; | 9 | import com.fourisland.fourpuzzle.Display; |
| 10 | import com.fourisland.fourpuzzle.Game; | 10 | import com.fourisland.fourpuzzle.Game; |
| 11 | import com.fourisland.fourpuzzle.KeyInput; | ||
| 11 | import com.fourisland.fourpuzzle.database.Database; | 12 | import com.fourisland.fourpuzzle.database.Database; |
| 12 | import com.fourisland.fourpuzzle.database.Sound; | 13 | import com.fourisland.fourpuzzle.database.Sound; |
| 14 | import com.fourisland.fourpuzzle.util.Inputable; | ||
| 15 | import com.fourisland.fourpuzzle.util.PauseTimer; | ||
| 13 | import com.fourisland.fourpuzzle.util.Renderable; | 16 | import com.fourisland.fourpuzzle.util.Renderable; |
| 14 | import java.awt.Graphics2D; | 17 | import java.awt.Graphics2D; |
| 15 | import java.awt.Rectangle; | 18 | import java.awt.Rectangle; |
| 16 | import java.awt.TexturePaint; | 19 | import java.awt.TexturePaint; |
| 20 | import java.awt.event.KeyEvent; | ||
| 17 | import java.awt.image.BufferedImage; | 21 | import java.awt.image.BufferedImage; |
| 18 | import java.util.List; | 22 | import java.util.List; |
| 19 | 23 | ||
| @@ -21,9 +25,40 @@ import java.util.List; | |||
| 21 | * | 25 | * |
| 22 | * @author hatkirby | 26 | * @author hatkirby |
| 23 | */ | 27 | */ |
| 24 | public class ChoiceWindow implements Renderable { | 28 | public class ChoiceWindow implements Renderable, Inputable { |
| 25 | 29 | ||
| 26 | private static final int SPACER = 4; | 30 | private static final int SPACER = 4; |
| 31 | |||
| 32 | public static class Builder | ||
| 33 | { | ||
| 34 | List<String> choices; | ||
| 35 | ChoiceWindowLocation location; | ||
| 36 | boolean center = false; | ||
| 37 | int width = 0; | ||
| 38 | |||
| 39 | public Builder(List<String> choices, ChoiceWindowLocation location) | ||
| 40 | { | ||
| 41 | this.choices = choices; | ||
| 42 | this.location = location; | ||
| 43 | } | ||
| 44 | |||
| 45 | public Builder center(boolean center) | ||
| 46 | { | ||
| 47 | this.center = center; | ||
| 48 | return this; | ||
| 49 | } | ||
| 50 | |||
| 51 | public Builder width(int width) | ||
| 52 | { | ||
| 53 | this.width = width; | ||
| 54 | return this; | ||
| 55 | } | ||
| 56 | |||
| 57 | public ChoiceWindow build() | ||
| 58 | { | ||
| 59 | return new ChoiceWindow(this); | ||
| 60 | } | ||
| 61 | } | ||
| 27 | 62 | ||
| 28 | private List<String> choices; | 63 | private List<String> choices; |
| 29 | int numChoices; | 64 | int numChoices; |
| @@ -33,27 +68,36 @@ public class ChoiceWindow implements Renderable { | |||
| 33 | BufferedImage cacheBase; | 68 | BufferedImage cacheBase; |
| 34 | int x; | 69 | int x; |
| 35 | int y; | 70 | int y; |
| 36 | public ChoiceWindow(List<String> choices, boolean center, ChoiceWindowLocation cwl) | 71 | String clickSound; |
| 72 | private ChoiceWindow(Builder builder) | ||
| 37 | { | 73 | { |
| 38 | this.choices = choices; | 74 | this.choices = builder.choices; |
| 39 | numChoices = choices.size(); | 75 | numChoices = choices.size(); |
| 40 | this.center = center; | 76 | this.center = builder.center; |
| 41 | 77 | ||
| 42 | for (String choice : choices) | 78 | for (String choice : choices) |
| 43 | { | 79 | { |
| 44 | int l = Display.getFontMetrics().stringWidth(choice); | 80 | if (builder.width == 0) |
| 45 | if (l > getWidth()) | ||
| 46 | { | 81 | { |
| 47 | width = l; | 82 | int l = Display.getFontMetrics().stringWidth(choice); |
| 83 | if (l > getWidth()) | ||
| 84 | { | ||
| 85 | width = l; | ||
| 86 | } | ||
| 48 | } | 87 | } |
| 49 | 88 | ||
| 50 | height += Display.getFontMetrics().getHeight() + SPACER; | 89 | height += Display.getFontMetrics().getHeight() + SPACER; |
| 51 | } | 90 | } |
| 52 | 91 | ||
| 92 | if (builder.width != 0) | ||
| 93 | { | ||
| 94 | width = builder.width; | ||
| 95 | } | ||
| 96 | |||
| 53 | cacheBase = Window.Default.getImage(width, height); | 97 | cacheBase = Window.Default.getImage(width, height); |
| 54 | 98 | ||
| 55 | x = cwl.getX(width); | 99 | x = builder.location.getX(width); |
| 56 | y = cwl.getY(height); | 100 | y = builder.location.getY(height); |
| 57 | } | 101 | } |
| 58 | 102 | ||
| 59 | public void render(Graphics2D g2) | 103 | public void render(Graphics2D g2) |
| @@ -167,4 +211,39 @@ public class ChoiceWindow implements Renderable { | |||
| 167 | } | 211 | } |
| 168 | } | 212 | } |
| 169 | 213 | ||
| 214 | Boolean hasInput = false; | ||
| 215 | PauseTimer pt = new PauseTimer(0); | ||
| 216 | public void processInput(KeyInput key) | ||
| 217 | { | ||
| 218 | if (key.getKey() == KeyEvent.VK_UP) | ||
| 219 | { | ||
| 220 | if (pt.isElapsed()) | ||
| 221 | { | ||
| 222 | moveUp(); | ||
| 223 | pt.setTimer(1); | ||
| 224 | } | ||
| 225 | } else if (key.getKey() == KeyEvent.VK_DOWN) | ||
| 226 | { | ||
| 227 | if (pt.isElapsed()) | ||
| 228 | { | ||
| 229 | moveDown(); | ||
| 230 | pt.setTimer(1); | ||
| 231 | } | ||
| 232 | } else if (key.isActionDown()) | ||
| 233 | { | ||
| 234 | synchronized (hasInput) | ||
| 235 | { | ||
| 236 | hasInput = true; | ||
| 237 | } | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | public boolean hasInput() | ||
| 242 | { | ||
| 243 | synchronized (hasInput) | ||
| 244 | { | ||
| 245 | return hasInput; | ||
| 246 | } | ||
| 247 | } | ||
| 248 | |||
| 170 | } \ No newline at end of file | 249 | } \ No newline at end of file |
