-
Notifications
You must be signed in to change notification settings - Fork 3
/
InteractiveItem.qml
114 lines (92 loc) · 3.17 KB
/
InteractiveItem.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import QtQuick 2.2
import Box2D 2.0
import Ros 1.0
Item {
id:item
width: 2*parent.height * sandbox.physicalCubeSize / sandbox.physicalMapWidth
height: width
rotation: Math.random() * 360
objectName: "interactive"
property string name
property string image
property var boundingbox:
Polygon {
id:bbpoly
vertices: [
Qt.point(origin.x, origin.y),
Qt.point(origin.x + image.sourceSize.width * bbratio, origin.y),
Qt.point(origin.x + image.sourceSize.width * bbratio, origin.y + image.sourceSize.height * bbratio),
Qt.point(origin.x, origin.y + image.sourceSize.height * bbratio),
]
density: 1
friction: 1
restitution: 0.1
}
property alias body: cubeBody
property double bbratio: 1 // set later (cf below) once paintedWidth is known
property alias origin: imageOrigin
Image {
id: image
fillMode: Image.PreserveAspectFit
anchors.fill: parent
source: parent.image
Item {
// this item sticks to the 'visual' origin of the object, taking into account
// possible margins appearing when resizing
id: imageOrigin
rotation: parent.rotation
x: parent.x + (parent.width - parent.paintedWidth)/2
y: parent.y + (parent.height - parent.paintedHeight)/2
}
onPaintedWidthChanged: {
bbratio= image.paintedWidth/image.sourceSize.width;
}
// Draw a red border around the interactive items
//Rectangle {
// color:"transparent"
// anchors.fill:parent
// border.color: "red"
//}
}
Body {
id: cubeBody
target: item
world: physicsWorld
bodyType: Body.Dynamic
Component.onCompleted: {
cubeBody.addFixture(item.boundingbox);
}
angularDamping: 5
linearDamping: 5
}
// PinchArea {
// anchors.fill: parent
// pinch.target: parent
// pinch.minimumRotation: -360
// pinch.maximumRotation: 360
// //pinch.minimumScale: 1
// //pinch.maximumScale: 1
// pinch.dragAxis: Pinch.XAndYAxis
// MouseArea {
// anchors.fill: parent
// drag.target: item
// scrollGestureEnabled: false
// }
// }
Item {
id: objectCenter
anchors.centerIn: parent
rotation: parent.rotation
TFBroadcaster {
target: parent
frame: parent.parent.name
active: sandbox.visible
origin: mapOrigin
parentframe: mapOrigin.name
pixelscale: sandbox.pixel2meter
}
}
function isIn(tx, ty) {
return (tx > x) && (tx < x + width) && (ty > y) && (ty < y + height);
}
}