comparison game/IDMButton.java @ 51:f81f76458b92

Work on widgets.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 23 Feb 2011 02:49:07 +0200
parents 496e616ff09d
children bca0112851d6
comparison
equal deleted inserted replaced
50:496e616ff09d 51:f81f76458b92
15 public class IDMButton extends IDMWidget 15 public class IDMButton extends IDMWidget
16 { 16 {
17 enum State { FOCUSED, PRESSED, NORMAL } 17 enum State { FOCUSED, PRESSED, NORMAL }
18 State state; 18 State state;
19 static BufferedImage imgUp, imgPressed; 19 static BufferedImage imgUp, imgPressed;
20 Point pos;
21 Font font; 20 Font font;
22 FontMetrics metrics; 21 FontMetrics metrics;
23 String text; 22 String text;
24 boolean active; 23 boolean active;
25 24
26 public IDMButton(Point pos, int keyCode, Font font, String text) 25 public IDMButton(IDMPoint pos, int keyCode, Font font, String text)
27 { 26 {
28 loadImages(); 27 loadImages();
29 this.pos = pos; 28 this.pos = pos;
30 this.font = font; 29 this.font = font;
31 this.keyCode = keyCode; 30 this.keyCode = keyCode;
32 this.text = text; 31 this.text = text;
33 state = State.NORMAL; 32 state = State.NORMAL;
34 active = false; 33 active = false;
35 } 34 }
36 35
37 public IDMButton(int x, int y, int keyCode, Font font, String text) 36 public IDMButton(float x, float y, int keyCode, Font font, String text)
38 { 37 {
39 this(new Point(x, y), keyCode, font, text); 38 this(new IDMPoint(x, y), keyCode, font, text);
40 } 39 }
41 40
42 public void move(Point pos)
43 {
44 this.pos = pos;
45 }
46
47 public void move(int x, int y)
48 {
49 this.pos = new Point(x, y);
50 }
51
52 private static void loadImages() 41 private static void loadImages()
53 { 42 {
54 try 43 try
55 { 44 {
56 ResourceLoader res = new ResourceLoader("graphics/button1_up.png"); 45 ResourceLoader res = new ResourceLoader("graphics/button1_up.png");
83 72
84 if (metrics == null) 73 if (metrics == null)
85 metrics = g.getFontMetrics(font); 74 metrics = g.getFontMetrics(font);
86 75
87 int textWidth = metrics.stringWidth(text); 76 int textWidth = metrics.stringWidth(text);
88 g.drawImage(img, pos.x + xoffs, pos.y + yoffs, null); 77 g.drawImage(img, getScaledX() + xoffs, getScaledY() + yoffs, null);
89 78
90 g.setFont(font); 79 g.setFont(font);
91 g.setPaint(Color.black); 80 g.setPaint(Color.black);
92 g.drawString(text, 81 g.drawString(text,
93 pos.x + xoffs * 2 + (img.getWidth() - textWidth) / 2, 82 getScaledX() + xoffs * 2 + (img.getWidth() - textWidth) / 2,
94 pos.y + yoffs * 2 + (img.getHeight() / 2)); 83 getScaledY() + yoffs * 2 + img.getHeight() / 2);
95 } 84 }
96 85
97 public boolean contains(Point where) 86 public boolean contains(Point where)
98 { 87 {
99 return (where.x >= pos.x && where.y >= pos.y && 88 return (where.x >= getScaledX() && where.y >= getScaledY() &&
100 where.x < pos.x + imgUp.getWidth() && 89 where.x < getScaledX() + imgUp.getWidth() &&
101 where.y < pos.y + imgUp.getHeight()); 90 where.y < getScaledY() + imgUp.getHeight());
102 } 91 }
103 92
104 public void mousePressed(MouseEvent e) 93 public void mousePressed(MouseEvent e)
105 { 94 {
106 state = State.PRESSED; 95 state = State.PRESSED;
115 } 104 }
116 105
117 public void mouseEntered(MouseEvent e) 106 public void mouseEntered(MouseEvent e)
118 { 107 {
119 if (active) 108 if (active)
120 {
121 state = State.PRESSED; 109 state = State.PRESSED;
122 }
123 } 110 }
124 111
125 public void mouseExited(MouseEvent e) 112 public void mouseExited(MouseEvent e)
126 { 113 {
127 state = State.NORMAL; 114 if (active)
115 state = State.NORMAL;
128 } 116 }
129 } 117 }