summaryrefslogtreecommitdiffstats
path: root/examples/declarative/tutorials/samegame/samegame2
diff options
context:
space:
mode:
authorAlan Alpert <alan.alpert@nokia.com>2009-10-07 01:03:21 (GMT)
committerAlan Alpert <alan.alpert@nokia.com>2009-10-07 01:03:21 (GMT)
commit95821d745a60e60422294dd718cbed5678ef5f1f (patch)
treef842b1f053c6c8f9664415e6dd2670e4cd9be919 /examples/declarative/tutorials/samegame/samegame2
parente25009d28c58b3ff7541374475f8c2c278cef02e (diff)
downloadQt-95821d745a60e60422294dd718cbed5678ef5f1f.zip
Qt-95821d745a60e60422294dd718cbed5678ef5f1f.tar.gz
Qt-95821d745a60e60422294dd718cbed5678ef5f1f.tar.bz2
Add SameGame based advanced tutorial
Could use some cleanup, as I didn't manage to get code snippets working correctly. Also might benefit from being re-written by a good tutor.
Diffstat (limited to 'examples/declarative/tutorials/samegame/samegame2')
-rw-r--r--examples/declarative/tutorials/samegame/samegame2/Block.qml10
-rw-r--r--examples/declarative/tutorials/samegame/samegame2/Button.qml25
-rw-r--r--examples/declarative/tutorials/samegame/samegame2/pics/background.pngbin0 -> 153328 bytes
-rw-r--r--examples/declarative/tutorials/samegame/samegame2/pics/redStone.pngbin0 -> 2604 bytes
-rw-r--r--examples/declarative/tutorials/samegame/samegame2/samegame.js59
-rw-r--r--examples/declarative/tutorials/samegame/samegame2/samegame.qml39
6 files changed, 133 insertions, 0 deletions
diff --git a/examples/declarative/tutorials/samegame/samegame2/Block.qml b/examples/declarative/tutorials/samegame/samegame2/Block.qml
new file mode 100644
index 0000000..228ac4e
--- /dev/null
+++ b/examples/declarative/tutorials/samegame/samegame2/Block.qml
@@ -0,0 +1,10 @@
+import Qt 4.6
+
+Item {
+ id:block
+
+ Image { id: img
+ source: "pics/redStone.png";
+ anchors.fill: parent
+ }
+}
diff --git a/examples/declarative/tutorials/samegame/samegame2/Button.qml b/examples/declarative/tutorials/samegame/samegame2/Button.qml
new file mode 100644
index 0000000..2354218
--- /dev/null
+++ b/examples/declarative/tutorials/samegame/samegame2/Button.qml
@@ -0,0 +1,25 @@
+import Qt 4.6
+
+Rectangle {
+ id: Container
+
+ signal clicked
+ property string text: "Button"
+
+ color: activePalette.button; smooth: true
+ width: txtItem.width + 20; height: txtItem.height + 6
+ border.width: 1; border.color: activePalette.darker(activePalette.button); radius: 8;
+
+ gradient: Gradient {
+ GradientStop {
+ id: topGrad; position: 0.0
+ color: if (mr.pressed) { activePalette.dark } else { activePalette.light } }
+ GradientStop { position: 1.0; color: activePalette.button }
+ }
+
+ MouseRegion { id: mr; anchors.fill: parent; onClicked: Container.clicked() }
+
+ Text {
+ id: txtItem; text: Container.text; anchors.centerIn: Container; color: activePalette.buttonText
+ }
+}
diff --git a/examples/declarative/tutorials/samegame/samegame2/pics/background.png b/examples/declarative/tutorials/samegame/samegame2/pics/background.png
new file mode 100644
index 0000000..25e885f
--- /dev/null
+++ b/examples/declarative/tutorials/samegame/samegame2/pics/background.png
Binary files differ
diff --git a/examples/declarative/tutorials/samegame/samegame2/pics/redStone.png b/examples/declarative/tutorials/samegame/samegame2/pics/redStone.png
new file mode 100644
index 0000000..b099f60
--- /dev/null
+++ b/examples/declarative/tutorials/samegame/samegame2/pics/redStone.png
Binary files differ
diff --git a/examples/declarative/tutorials/samegame/samegame2/samegame.js b/examples/declarative/tutorials/samegame/samegame2/samegame.js
new file mode 100644
index 0000000..064d87e
--- /dev/null
+++ b/examples/declarative/tutorials/samegame/samegame2/samegame.js
@@ -0,0 +1,59 @@
+//Note that X/Y referred to here are in game coordinates
+var maxX = 10;//Nums are for tileSize 40
+var maxY = 15;
+var tileSize = 40;
+var maxIndex = maxX*maxY;
+var board = new Array(maxIndex);
+var tileSrc = "Block.qml";
+var component;
+
+//Index function used instead of a 2D array
+function index(xIdx,yIdx) {
+ return xIdx + (yIdx * maxX);
+}
+
+function initBoard()
+{
+ //Calculate board size
+ maxX = Math.floor(background.width/tileSize);
+ maxY = Math.floor(background.height/tileSize);
+ maxIndex = maxY*maxX;
+
+ //Initialize Board
+ board = new Array(maxIndex);
+ for(xIdx=0; xIdx<maxX; xIdx++){
+ for(yIdx=0; yIdx<maxY; yIdx++){
+ board[index(xIdx,yIdx)] = null;
+ createBlock(xIdx,yIdx);
+ }
+ }
+}
+
+function createBlock(xIdx,yIdx){
+ if(component==null)
+ component = createComponent(tileSrc);
+
+ // Note that we don't wait for the component to become ready. This will
+ // only work if the block QML is a local file. Otherwise the component will
+ // not be ready immediately. There is a statusChanged signal on the
+ // component you could use if you want to wait to load remote files.
+ if(component.isReady){
+ dynamicObject = component.createObject();
+ if(dynamicObject == null){
+ print("error creating block");
+ print(component.errorsString());
+ return false;
+ }
+ dynamicObject.parent = background;
+ dynamicObject.x = xIdx*tileSize;
+ dynamicObject.y = yIdx*tileSize;
+ dynamicObject.width = tileSize;
+ dynamicObject.height = tileSize;
+ board[index(xIdx,yIdx)] = dynamicObject;
+ }else{//isError or isLoading
+ print("error loading block component");
+ print(component.errorsString());
+ return false;
+ }
+ return true;
+}
diff --git a/examples/declarative/tutorials/samegame/samegame2/samegame.qml b/examples/declarative/tutorials/samegame/samegame2/samegame.qml
new file mode 100644
index 0000000..e446fa4
--- /dev/null
+++ b/examples/declarative/tutorials/samegame/samegame2/samegame.qml
@@ -0,0 +1,39 @@
+import Qt 4.6
+
+Rectangle {
+ id: Screen
+ width: 490; height: 720
+
+ SystemPalette { id: activePalette; colorGroup: Qt.Active }
+ Script { source: "samegame.js" }
+
+ Item {
+ width: parent.width; anchors.top: parent.top; anchors.bottom: ToolBar.top
+
+ Image {
+ id: background
+ anchors.fill: parent; source: "pics/background.png"
+ fillMode: "PreserveAspectCrop"
+ }
+ }
+
+ Rectangle {
+ id: ToolBar
+ color: activePalette.window
+ height: 32; width: parent.width
+ anchors.bottom: Screen.bottom
+
+ Button {
+ id: btnA; text: "New Game"; onClicked: initBoard();
+ anchors.left: parent.left; anchors.leftMargin: 3
+ anchors.verticalCenter: parent.verticalCenter
+ }
+
+ Text {
+ id: Score
+ text: "Score: Who knows?"; font.bold: true
+ anchors.right: parent.right; anchors.rightMargin: 3
+ anchors.verticalCenter: parent.verticalCenter
+ }
+ }
+}