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