Mercurial > hg > ristipolku
annotate game/IDMButton.java @ 169:32b1c41e194a
Implement smooth scaling of buttons.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 02 Mar 2017 14:18:21 +0200 |
parents | dda7152d2402 |
children | 981a8e20b363 |
rev | line source |
---|---|
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 * Ristipolku IDM button widget |
151 | 3 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org> |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
4 */ |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 package game; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 import java.awt.*; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
8 import java.awt.event.*; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 import java.awt.image.*; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 import java.awt.geom.*; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
11 import javax.imageio.*; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 import java.io.*; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
13 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
14 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
15 public class IDMButton extends IDMWidget |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
16 { |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
17 enum State { FOCUSED, PRESSED, NORMAL } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
18 State state; |
169
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
19 static BufferedImage imgOrigUnpressed, imgOrigPressed; |
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
20 static BufferedImage imgUnpressed, imgPressed; |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 Font font; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
22 FontMetrics metrics; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
23 String text; |
50 | 24 boolean active; |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
133
diff
changeset
|
25 |
51 | 26 public IDMButton(IDMPoint pos, int keyCode, Font font, String text) |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
27 { |
75 | 28 super(pos); |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 loadImages(); |
169
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
30 setSize(imgOrigUnpressed.getWidth(), imgOrigUnpressed.getHeight()); |
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
31 setScale(1, 1); |
75 | 32 |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
33 this.font = font; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
34 this.keyCode = keyCode; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
35 this.text = text; |
75 | 36 |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 state = State.NORMAL; |
50 | 38 active = false; |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
39 } |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
133
diff
changeset
|
40 |
51 | 41 public IDMButton(float x, float y, int keyCode, Font font, String text) |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 { |
51 | 43 this(new IDMPoint(x, y), keyCode, font, text); |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 } |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
133
diff
changeset
|
45 |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 private static void loadImages() |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 { |
169
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
48 if (imgUnpressed != null && imgPressed != null) |
64
bca0112851d6
Only load button graphics once.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
49 return; |
bca0112851d6
Only load button graphics once.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
50 |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 try |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 { |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
53 ResourceLoader res = new ResourceLoader("graphics/button1_up.png"); |
169
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
54 imgOrigUnpressed = ImageIO.read(res.getStream()); |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
56 res = new ResourceLoader("graphics/button1_down.png"); |
169
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
57 imgOrigPressed = ImageIO.read(res.getStream()); |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 catch (IOException e) |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 { |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 System.out.print(e.getMessage()); |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 } |
133
881deac2daf8
Some more work on scaling widgets, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
64 |
169
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
65 public BufferedImage scaleImage(BufferedImage src) |
133
881deac2daf8
Some more work on scaling widgets, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
66 { |
169
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
67 BufferedImage dst = new BufferedImage(getScaledWidth(), getScaledHeight(), BufferedImage.TYPE_INT_ARGB); |
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
68 Graphics2D gimg = dst.createGraphics(); |
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
69 |
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
70 gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, |
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
71 RenderingHints.VALUE_INTERPOLATION_BICUBIC); |
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
72 |
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
73 gimg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
74 RenderingHints.VALUE_ANTIALIAS_ON); |
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
75 |
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
76 gimg.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, |
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
77 RenderingHints.VALUE_TEXT_ANTIALIAS_ON); |
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
78 |
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
79 gimg.drawImage(src, 0, 0, getScaledWidth(), getScaledHeight(), null); |
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
80 return dst; |
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
81 } |
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
82 |
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
83 public void setScale(IDMPoint scale) |
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
84 { |
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
85 super.setScale(scale); |
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
86 imgPressed = scaleImage(imgOrigPressed); |
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
87 imgUnpressed = scaleImage(imgOrigUnpressed); |
133
881deac2daf8
Some more work on scaling widgets, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
88 } |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
133
diff
changeset
|
89 |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
90 public void paint(Graphics2D g) |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
91 { |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
92 BufferedImage img; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
93 int xoffs, yoffs; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
94 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
95 if (state == State.PRESSED) |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
96 { |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
97 img = imgPressed; |
50 | 98 xoffs = yoffs = 5; |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
99 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
100 else |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
101 { |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
102 xoffs = yoffs = 0; |
169
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
103 img = imgUnpressed; |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
104 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
105 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
106 if (metrics == null) |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
107 metrics = g.getFontMetrics(font); |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
133
diff
changeset
|
108 |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
109 int textWidth = metrics.stringWidth(text); |
169
32b1c41e194a
Implement smooth scaling of buttons.
Matti Hamalainen <ccr@tnsp.org>
parents:
163
diff
changeset
|
110 g.drawImage(img, getScaledX() + xoffs, getScaledY() + yoffs, null); |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
111 g.setFont(font); |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
112 g.setPaint(Color.black); |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
113 g.drawString(text, |
75 | 114 getScaledX() + xoffs + (getScaledWidth() - textWidth) / 2, |
115 getScaledY() + yoffs + getScaledHeight() / 2); | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
116 } |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
133
diff
changeset
|
117 |
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
133
diff
changeset
|
118 |
75 | 119 public boolean mousePressed(MouseEvent e) |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
120 { |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
121 state = State.PRESSED; |
50 | 122 active = true; |
75 | 123 return true; |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
124 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
125 |
75 | 126 public boolean mouseReleased(MouseEvent e) |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
127 { |
75 | 128 boolean oldActive = active; |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
129 super.mouseReleased(e); |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
130 state = State.NORMAL; |
50 | 131 active = false; |
75 | 132 return oldActive; |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
133 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
134 |
75 | 135 public boolean mouseEntered(MouseEvent e) |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
136 { |
50 | 137 if (active) |
75 | 138 { |
50 | 139 state = State.PRESSED; |
75 | 140 return true; |
141 } | |
142 else | |
143 return false; | |
50 | 144 } |
145 | |
75 | 146 public boolean mouseExited(MouseEvent e) |
50 | 147 { |
51 | 148 if (active) |
75 | 149 { |
51 | 150 state = State.NORMAL; |
75 | 151 return true; |
152 } | |
153 else | |
154 return false; | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
155 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
156 } |