changeset 49:e6da5c71be28

Add more code to IDM widgets, do preliminary work for integrating them.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 22 Feb 2011 10:52:08 +0200
parents f13bab4cccd3
children 496e616ff09d
files Makefile game/Engine.java game/IDMButton.java game/IDMContainer.java game/IDMWidget.java
diffstat 5 files changed, 283 insertions(+), 118 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Tue Feb 22 07:59:41 2011 +0200
+++ b/Makefile	Tue Feb 22 10:52:08 2011 +0200
@@ -6,8 +6,9 @@
 CLASSES=game/Piece.class game/PieceType.class \
 	game/Engine.class game/Interpolate.class \
 	game/ResourceLoader.class game/Sound.class \
-	game/SoundElement.class \
-	game/SoundManager.class
+	game/SoundElement.class game/SoundManager.class \
+	game/IDMWidget.class game/IDMButton.class \
+	game/IDMContainer.class
 
 # Utils
 JAVAC=javac -g
--- a/game/Engine.java	Tue Feb 22 07:59:41 2011 +0200
+++ b/game/Engine.java	Tue Feb 22 10:52:08 2011 +0200
@@ -56,106 +56,6 @@
 }
 */
 
-class IDMWidget
-{
-    int keyCode;
-    
-    public IDMWidget()
-    {
-        keyCode = -1;
-    }
-
-    public void paint(Graphics2D g)
-    {
-    }
-    
-    public boolean contains(Point pos)
-    {
-        return false;
-    }
-    
-    public void mousePressed(MouseEvent e)
-    {
-    }
-
-    public void mouseReleased(MouseEvent e)
-    {
-        if (contains(e.getPoint()))
-        {
-            clicked();
-        }
-    }
-
-    // Generic key handler
-    public boolean keyHandler(KeyEvent e)
-    {
-        if (e.getKeyCode() == keyCode)
-        {
-            clicked();
-            return true;
-        }
-        else
-            return false;
-    }
-    
-    public void clicked()
-    {
-    }
-}
-
-class IDMButton extends IDMWidget
-{
-    enum State { FOCUSED, PRESSED, NORMAL }
-    State state;
-    BufferedImage imgUp, imgPressed, img;
-    Point pos;
- 
-    public IDMButton(float x, float y, int key, String text)
-    {
-        try
-        {
-            ResourceLoader res = new ResourceLoader("graphics/button1_up.png");
-            imgUp = ImageIO.read(res.getStream());
-
-            res = new ResourceLoader("graphics/button1_down.png");
-            imgPressed = ImageIO.read(res.getStream());
-        }
-        catch (IOException e)
-        {
-            System.out.print(e.getMessage());
-        }
-        
-        keyCode = key;
-        setState(State.NORMAL);
-    }
-    
-    private void setState(State newState)
-    {
-        state = newState;
-        if (state == State.PRESSED)
-            img = imgPressed;
-        else
-            img = imgUp;
-    }
-
-    public void paint(Graphics2D g)
-    {
-        g.drawImage(img, pos.x, pos.y, null);
-    }
-
-    public boolean contains(Point where)
-    {
-        return (where.x >= pos.x && where.y >= pos.y &&
-                where.x < pos.x + img.getWidth() &&
-                where.y < pos.y + img.getHeight());
-    }
-    
-    public void clicked()
-    {
-    }
-}
-
-
 class GameBoard extends IDMWidget
 {
     public static final int boardSize = 9;
@@ -310,7 +210,7 @@
         }
     }
 
-    public boolean keyHandler(KeyEvent e)
+    public boolean keyPressed(KeyEvent e)
     {
         switch (e.getKeyCode())
         {
@@ -347,20 +247,18 @@
     BufferedImage lautaBG = null, lautaBGScaled = null;
     Dimension lautaDim;
 
-
     static final AudioFormat sfmt = new AudioFormat(22050, 16, 1, true, false);
     SoundManager soundManager;
     Sound musa;
 
+    IDMContainer widgets;
+    BtnNewGame btnNewGame;
+
     public Engine()
     {
         // Initialize globals
         System.out.print("Engine() constructor\n");
         
-        gameClock = 0;
-        gameFrames = 0;
-        startTime = new Date().getTime();
-
         // Sound system
         soundManager = new SoundManager(sfmt, 16);
 
@@ -393,8 +291,15 @@
             System.out.print(e.getMessage());
         }
 
-        // Initialize game components
-        lauta = new GameBoard(soundManager);
+        // UI IDM widgets
+        widgets = new IDMContainer();
+        btnNewGame = new BtnNewGame();
+        widgets.add(btnNewGame);
+
+        // Game
+        startNewGame();
+        
+        // Initialize event listeners
         addKeyListener(this);
         addMouseListener(this);
 
@@ -408,6 +313,14 @@
         soundManager.play(musa);
     }
 
+    public void startNewGame()
+    {
+        gameClock = 0;
+        gameFrames = 0;
+        startTime = new Date().getTime();
+
+        lauta = new GameBoard(soundManager);
+    }
 
     public void paintComponent(Graphics g)
     {
@@ -433,12 +346,16 @@
             lautaDim = dim;
 
             System.out.print("scale changed\n");
+            
+            btnNewGame.move(dim.width - 200, dim.height - 100);
         }
         
         // Background, pieces
         g2.drawImage(lautaBGScaled, 0, 0, null);
         lauta.paint(g2, 100, 150, 60);
 
+        btnNewGame.paint(g2);
+        
         // Scores
         g2.setFont(font1);
         g2.setPaint(Color.white);
@@ -478,8 +395,16 @@
 
     public void mouseEntered(MouseEvent e) { }
     public void mouseExited(MouseEvent e) { }
-    public void mousePressed(MouseEvent e) { }
-    public void mouseReleased(MouseEvent e) { }
+
+    public void mousePressed(MouseEvent e)
+    {
+        widgets.mousePressed(e);
+    }
+
+    public void mouseReleased(MouseEvent e)
+    {
+        widgets.mouseReleased(e);
+    }
 
     public void mouseClicked(MouseEvent e)
     {
@@ -502,14 +427,10 @@
     public void keyPressed(KeyEvent e)
     {
         // Handle keyboard input
-        if (lauta.keyHandler(e))
+        if (lauta.keyPressed(e))
             return;
         
-        switch (e.getKeyCode())
-        {
-            case KeyEvent.VK_ESCAPE:
-                break;
-        }
+        widgets.keyPressed(e);
     }
     
     public void run()
@@ -537,4 +458,17 @@
             }
         }
     }
+    
+    class BtnNewGame extends IDMButton
+    {
+        public BtnNewGame()
+        {
+            super(0, 0, KeyEvent.VK_ESCAPE, font1, "New Game");
+        }
+
+        public void clicked()
+        {
+            startNewGame();
+        }
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/game/IDMButton.java	Tue Feb 22 10:52:08 2011 +0200
@@ -0,0 +1,116 @@
+/*
+ * Ristipolku IDM button widget
+ * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
+ */
+package game;
+
+import java.awt.*;
+import java.awt.event.*;
+import java.awt.image.*;
+import java.awt.geom.*;
+import javax.imageio.*;
+import java.io.*;
+
+
+public class IDMButton extends IDMWidget
+{
+    enum State { FOCUSED, PRESSED, NORMAL }
+    State state;
+    static BufferedImage imgUp, imgPressed;
+    Point pos;
+    Font font;
+    FontMetrics metrics;
+    String text;
+ 
+    public IDMButton(Point pos, int keyCode, Font font, String text)
+    {
+        loadImages();
+        this.pos = pos;
+        this.font = font;
+        this.keyCode = keyCode;
+        this.text = text;
+        state = State.NORMAL;
+    }
+    
+    public IDMButton(int x, int y, int keyCode, Font font, String text)
+    {
+        this(new Point(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
+        {
+            ResourceLoader res = new ResourceLoader("graphics/button1_up.png");
+            imgUp = ImageIO.read(res.getStream());
+
+            res = new ResourceLoader("graphics/button1_down.png");
+            imgPressed = ImageIO.read(res.getStream());
+        }
+        catch (IOException e)
+        {
+            System.out.print(e.getMessage());
+        }
+    }
+    
+    public void paint(Graphics2D g)
+    {
+        BufferedImage img;
+        int xoffs, yoffs;
+
+        if (state == State.PRESSED)
+        {
+            img = imgPressed;
+            xoffs = yoffs = 15;
+        }
+        else
+        {
+            xoffs = yoffs = 0;
+            img = imgUp;
+        }
+
+        if (metrics == null)
+            metrics = g.getFontMetrics(font);
+        
+        int textWidth = metrics.stringWidth(text);
+        g.drawImage(img, pos.x + xoffs, pos.y + yoffs, null);
+
+        g.setFont(font);
+        g.setPaint(Color.black);
+        g.drawString(text,
+            pos.x + xoffs + (img.getWidth() - textWidth) / 2,
+            pos.y + yoffs + (img.getHeight() - metrics.getHeight() / 2) / 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());
+    }
+    
+    public void mousePressed(MouseEvent e)
+    {
+        state = State.PRESSED;
+    }
+
+    public void mouseReleased(MouseEvent e)
+    {
+        super.mouseReleased(e);
+        state = State.NORMAL;
+    }
+
+    public void clicked()
+    {
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/game/IDMContainer.java	Tue Feb 22 10:52:08 2011 +0200
@@ -0,0 +1,58 @@
+/*
+ * Ristipolku IDM widget container
+ * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
+ */
+package game;
+
+import java.awt.*;
+import java.awt.event.*;
+import java.util.*;
+
+
+public class IDMContainer extends IDMWidget
+{
+    ArrayList<IDMWidget> widgets;
+
+    public IDMContainer()
+    {
+        widgets = new ArrayList<IDMWidget>();
+    }
+    
+    public void add(IDMWidget widget)
+    {
+        widgets.add(widget);
+    }
+    
+    public void paint(Graphics2D g)
+    {
+        for (IDMWidget widget : widgets)
+            widget.paint(g);
+    }
+    
+    public void mousePressed(MouseEvent e)
+    {
+        for (IDMWidget widget : widgets)
+        {
+            if (widget.contains(e.getPoint()))
+                widget.mousePressed(e);
+        }
+    }
+
+    public void mouseReleased(MouseEvent e)
+    {
+        for (IDMWidget widget : widgets)
+        {
+            widget.mousePressed(e);
+        }
+    }
+
+    public boolean keyPressed(KeyEvent e)
+    {
+        for (IDMWidget widget : widgets)
+        {
+            if (widget.keyPressed(e))
+                return true;
+        }
+        return false;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/game/IDMWidget.java	Tue Feb 22 10:52:08 2011 +0200
@@ -0,0 +1,56 @@
+/*
+ * Ristipolku IDM base widget
+ * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
+ */
+package game;
+
+import java.awt.*;
+import java.awt.event.*;
+
+
+public class IDMWidget
+{
+    int keyCode;
+    
+    public IDMWidget()
+    {
+        keyCode = -1;
+    }
+
+    public void paint(Graphics2D g)
+    {
+    }
+    
+    public boolean contains(Point pos)
+    {
+        return false;
+    }
+    
+    public void mousePressed(MouseEvent e)
+    {
+    }
+
+    public void mouseReleased(MouseEvent e)
+    {
+        if (contains(e.getPoint()))
+        {
+            clicked();
+        }
+    }
+
+    // Generic key handler
+    public boolean keyPressed(KeyEvent e)
+    {
+        if (e.getKeyCode() == keyCode)
+        {
+            clicked();
+            return true;
+        }
+        else
+            return false;
+    }
+    
+    public void clicked()
+    {
+    }
+}