aboutsummaryrefslogtreecommitdiffstats
path: root/examples/qml/dynamicscene/content/itemCreation.js
diff options
context:
space:
mode:
authorAlan Alpert <alan.alpert@nokia.com>2012-02-28 11:30:16 +1000
committerQt by Nokia <qt-info@nokia.com>2012-02-28 09:19:53 +0100
commit01bfcfba0287fa7e9e051e2540ebbdb01fee5a9e (patch)
tree934353470a620fd2658a220db1d35093318bd814 /examples/qml/dynamicscene/content/itemCreation.js
parent7d32bacfc574dabb469ad8c0a83c70bb0fc6d770 (diff)
Move some toys back to examples
tvtennis, corkboards and dynamicscene are more examples than demos. clocks and tic-tac-toe are remaining demos for now. Change-Id: I3d9501a4742349a9eb7efdad0d06aa6e7cb02c14 Reviewed-by: Michael Brasser <michael.brasser@nokia.com>
Diffstat (limited to 'examples/qml/dynamicscene/content/itemCreation.js')
-rw-r--r--examples/qml/dynamicscene/content/itemCreation.js62
1 files changed, 62 insertions, 0 deletions
diff --git a/examples/qml/dynamicscene/content/itemCreation.js b/examples/qml/dynamicscene/content/itemCreation.js
new file mode 100644
index 0000000000..40f5415f9f
--- /dev/null
+++ b/examples/qml/dynamicscene/content/itemCreation.js
@@ -0,0 +1,62 @@
+var itemComponent = null;
+var draggedItem = null;
+var startingMouse;
+var posnInWindow;
+
+function startDrag(mouse)
+{
+ posnInWindow = paletteItem.mapToItem(window, 0, 0);
+ startingMouse = { x: mouse.x, y: mouse.y }
+ loadComponent();
+}
+
+//Creation is split into two functions due to an asynchronous wait while
+//possible external files are loaded.
+
+function loadComponent() {
+ if (itemComponent != null) { // component has been previously loaded
+ createItem();
+ return;
+ }
+
+ itemComponent = Qt.createComponent(paletteItem.componentFile);
+ if (itemComponent.status == Component.Loading) //Depending on the content, it can be ready or error immediately
+ component.statusChanged.connect(createItem);
+ else
+ createItem();
+}
+
+function createItem() {
+ if (itemComponent.status == Component.Ready && draggedItem == null) {
+ draggedItem = itemComponent.createObject(window, {"image": paletteItem.image, "x": posnInWindow.x, "y": posnInWindow.y, "z": 3});
+ // make sure created item is above the ground layer
+ } else if (itemComponent.status == Component.Error) {
+ draggedItem = null;
+ console.log("error creating component");
+ console.log(itemComponent.errorString());
+ }
+}
+
+function continueDrag(mouse)
+{
+ if (draggedItem == null)
+ return;
+
+ draggedItem.x = mouse.x + posnInWindow.x - startingMouse.x;
+ draggedItem.y = mouse.y + posnInWindow.y - startingMouse.y;
+}
+
+function endDrag(mouse)
+{
+ if (draggedItem == null)
+ return;
+
+ if (draggedItem.y < toolbox.height) { //Don't drop it in the toolbox
+ draggedItem.destroy();
+ draggedItem = null;
+ } else {
+ draggedItem.created = true;
+ draggedItem = null;
+ }
+}
+