changeset 51:f81f76458b92

Work on widgets.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 23 Feb 2011 02:49:07 +0200
parents 496e616ff09d
children 01851bae3da3
files Makefile Ristipolku.html game/Engine.java game/IDMButton.java game/IDMContainer.java game/IDMWidget.java
diffstat 6 files changed, 70 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Tue Feb 22 11:45:40 2011 +0200
+++ b/Makefile	Wed Feb 23 02:49:07 2011 +0200
@@ -7,8 +7,8 @@
 	game/Engine.class game/Interpolate.class \
 	game/ResourceLoader.class game/Sound.class \
 	game/SoundElement.class game/SoundManager.class \
-	game/IDMWidget.class game/IDMButton.class \
-	game/IDMContainer.class
+	game/IDMPoint.class game/IDMWidget.class \
+	game/IDMButton.class game/IDMContainer.class
 
 # Utils
 JAVAC=javac -g
--- a/Ristipolku.html	Tue Feb 22 11:45:40 2011 +0200
+++ b/Ristipolku.html	Wed Feb 23 02:49:07 2011 +0200
@@ -20,6 +20,6 @@
  </script>
 </head>
 <body>
-<applet code="Ristipolku.class" archive="Ristipolku.jar" width="800" height="600"></applet>
+<applet code="Ristipolku.class" archive="Ristipolku.jar" width="1024" height="768"></applet>
 </body>
 </html>
--- a/game/Engine.java	Tue Feb 22 11:45:40 2011 +0200
+++ b/game/Engine.java	Wed Feb 23 02:49:07 2011 +0200
@@ -91,7 +91,7 @@
 //        sndPlaced = soundManager.getSound("sounds/placed.wav");
     }
 
-    public void paint(Graphics2D g, int sx, int sy, float scale)
+    public void paint(Graphics2D g, float sx, float sy, float scale)
     {
         for (int y = 0; y < boardSize; y++)
         for (int x = 0; x < boardSize; x++)
@@ -116,7 +116,6 @@
         {
             board[x][y].animate(time);
         }
-
     }
    
     public void pieceRotate(Piece.RotateDir dir)
@@ -252,8 +251,6 @@
     Sound musa;
 
     IDMContainer widgets;
-    BtnNewGame btnNewGame;
-    BtnSwapPiece btnSwapPiece;
 
     public Engine()
     {
@@ -294,11 +291,9 @@
 
         // UI IDM widgets
         widgets = new IDMContainer();
-        btnNewGame = new BtnNewGame();
-        widgets.add(btnNewGame);
-
-        btnSwapPiece = new BtnSwapPiece();
-        widgets.add(btnSwapPiece);
+        
+        widgets.add(new BtnNewGame(0.75f, 0.75f));
+        widgets.add(new BtnSwapPiece(0.75f, 0.85f));
 
         // Game
         startNewGame();
@@ -349,10 +344,8 @@
             gimg.drawImage(lautaBG, 0, 0, dim.width, dim.height, null); 
             lautaDim = dim;
 
+            widgets.setScale(dim.width, dim.height);
             System.out.print("scale changed\n");
-            
-            btnNewGame.move(dim.width - 200, dim.height - 150);
-            btnSwapPiece.move(dim.width - 200, dim.height - 400);
         }
         
         // Background, pieces
@@ -463,9 +456,9 @@
     
     class BtnNewGame extends IDMButton
     {
-        public BtnNewGame()
+        public BtnNewGame(float x, float y)
         {
-            super(0, 0, KeyEvent.VK_ESCAPE, font1, "New Game");
+            super(x, y, KeyEvent.VK_ESCAPE, font1, "Uusi peli");
         }
 
         public void clicked()
@@ -476,9 +469,9 @@
 
     class BtnSwapPiece extends IDMButton
     {
-        public BtnSwapPiece()
+        public BtnSwapPiece(float x, float y)
         {
-            super(0, 0, KeyEvent.VK_SPACE, font1, "Swap Piece");
+            super(x, y, KeyEvent.VK_SPACE, font1, "Vaihda");
         }
 
         public void clicked()
--- 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;
     }
 }
--- a/game/IDMContainer.java	Tue Feb 22 11:45:40 2011 +0200
+++ b/game/IDMContainer.java	Wed Feb 23 02:49:07 2011 +0200
@@ -72,4 +72,12 @@
         }
         return false;
     }
+
+    public void setScale(IDMPoint scale)
+    {
+        for (IDMWidget widget : widgets)
+        {
+            widget.setScale(scale);
+        }
+    }
 }
--- a/game/IDMWidget.java	Tue Feb 22 11:45:40 2011 +0200
+++ b/game/IDMWidget.java	Wed Feb 23 02:49:07 2011 +0200
@@ -10,11 +10,50 @@
 
 public class IDMWidget
 {
+    IDMPoint pos, scale;
     int keyCode;
     
     public IDMWidget()
     {
         keyCode = -1;
+        this.scale = new IDMPoint(1, 1);
+        this.pos = new IDMPoint(0, 0);
+    }
+
+    public IDMWidget(IDMPoint pos)
+    {
+        this();
+        this.pos = pos;
+    }
+
+    public void move(IDMPoint pos)
+    {
+        this.pos = pos;
+    }
+
+    public void move(float x, float y)
+    {
+        this.pos = new IDMPoint(x, y);
+    }
+
+    public void setScale(IDMPoint scale)
+    {
+        this.scale = scale;
+    }
+    
+    public void setScale(float x, float y)
+    {
+        this.setScale(new IDMPoint(x, y));
+    }
+
+    public int getScaledX()
+    {
+        return (int) (this.pos.x * this.scale.x);
+    }
+
+    public int getScaledY()
+    {
+        return (int) (this.pos.y * this.scale.y);
     }
 
     public void paint(Graphics2D g)