diff options
author | No'am Rosenthal <noam.rosenthal@nokia.com> | 2009-11-26 15:17:27 -0800 |
---|---|---|
committer | No'am Rosenthal <noam.rosenthal@nokia.com> | 2009-11-26 15:17:27 -0800 |
commit | cdb910b166c6d139dac3a7801d6aac848d8b51d9 (patch) | |
tree | b249f550caf9e6cf190dda84d4aea8e6b18df2aa | |
parent | 88302167db167becb15b46ad1ef821251aed1769 (diff) |
WIP: QmlScxml
-rw-r--r-- | examples/examples.pro | 2 | ||||
-rw-r--r-- | examples/qml/blackjack/blackjack.pro | 5 | ||||
-rw-r--r-- | examples/qml/blackjack/blackjack.qml | 95 | ||||
-rw-r--r-- | examples/qml/blackjack/blackjack.scxml | 224 | ||||
-rw-r--r-- | examples/qml/blackjack/main.cpp | 58 | ||||
-rw-r--r-- | qmlscxml/qmlscxml.cpp | 42 | ||||
-rw-r--r-- | qmlscxml/qmlscxml.h | 40 | ||||
-rw-r--r-- | src/qscxml.cpp | 5 |
8 files changed, 468 insertions, 3 deletions
diff --git a/examples/examples.pro b/examples/examples.pro index 53d0ca3..0af767f 100644 --- a/examples/examples.pro +++ b/examples/examples.pro @@ -1,2 +1,2 @@ TEMPLATE = subdirs -SUBDIRS += blackjack calc mediaplayer
\ No newline at end of file +SUBDIRS += blackjack calc mediaplayer scc qml diff --git a/examples/qml/blackjack/blackjack.pro b/examples/qml/blackjack/blackjack.pro new file mode 100644 index 0000000..eac00d5 --- /dev/null +++ b/examples/qml/blackjack/blackjack.pro @@ -0,0 +1,5 @@ +TEMPLATE = app +QT += declarative +target = blackjack +include(../../../qmlscxml/qmlscxml.pri) +SOURCES += main.cpp diff --git a/examples/qml/blackjack/blackjack.qml b/examples/qml/blackjack/blackjack.qml new file mode 100644 index 0000000..976661a --- /dev/null +++ b/examples/qml/blackjack/blackjack.qml @@ -0,0 +1,95 @@ +import Qt 4.6 +import Scxml 1.0 + +Rectangle { + color: "white" + width: 400 + height: 300 + Scxml { + id: controller + source: "blackjack.scxml" + } + + Text { + id: welcomeText + text: "Blackjack" + font.pixelSize: 24 + states: [ + State { + name: "placeBets" + when: controller.current.waitForBet + PropertyChanges { + target: welcomeText + text: "Please place your bet" + } + }, + State { + name: "game" + when: controller.current.game + PropertyChanges { + target: welcomeText + text: "Welcome" + } + } + ] + } + Rectangle { + color: "white" + x: 100 + y: 100 + Text { + color: "blue" + text: "You have " + controller.data.points + " points" + } + } + Rectangle { + color: "white" + id: betEditBg + x: 80 + y: 400 + width: 100 + height: 20 + TextInput { + id: betEdit + color: "black" + anchors.fill: parent + text: controller.data.pointsToBet + } + states: [ + State { + name: "placeBets" + when: controller.current.waitForBet + PropertyChanges { + target: betEditBg + color: "#ffcc33" + opacity: 1 + } + }, + State { + name: "default" + when: !controller.current.waitForBet + PropertyChanges { + target: betEditBg + opacity: 0 + } + } + ] + } + Rectangle { + anchors.left: betEditBg.right + anchors.top: betEditBg.top + anchors.bottom: betEditBg.bottom + width: 100 + color: "blue" + MouseRegion { + anchors.fill: parent + onClicked: { controller.events.bet.raise(); } + } + Text { + color: "yellow" + text: "Bet" + } + } + + +} diff --git a/examples/qml/blackjack/blackjack.scxml b/examples/qml/blackjack/blackjack.scxml new file mode 100644 index 0000000..ab1b9cd --- /dev/null +++ b/examples/qml/blackjack/blackjack.scxml @@ -0,0 +1,224 @@ +<scxml + xmlns="http://www.w3.org/2005/07/scxml" + initial="root" profile="ecmascript"> + <datamodel> + <data id="points" expr="1000" /> + <data id="pot" expr="0" /> + <data id="pointsToBet" expr="0" /> + </datamodel> + <script><![CDATA[ + var Suits = "CDHS"; + var Ranks = "-A23456789TJQK"; + + function Card (r,s) + { + this.rank = r; + this.suit = s; + this.minValue = Math.min(r,10); + this.toString = function() { + return "" + Ranks[this.rank] + Suits[this.suit]; + }; +; + } + + function Deck() + { + this.draw = function() + { + return this.cards.pop(); + }; + this.cards = new Array(); + this.reset = function() + { + this.clear (); + for (var i=1; i <= 13; ++i) + for (var j = 0; j < 4; ++j) + this.cards.push(new Card(i,j)); + this.cards.sort(function (a,b) + { + return Math.random() * 3 - 1; + }); + }; + + this.clear = function() + { + this.cards = new Array; + }; + this.evalMin = function () + { + var minVal = 0; + var cardCount = this.cards.length; + for (c in this.cards) { + minVal += this.cards[c].minValue; + } + if (cardCount > 4 && minVal < 22) + minVal = 21; + return minVal; + }; + + this.evalBest = function() + { + var bestVal = this.evalMin(); + if (bestVal > 21) + return 0; + else if (bestVal == 21) + return bestVal; + + for (i in this.cards) { + if (this.cards[i].rank == 1) + { + var v = bestVal + 10; + if (v <= 21) + bestVal = v; + } + } + return bestVal; + + }; + this.toString = function() + { + var s = ""; + for (i in this.cards) + s += this.cards[i].toString() + ":"; + + return s; + }; + + this.drawFrom = function(d) + { + var c = d.draw (); + this.cards.push(c); +// updateDisplay (); + }; + } + + + function hitMe () + { + myDeck.drawFrom (availDeck); + } + ]]></script> + <final id="exit" /> + <state id="root" initial="newgame"> + <onentry> + <script> + var myDeck = new Deck; + var dealerCards = new Deck; + var availDeck = new Deck; + </script> + + </onentry> + <transition event="NEWGAME" target="newgame" /> + <state id="newgame"> + <transition target="newround" /> + </state> + <state id="quitdlg"> + <transition event="YES" target="exit" /> + <transition event="NO" target="gamestate" /> + </state> + <state id="game"> + <history type="deep" id="gamestate" /> + <transition event="EXIT" target="quitdlg" /> + <state id="newround"> + <onentry> + <assign dataid="pot" expr="0" /> + <script> + availDeck.reset (); + myDeck.clear (); + dealerCards.clear(); + dealerCards.drawFrom(availDeck); + hitMe (); + hitMe (); + </script> + </onentry> + <transition target="waitForBet" /> + </state> + <state id="waitForBet"> + <transition event="BET" target="testCards" cond="parseInt(_data.pointsToBet) <= _data.points"> + <assign dataid="pot" expr="_data.pointsToBet" /> + <assign dataid="points" expr="_data.points-_data.pot" /> + </transition> + <transition event="BET" target="betTooHigh" cond="parseInt(_data.pointsToBet) > _data.points"> + </transition> + <transition event="SURRENDER" target="newround" /> + </state> + <state id="betTooHigh"> + <transition event="OK" target="waitForBet" /> + <transition event="TIMEOUT" target="waitForBet" /> + <onentry> + <send event="TIMEOUT" delay="1500ms" /> + </onentry> + </state> + <state id="testCards"> + <transition target="loss" cond="myDeck.evalBest() == 0" /> + <transition target="getDealerCards" cond="myDeck.evalBest() == 21" /> + <transition target="waitForAction" cond="myDeck.evalBest() %21 != 0" /> + </state> + + <state id="waitForAction"> + <transition event="HIT" target="testCards"> + <script>hitMe (); </script> + </transition> + <transition event="STAND" target="getDealerCards" /> + </state> + + <state id="getDealerCards"> + <onentry> + <script><![CDATA[ + while (dealerCards.evalBest() > 0 && dealerCards.evalBest() < 17) + dealerCards.drawFrom(availDeck); + ]]></script> + <raise event="DONE" /> + </onentry> + <transition target="checkWinner" /> + </state> + + <state id="checkWinner"> + + <onentry> + <assign location="_data.diff" expr="myDeck.evalBest() - dealerCards.evalBest()" /> + </onentry> + + <transition cond="diff>0" target="win" /> + <transition cond="diff<0" target="loss" /> + <transition cond="diff==0" target="draw" /> + </state> + <state id="endGame"> + <invoke type="q-bindings"><content>[[welcomeLabel,"text","Game Over"]]</content></invoke> + <transition event="TIMEOUT" target="newgame" /> + <onentry> + <send event="TIMEOUT" delay="3s" /> + </onentry> + </state> + <state id="endRound"> + <invoke type="q-bindings"><content>[[newRoundButton,"enabled",true]]</content></invoke> + <transition event="NEWROUND" target="newround" /> + <transition event="TIMEOUT" target="newround" /> + <onentry> + <send event="TIMEOUT" delay="3s" /> + </onentry> + + <state id="win"> + <onentry> + <assign dataid="points" expr="Math.floor(_data.points) + Math.floor(_data.pot) * 2" /> + </onentry> + </state> + <state id="loss"> + <invoke type="q-bindings"><content>[[welcomeLabel,"text","You Lost..."]]</content></invoke> + <transition cond="points == 0" target="endGame" /> + </state> + <state id="draw"> + <invoke type="q-bindings"><content>[[welcomeLabel,"text","You It's a draw."]]</content></invoke> + <onentry> + <assign dataid="points" expr="Math.floor(_data.points) + Math.floor(pot)" /> + </onentry> + </state> + </state> + </state> + + </state> + +</scxml> + + + diff --git a/examples/qml/blackjack/main.cpp b/examples/qml/blackjack/main.cpp new file mode 100644 index 0000000..dd7777a --- /dev/null +++ b/examples/qml/blackjack/main.cpp @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QApplication> +#include <QmlEngine> +#include <QmlComponent> +#include <QmlView> +#include <QDebug> +#include <QFile> +#include <QGraphicsObject> +int main(int argc, char ** argv) +{ + QApplication app(argc, argv); + + QmlView view(NULL); + QmlComponent component(view.engine(), "blackjack.qml"); + QGraphicsObject* o = qobject_cast<QGraphicsObject*>(component.create()); + view.scene()->addItem(o); + view.show(); + return app.exec(); +} diff --git a/qmlscxml/qmlscxml.cpp b/qmlscxml/qmlscxml.cpp index 2183945..04a4c89 100644 --- a/qmlscxml/qmlscxml.cpp +++ b/qmlscxml/qmlscxml.cpp @@ -1,3 +1,43 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of SCXML on Qt labs +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ #include "qmlscxml.h" #include "qscxml.h" #include <QDebug> @@ -147,12 +187,14 @@ void QmlScxml::setSource(const QUrl & u) void QmlScxml::onDataChanged(const QString & key) { + qDebug() << "onDataChanged" << key; m_data->blockSignals(true); scxml->setData(key,(*m_data)[key]); m_data->blockSignals(false); } void QmlScxml::onDataChanged(const QString & key, const QVariant & value) { + qDebug() << "onDataChanged" << key << value; if (m_data) { (*m_data)[key] = value; } diff --git a/qmlscxml/qmlscxml.h b/qmlscxml/qmlscxml.h index b44b45f..c727ecc 100644 --- a/qmlscxml/qmlscxml.h +++ b/qmlscxml/qmlscxml.h @@ -1,3 +1,43 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of SCXML on Qt labs +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ #ifndef QMLSCXML_H #define QMLSCXML_H diff --git a/src/qscxml.cpp b/src/qscxml.cpp index d6142e5..a0b2019 100644 --- a/src/qscxml.cpp +++ b/src/qscxml.cpp @@ -455,7 +455,7 @@ static QScriptValue invoke(QScriptContext *context, QScriptEngine *engine) return QScriptValue(); } -static QScriptValue dataAccess(QScriptContext *context, QScriptEngine *) +static QScriptValue dataAccess(QScriptContext *context, QScriptEngine * engine) { if (context->argumentCount() == 0) { // getter @@ -464,7 +464,8 @@ static QScriptValue dataAccess(QScriptContext *context, QScriptEngine *) // setter QScriptValue val = context->argument(0); context->callee().setProperty("value",val); - QScxml* scxml = qobject_cast<QScxml*>(context->callee().property("scxml").toQObject()); + qDebug() << "data set" << val.toVariant() << context->callee().property("key").toString(); + QScxml* scxml = qobject_cast<QScxml*>(engine->globalObject().property("scxml").toQObject()); if (scxml) { scxml->dataChanged(context->callee().property("key").toString(),val.toVariant()); } |