void Tennis::setPaddlePos(int p) { paddle_pos = p; if(isClient) emit moveRightPaddle(paddle_pos); else emit moveLeftPaddle(paddle_pos); }
void Tennis::serverConnected(const QString &name) { board->setStatus("Server for " + name, 100, 0); m_discoveryAgent->stop(); isConnected = true; board->animateConnect(false); board->fadeConnect(true); emit moveLeftPaddle(paddle_pos); }
//! [readSocket] void TennisClient::readSocket() { if (!socket) return; while (socket->bytesAvailable()) { QString str; *stream >> str; QStringList args = str.split(QChar(' ')); QString s = args.takeFirst(); if (s == "m" && args.count() == 2) { emit moveBall(args.at(0).toInt(), args.at(1).toInt()); } else if (s == "s" && args.count() == 2){ emit score(args.at(0).toInt(), args.at(1).toInt()); } else if (s == "l" && args.count() == 1){ emit moveLeftPaddle(args.at(0).toInt()); } else if (s == "e"){ // echo QByteArray b; QDataStream s(&b, QIODevice::WriteOnly); s << str; socket->write(b); } else if (s == "E"){ lagTimeout = 0; QTime then = QTime::fromString(args.at(0), "hh:mm:ss.zzz"); if (then.isValid()) { emit lag(then.msecsTo(QTime::currentTime())); // qDebug() << "RTT: " << then.msecsTo(QTime::currentTime()) << "ms"; } } else { qDebug() << Q_FUNC_INFO << "Unknown command" << str; } } }
Tennis::Tennis(QWidget *parent) : QDialog(parent), ui(new Ui_Tennis), board(new Board), controller(new Controller), socket(0), m_discoveryAgent(new QBluetoothServiceDiscoveryAgent), m_handover(0) { // start Bluetooth if not started QBluetoothLocalDevice *device = new QBluetoothLocalDevice(); device->powerOn(); delete device; device = 0; //! [Construct UI] ui->setupUi(this); isClient = false; isConnected = false; quickDiscovery = true; #if defined (Q_OS_SYMBIAN) || defined(Q_OS_WINCE) || defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6) setWindowState(Qt::WindowMaximized); #endif ui->pongView->setScene(board->getScene()); connect(ui->pongView, SIGNAL(mouseMove(int, int)), this, SLOT(mouseMove(int, int))); ui->pongView->setMouseTracking(false); connect(board, SIGNAL(ballCollision(Board::Edge)), controller, SLOT(ballCollision(Board::Edge))); connect(board, SIGNAL(scored(Board::Edge)), controller, SLOT(scored(Board::Edge))); connect(controller, SIGNAL(moveBall(int,int)), board, SLOT(setBall(int,int))); connect(this, SIGNAL(moveLeftPaddle(int)), board, SLOT(setLeftPaddle(int))); connect(this, SIGNAL(moveRightPaddle(int)), board, SLOT(setRightPaddle(int))); connect(controller, SIGNAL(score(int,int)), board, SLOT(setScore(int,int))); connect(controller, SIGNAL(fps(const QString&)), this, SLOT(fps(const QString&))); setFocusPolicy(Qt::WheelFocus); paddle_pos = (Board::Height-12)/2-Board::Paddle/2; endPaddlePos = paddle_pos; emit moveLeftPaddle(paddle_pos); board->setRightPaddle(paddle_pos); server = new TennisServer(this); connect(controller, SIGNAL(moveBall(int,int)), server, SLOT(moveBall(int,int))); connect(controller, SIGNAL(score(int,int)), server, SLOT(score(int,int))); connect(this, SIGNAL(moveLeftPaddle(int)), server, SLOT(moveLeftPaddle(int))); connect(server, SIGNAL(clientConnected(QString)), this, SLOT(serverConnected(QString))); connect(server, SIGNAL(clientDisconnected(QString)), this, SLOT(serverDisconnected())); connect(server, SIGNAL(moveRightPaddle(int)), board, SLOT(setRightPaddle(int))); connect(server, SIGNAL(lag(int)), this, SLOT(lagReport(int))); connect(server, SIGNAL(clientConnected(QString)), controller, SLOT(refresh())); server->startServer(); client = new TennisClient(this); connect(client, SIGNAL(moveBall(int,int)), board, SLOT(setBall(int,int))); connect(client, SIGNAL(moveLeftPaddle(int)), board, SLOT(setLeftPaddle(int))); connect(client, SIGNAL(connected(QString)), this, SLOT(clientConnected(QString))); connect(client, SIGNAL(disconnected()), this, SLOT(clientDisconnected())); connect(this, SIGNAL(moveRightPaddle(int)), client, SLOT(moveRightPaddle(int))); connect(client, SIGNAL(score(int,int)), board, SLOT(setScore(int,int))); connect(client, SIGNAL(lag(int)), this, SLOT(lagReport(int))); connect(this, SIGNAL(moveLeftPaddle(int)), controller, SLOT(moveLeftPaddle(int))); connect(this, SIGNAL(moveRightPaddle(int)), controller, SLOT(moveRightPaddle(int))); connect(server, SIGNAL(moveRightPaddle(int)), controller, SLOT(moveRightPaddle(int))); // ui->pongView->setBackgroundBrush(QBrush(Qt::white)); ui->pongView->setCacheMode(QGraphicsView::CacheBackground); QNearFieldManager nearFieldManager; if (nearFieldManager.isAvailable()) { m_handover = new Handover(server->serverPort(), this); connect(m_handover, SIGNAL(bluetoothServiceChanged()), this, SLOT(nearFieldHandover())); connect(m_discoveryAgent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)), this, SLOT(serviceDiscovered(QBluetoothServiceInfo))); connect(m_discoveryAgent, SIGNAL(finished()), this, SLOT(discoveryFinished())); } m_discoveryAgent->setUuidFilter(QBluetoothUuid(serviceUuid)); QString address; QString port; QStringList args = QCoreApplication::arguments(); if(args.length() >= 2){ address = args.at(1); if(args.length() >= 3){ port = args.at(2); } } if(address.isEmpty()){ QSettings settings("QtDF", "bttennis"); address = settings.value("lastclient").toString(); } if(!address.isEmpty()){ qDebug() << "Connect to" << address << port; QBluetoothDeviceInfo device = QBluetoothDeviceInfo(QBluetoothAddress(address), "", QBluetoothDeviceInfo::ComputerDevice); QBluetoothServiceInfo service; if (!port.isEmpty()) { QBluetoothServiceInfo::Sequence protocolDescriptorList; QBluetoothServiceInfo::Sequence protocol; protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::L2cap)) << QVariant::fromValue(port.toUShort()); protocolDescriptorList.append(QVariant::fromValue(protocol)); service.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList, protocolDescriptorList); qDebug() << "port" << port.toUShort() << service.protocolServiceMultiplexer(); } else { service.setServiceUuid(QBluetoothUuid(serviceUuid)); } service.setDevice(device); client->startClient(service); board->setStatus("Connecting", 100, 25); } else if (nearFieldManager.isAvailable()) { board->setStatus(tr("Touch to play"), 100, 25); } setEnabled(true); paddleAnimation = new QPropertyAnimation(this, "paddlePos", this); paddleAnimation->setEasingCurve(QEasingCurve::InOutQuad); ui->pongView->installEventFilter(this); }