1 | // |
2 | // KBlackBox |
3 | // |
4 | // A simple game inspired by an emacs module |
5 | // |
6 | /*************************************************************************** |
7 | * Copyright (c) 2007, Nicolas Roffet * |
8 | * nicolas-kde@roffet.com * |
9 | * * |
10 | * * |
11 | * This program is free software; you can redistribute it and/or modify * |
12 | * it under the terms of the GNU General Public License as published by * |
13 | * the Free Software Foundation; either version 2 of the License, or * |
14 | * (at your option) any later version. * |
15 | * * |
16 | * This program is distributed in the hope that it will be useful, * |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
19 | * GNU General Public License for more details. * |
20 | * * |
21 | * You should have received a copy of the GNU General Public License * |
22 | * along with this program; if not, write to the * |
23 | * Free Software Foundation, Inc., * |
24 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA * |
25 | ***************************************************************************/ |
26 | |
27 | #include "kbbgraphicsitemcursor.h" |
28 | |
29 | |
30 | |
31 | // |
32 | // Constructor / Destructor |
33 | // |
34 | |
35 | KBBGraphicsItemCursor::KBBGraphicsItemCursor(KBBScalableGraphicWidget* parent, KBBThemeManager* themeManager) : KBBGraphicsItem(KBBScalableGraphicWidget::cursor, parent->scene(), themeManager) |
36 | { |
37 | setBoardSize(1,1); |
38 | } |
39 | |
40 | |
41 | |
42 | // |
43 | // Public |
44 | // |
45 | |
46 | int KBBGraphicsItemCursor::borderPosition() |
47 | { |
48 | return m_borderPosition; |
49 | } |
50 | |
51 | |
52 | int KBBGraphicsItemCursor::boxPosition() |
53 | { |
54 | return m_boxPosition; |
55 | } |
56 | |
57 | |
58 | void KBBGraphicsItemCursor::moveDown() |
59 | { |
60 | if (m_borderPosition!=NO_POSITION) { |
61 | if (m_borderPosition<m_columns) { |
62 | m_boxPosition = m_borderPosition; |
63 | m_borderPosition = NO_POSITION; |
64 | } else if ((m_borderPosition>=m_columns) && (m_borderPosition<m_columns+m_rows-1)) |
65 | m_borderPosition++; |
66 | else if ((m_borderPosition>=2*m_columns+m_rows+1) && (m_borderPosition<2*m_columns+2*m_rows)) |
67 | m_borderPosition--; |
68 | } else if (m_boxPosition!=NO_POSITION) { |
69 | if ((m_boxPosition+m_columns)<(m_columns*m_rows)) |
70 | m_boxPosition += m_columns; |
71 | else { |
72 | m_borderPosition = 2*m_columns+m_rows-(m_boxPosition+m_columns-m_columns*m_rows)-1; |
73 | m_boxPosition = NO_POSITION; |
74 | } |
75 | } |
76 | |
77 | updatePositions(); |
78 | } |
79 | |
80 | |
81 | void KBBGraphicsItemCursor::moveLeft() |
82 | { |
83 | if (m_borderPosition!=NO_POSITION) { |
84 | if ((m_borderPosition>0) && (m_borderPosition<m_columns)) |
85 | m_borderPosition--; |
86 | else if ((m_borderPosition>=m_columns) && (m_borderPosition<m_columns+m_rows)) { |
87 | m_boxPosition = m_columns*(m_borderPosition-m_columns+1)-1; |
88 | m_borderPosition = NO_POSITION; |
89 | } else if ((m_borderPosition>=m_columns+m_rows) && (m_borderPosition<2*m_columns+m_rows-1)) |
90 | m_borderPosition++; |
91 | } else if (m_boxPosition!=NO_POSITION) { |
92 | if ((m_boxPosition % m_columns)!=0) |
93 | m_boxPosition--; |
94 | else { |
95 | m_borderPosition = 2*m_columns + 2*m_rows - m_boxPosition/m_columns -1; |
96 | m_boxPosition = NO_POSITION; |
97 | } |
98 | } |
99 | |
100 | updatePositions(); |
101 | } |
102 | |
103 | |
104 | void KBBGraphicsItemCursor::moveRight() |
105 | { |
106 | if (m_borderPosition!=NO_POSITION) { |
107 | if (m_borderPosition<m_columns-1) |
108 | m_borderPosition++; |
109 | else if ((m_borderPosition>=m_columns+m_rows+1) && (m_borderPosition<2*m_columns+m_rows)) |
110 | m_borderPosition--; |
111 | else if (m_borderPosition>=2*m_columns+m_rows) { |
112 | m_boxPosition = m_columns*(2*m_columns+2*m_rows-m_borderPosition-1); |
113 | m_borderPosition = NO_POSITION; |
114 | } |
115 | } else if (m_boxPosition!=NO_POSITION) { |
116 | if (((m_boxPosition+1) % m_columns)!=0) |
117 | m_boxPosition++; |
118 | else { |
119 | m_borderPosition = m_columns + m_boxPosition/m_columns; |
120 | m_boxPosition = NO_POSITION; |
121 | } |
122 | } |
123 | |
124 | updatePositions(); |
125 | } |
126 | |
127 | |
128 | void KBBGraphicsItemCursor::moveUp() |
129 | { |
130 | if (m_borderPosition!=NO_POSITION) { |
131 | if ((m_borderPosition>=m_columns+1) && (m_borderPosition<m_columns+m_rows)) |
132 | m_borderPosition--; |
133 | else if ((m_borderPosition>=m_columns+m_rows) && (m_borderPosition<2*m_columns+m_rows)) { |
134 | m_boxPosition = m_columns*m_rows-(m_borderPosition-m_columns-m_rows)-1; |
135 | m_borderPosition = NO_POSITION; |
136 | } else if ((m_borderPosition>=2*m_columns+m_rows) && (m_borderPosition<2*m_columns+2*m_rows-1)) |
137 | m_borderPosition++; |
138 | } else if (m_boxPosition!=NO_POSITION) { |
139 | if ((m_boxPosition-m_columns)>=0) |
140 | m_boxPosition -= m_columns; |
141 | else { |
142 | m_borderPosition = m_boxPosition; |
143 | m_boxPosition = NO_POSITION; |
144 | } |
145 | } |
146 | |
147 | updatePositions(); |
148 | } |
149 | |
150 | |
151 | void KBBGraphicsItemCursor::setBoardSize(const int columns, const int rows) |
152 | { |
153 | m_columns = columns; |
154 | m_rows = rows; |
155 | |
156 | m_boxPosition = 0; |
157 | m_borderPosition = NO_POSITION; |
158 | |
159 | updatePositions(); |
160 | hide(); |
161 | } |
162 | |
163 | |
164 | void KBBGraphicsItemCursor::setBorderPosition(const int borderPosition) |
165 | { |
166 | if (borderPosition!=NO_POSITION) { |
167 | m_borderPosition = borderPosition; |
168 | m_boxPosition = NO_POSITION; |
169 | |
170 | const int b = KBBScalableGraphicWidget::BORDER_SIZE; |
171 | const int r = KBBScalableGraphicWidget::RATIO; |
172 | const int offset = 0; |
173 | int x; |
174 | int y; |
175 | if (m_borderPosition<m_columns) { |
176 | x = borderPosition*r + b; |
177 | y = offset; |
178 | } else if (borderPosition<m_columns + m_rows) { |
179 | x = m_columns*r + b + b/2 - offset; |
180 | y = (borderPosition - m_columns)*r + b; |
181 | } else if (borderPosition<2*m_columns + m_rows) { |
182 | x = (2*m_columns + m_rows - borderPosition)*r + b/2; |
183 | y = m_rows*r + 3*b/2 - offset; |
184 | } else { |
185 | x = offset; |
186 | y = (2*m_columns + 2*m_rows - borderPosition)*r + b/2; |
187 | } |
188 | setPos(x, y); |
189 | } |
190 | } |
191 | |
192 | |
193 | void KBBGraphicsItemCursor::setBoxPosition(const int boxPosition) |
194 | { |
195 | if (boxPosition!=NO_POSITION) { |
196 | m_borderPosition = NO_POSITION; |
197 | m_boxPosition = boxPosition; |
198 | |
199 | setPos(KBBScalableGraphicWidget::BORDER_SIZE + KBBScalableGraphicWidget::RATIO*(m_boxPosition % m_columns), KBBScalableGraphicWidget::BORDER_SIZE + KBBScalableGraphicWidget::RATIO*(m_boxPosition / m_columns)); |
200 | } |
201 | } |
202 | |
203 | |
204 | // |
205 | // Private |
206 | // |
207 | |
208 | void KBBGraphicsItemCursor::updatePositions() |
209 | { |
210 | setBoxPosition(m_boxPosition); |
211 | setBorderPosition(m_borderPosition); |
212 | |
213 | emit cursorAtNewPosition(m_borderPosition); |
214 | } |
215 | |
216 | #include "kbbgraphicsitemcursor.moc" |
217 | |