tennis.qml Example File
../demos/qmltennis/tennis.qml
import Qt 4.7
import QtMobility.connectivity 1.2
import Controller 1.0
Rectangle {
id: bounds
property BluetoothService currentService
property alias minimalDiscovery: myModel.minimalDiscovery
property alias uuidFilder: myModel.uuidFilter
property color fg: "white"
property color bg: "black"
Rectangle {
id: page
width: 640
height: 360
color: bg
focus: true
BluetoothDiscoveryModel {
id: myModel
function connect(s){
socket.setService(s);
socket.setConnected(true);
}
discovery: false
minimalDiscovery: true
onNewServiceDiscovered: { connect(service); discovery = false; }
uuidFilter: "e8e10f95-1a70-4b27-9ccf-02010264e9c9"
}
BluetoothSocket {
id: socket
function parse(s){
var args = s.split(" ");
if(args[0] == "m"){
ball.x = args[1]-ball.width/2;
ball.y = args[2]-ball.height/2;
}
else if(args[0] == "l"){
leftPaddle.y = Number(args[1])+topBumper.height;
}
else if(args[0] == "s"){
scoreLeft.text = args[1];
scoreRight.text = args[2];
}
else if(args[0] == "e"){
socket.sendStringData(s);
}
else if(args[0] == "E"){
var d = new Date();
var lag = d.getTime() - args[1];
if(lag > 250){
statusText.text = "LAG! " + lag + "ms";
}
}
}
onStateChanged: console.log("New socket state " + state);
onErrorChanged: { statusText.text = error; }
onServiceChanged: console.log("New service");
onConnectedChanged: {
console.log("Connected: " + connected);
if(connected) {
statusText.text = "Connected";
controller.stop();
}
else {
statusText.text = "Unconnected " + error;
controller.start();
controller.refresh();
}
}
onDataAvailable: parse(socket.stringData);
}
Timer {
id: lagTimer
function sendEcho() {
if(socket.connected){
var s = "E " + new Date().getTime();
socket.sendStringData(s);
}
if(server_socket.connected){
var s = "e " + new Date().getTime();
server_socket.sendStringData(s);
}
}
interval: 1000
repeat: true
running: socket.connected || server_socket.connected
onTriggered: sendEcho();
}
BluetoothService {
id: btserver
serviceName: "bttennis"
serviceDescription: "QML Tennis Service"
serviceProtocol: "l2cap"
serviceUuid: "e8e10f95-1a70-4b27-9ccf-02010264e9c9"
registered: true
onNewClient: {
console.log("Got a new connections!");
if (!server_socket.connected) {
assignNextClient(server_socket);
} else {
nextClient().connected = false;
}
}
}
BluetoothSocket {
id: server_socket
function parse(s){
var args = s.split(" ");
if(args[0] == "r"){
rightPaddle.y = Number(args[1])+topBumper.height;
}
else if(args[0] == "E"){
sendStringData(s);
}
else if(args[0] == "E"){
var d = new Date();
var lag = d.getTime() - args[1];
if(lag > 250){
statusText.text = "LAG! " + lag + "ms";
}
}
}
onStateChanged: console.log("SERVER: New socket state " + state);
onErrorChanged: { console.log("SERVER: error: " + error); statusText.text = error; }
onServiceChanged: console.log("SERVER: New service");
onConnectedChanged: {
console.log("SERVER Connected: " + connected);
if(connected) {
statusText.text = "Server Connected";
controller.refresh();
}
else {
statusText.text = "Server Unconnected " + error;
}
}
onDataAvailable: parse(stringData);
}
Connections {
target: deviceSensor.item
onReadingChanged: {
if (x >= 20 && x <= 70)
rightPaddle.paddlePos = ((x - 20) / 50 * page.height) - rightPaddle.height / 2;
}
}
Handover {
id: handover
bluetoothService: BluetoothService {
serviceName: "bttennis"
serviceProtocol: "l2cap";
serviceUuid: "e8e10f95-1a70-4b27-9ccf-02010264e9c9"
}
onBluetoothServiceChanged: {
console.log("Connecting to NFC provided address " + bluetoothService.deviceAddress);
statusText.text = "Connecting: " + bluetoothService.deviceAddress + " " + bluetoothService.serverPort;
socket.setService(bluetoothService);
socket.setConnected(true);
}
onAvailableChanged: statusText.text = "Touch to play";
}
MouseArea {
anchors.fill: parent
enabled: true
onMousePositionChanged: {
var y = mouseY-rightPaddle.height/2;
var p = server_socket.connected ? leftPaddle : rightPaddle;
if(y < topBumper.height)
y = topBumper.height;
if(y > page.height - bottomBumper.height - rightPaddle.height)
y = page.height - bottomBumper.height - rightPaddle.height;
p.paddlePos = y;
}
}
ConnectButton {
id: connectButton
mymodel: myModel
socket: socket
server_socket: server_socket
}
Rectangle {
id: ball
x: page.width/2-width/2;
y: page.height/2-height/2;
width: 12; height: 12; z: 1
color: fg
}
Paddle {
id: leftPaddle
property int lastsent: 0
onPaddleMoved: {
controller.moveLeftPaddle(y);
if (server_socket.connected) {
var c = Math.round(y-topBumper.height);
if (c == lastsent){
return;
}
lastsent = c;
server_socket.sendStringData("l " + c);
}
}
}
Paddle {
id: rightPaddle
x: page.width - 12;
onPaddleMoved: {
if(socket.connected)
socket.sendStringData("r " + Math.round(rightPaddle.y-topBumper.height));
controller.moveRightPaddle(y);
}
}
Bumper {
id: topBumper
}
Bumper {
id: bottomBumper
y: page.height-height
}
Score {
id: scoreLeft
}
Score {
id: scoreRight
x: 3*page.width/4-paintedWidth/2
}
Repeater {
model: page.height / 8
Rectangle { color: fg; x: page.width/2; y: index * 8; width: 2; height: 5 }
}
Text {
id: statusText
font.family: "Old English"
font.pixelSize: 25; font.bold: true
text: ""
color: fg
y: page.height-height-25
x: 24
onTextChanged: NumberAnimation { target: statusText; property: "opacity"; easing.type: Easing.InOutSine; from: 1; to: 0; duration: 2000 }
}
Keys.onPressed: {
var p = server_socket.connected ? leftPaddle : rightPaddle;
if(event.key == Qt.Key_Up){
p.paddlePos -= 20;
event.accepted = true;
}
else if(event.key == Qt.Key_Down){
p.paddlePos += 20;
event.accepted = true;
}
}
Loader {
id: deviceSensor
source: "sensor.qml"
}
transform: Scale { xScale: bounds.width/page.width; yScale: bounds.height/page.height }
Controller {
id: controller
property bool dir: true;
property int lastx: page.width/2;
onMoveBall: {
ball.x = x-ball.width/2;
ball.y = y-ball.height/2;
if((ball.x+ball.width > rightPaddle.x) &&
(ball.y+ball.width > rightPaddle.y) &&
(ball.y < rightPaddle.y + rightPaddle.height)){
ballCollision(4);
}
else if((ball.x < leftPaddle.width) &&
(ball.y+ball.width > leftPaddle.y) &&
(ball.y < leftPaddle.y + leftPaddle.height)){
ballCollision(3);
}
else if(ball.x > page.width){
scored(4);
}
else if(x < 0){
scored(3);
}
else if(ball.y < topBumper.height){
ballCollision(1);
}
else if((y + ball.height/2) > bottomBumper.y){
ballCollision(2);
}
if(lastx > x){
dir = true;
}
else {
dir = false;
}
lastx = x;
if (!socket.connected && !server_socket.connected){
if(dir && ball.x < page.width/2) {
leftPaddle.paddlePos = ball.y - leftPaddle.height/2;
}
else if(!dir && ball.x > page.width/2){
rightPaddle.paddlePos = ball.y - leftPaddle.height/2;
}
}
if (server_socket.connected){
var s = "m " + ball.x + " " + ball.y;
server_socket.sendStringData(s);
}
}
onScore: {
scoreLeft.text = left;
scoreRight.text = right;
if (server_socket.connected) {
var s = "s " + left + " " + right;
server_socket.sendStringData(s);
}
}
}
}
}
[+] Documentation Feedback