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 !=3D -1) { Activity.moveTo(items.from, piece.Drag.tar= get.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 =3D 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 =3D Engine.p4_fen2state(items.fen[currentLevel][1]) items.from =3D -1 items.gameOver =3D false + items.redo_stack =3D [] refresh() Engine.p4_prepare(state) items.positions =3D 0 // Force a model reload @@ -207,6 +208,7 @@ function moveTo(from, to) { visibleMove(move, from, to) refresh(move) clearAcceptMove() + items.redo_stack =3D [] if(!items.twoPlayer) items.trigComputerMove.start() items.from =3D -1; @@ -217,18 +219,43 @@ function moveTo(from, to) { } = function undo() { + var redo_stack =3D 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 !=3D 0) { + redo_stack.push(state.history[state.moveno - 1]) state.jump_to_moveno(state.moveno - 1) } + items.redo_stack =3D redo_stack refresh() - items.positions =3D 0 // Force a model reload + items.positions =3D [] // Force a model reload items.positions =3D simplifiedState(state['board']) } = +function moveByEngine(engineMove) { + var move =3D state.move(engineMove[0], engineMove[1]) + visibleMove(move, engineToViewPos(engineMove[0]), engineToViewPos(engi= neMove[1])) + refresh(move) +} + +function redo() { + var redo_stack =3D 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 !=3D 0) { + items.redoTimer.moveByEngine(items.redo_stack.pop()) + } + + // Force refresh + items.redo_stack =3D [] + items.redo_stack =3D redo_stack +} + // Random move depending on the level function randomMove() { if(!items.difficultyByLevel) {