[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: [kmines/viranch/qtquick] src/qml: Put the border cells inside the grid itself
From: Viranch Mehta <viranch.mehta () gmail ! com>
Date: 2013-03-28 11:59:42
Message-ID: 20130328115942.DB2C0A60C8 () git ! kde ! org
[Download RAW message or body]
Git commit 23447b3a27b4c25503b16ff0b81bc210c110ee64 by Viranch Mehta.
Committed on 07/08/2012 at 08:10.
Pushed by viranch into branch 'viranch/qtquick'.
Put the border cells inside the grid itself
This minimizes code to a large extent
This commit also implements functions for
translating index wrt grid including borders
to index wrt grid excluding borders. This is
required because the game engine sees the
mine field as grid containing the cells and
not the border cells.
M +4 -2 src/qml/CellItem.qml
M +65 -104 src/qml/MineField.qml
M +9 -14 src/qml/logic.js
M +1 -1 src/qml/main.qml
http://commits.kde.org/kmines/23447b3a27b4c25503b16ff0b81bc210c110ee64
diff --git a/src/qml/CellItem.qml b/src/qml/CellItem.qml
index b461ea8..df67370 100644
--- a/src/qml/CellItem.qml
+++ b/src/qml/CellItem.qml
@@ -20,7 +20,9 @@ import org.kde.games.core 0.1 as KgCore
KgCore.CanvasItem {
id: cell
- spriteKey: pressed || revealed ? "cell_down" : "cell_up"
+ spriteKey: border!="" ? border : (pressed || revealed ? "cell_down" : "cell_up")
+
+ property string border
property bool hasMine: false
property int digit: 0
@@ -36,7 +38,7 @@ KgCore.CanvasItem {
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.LeftButton | Qt.RightButton
- enabled: !revealed
+ enabled: !revealed && border==""
onPressed: {
if (mouse.button == Qt.LeftButton) {
cell.pressed = true;
diff --git a/src/qml/MineField.qml b/src/qml/MineField.qml
index 726e4da..5b11dca 100644
--- a/src/qml/MineField.qml
+++ b/src/qml/MineField.qml
@@ -24,7 +24,7 @@ Item {
width: height*(columns+2)/(rows+2)
height: Math.floor(parent.height/(rows+2))*(rows+2)
- signal cellClicked(int index, int row, int column)
+ signal cellClicked(int index)
property int rows
property int columns
@@ -34,122 +34,83 @@ Item {
property real cellSize: width/(columns+2)
- /* === BORDERS === */
-
- KgCore.CanvasItem {
- spriteKey: "border.outsideCorner.nw"
- anchors { top: parent.top; left: parent.left; bottom: field.top; right: field.left }
- }
- KgCore.CanvasItem {
- spriteKey: "border.outsideCorner.ne"
- anchors { top: parent.top; left: field.right; bottom: field.top; right: parent.right }
- }
- KgCore.CanvasItem {
- spriteKey: "border.outsideCorner.sw"
- anchors { top: field.bottom; left: parent.left; bottom: parent.bottom; right: field.left }
- }
- KgCore.CanvasItem {
- spriteKey: "border.outsideCorner.se"
- anchors { top: field.bottom; left: field.right; bottom: parent.bottom; right: parent.right }
- }
+ Grid {
+ id: grid
+ anchors.fill: parent
+ rows: parent.rows+2
+ columns: parent.columns+2
- Row {
- anchors {
- top: parent.top
- left: field.left
- right: field.right
- bottom: field.top
- }
Repeater {
- model: columns
- KgCore.CanvasItem {
- spriteKey: "border.edge.north"
- width: cellSize
- height: cellSize
- }
- }
- }
+ id: cellRepeater
+ model: grid.rows*grid.columns
- Column {
- anchors {
- top: field.top
- left: parent.left
- right: field.left
- bottom: field.bottom
- }
- Repeater {
- model: rows
- KgCore.CanvasItem {
- spriteKey: "border.edge.west"
- width: cellSize
- height: cellSize
+ CellItem {
+ width: grid.width/grid.columns
+ height: grid.height/grid.rows
+
+ border: getBorderSprite(index)
+
+ onClicked: {
+ var row = Math.floor(index/grid.columns);
+ var column = index%grid.columns;
+ container.cellClicked((row-1)*container.columns + (column-1));
+ }
}
}
}
- Column {
- anchors {
- top: field.top
- left: field.right
- right: parent.right
- bottom: field.bottom
- }
- Repeater {
- model: rows
- KgCore.CanvasItem {
- spriteKey: "border.edge.east"
- width: cellSize
- height: cellSize
- }
- }
+ // index is wrt the inner grid
+ function itemAtIndex(index) {
+ var row = Math.floor(index/columns);
+ var column = index%columns;
+ return itemAtRowCol(row, column);
}
- Row {
- anchors {
- top: field.bottom
- left: field.left
- right: field.right
- bottom: parent.bottom
- }
- Repeater {
- model: columns
- KgCore.CanvasItem {
- spriteKey: "border.edge.south"
- width: cellSize
- height: cellSize
- }
- }
+ // row/col are wrt inner grid
+ function itemAtRowCol(row, column) {
+ // the +1's in row+1 & column+1 are for border cells
+ var index = (row+1)*grid.columns + (column+1);
+ return cells.itemAt(index);
}
- /* === END BORDERS === */
+ function getBorderSprite(index) {
+ var row = Math.floor(index/grid.columns);
+ var col = index%grid.columns;
- Grid {
- id: field
- anchors {
- fill: parent
- margins: cellSize
+ if( row == 0 && col == 0)
+ {
+ return "border.outsideCorner.nw";
}
- rows: parent.rows
- columns: parent.columns
-
- Repeater {
- id: cellRepeater
- model: (rows+0)*(columns+0)
-
- CellItem {
- width: field.width/field.columns
- height: field.height/field.rows
-
- property int row: Math.floor(index/field.columns)
- property int column: index%field.columns
-
- onClicked: container.cellClicked(index, row, column);
- }
+ else if( row == 0 && col == columns+1)
+ {
+ return "border.outsideCorner.ne";
+ }
+ else if( row == rows+1 && col == 0 )
+ {
+ return "border.outsideCorner.sw";
+ }
+ else if( row == rows+1 && col == columns+1 )
+ {
+ return "border.outsideCorner.se";
+ }
+ else if( row == 0 )
+ {
+ return "border.edge.north";
+ }
+ else if( row == rows+1 )
+ {
+ return "border.edge.south";
+ }
+ else if( col == 0 )
+ {
+ return "border.edge.west";
+ }
+ else if( col == columns+1 )
+ {
+ return "border.edge.east";
+ }
+ else {
+ return "";
}
- }
-
- function itemAt(row, column) {
- var index = row*field.columns + column;
- return cells.itemAt(index);
}
}
diff --git a/src/qml/logic.js b/src/qml/logic.js
index ef284f2..4869c22 100644
--- a/src/qml/logic.js
+++ b/src/qml/logic.js
@@ -1,30 +1,30 @@
var firstClick = true;
-function revealCell(index, row, column) {
+function revealCell(index) {
if (firstClick) {
firstClick = false;
generateField(index);
}
- var cell = field.cells.itemAt(index);
+ var cell = field.itemAtIndex(index);
if (cell.hasMine) {
} else if (cell.digit == 0) {
- revealEmptyCells(row, column);
+ revealEmptyCells(index);
}
}
-function revealEmptyCells(row, column) {
+function revealEmptyCells(index) {
// recursively reveal neighbour cells until we find cells with digit
- var list = adjacentRowColsFor(row, column, -1);
+ var list = adjacentCells(index);
var item;
for (var i=0; i<list.length; i++) {
// first is row, second is col
- item = field.cells.itemAt(list[i]);
+ item = field.itemAtIndex(list[i]);
if (item.revealed || item.flagged || item.questioned)
continue;
if (item.digit == 0) {
item.revealed = true;
- revealEmptyCells(item.row, item.column);
+ revealEmptyCells(list[i]);
} else {
item.revealed = true;
}
@@ -47,7 +47,7 @@ function generateField(clickedIndex) {
while (minesToPlace != 0) {
randomIndex = Math.floor(Math.random()*field.rows*field.columns);
- cell = field.cells.itemAt(randomIndex);
+ cell = field.itemAtIndex(randomIndex);
if (!cell.hasMine
&& adjacentIndex.indexOf(randomIndex) == -1
&& randomIndex != clickedIndex) {
@@ -61,7 +61,7 @@ function generateField(clickedIndex) {
for (var i=0; i<cellsWithMines.length; i++) {
var neighbours = adjacentCells(cellsWithMines[i]);
for (var j=0; j<neighbours.length; j++) {
- cell = field.cells.itemAt(neighbours[j]);
+ cell = field.itemAtIndex(neighbours[j]);
if (!cell.hasMine)
cell.digit++;
}
@@ -71,12 +71,7 @@ function generateField(clickedIndex) {
function adjacentCells(index) {
var row = Math.floor(index/field.columns);
var col = index%field.columns;
- return adjacentRowColsFor(row, col, index);
-}
-function adjacentRowColsFor(row, col, index) {
- if (index==-1)
- index = row*field.columns + col;
var adjacent = [];
if(row != 0 && col != 0) // upper-left diagonal
diff --git a/src/qml/main.qml b/src/qml/main.qml
index 0bc30a9..a1f553e 100644
--- a/src/qml/main.qml
+++ b/src/qml/main.qml
@@ -38,6 +38,6 @@ Item {
columns: 10
mines: 10
anchors.centerIn: parent
- onCellClicked: Logic.revealCell(index, row, column);
+ onCellClicked: Logic.revealCell(index);
}
}
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic