diff 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
line wrap: on
line diff
--- a/game/IDMButton.java	Tue Feb 22 11:45:40 2011 +0200
+++ b/game/IDMButton.java	Wed Feb 23 02:49:07 2011 +0200
@@ -17,13 +17,12 @@
     enum State { FOCUSED, PRESSED, NORMAL }
     State state;
     static BufferedImage imgUp, imgPressed;
-    Point pos;
     Font font;
     FontMetrics metrics;
     String text;
     boolean active;
  
-    public IDMButton(Point pos, int keyCode, Font font, String text)
+    public IDMButton(IDMPoint pos, int keyCode, Font font, String text)
     {
         loadImages();
         this.pos = pos;
@@ -34,21 +33,11 @@
         active = false;
     }
     
-    public IDMButton(int x, int y, int keyCode, Font font, String text)
+    public IDMButton(float x, float y, int keyCode, Font font, String text)
     {
-        this(new Point(x, y), keyCode, font, text);
+        this(new IDMPoint(x, y), keyCode, font, text);
     }
     
-    public void move(Point pos)
-    {
-        this.pos = pos;
-    }
-    
-    public void move(int x, int y)
-    {
-        this.pos = new Point(x, y);
-    }
- 
     private static void loadImages()
     {
         try
@@ -85,20 +74,20 @@
             metrics = g.getFontMetrics(font);
         
         int textWidth = metrics.stringWidth(text);
-        g.drawImage(img, pos.x + xoffs, pos.y + yoffs, null);
+        g.drawImage(img, getScaledX() + xoffs, getScaledY() + yoffs, null);
 
         g.setFont(font);
         g.setPaint(Color.black);
         g.drawString(text,
-            pos.x + xoffs * 2 + (img.getWidth() - textWidth) / 2,
-            pos.y + yoffs * 2 + (img.getHeight() / 2));
+           getScaledX() + xoffs * 2 + (img.getWidth() - textWidth) / 2,
+           getScaledY() + yoffs * 2 + img.getHeight() / 2);
     }
     
     public boolean contains(Point where)
     {
-        return (where.x >= pos.x && where.y >= pos.y &&
-                where.x < pos.x + imgUp.getWidth() &&
-                where.y < pos.y + imgUp.getHeight());
+        return (where.x >= getScaledX() && where.y >= getScaledY() &&
+                where.x < getScaledX() + imgUp.getWidth() &&
+                where.y < getScaledY() + imgUp.getHeight());
     }
     
     public void mousePressed(MouseEvent e)
@@ -117,13 +106,12 @@
     public void mouseEntered(MouseEvent e)
     {
         if (active)
-        {
             state = State.PRESSED;
-        }
     }
 
     public void mouseExited(MouseEvent e)
     {
-        state = State.NORMAL;
+        if (active)
+            state = State.NORMAL;
     }
 }