1 | // |
2 | // KBlackBox |
3 | // |
4 | // A simple game inspired by an emacs module |
5 | // |
6 | /*************************************************************************** |
7 | * Copyright (c) 1999-2000, Robert Cimrman * |
8 | * cimrman3@students.zcu.cz * |
9 | * * |
10 | * Copyright (c) 2007, Nicolas Roffet * |
11 | * nicolas-kde@roffet.com * |
12 | * * |
13 | * * |
14 | * This program is free software; you can redistribute it and/or modify * |
15 | * it under the terms of the GNU General Public License as published by * |
16 | * the Free Software Foundation; either version 2 of the License, or * |
17 | * (at your option) any later version. * |
18 | * * |
19 | * This program is distributed in the hope that it will be useful, * |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
22 | * GNU General Public License for more details. * |
23 | * * |
24 | * You should have received a copy of the GNU General Public License * |
25 | * along with this program; if not, write to the * |
26 | * Free Software Foundation, Inc., * |
27 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA * |
28 | ***************************************************************************/ |
29 | |
30 | #include "kbbgraphicsitemball.h" |
31 | |
32 | |
33 | |
34 | #include <QGraphicsSceneHoverEvent> |
35 | #include <QTimer> |
36 | |
37 | |
38 | #include "kbbgraphicsiteminteractioninfo.h" |
39 | #include "kbbgraphicsitemonbox.h" |
40 | #include "kbbscalablegraphicwidget.h" |
41 | #include "kbbthememanager.h" |
42 | |
43 | |
44 | |
45 | // |
46 | // Constructor / Destructor |
47 | // |
48 | |
49 | KBBGraphicsItemBall::KBBGraphicsItemBall(KBBScalableGraphicWidget::itemType itemType, KBBScalableGraphicWidget* parent, KBBThemeManager* themeManager, int boxPosition, int columns, int rows) : KBBGraphicsItemOnBox( itemType, parent, themeManager, boxPosition, columns, rows) |
50 | { |
51 | m_timer = NULL; |
52 | m_ballType = itemType; |
53 | m_themeManager = themeManager; |
54 | |
55 | setAcceptsHoverEvents(true); |
56 | |
57 | for (int i=0; i<8;i++) |
58 | m_interactionInfos[i] = NULL; |
59 | } |
60 | |
61 | |
62 | KBBGraphicsItemBall::~KBBGraphicsItemBall() |
63 | { |
64 | removeInteractionInfos(); |
65 | } |
66 | |
67 | |
68 | |
69 | // |
70 | // Private slots |
71 | // |
72 | |
73 | void KBBGraphicsItemBall::showInteractions() |
74 | { |
75 | delete m_timer; |
76 | m_timer = NULL; |
77 | |
78 | const int offsetX[8] = {0, 1, 2, 2, 2, 1, 0, 0}; |
79 | const int offsetY[8] = {0, 0, 0, 1, 2, 2, 2, 1}; |
80 | qreal posX; |
81 | qreal posY; |
82 | |
83 | // General interactions for every balls: |
84 | for (int i=0; i<8;i++) { |
85 | posX = x() - KBBScalableGraphicWidget::RATIO/2 + offsetX[i]*KBBScalableGraphicWidget::RATIO; |
86 | posY = y() - KBBScalableGraphicWidget::RATIO/2 + offsetY[i]*KBBScalableGraphicWidget::RATIO; |
87 | KBBScalableGraphicWidget::itemType type = KBBScalableGraphicWidget::interactionInfoDeflection; |
88 | if (i%2 == 1) |
89 | type = KBBScalableGraphicWidget::interactionInfoHit; |
90 | m_interactionInfos[i] = new KBBGraphicsItemInteractionInfo(m_widget, m_themeManager, type, posX, posY, i*45 ); |
91 | } |
92 | |
93 | // If the ball is on a border: |
94 | const KBBScalableGraphicWidget::itemType r = KBBScalableGraphicWidget::interactionInfoReflection; |
95 | const KBBScalableGraphicWidget::itemType rS = KBBScalableGraphicWidget::interactionInfoReflectionSym; |
96 | if (position()<m_columns) { |
97 | m_interactionInfos[0]->setType(r); |
98 | m_interactionInfos[2]->setType(rS); |
99 | } |
100 | if (position()>=m_columns*(m_rows-1)) { |
101 | m_interactionInfos[4]->setType(r); |
102 | m_interactionInfos[6]->setType(rS); |
103 | } |
104 | if (position()%m_columns == 0) { |
105 | m_interactionInfos[6]->setType(r); |
106 | m_interactionInfos[0]->setType(rS); |
107 | } |
108 | if (position()%m_columns == (m_columns-1)) { |
109 | m_interactionInfos[2]->setType(r); |
110 | m_interactionInfos[4]->setType(rS); |
111 | } |
112 | |
113 | // If the ball is on a corner: |
114 | const KBBScalableGraphicWidget::itemType n = KBBScalableGraphicWidget::interactionInfoNothing; |
115 | if (position()==0) |
116 | m_interactionInfos[0]->setType(n); |
117 | if (position()==m_columns-1) |
118 | m_interactionInfos[2]->setType(n); |
119 | if (position()==m_rows*m_columns-1) |
120 | m_interactionInfos[4]->setType(n); |
121 | if (position()==(m_rows-1)*m_columns) |
122 | m_interactionInfos[6]->setType(n); |
123 | } |
124 | |
125 | |
126 | |
127 | // |
128 | // Private |
129 | // |
130 | |
131 | void KBBGraphicsItemBall::hoverEnterEvent (QGraphicsSceneHoverEvent*) |
132 | { |
133 | if (m_timer==NULL) { |
134 | m_timer = new QTimer(this); |
135 | connect(m_timer, SIGNAL(timeout()), this, SLOT(showInteractions())); |
136 | m_timer->start(TIME_TO_WAIT_BEFORE_SHOWING_INTERACTIONS); |
137 | } |
138 | } |
139 | |
140 | |
141 | void KBBGraphicsItemBall::hoverLeaveEvent (QGraphicsSceneHoverEvent*) |
142 | { |
143 | delete m_timer; |
144 | m_timer = NULL; |
145 | removeInteractionInfos(); |
146 | } |
147 | |
148 | |
149 | void KBBGraphicsItemBall::removeInteractionInfos() |
150 | { |
151 | for (int i=0; i<8;i++) { |
152 | delete m_interactionInfos[i]; |
153 | m_interactionInfos[i] = NULL; |
154 | } |
155 | } |
156 | |
157 | #include "kbbgraphicsitemball.moc" |
158 | |