changeset 181:2e033eced6e5

Implement "new game" confirmation dialog.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 16 Mar 2017 12:23:02 +0200
parents fa9f29387cff
children 88b364dfd501
files game/Engine.java
diffstat 1 files changed, 70 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/game/Engine.java	Thu Mar 16 12:22:30 2017 +0200
+++ b/game/Engine.java	Thu Mar 16 12:23:02 2017 +0200
@@ -101,6 +101,65 @@
 }
 
 
+class NewGameDialog extends IDMContainer
+{
+    public NewGameDialog()
+    {
+        setPos(150f, 350f);
+        setSize(720f, 200f);
+        setModal(true);
+
+        add(new BtnYes(270f, 120f));
+        add(new BtnCancel(500f, 120f));
+    }
+
+    public void paint(Graphics2D g)
+    {
+        g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.7f));
+        g.fill(new RoundRectangle2D.Float(getScaledX(), getScaledY(), getScaledWidth(), getScaledHeight(), 10, 10));
+
+        setTextFont(G.fonts[3], G.metrics[3]);
+        setTextPaint(Color.white);
+        setCurrPosScaledRel(25, 35);
+        drawString(g, "Really start a new game?\n");
+
+        super.paint(g);
+    }
+
+    public void clicked()
+    {
+        parent.remove(this);
+    }
+
+    class BtnYes extends IDMButton
+    {
+        public BtnYes(float x, float y)
+        {
+            super(x, y, KeyEvent.VK_Y, G.fonts[1], "Yes");
+        }
+
+        public void clicked()
+        {
+            sendMessage("NEWGAME");
+            parent.clicked();
+        }
+    }
+
+    class BtnCancel extends IDMButton
+    {
+        public BtnCancel(float x, float y)
+        {
+            super(x, y, KeyEvent.VK_ESCAPE, G.fonts[1], "Cancel");
+        }
+
+        public void clicked()
+        {
+            parent.clicked();
+        }
+    }
+}
+
+
 class GameBoard extends IDMWidget
 {
     static final int boardSize = 9;
@@ -110,6 +169,7 @@
 
     public boolean flagGameOver, flagBoardIsDirty;
     int gameScore;
+    int moves;
 
     Piece currPiece, nextPiece;
     int currX, currY, currPoint;
@@ -147,6 +207,7 @@
         flagBoardIsDirty = true;
         pieceFinishTurn();
         gameScore = 0;
+        moves = 0;
     }
 
     public void paintBackPlate(Graphics2D g)
@@ -437,6 +498,7 @@
 
         // Compute and add score
         gameScore += connections * connections;
+        moves++;
 
         // If game over, clear the game
         if (flagGameOver)
@@ -505,6 +567,7 @@
     InputStream musa;
     IDMWindow widgets;
     AboutBox aboutBox;
+    NewGameDialog newGameDialog;
 
     public void dbg(String msg)
     {
@@ -562,6 +625,7 @@
         widgets.add(new BtnNewGame  (767f, 630f));
 
         aboutBox = new AboutBox();
+        newGameDialog = new NewGameDialog();
 
         // Game
         startNewGame();
@@ -783,6 +847,12 @@
 
         public void clicked()
         {
+            if (!lauta.flagGameOver && lauta.moves > 0)
+            {
+                if (!widgets.containsObject(newGameDialog))
+                    widgets.add(newGameDialog);
+            }
+            else
                 sendMessage("NEWGAME");
         }
     }