main.qml Example File
xandos/assets/main.qml
import bb.cascades 1.2
import bb.system 1.2
Page {
property int userScore: 0
property int droidScore: 0
property bool gameEnabled: false
property int leftpadding: 50
property int rightpadding: 50
Container {
rightPadding: rightpadding
leftPadding: leftpadding
bottomPadding: 150
layout: DockLayout {
}
Container {
topPadding: 120
verticalAlignment: VerticalAlignment.Top
horizontalAlignment: HorizontalAlignment.Fill
Container {
horizontalAlignment: HorizontalAlignment.Fill
verticalAlignment: VerticalAlignment.Center
layout: DockLayout {
}
Container {
horizontalAlignment: HorizontalAlignment.Left
verticalAlignment: VerticalAlignment.Center
layout: StackLayout {
orientation: LayoutOrientation.LeftToRight
}
Label {
text: "USER:"
textStyle.color: Color.White
textStyle.fontSize: FontSize.Large
}
Label {
id: user
text: "" + userScore
textStyle.color: Color.create("#40BB9E")
textStyle.fontSize: FontSize.Large
}
}
Container {
horizontalAlignment: HorizontalAlignment.Right
verticalAlignment: VerticalAlignment.Center
layout: StackLayout {
orientation: LayoutOrientation.LeftToRight
}
Label {
id: droid
text: "" + droidScore
textStyle.color: Color.create("#DF6054")
textStyle.fontSize: FontSize.Large
}
Label {
text: ":DROID"
textStyle.color: Color.White
textStyle.fontSize: FontSize.Large
}
}
}
}
Container {
layout: DockLayout {
}
topPadding: 50
horizontalAlignment: HorizontalAlignment.Center
verticalAlignment: VerticalAlignment.Center
ImageView {
visible: !gameEnabled
imageSource: "asset:///images/grid.png"
}
ImageView {
visible: gameEnabled
imageSource: "asset:///images/grid_start.png"
}
}
Container {
horizontalAlignment: HorizontalAlignment.Left
verticalAlignment: VerticalAlignment.Center
leftPadding: 51
topPadding: 55
Container {
layout: StackLayout {
orientation: LayoutOrientation.LeftToRight
}
ImageView {
id: i0
objectName: "0"
maxWidth: 140
maxHeight: 140
visible: gameEnabled
imageSource: "asset:///images/blank.png"
onTouch: {
if (event.isDown() && imageSource.toString().search("blank") != -1) {
imageSource = "asset:///images/x.png"
_xandos.select(0, 1)
}
}
}
ImageView {
id: i1
objectName: "1"
visible: gameEnabled
leftMargin: 75
maxWidth: 140
maxHeight: 140
imageSource: "asset:///images/blank.png"
onTouch: {
if (event.isDown() && imageSource.toString().search("blank") != -1) {
imageSource = "asset:///images/x.png"
_xandos.select(1, 1)
}
}
}
ImageView {
id: i2
objectName: "2"
visible: gameEnabled
leftMargin: 72
maxWidth: 140
maxHeight: 140
imageSource: "asset:///images/blank.png"
onTouch: {
if (event.isDown() && imageSource.toString().search("blank") != -1) {
imageSource = "asset:///images/x.png"
_xandos.select(2, 1)
}
}
}
}
Container {
topPadding: 60
layout: StackLayout {
orientation: LayoutOrientation.LeftToRight
}
ImageView {
id: i3
objectName: "3"
visible: gameEnabled
maxWidth: 140
maxHeight: 140
imageSource: "asset:///images/blank.png"
onTouch: {
if (event.isDown() && imageSource.toString().search("blank") != -1) {
imageSource = "asset:///images/x.png"
_xandos.select(3, 1)
}
}
}
ImageView {
id: i4
objectName: "4"
visible: gameEnabled
leftMargin: 75
maxWidth: 140
maxHeight: 140
imageSource: "asset:///images/blank.png"
onTouch: {
if (event.isDown() && imageSource.toString().search("blank") != -1) {
imageSource = "asset:///images/x.png"
_xandos.select(4, 1)
}
}
}
ImageView {
id: i5
objectName: "5"
visible: gameEnabled
leftMargin: 72
maxWidth: 140
maxHeight: 140
imageSource: "asset:///images/blank.png"
onTouch: {
if (event.isDown() && imageSource.toString().search("blank") != -1) {
imageSource = "asset:///images/x.png"
_xandos.select(5, 1)
}
}
}
}
Container {
topPadding: 70
layout: StackLayout {
orientation: LayoutOrientation.LeftToRight
}
ImageView {
id: i6
objectName: "6"
visible: gameEnabled
maxWidth: 140
maxHeight: 140
imageSource: "asset:///images/blank.png"
onTouch: {
if (event.isDown() && imageSource.toString().search("blank") != -1) {
imageSource = "asset:///images/x.png"
_xandos.select(6, 1)
}
}
}
ImageView {
id: i7
objectName: "7"
visible: gameEnabled
leftMargin: 75
maxWidth: 140
maxHeight: 140
imageSource: "asset:///images/blank.png"
onTouch: {
if (event.isDown() && imageSource.toString().search("blank") != -1) {
imageSource = "asset:///images/x.png"
_xandos.select(7, 1)
}
}
}
ImageView {
id: i8
objectName: "8"
visible: gameEnabled
leftMargin: 72
maxWidth: 140
maxHeight: 140
imageSource: "asset:///images/blank.png"
onTouch: {
if (event.isDown() && imageSource.toString().search("blank") != -1) {
imageSource = "asset:///images/x.png"
_xandos.select(8, 1)
}
}
}
}
}
}
attachedObjects: [
SystemDialog {
id: dialog
title: qsTr("Start Game of XandOs?")
body: qsTr("The droid is ready when the grid turns green")
confirmButton.label: qsTr("Start")
confirmButton.enabled: true
cancelButton.label: qsTr("Cancel")
cancelButton.enabled: true
onFinished: {
var x = result;
if (x == SystemUiResult.ConfirmButtonSelection) {
console.log("confirm");
_xandos.startDroid();
_xandos.won.connect(playerWon);
_xandos.droidReady.connect(droidStart)
} else if (x == SystemUiResult.CancelButtonSelection) {
console.log("cancel");
Application.requestExit()
}
}
},
SystemDialog {
id: gameResult
title: qsTr("")
body: qsTr("Would you like to play again?")
confirmButton.label: qsTr("Yes")
confirmButton.enabled: true
cancelButton.label: qsTr("No")
cancelButton.enabled: true
onFinished: {
var x = result;
if (x == SystemUiResult.ConfirmButtonSelection) {
console.log("confirm");
clearGrid()
_xandos.resetGame();
_xandos.startDroid();
_xandos.won.connect(playerWon);
_xandos.droidReady.connect(droidStart)
} else if (x == SystemUiResult.CancelButtonSelection) {
console.log("cancel");
Application.requestExit()
}
}
}
]
onCreationCompleted: {
dialog.show();
}
function playerWon(x) {
gameEnabled = false;
console.log("winner is: " + x)
_xandos.won.disconnect(playerWon);
if(-1 == x) {
droidScore += 1
gameResult.title = qsTr("Droid has WON!")
} else if(1 == x) {
gameResult.title = qsTr("You have WON!")
userScore += 1
} else {
gameResult.title = qsTr("It's a TIE!")
}
gameResult.show()
}
function clearGrid() {
i0.imageSource = "asset:///images/blank.png"
i1.imageSource = "asset:///images/blank.png"
i2.imageSource = "asset:///images/blank.png"
i3.imageSource = "asset:///images/blank.png"
i4.imageSource = "asset:///images/blank.png"
i5.imageSource = "asset:///images/blank.png"
i6.imageSource = "asset:///images/blank.png"
i7.imageSource = "asset:///images/blank.png"
i8.imageSource = "asset:///images/blank.png"
}
function droidStart() {
gameEnabled = true
}
}