diff game/Engine.java @ 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
line wrap: on
line diff
--- 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();
+        }
+    }
 }