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

List:       kde-commits
Subject:    [gcompris] src/activities/chess: chess, add support for drag and drop
From:       Bruno Coudoin <bruno.coudoin () gcompris ! net>
Date:       2015-08-31 21:47:32
Message-ID: E1ZWWvM-0000rO-R3 () scm ! kde ! org
[Download RAW message or body]

Git commit 86ee587acfd817c0df9c782224f6ff0c086904ef by Bruno Coudoin.
Committed on 30/08/2015 at 07:49.
Pushed by bcoudoin into branch 'master'.

chess, add support for drag and drop

It is now possible to move pieces by click or by drag.
Added a new square object to model the board independantly of
the pieces.

M  +62   -4    src/activities/chess/Chess.qml
M  +13   -11   src/activities/chess/Piece.qml
M  +2    -2    src/activities/chess/chess.js

http://commits.kde.org/gcompris/86ee587acfd817c0df9c782224f6ff0c086904ef

diff --git a/src/activities/chess/Chess.qml b/src/activities/chess/Chess.qml
index 9d2187a..062a4ae 100644
--- a/src/activities/chess/Chess.qml
+++ b/src/activities/chess/Chess.qml
@@ -71,6 +71,7 @@ ActivityBase {
             property bool difficultyByLevel: activity.difficultyByLevel
             property var positions
             property var pieces: pieces
+            property var squares: squares
             property var history
             property int from
             property bool blackTurn
@@ -173,6 +174,42 @@ ActivityBase {
         }
 
         Repeater {
+            id: squares
+            model: items.positions
+            delegate: square
+            parent: chessboard
+
+            DropArea {
+                id: square
+                x: items.cellSize * (7 - pos % 8) + spacing / 2
+                y: items.cellSize * Math.floor(pos / 8) + spacing / 2
+                width: items.cellSize - spacing
+                height: items.cellSize - spacing
+                z: 1
+                keys: acceptMove ? ['acceptMe'] : ['sorryNo']
+                property bool acceptMove : false
+                property int pos: modelData.pos
+                property int spacing: 6 * ApplicationInfo.ratio
+                Rectangle {
+                    id: possibleMove
+                    anchors.fill: parent
+                    color: parent.containsDrag ? 'green' : 'transparent'
+                    border.width: parent.acceptMove ? 5 : 0
+                    border.color: "black"
+                    z: 1
+                }
+            }
+
+            function getSquareAt(pos) {
+                for(var i=0; i < squares.count; i++) {
+                    if(squares.itemAt(i).pos === pos)
+                        return squares.itemAt(i)
+                }
+                return(undefined)
+            }
+        }
+
+        Repeater {
             id: pieces
             model: items.positions
             delegate: piece
@@ -187,24 +224,46 @@ ActivityBase {
                 img: modelData.img
                 x: items.cellSize * (7 - pos % 8) + spacing / 2
                 y: items.cellSize * Math.floor(pos / 8) + spacing / 2
+                z: 1
                 pos: modelData.pos
                 newPos: modelData.pos
                 rotation: - chessboard.rotation
 
                 property int spacing: 6 * ApplicationInfo.ratio
 
+                Drag.active: dragArea.drag.active
+                Drag.hotSpot.x: width / 2
+                Drag.hotSpot.y: height / 2
+
                 MouseArea {
+                    id: dragArea
                     anchors.fill: parent
                     enabled: !items.gameOver
-                    onClicked: {
+                    drag.target: parent
+                    onPressed: {
+                        piece.Drag.keys = ['acceptMe']
+                        parent.z = 100
                         if(parent.isWhite == 1 && !items.blackTurn ||
                                 parent.isWhite == 0 && items.blackTurn) {
                             items.from = parent.newPos
                             Activity.showPossibleMoves(items.from)
-                        } else if(items.from != -1 && parent.acceptMove) {
+                        } else if(items.from != -1 && \
squares.getSquareAt(parent.newPos)['acceptMove']) {  Activity.moveTo(items.from, \
parent.newPos)  }
                     }
+                    onReleased: {
+                        if(piece.Drag.target) {
+                            console.log("1")
+                            if(items.from != -1) {
+                                Activity.moveTo(items.from, piece.Drag.target.pos)
+                            }
+                        } else {
+                            var pos = parent.pos
+                            // Force recalc of the old x,y position
+                            parent.pos = 0
+                            pieces.getPieceAt(pos).move(pos)
+                        }
+                    }
                 }
             }
 
@@ -212,8 +271,7 @@ ActivityBase {
                 var fromPiece = getPieceAt(from)
                 var toPiece = getPieceAt(to)
                 toPiece.hide(from)
-                fromPiece.pos = to
-                fromPiece.newPos = to
+                fromPiece.move(to)
             }
 
             function promotion(to) {
diff --git a/src/activities/chess/Piece.qml b/src/activities/chess/Piece.qml
index ecdcebd..f9ad25b 100644
--- a/src/activities/chess/Piece.qml
+++ b/src/activities/chess/Piece.qml
@@ -37,7 +37,7 @@ Image {
 
 
     SequentialAnimation {
-        id: moveAnim
+        id: hideAnim
         NumberAnimation {
             target: piece
             property: "scale"
@@ -59,6 +59,11 @@ Image {
             property: 'scale'
             value: 1
         }
+        PropertyAction {
+            target: piece
+            property: 'z'
+            value: 2
+        }
     }
 
     SequentialAnimation {
@@ -84,22 +89,19 @@ Image {
         }
     }
 
-    Rectangle {
-        id: possibleMove
-        anchors.fill: parent
-        color: 'transparent'
-        border.width: parent.acceptMove ? 5 : 0
-        border.color: "black"
-        z: 1
-    }
-
     function hide(newPos) {
         piece.newPos = newPos
-        moveAnim.start()
+        hideAnim.start()
     }
 
     function promotion() {
         promotionAnim.start()
     }
+
+    function move(to) {
+        piece.newPos = to
+        piece.pos = to
+        piece.z = 2
+    }
 }
 
diff --git a/src/activities/chess/chess.js b/src/activities/chess/chess.js
index be07d19..822db8b 100644
--- a/src/activities/chess/chess.js
+++ b/src/activities/chess/chess.js
@@ -257,7 +257,7 @@ function randomMove() {
 // Clear all accept move marker from the chessboard
 function clearAcceptMove() {
     for(var i=0; i < items.positions.length; ++i)
-        items.pieces.getPieceAt(i)['acceptMove'] = false
+        items.squares.getSquareAt(i)['acceptMove'] = false
 }
 
 // Highlight the possible moves for the piece at position 'from'
@@ -267,7 +267,7 @@ function showPossibleMoves(from) {
     for(var i=0; i < result.length; ++i) {
         if(viewPosToEngine(from) == result[i][1]) {
             var pos = engineToViewPos(result[i][2])
-            items.pieces.getPieceAt(pos)['acceptMove'] = true
+            items.squares.getSquareAt(pos)['acceptMove'] = true
         }
     }
 }


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

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