1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples 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 BOAT_P_H
52#define BOAT_P_H
53
54//
55// W A R N I N G
56// -------------
57//
58// This file is not part of the Qt API. It exists purely as an
59// implementation detail. This header file may change from version to
60// version without notice, or even be removed.
61//
62// We mean it.
63//
64
65//Own
66#include "bomb.h"
67#include "graphicsscene.h"
68
69// Qt
70#include <QGraphicsRotation>
71#include <QKeyEventTransition>
72#include <QState>
73
74static const int MAX_BOMB = 5;
75
76
77//These transtion test if we have to stop the boat (i.e current speed is 1)
78class KeyStopTransition : public QKeyEventTransition
79{
80public:
81 KeyStopTransition(Boat *b, QEvent::Type t, int k)
82 : QKeyEventTransition(b, t, k), boat(b)
83 {
84 }
85protected:
86 bool eventTest(QEvent *event) override
87 {
88 if (!QKeyEventTransition::eventTest(event))
89 return false;
90 return (boat->currentSpeed() == 1);
91 }
92private:
93 Boat *boat;
94};
95
96//These transtion test if we have to move the boat (i.e current speed was 0 or another value)
97 class KeyMoveTransition : public QKeyEventTransition
98{
99public:
100 KeyMoveTransition(Boat *b, QEvent::Type t, int k)
101 : QKeyEventTransition(b, t, k), boat(b), key(k)
102 {
103 }
104protected:
105 bool eventTest(QEvent *event) override
106 {
107 if (!QKeyEventTransition::eventTest(event))
108 return false;
109 return (boat->currentSpeed() >= 0);
110 }
111 void onTransition(QEvent *) override
112 {
113 //We decrease the speed if needed
114 if (key == Qt::Key_Left && boat->currentDirection() == Boat::Right)
115 boat->setCurrentSpeed(boat->currentSpeed() - 1);
116 else if (key == Qt::Key_Right && boat->currentDirection() == Boat::Left)
117 boat->setCurrentSpeed(boat->currentSpeed() - 1);
118 else if (boat->currentSpeed() < 3)
119 boat->setCurrentSpeed(boat->currentSpeed() + 1);
120 boat->updateBoatMovement();
121 }
122private:
123 Boat *boat;
124 int key;
125};
126
127//This transition trigger the bombs launch
128 class KeyLaunchTransition : public QKeyEventTransition
129{
130public:
131 KeyLaunchTransition(Boat *boat, QEvent::Type type, int key)
132 : QKeyEventTransition(boat, type, key), boat(boat)
133 {
134 }
135protected:
136 bool eventTest(QEvent *event) override
137 {
138 if (!QKeyEventTransition::eventTest(event))
139 return false;
140 //We have enough bomb?
141 return (boat->bombsLaunched() < MAX_BOMB);
142 }
143private:
144 Boat *boat;
145};
146
147//This state is describing when the boat is moving right
148class MoveStateRight : public QState
149{
150public:
151 explicit MoveStateRight(Boat *boat, QState *parent = nullptr)
152 : QState(parent), boat(boat)
153 {
154 }
155protected:
156 void onEntry(QEvent *) override
157 {
158 boat->setCurrentDirection(Boat::Right);
159 boat->updateBoatMovement();
160 }
161private:
162 Boat *boat;
163};
164
165 //This state is describing when the boat is moving left
166class MoveStateLeft : public QState
167{
168public:
169 explicit MoveStateLeft(Boat *boat, QState *parent = nullptr)
170 : QState(parent), boat(boat)
171 {
172 }
173protected:
174 void onEntry(QEvent *) override
175 {
176 boat->setCurrentDirection(Boat::Left);
177 boat->updateBoatMovement();
178 }
179private:
180 Boat *boat;
181};
182
183//This state is describing when the boat is in a stand by position
184class StopState : public QState
185{
186public:
187 explicit StopState(Boat *boat, QState *parent = nullptr)
188 : QState(parent), boat(boat)
189 {
190 }
191protected:
192 void onEntry(QEvent *) override
193 {
194 boat->setCurrentSpeed(0);
195 boat->setCurrentDirection(Boat::None);
196 boat->updateBoatMovement();
197 }
198private:
199 Boat *boat;
200};
201
202//This state is describing the launch of the torpedo on the right
203class LaunchStateRight : public QState
204{
205public:
206 explicit LaunchStateRight(Boat *boat, QState *parent = nullptr)
207 : QState(parent), boat(boat)
208 {
209 }
210protected:
211 void onEntry(QEvent *) override
212 {
213 Bomb *b = new Bomb;
214 b->setPos(ax: boat->x()+boat->size().width(),ay: boat->y());
215 GraphicsScene *scene = static_cast<GraphicsScene *>(boat->scene());
216 scene->addItem(bomb: b);
217 b->launch(direction: Bomb::Right);
218 boat->setBombsLaunched(boat->bombsLaunched() + 1);
219 }
220private:
221 Boat *boat;
222};
223
224//This state is describing the launch of the torpedo on the left
225class LaunchStateLeft : public QState
226{
227public:
228 explicit LaunchStateLeft(Boat *boat, QState *parent = nullptr)
229 : QState(parent), boat(boat)
230 {
231 }
232protected:
233 void onEntry(QEvent *) override
234 {
235 Bomb *b = new Bomb;
236 b->setPos(ax: boat->x() - b->size().width(), ay: boat->y());
237 GraphicsScene *scene = static_cast<GraphicsScene *>(boat->scene());
238 scene->addItem(bomb: b);
239 b->launch(direction: Bomb::Left);
240 boat->setBombsLaunched(boat->bombsLaunched() + 1);
241 }
242private:
243 Boat *boat;
244};
245
246#endif // BOAT_P_H
247

source code of qtbase/examples/widgets/animation/sub-attaq/boat_p.h