[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [gcompris] src/activities/chess: chess, add a redo feature
From:       Bruno Coudoin <bruno.coudoin () gcompris ! net>
Date:       2015-08-31 21:47:33
Message-ID: E1ZWWvN-0000rO-MH () scm ! kde ! org
[Download RAW message or body]

Git commit 41d4c414789a2f5bd721bd487a9df2597ca32044 by Bruno Coudoin.
Committed on 31/08/2015 at 21:42.
Pushed by bcoudoin into branch 'master'.

chess, add a redo feature

M  +32   -1    src/activities/chess/Chess.qml
M  +28   -1    src/activities/chess/chess.js

http://commits.kde.org/gcompris/41d4c414789a2f5bd721bd487a9df2597ca32044

diff --git a/src/activities/chess/Chess.qml b/src/activities/chess/Chess.qml
index cba75ec..d0d447f 100644
--- a/src/activities/chess/Chess.qml
+++ b/src/activities/chess/Chess.qml
@@ -73,6 +73,8 @@ ActivityBase {
             property var pieces: pieces
             property var squares: squares
             property var history
+            property var redo_stack
+            property alias redoTimer: redoTimer
             property int from
             property bool blackTurn
             property bool gameOver
@@ -137,6 +139,22 @@ ActivityBase {
                 }
 
                 Button {
+                    id: redo
+                    anchors.horizontalCenter: parent.horizontalCenter
+                    height: 30 * ApplicationInfo.ratio
+                    text: qsTr("Redo");
+                    style: GCButtonStyle {}
+                    onClicked: Activity.redo()
+                    opacity: items.redo_stack.length > 0 ? 1 : 0
+                    Behavior on opacity {
+                        PropertyAnimation {
+                            easing.type: Easing.InQuad
+                            duration: 200
+                        }
+                    }
+                }
+
+                Button {
                     anchors.horizontalCenter: parent.horizontalCenter
                     height: 30 * ApplicationInfo.ratio
                     text: qsTr("Swap");
@@ -261,7 +279,6 @@ ActivityBase {
                     }
                     onReleased: {
                         if(piece.Drag.target) {
-                            console.log("1")
                             if(items.from != -1) {
                                 Activity.moveTo(items.from, piece.Drag.target.pos)
                             }
@@ -303,6 +320,20 @@ ActivityBase {
             onTriggered: Activity.randomMove()
         }
 
+        // Use to redo the computer move after the user move
+        Timer {
+            id: redoTimer
+            repeat: false
+            interval: 400
+            onTriggered: Activity.moveByEngine(move)
+            property var move
+
+            function moveByEngine(engineMove) {
+                move = engineMove
+                redoTimer.start()
+            }
+        }
+
         DialogHelp {
             id: dialogHelp
             onClose: home()
diff --git a/src/activities/chess/chess.js b/src/activities/chess/chess.js
index 822db8b..64f1d78 100644
--- a/src/activities/chess/chess.js
+++ b/src/activities/chess/chess.js
@@ -46,6 +46,7 @@ function initLevel() {
     state = Engine.p4_fen2state(items.fen[currentLevel][1])
     items.from = -1
     items.gameOver = false
+    items.redo_stack = []
     refresh()
     Engine.p4_prepare(state)
     items.positions = 0 // Force a model reload
@@ -207,6 +208,7 @@ function moveTo(from, to) {
         visibleMove(move, from, to)
         refresh(move)
         clearAcceptMove()
+        items.redo_stack = []
         if(!items.twoPlayer)
             items.trigComputerMove.start()
         items.from = -1;
@@ -217,18 +219,43 @@ function moveTo(from, to) {
 }
 
 function undo() {
+    var redo_stack = items.redo_stack
+    redo_stack.push(state.history[state.moveno - 1])
     state.jump_to_moveno(state.moveno - 1)
     // In computer mode, the white always starts, take care
     // of undo after a mate which requires us to revert on
     // a white play
     if(!items.twoPlayer && state.to_play != 0) {
+        redo_stack.push(state.history[state.moveno - 1])
         state.jump_to_moveno(state.moveno - 1)
     }
+    items.redo_stack = redo_stack
     refresh()
-    items.positions = 0 // Force a model reload
+    items.positions = [] // Force a model reload
     items.positions = simplifiedState(state['board'])
 }
 
+function moveByEngine(engineMove) {
+    var move = state.move(engineMove[0], engineMove[1])
+    visibleMove(move, engineToViewPos(engineMove[0]), engineToViewPos(engineMove[1]))
+    refresh(move)
+}
+
+function redo() {
+    var redo_stack = items.redo_stack
+    moveByEngine(items.redo_stack.pop())
+    // In computer mode, the white always starts, take care
+    // of undo after a mate which requires us to revert on
+    // a white play
+    if(!items.twoPlayer && state.to_play != 0) {
+        items.redoTimer.moveByEngine(items.redo_stack.pop())
+    }
+
+    // Force refresh
+    items.redo_stack = []
+    items.redo_stack = redo_stack
+}
+
 // Random move depending on the level
 function randomMove() {
     if(!items.difficultyByLevel) {
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic