1 /***************************************************************************
2 **
3 ** Copyright (C) 2014 BlackBerry Limited. All rights reserved.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the QtBluetooth module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 **   * Redistributions of source code must retain the above copyright
25 **     notice, this list of conditions and the following disclaimer.
26 **   * Redistributions in binary form must reproduce the above copyright
27 **     notice, this list of conditions and the following disclaimer in
28 **     the documentation and/or other materials provided with the
29 **     distribution.
30 **   * Neither the name of The Qt Company Ltd nor the names of its
31 **     contributors may be used to endorse or promote products derived
32 **     from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 #ifndef PINGPONG_H
52 #define PINGPONG_H
53 
54 #include <QTimer>
55 #include <QObject>
56 #include <qbluetoothserver.h>
57 #include <qbluetoothserviceinfo.h>
58 #include <qbluetoothlocaldevice.h>
59 #include <qbluetoothservicediscoveryagent.h>
60 
61 static QString serviceUuid(QStringLiteral("e8e10f95-1a70-4b27-9ccf-02010264e9c9"));
62 static QString androidUuid(QStringLiteral("c9e96402-0102-cf9c-274b-701a950fe1e8"));
63 
64 class PingPong: public QObject
65 {
66     Q_OBJECT
67     Q_PROPERTY(float ballX READ ballX NOTIFY ballChanged)
68     Q_PROPERTY(float ballY READ ballY NOTIFY ballChanged)
69     Q_PROPERTY(float leftBlockY READ leftBlockY NOTIFY leftBlockChanged)
70     Q_PROPERTY(float rightBlockY READ rightBlockY NOTIFY rightBlockChanged)
71     Q_PROPERTY(bool showDialog READ showDialog NOTIFY showDialogChanged)
72     Q_PROPERTY(QString message READ message NOTIFY showDialogChanged)
73     Q_PROPERTY(int role READ role NOTIFY roleChanged)
74     Q_PROPERTY(int leftResult READ leftResult NOTIFY resultChanged)
75     Q_PROPERTY(int rightResult READ rightResult NOTIFY resultChanged)
76 public:
77     PingPong();
78     ~PingPong();
79     float ballX() const;
80     float ballY() const;
81     float leftBlockY() const;
82     float rightBlockY() const;
83     void checkBoundaries();
84     void updateDirection();
85     bool showDialog() const;
86     QString message() const;
87     void setMessage(const QString &message);
88     int role() const;
89     int leftResult() const;
90     int rightResult() const;
91     void checkResult();
92 
93 public slots:
94     void startGame();
95     void update();
96     void setSize(const float &x, const float &y);
97     void updateBall(const float &bX, const float &bY);
98     void updateLeftBlock(const float &lY);
99     void updateRightBlock(const float &rY);
100     void startServer();
101     void startClient();
102     void clientConnected();
103     void clientDisconnected();
104     void serverConnected();
105     void serverDisconnected();
106     void socketError(QBluetoothSocket::SocketError);
107     void serverError(QBluetoothServer::Error);
108     void serviceScanError(QBluetoothServiceDiscoveryAgent::Error);
109     void done();
110     void addService(const QBluetoothServiceInfo &);
111     void readSocket();
112 
113 Q_SIGNALS:
114     void ballChanged();
115     void leftBlockChanged();
116     void rightBlockChanged();
117     void showDialogChanged();
118     void roleChanged();
119     void resultChanged();
120 
121 private:
122     QBluetoothServer *m_serverInfo;
123     QBluetoothServiceInfo m_serviceInfo;
124     QBluetoothSocket *socket;
125     QBluetoothServiceDiscoveryAgent *discoveryAgent;
126 
127     float m_ballX;
128     float m_ballY;
129     float m_ballPreviousX;
130     float m_ballPreviousY;
131     float m_leftBlockY;
132     float m_rightBlockY;
133     QTimer *m_timer;
134     float m_boardWidth;
135     float m_boardHeight;
136     float m_direction;
137     float m_targetX;
138     float m_targetY;
139     int interval;
140     int m_resultLeft;
141     int m_resultRight;
142     bool m_showDialog;
143     QString m_message;
144     int m_role;
145     float m_proportionX;
146     float m_proportionY;
147     bool m_serviceFound;
148 };
149 
150 #endif // PINGPONG_H
151