1 | /* |
2 | * Copyright (C) 2000-2005 Stefan Schimanski <1Stein@gmx.de> |
3 | * |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU Library General Public |
6 | * License as published by the Free Software Foundation; either |
7 | * version 2 of the License,Life or (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Library General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU Library General Public |
15 | * License along with this program; if not, write to the Free |
16 | * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | */ |
18 | |
19 | #include "mainwindow.h" |
20 | |
21 | #include <KAction> |
22 | #include <KActionCollection> |
23 | #include <KConfigDialog> |
24 | #include <kdebug.h> |
25 | #include <KGlobal> |
26 | #include <KLocale> |
27 | #include <KMessageBox> |
28 | #include <krandom.h> |
29 | #include <KStatusBar> |
30 | #include <KStandardDirs> |
31 | #include <KToggleAction> |
32 | |
33 | #include <kstandardgameaction.h> |
34 | #include <KScoreDialog> |
35 | #include <KgThemeSelector> |
36 | #include <KgDifficulty> |
37 | |
38 | |
39 | #include "gamewidget.h" |
40 | #include "settings.h" |
41 | #include "backgroundselector.h" |
42 | |
43 | KBounceMainWindow::KBounceMainWindow() |
44 | { |
45 | m_statusBar = statusBar(); |
46 | m_statusBar->insertItem( i18n( "Level: %1" , QString::fromLatin1( "XX" ) ), 1, 1 ); |
47 | m_statusBar->insertItem( i18n( "Score: %1" , QString::fromLatin1( "XXXXXX" ) ), 2, 1 ); |
48 | m_statusBar->insertItem( i18n( "Filled: %1%" , QString::fromLatin1( "XX" ) ), 3, 1 ); |
49 | m_statusBar->insertItem( i18n( "Lives: %1" , QString::fromLatin1( "XX" ) ), 4, 1 ); |
50 | m_statusBar->insertItem( i18n( "Time: %1" , QString::fromLatin1( "XXX" ) ), 5, 1 ); |
51 | |
52 | m_gameWidget = new KBounceGameWidget( this ); |
53 | connect( m_gameWidget, SIGNAL(levelChanged(int)), this, SLOT(displayLevel(int)) ); |
54 | connect( m_gameWidget, SIGNAL(scoreChanged(int)), this, SLOT(displayScore(int)) ); |
55 | connect( m_gameWidget, SIGNAL(livesChanged(int)), this, SLOT(displayLives(int)) ); |
56 | connect( m_gameWidget, SIGNAL(filledChanged(int)), this, SLOT(displayFilled(int)) ); |
57 | connect( m_gameWidget, SIGNAL(timeChanged(int)), this, SLOT(displayTime(int)) ); |
58 | connect( m_gameWidget, SIGNAL(stateChanged(KBounceGameWidget::State)), this, SLOT(gameStateChanged(KBounceGameWidget::State)) ); |
59 | //connect( m_gameWidget, SIGNAL(gameOver()), this, SLOT(gameOverNow()) ); |
60 | setCentralWidget( m_gameWidget ); |
61 | |
62 | initXMLUI(); |
63 | |
64 | setFocusPolicy(Qt::StrongFocus); |
65 | setFocus(); |
66 | setupGUI(); |
67 | |
68 | readSettings(); |
69 | } |
70 | |
71 | KBounceMainWindow::~KBounceMainWindow() |
72 | { |
73 | } |
74 | |
75 | /** |
76 | * create the action events create the gui. |
77 | */ |
78 | void KBounceMainWindow::initXMLUI() |
79 | { |
80 | // Game |
81 | m_newAction = KStandardGameAction::gameNew(this, SLOT(newGame()), actionCollection()); |
82 | KStandardGameAction::end(this, SLOT(closeGame()), actionCollection()); |
83 | m_pauseAction = KStandardGameAction::pause(this, SLOT(pauseGame()), actionCollection()); |
84 | KStandardGameAction::highscores(this, SLOT(showHighscore()), actionCollection()); |
85 | KStandardGameAction::quit(this, SLOT(close()), actionCollection()); |
86 | |
87 | // Difficulty |
88 | Kg::difficulty()->addStandardLevelRange( |
89 | KgDifficultyLevel::Easy, KgDifficultyLevel::Hard |
90 | ); |
91 | KgDifficultyGUI::init(this); |
92 | connect(Kg::difficulty(), SIGNAL(currentLevelChanged(const KgDifficultyLevel*)), m_gameWidget, SLOT(levelChanged())); |
93 | |
94 | // Settings |
95 | KStandardAction::preferences( this, SLOT(configureSettings()), actionCollection() ); |
96 | m_soundAction = new KToggleAction( i18n("&Play Sounds" ), this ); |
97 | actionCollection()->addAction( QLatin1String( "toggle_sound" ), m_soundAction ); |
98 | connect( m_soundAction, SIGNAL(triggered(bool)), this, SLOT(setSounds(bool)) ); |
99 | } |
100 | |
101 | |
102 | |
103 | void KBounceMainWindow::newGame() |
104 | { |
105 | // Check for running game |
106 | closeGame(); |
107 | if ( m_gameWidget->state() == KBounceGameWidget::BeforeFirstGame || m_gameWidget->state() == KBounceGameWidget::GameOver ) |
108 | { |
109 | m_gameWidget->newGame(); |
110 | } |
111 | } |
112 | |
113 | void KBounceMainWindow::pauseGame() |
114 | { |
115 | if ( m_gameWidget->state() == KBounceGameWidget::Paused ) |
116 | { |
117 | m_gameWidget->setPaused( false ); |
118 | } |
119 | else |
120 | { |
121 | m_gameWidget->setPaused( true ); |
122 | } |
123 | } |
124 | |
125 | void KBounceMainWindow::closeGame() |
126 | { |
127 | if ( m_gameWidget->state() == KBounceGameWidget::BeforeFirstGame || m_gameWidget->state() == KBounceGameWidget::GameOver ) |
128 | { |
129 | return; |
130 | } |
131 | |
132 | KBounceGameWidget::State old_state = m_gameWidget->state(); |
133 | if ( old_state == KBounceGameWidget::Running ) |
134 | m_gameWidget->setPaused( true ); |
135 | int ret = KMessageBox::questionYesNo( this, i18n( "Do you really want to close the running game?" ), QString(), KStandardGuiItem::yes(), KStandardGuiItem::cancel() ); |
136 | if ( ret == KMessageBox::Yes ) |
137 | { |
138 | m_gameWidget->closeGame(); |
139 | } |
140 | else if ( old_state == KBounceGameWidget::Running ) |
141 | { |
142 | m_gameWidget->setPaused( false ); |
143 | } |
144 | } |
145 | |
146 | void KBounceMainWindow::gameOverNow() |
147 | { |
148 | statusBar()->showMessage( i18n("Game over. Click to start a game" ) ); |
149 | highscore(); |
150 | } |
151 | |
152 | /** |
153 | * Bring up the standard kde high score dialog. |
154 | */ |
155 | void KBounceMainWindow::showHighscore() |
156 | { |
157 | KScoreDialog ksdialog( KScoreDialog::Name | KScoreDialog::Score, this ); |
158 | ksdialog.initFromDifficulty(Kg::difficulty()); |
159 | ksdialog.exec(); |
160 | } |
161 | |
162 | void KBounceMainWindow::highscore() |
163 | { |
164 | if ( m_gameWidget->score() == 0 ) { |
165 | return; |
166 | } |
167 | |
168 | kDebug() ; |
169 | KScoreDialog ksdialog( KScoreDialog::Name | KScoreDialog::Score | KScoreDialog::Level, this ); |
170 | ksdialog.initFromDifficulty(Kg::difficulty()); |
171 | KScoreDialog::FieldInfo info; |
172 | info[KScoreDialog::Score].setNum( m_gameWidget->score() ); |
173 | info[KScoreDialog::Level].setNum( m_gameWidget->level() ); |
174 | if ( ksdialog.addScore( info ) ) |
175 | ksdialog.exec(); |
176 | } |
177 | |
178 | void KBounceMainWindow::configureSettings() |
179 | { |
180 | if ( KConfigDialog::showDialog( "settings" ) ) return; |
181 | |
182 | KConfigDialog* dialog = new KConfigDialog( this, "settings" , KBounceSettings::self()); |
183 | dialog->addPage( new KgThemeSelector(m_gameWidget->renderer()->themeProvider(), 0, dialog), i18n( "Theme" ), "games-config-theme" ); |
184 | dialog->addPage( new BackgroundSelector(dialog,KBounceSettings::self() ),i18n("Background" ),"games-config-background" ); |
185 | dialog->setHelp(QString(),"kbounce" ); |
186 | dialog->show(); |
187 | connect( dialog, SIGNAL(settingsChanged(QString)), this, SLOT(settingsChanged()) ); |
188 | } |
189 | |
190 | void KBounceMainWindow::readSettings() |
191 | { |
192 | m_soundAction->setChecked( KBounceSettings::playSounds() ); |
193 | m_gameWidget->settingsChanged(); |
194 | } |
195 | |
196 | void KBounceMainWindow::settingsChanged() |
197 | { |
198 | m_gameWidget->settingsChanged(); |
199 | KBounceSettings::self()->writeConfig(); // Bug 184606 |
200 | } |
201 | |
202 | void KBounceMainWindow::setSounds( bool val ) |
203 | { |
204 | KBounceSettings::setPlaySounds( val ); |
205 | settingsChanged(); |
206 | } |
207 | |
208 | void KBounceMainWindow::displayLevel( int level ) |
209 | { |
210 | m_statusBar->changeItem( i18n( "Level: %1" , level ), 1 ); |
211 | } |
212 | |
213 | void KBounceMainWindow::displayScore( int score ) |
214 | { |
215 | m_statusBar->changeItem( i18n( "Score: %1" , score ), 2 ); |
216 | } |
217 | |
218 | void KBounceMainWindow::displayFilled( int filled ) |
219 | { |
220 | m_statusBar->changeItem( i18n( "Filled: %1%" , filled ), 3 ); |
221 | } |
222 | |
223 | void KBounceMainWindow::displayLives( int lives ) |
224 | { |
225 | m_statusBar->changeItem( i18n( "Lives: %1" , lives - 1 ), 4 ); |
226 | } |
227 | |
228 | void KBounceMainWindow::displayTime( int time ) |
229 | { |
230 | m_statusBar->changeItem( i18n( "Time: %1" , time ), 5 ); |
231 | } |
232 | |
233 | void KBounceMainWindow::gameStateChanged( KBounceGameWidget::State state ) |
234 | { |
235 | switch ( state ) |
236 | { |
237 | case KBounceGameWidget::BeforeFirstGame : |
238 | break; |
239 | case KBounceGameWidget::BetweenLevels : |
240 | break; |
241 | case KBounceGameWidget::Suspended : |
242 | break; |
243 | case KBounceGameWidget::Paused : |
244 | m_pauseAction->setChecked( true ); |
245 | m_statusBar->clearMessage(); |
246 | break; |
247 | case KBounceGameWidget::Running : |
248 | m_pauseAction->setChecked( false ); |
249 | m_statusBar->clearMessage(); |
250 | break; |
251 | case KBounceGameWidget::GameOver : |
252 | statusBar()->showMessage( i18n("Game over. Click to start a game" ) ); |
253 | highscore(); |
254 | break; |
255 | } |
256 | } |
257 | |
258 | void KBounceMainWindow::focusOutEvent( QFocusEvent *ev ) |
259 | { |
260 | if ( m_gameWidget->state() == KBounceGameWidget::Running && |
261 | focusWidget() != m_gameWidget ) |
262 | { |
263 | m_gameWidget->setPaused( true ); |
264 | } |
265 | |
266 | KXmlGuiWindow::focusOutEvent( ev ); |
267 | } |
268 | |
269 | void KBounceMainWindow::focusInEvent ( QFocusEvent *ev ) |
270 | { |
271 | //m_board->setSuspended( true ); |
272 | KXmlGuiWindow::focusInEvent( ev ); |
273 | } |
274 | |
275 | // void KBounceMainWindow::switchLevel() |
276 | // { |
277 | /* |
278 | m_game.score += m_level.score; |
279 | |
280 | // make sure the LCD provides enough digits for the score |
281 | // (fixes #96841) |
282 | int numDigits=0; |
283 | int temp_score = m_game.score; |
284 | for ( ; temp_score > 0; ++numDigits ) temp_score /= 10; |
285 | if ( numDigits < 5 ) numDigits = 5; // set numDigits to at least 5, otherwise it does not look well |
286 | |
287 | m_scoreLCD->setNumDigits( numDigits ); |
288 | m_scoreLCD->display( m_game.score ); |
289 | |
290 | QString score; |
291 | score.setNum( m_level.score ); |
292 | |
293 | QString level; |
294 | level.setNum( m_game.level ); |
295 | |
296 | QString foo = |
297 | i18n("You have successfully cleared more than 75% of the board.\n") + |
298 | i18n("%1 points: 15 points per remaining life\n", m_level.lifes*15) + |
299 | i18n("%1 points: Bonus\n", (m_gameWidget->percent()-75)*2*(m_game.level+5)) + |
300 | i18n("%1 points: Total score for this level\n", score) + |
301 | i18n("On to level %1. Remember you get %2 lives this time!", m_game.level+1, m_game.level+2); |
302 | |
303 | KMessageBox::information( this,foo ); |
304 | |
305 | |
306 | // KMessageBox::information( this, i18n("You've completed level %1 with " |
307 | // "a score of %2.\nGet ready for the next one!").arg(level).arg(score)); |
308 | |
309 | m_game.level++; |
310 | m_levelLCD->display( m_game.level ); |
311 | |
312 | createLevel( m_game.level ); |
313 | startLevel(); |
314 | */ |
315 | // } |
316 | |
317 | |
318 | #include "mainwindow.moc" |
319 | |