diff options
author | Starla Insigna <hatkirby@fourisland.com> | 2009-02-08 12:09:25 -0500 |
---|---|---|
committer | Starla Insigna <hatkirby@fourisland.com> | 2009-02-08 12:09:25 -0500 |
commit | eca79b20cdb258768668c2ecf1b7333de62c7d7f (patch) | |
tree | d8999e81b3569aeff85138dc8df244c9c290a7a0 /src/com | |
parent | 3724f4ecbe61e6621d4a7e993cd80ab75ee41266 (diff) | |
download | fourpuzzle-eca79b20cdb258768668c2ecf1b7333de62c7d7f.tar.gz fourpuzzle-eca79b20cdb258768668c2ecf1b7333de62c7d7f.tar.bz2 fourpuzzle-eca79b20cdb258768668c2ecf1b7333de62c7d7f.zip |
Implemented a basic ChoiceWindow
This window should be used when there are a few choices available (such as in TitleScreenGameState and MenuGameState) and one needs to be chosen. The implementation currently is bad, the code is messy and it could be optimized. Plus, there are a few bugs in it and no choice can currently be made, only the rendering has been done.
Diffstat (limited to 'src/com')
6 files changed, 260 insertions, 0 deletions
diff --git a/src/com/fourisland/fourpuzzle/Database.java b/src/com/fourisland/fourpuzzle/Database.java index 8d51f9a..b6e97cf 100644 --- a/src/com/fourisland/fourpuzzle/Database.java +++ b/src/com/fourisland/fourpuzzle/Database.java | |||
@@ -31,7 +31,13 @@ public class Database { | |||
31 | 31 | ||
32 | private static void loadDefaultVocabulary() | 32 | private static void loadDefaultVocabulary() |
33 | { | 33 | { |
34 | /* Global */ | ||
34 | vocabulary.put("Title", "Untitled Game"); | 35 | vocabulary.put("Title", "Untitled Game"); |
36 | |||
37 | /* TitleScreen */ | ||
38 | vocabulary.put("NewGame", "New Game"); | ||
39 | vocabulary.put("LoadGame", "Load Game"); | ||
40 | vocabulary.put("EndGame", "End"); | ||
35 | } | 41 | } |
36 | 42 | ||
37 | public static String getVocab(String key) | 43 | public static String getVocab(String key) |
diff --git a/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java b/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java index 9247282..621ec0d 100644 --- a/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java +++ b/src/com/fourisland/fourpuzzle/gamestate/TitleScreenGameState.java | |||
@@ -10,8 +10,10 @@ import com.fourisland.fourpuzzle.gamestate.mapview.MapViewGameState; | |||
10 | import com.fourisland.fourpuzzle.transition.SquareTransition; | 10 | import com.fourisland.fourpuzzle.transition.SquareTransition; |
11 | import com.fourisland.fourpuzzle.transition.TransitionDirection; | 11 | import com.fourisland.fourpuzzle.transition.TransitionDirection; |
12 | import com.fourisland.fourpuzzle.util.ObjectLoader; | 12 | import com.fourisland.fourpuzzle.util.ObjectLoader; |
13 | import com.fourisland.fourpuzzle.window.ChoiceWindow; | ||
13 | import java.awt.Graphics2D; | 14 | import java.awt.Graphics2D; |
14 | import java.awt.event.KeyEvent; | 15 | import java.awt.event.KeyEvent; |
16 | import java.util.Arrays; | ||
15 | 17 | ||
16 | /** | 18 | /** |
17 | * | 19 | * |
@@ -19,9 +21,17 @@ import java.awt.event.KeyEvent; | |||
19 | */ | 21 | */ |
20 | public class TitleScreenGameState implements GameState { | 22 | public class TitleScreenGameState implements GameState { |
21 | 23 | ||
24 | ChoiceWindow choices; | ||
25 | int wx, wy; | ||
26 | |||
22 | public void initalize() | 27 | public void initalize() |
23 | { | 28 | { |
24 | Audio.playMusic(Database.getMusic("Title")); | 29 | Audio.playMusic(Database.getMusic("Title")); |
30 | |||
31 | choices = new ChoiceWindow(Arrays.asList(Database.getVocab("NewGame"), Database.getVocab("LoadGame"), Database.getVocab("EndGame"))); | ||
32 | |||
33 | wx = (Game.WIDTH/2)-(choices.getWidth()/2); | ||
34 | wy = (Game.HEIGHT/4*3)-(choices.getHeight()/2); | ||
25 | } | 35 | } |
26 | 36 | ||
27 | public void deinitalize() | 37 | public void deinitalize() |
@@ -63,6 +73,7 @@ public class TitleScreenGameState implements GameState { | |||
63 | public void render(Graphics2D g) | 73 | public void render(Graphics2D g) |
64 | { | 74 | { |
65 | g.drawImage(ObjectLoader.getImage("Picture", "Title"), 0, 0, null); | 75 | g.drawImage(ObjectLoader.getImage("Picture", "Title"), 0, 0, null); |
76 | choices.render(g, wx, wy); | ||
66 | } | 77 | } |
67 | 78 | ||
68 | } | 79 | } |
diff --git a/src/com/fourisland/fourpuzzle/util/TransparentPixelFilter.java b/src/com/fourisland/fourpuzzle/util/TransparentPixelFilter.java new file mode 100644 index 0000000..ed1c49c --- /dev/null +++ b/src/com/fourisland/fourpuzzle/util/TransparentPixelFilter.java | |||
@@ -0,0 +1,35 @@ | |||
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 java.awt.image.RGBImageFilter; | ||
9 | |||
10 | /** | ||
11 | * | ||
12 | * @author hatkirby | ||
13 | */ | ||
14 | public class TransparentPixelFilter extends RGBImageFilter { | ||
15 | |||
16 | private int trans; | ||
17 | public TransparentPixelFilter(int i) | ||
18 | { | ||
19 | trans = i; | ||
20 | |||
21 | canFilterIndexColorModel = true; | ||
22 | } | ||
23 | |||
24 | @Override | ||
25 | public int filterRGB(int x, int y, int rgb) | ||
26 | { | ||
27 | if (rgb == trans) | ||
28 | { | ||
29 | return 0; | ||
30 | } else { | ||
31 | return rgb; | ||
32 | } | ||
33 | } | ||
34 | |||
35 | } | ||
diff --git a/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java b/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java new file mode 100644 index 0000000..ed20ab0 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/window/ChoiceWindow.java | |||
@@ -0,0 +1,109 @@ | |||
1 | /* | ||
2 | * To change this template, choose Tools | Templates | ||
3 | * and open the template in the editor. | ||
4 | */ | ||
5 | |||
6 | package com.fourisland.fourpuzzle.window; | ||
7 | |||
8 | import com.fourisland.fourpuzzle.util.TransparentPixelFilter; | ||
9 | import java.awt.Font; | ||
10 | import java.awt.Toolkit; | ||
11 | import java.awt.Graphics2D; | ||
12 | import java.awt.Rectangle; | ||
13 | import java.awt.TexturePaint; | ||
14 | import java.awt.image.BufferedImage; | ||
15 | import java.awt.image.FilteredImageSource; | ||
16 | import java.util.List; | ||
17 | import static com.fourisland.fourpuzzle.window.SystemChoiceArea.*; | ||
18 | |||
19 | /* TODO Find a more elegant way to implement window, it looks terrible now */ | ||
20 | |||
21 | /** | ||
22 | * | ||
23 | * @author hatkirby | ||
24 | */ | ||
25 | public class ChoiceWindow { | ||
26 | |||
27 | private final int SPACER = 4; | ||
28 | |||
29 | private List<String> choices; | ||
30 | int numChoices; | ||
31 | public ChoiceWindow(List<String> choices) | ||
32 | { | ||
33 | this.choices = choices; | ||
34 | numChoices = choices.size(); | ||
35 | |||
36 | createGraphic(new BufferedImage(TopLeft.getWidth()+getWidth()+TopRight.getWidth(), TopLeft.getHeight()+getHeight()+BottomLeft.getHeight(), BufferedImage.TYPE_INT_ARGB).createGraphics()); | ||
37 | } | ||
38 | |||
39 | private int width; | ||
40 | private int height; | ||
41 | BufferedImage cacheBase; | ||
42 | private void createGraphic(Graphics2D g3) | ||
43 | { | ||
44 | for (String choice : choices) | ||
45 | { | ||
46 | int l = g3.getFontMetrics().stringWidth(choice); | ||
47 | if (l > getWidth()) | ||
48 | { | ||
49 | width = l; | ||
50 | } | ||
51 | |||
52 | height += g3.getFontMetrics().getHeight() + SPACER; | ||
53 | } | ||
54 | |||
55 | width += SPACER*2; | ||
56 | height -= SPACER; | ||
57 | |||
58 | BufferedImage temp = new BufferedImage(TopLeft.getWidth()+getWidth()+TopRight.getWidth(), TopLeft.getHeight()+getHeight()+BottomLeft.getHeight(), BufferedImage.TYPE_INT_ARGB); | ||
59 | Graphics2D g = temp.createGraphics(); | ||
60 | |||
61 | g.drawImage(SystemGraphic.getChoiceArea(TopLeft), 0, 0, null); | ||
62 | g.drawImage(SystemGraphic.getChoiceArea(Top), TopLeft.getWidth(), 0, getWidth(),Top.getHeight(), null); | ||
63 | g.drawImage(SystemGraphic.getChoiceArea(TopRight), TopLeft.getWidth()+getWidth(), 0, null); | ||
64 | g.drawImage(SystemGraphic.getChoiceArea(Left), 0, TopLeft.getHeight(), Left.getWidth(),getHeight(), null); | ||
65 | g.drawImage(SystemGraphic.getChoiceArea(BottomLeft), 0, TopLeft.getHeight()+getHeight(), null); | ||
66 | g.drawImage(SystemGraphic.getChoiceArea(Bottom), BottomLeft.getWidth(), (getHeight()+TopLeft.getHeight()+BottomLeft.getHeight())-Bottom.getHeight(), getWidth(),Bottom.getHeight(), null); | ||
67 | g.drawImage(SystemGraphic.getChoiceArea(BottomRight), BottomRight.getWidth()+getWidth(), TopRight.getHeight()+getHeight(), null); | ||
68 | g.drawImage(SystemGraphic.getChoiceArea(Right), (getWidth()+TopLeft.getWidth()+TopRight.getWidth())-Right.getWidth(), TopRight.getHeight(), Right.getWidth(),getHeight(), null); | ||
69 | |||
70 | cacheBase = new BufferedImage(TopLeft.getWidth()+getWidth()+TopRight.getWidth(), TopLeft.getHeight()+getHeight()+BottomLeft.getHeight(), BufferedImage.TYPE_INT_ARGB); | ||
71 | Graphics2D g2 = cacheBase.createGraphics(); | ||
72 | |||
73 | g2.drawImage(SystemGraphic.getMessageBackground(), 1, 1, TopLeft.getWidth()+getWidth()+TopRight.getWidth()-2, TopLeft.getHeight()+getHeight()+BottomLeft.getHeight()-2, null); | ||
74 | g2.drawImage(Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(temp.getSource(), new TransparentPixelFilter(-25600))), 0, 0, null); | ||
75 | g2.setFont(g2.getFont().deriveFont(Font.BOLD)); | ||
76 | |||
77 | int tx = TopLeft.getWidth(); | ||
78 | int ty = TopLeft.getHeight()+g2.getFontMetrics().getHeight()-SPACER; | ||
79 | for (String choice : choices) | ||
80 | { | ||
81 | /* TODO The following code paints the text onto the window. However, | ||
82 | * when it paints the lowercase 'y', the tail is white, not the | ||
83 | * correct gradient. */ | ||
84 | |||
85 | g2.setPaint(new TexturePaint(SystemGraphic.getTextColor(), new Rectangle(tx, ty, g2.getFontMetrics().stringWidth(choice),g2.getFontMetrics().getHeight()))); | ||
86 | g2.drawString(choice, tx, ty); | ||
87 | |||
88 | ty+=(SPACER+g2.getFontMetrics().getHeight()); | ||
89 | } | ||
90 | |||
91 | g2.setFont(g2.getFont().deriveFont(Font.PLAIN)); | ||
92 | } | ||
93 | |||
94 | public void render(Graphics2D g2, int x, int y) | ||
95 | { | ||
96 | g2.drawImage(cacheBase, x, y, null); | ||
97 | } | ||
98 | |||
99 | public int getWidth() | ||
100 | { | ||
101 | return width; | ||
102 | } | ||
103 | |||
104 | public int getHeight() | ||
105 | { | ||
106 | return height; | ||
107 | } | ||
108 | |||
109 | } | ||
diff --git a/src/com/fourisland/fourpuzzle/window/SystemChoiceArea.java b/src/com/fourisland/fourpuzzle/window/SystemChoiceArea.java new file mode 100644 index 0000000..8908c10 --- /dev/null +++ b/src/com/fourisland/fourpuzzle/window/SystemChoiceArea.java | |||
@@ -0,0 +1,56 @@ | |||
1 | /* | ||
2 | * To change this template, choose Tools | Templates | ||
3 | * and open the template in the editor. | ||
4 | */ | ||
5 | |||
6 | package com.fourisland.fourpuzzle.window; | ||
7 | |||
8 | /** | ||
9 | * | ||
10 | * @author hatkirby | ||
11 | */ | ||
12 | public enum SystemChoiceArea | ||
13 | { | ||
14 | Top(47, 0, 1, 4), | ||
15 | TopRight(56, 0, 8, 8), | ||
16 | Right(60, 15, 4, 1), | ||
17 | BottomRight(56, 24, 8, 8), | ||
18 | Bottom(47, 28, 1, 4), | ||
19 | BottomLeft(32, 24, 8, 8), | ||
20 | Left(32, 15, 4, 1), | ||
21 | TopLeft(32, 0, 8, 8), | ||
22 | UpArrow(43, 9, 10, 6), | ||
23 | DownArrow(43, 17, 10, 6); | ||
24 | |||
25 | private final int x; | ||
26 | private final int y; | ||
27 | private final int width; | ||
28 | private final int height; | ||
29 | private SystemChoiceArea(int x, int y, int width, int height) | ||
30 | { | ||
31 | this.x = x; | ||
32 | this.y = y; | ||
33 | this.width = width; | ||
34 | this.height = height; | ||
35 | } | ||
36 | |||
37 | public int getX() | ||
38 | { | ||
39 | return x; | ||
40 | } | ||
41 | |||
42 | public int getY() | ||
43 | { | ||
44 | return y; | ||
45 | } | ||
46 | |||
47 | public int getWidth() | ||
48 | { | ||
49 | return width; | ||
50 | } | ||
51 | |||
52 | public int getHeight() | ||
53 | { | ||
54 | return height; | ||
55 | } | ||
56 | } \ No newline at end of file | ||
diff --git a/src/com/fourisland/fourpuzzle/window/SystemGraphic.java b/src/com/fourisland/fourpuzzle/window/SystemGraphic.java new file mode 100644 index 0000000..70fc7fe --- /dev/null +++ b/src/com/fourisland/fourpuzzle/window/SystemGraphic.java | |||
@@ -0,0 +1,43 @@ | |||
1 | /* | ||
2 | * To change this template, choose Tools | Templates | ||
3 | * and open the template in the editor. | ||
4 | */ | ||
5 | |||
6 | package com.fourisland.fourpuzzle.window; | ||
7 | |||
8 | import com.fourisland.fourpuzzle.util.ObjectLoader; | ||
9 | import java.awt.image.BufferedImage; | ||
10 | |||
11 | /** | ||
12 | * | ||
13 | * @author hatkirby | ||
14 | */ | ||
15 | public class SystemGraphic { | ||
16 | |||
17 | private static BufferedImage systemGraphic; | ||
18 | static | ||
19 | { | ||
20 | initalize("System"); | ||
21 | } | ||
22 | |||
23 | public static void initalize(String graphic) | ||
24 | { | ||
25 | systemGraphic = ObjectLoader.getImage("Picture", graphic); | ||
26 | } | ||
27 | |||
28 | public static BufferedImage getMessageBackground() | ||
29 | { | ||
30 | return systemGraphic.getSubimage(0, 0, 32, 32); | ||
31 | } | ||
32 | |||
33 | public static BufferedImage getChoiceArea(SystemChoiceArea sca) | ||
34 | { | ||
35 | return systemGraphic.getSubimage(sca.getX(), sca.getY(), sca.getWidth(), sca.getHeight()); | ||
36 | } | ||
37 | |||
38 | public static BufferedImage getTextColor() | ||
39 | { | ||
40 | return systemGraphic.getSubimage(0, 48, 16, 16); | ||
41 | } | ||
42 | |||
43 | } | ||