SVN_SILENT made messages (.desktop file)
[kdegames.git] / killbots / mainwindow.cpp
blob5b756c8962edb9929c7061e2457887f4e49957fb
1 /*
2 * Copyright 2006-2009 Parker Coates <parker.coates@gmail.com>
4 * This file is part of Killbots.
6 * Killbots is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * Killbots is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Killbots. If not, see <http://www.gnu.org/licenses/>.
20 #include "mainwindow.h"
22 #include "engine.h"
23 #include "optionspage.h"
24 #include "render.h"
25 #include "rulesetselector.h"
26 #include "scene.h"
27 #include "settings.h"
28 #include "view.h"
30 #include <kgamethemeselector.h>
31 #include <highscore/kscoredialog.h>
32 #include <kstandardgameaction.h>
34 #include <KDE/KAction>
35 #include <KDE/KActionCollection>
36 #include <KDE/KApplication>
37 #include <KDE/KConfigDialog>
38 #include <KDE/KLocalizedString>
39 #include <KDE/KMessageBox>
40 #include <KDE/KShortcutsDialog>
41 #include <KDE/KStandardAction>
42 #include <KDE/KStandardDirs>
44 #include <QtCore/QSignalMapper>
45 #include <QtCore/QTimer>
47 Killbots::MainWindow::MainWindow( QWidget * parent )
48 : KXmlGuiWindow( parent ),
49 m_scoreDialog( 0 ),
50 m_keyboardActions( new KActionCollection( this, KGlobal::mainComponent() ) ),
51 m_keyboardMapper( new QSignalMapper( this ) ),
52 m_rulesetChanged( false )
54 setAcceptDrops(false);
56 if ( !Render::loadTheme( Settings::theme() ) )
58 Render::loadDefaultTheme();
59 Settings::setTheme("themes/default.desktop");
62 m_scene = new Scene( this );
63 m_engine = new Engine( m_scene, this );
65 m_view = new View( m_scene, this );
66 m_view->setMinimumSize( 400, 280 );
67 m_view->setWhatsThis( i18n("This is the main game area used to interact with Killbots. It shows the current state of the game grid and allows one to control the hero using the mouse. It also displays certain statistics about the game in progress.") );
68 setCentralWidget( m_view );
70 setupActions();
72 setupGUI( Save | Create | ToolBar );
74 connect( m_view, SIGNAL(sizeChanged(QSize)), m_scene, SLOT(doLayout()) );
76 connect( m_scene, SIGNAL(clicked(int)), m_engine, SLOT(requestAction(int)) );
78 connect( m_engine, SIGNAL(newGame(int,int,bool)), m_scene, SLOT(onNewGame(int,int,bool)) );
79 connect( m_engine, SIGNAL(gameOver(int,int)), this, SLOT(onGameOver(int,int)) );
81 connect( m_engine, SIGNAL(roundChanged(int)), m_scene, SLOT(updateRound(int)) );
82 connect( m_engine, SIGNAL(scoreChanged(int)), m_scene, SLOT(updateScore(int)) );
83 connect( m_engine, SIGNAL(enemyCountChanged(int)), m_scene, SLOT(updateEnemyCount(int)) );
84 connect( m_engine, SIGNAL(energyChanged(int)), m_scene, SLOT(updateEnergy(int)) );
86 connect( m_engine, SIGNAL(showNewGameMessage()), m_scene, SLOT(showNewGameMessage()) );
87 connect( m_engine, SIGNAL(showRoundCompleteMessage()), m_scene, SLOT(showRoundCompleteMessage()) );
88 connect( m_engine, SIGNAL(showBoardFullMessage()), m_scene, SLOT(showBoardFullMessage()) );
89 connect( m_engine, SIGNAL(showGameOverMessage()), m_scene, SLOT(showGameOverMessage()) );
91 connect( m_engine, SIGNAL(teleportAllowed(bool)), actionCollection()->action("teleport"), SLOT(setEnabled(bool)) );
92 connect( m_engine, SIGNAL(teleportAllowed(bool)), actionCollection()->action("teleport_sip"), SLOT(setEnabled(bool)) );
93 connect( m_engine, SIGNAL(teleportSafelyAllowed(bool)), actionCollection()->action("teleport_safely"), SLOT(setEnabled(bool)) );
94 connect( m_engine, SIGNAL(sonicScrewdriverAllowed(bool)), actionCollection()->action("screwdriver"), SLOT(setEnabled(bool)) );
95 connect( m_engine, SIGNAL(waitOutRoundAllowed(bool)), actionCollection()->action("wait_out_round"), SLOT(setEnabled(bool)) );
97 QTimer::singleShot( 25, m_engine, SLOT(requestNewGame()) );
101 Killbots::MainWindow::~MainWindow()
106 void Killbots::MainWindow::configureShortcuts()
108 KShortcutsDialog dialog( KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcutsAllowed, this );
109 dialog.addCollection( actionCollection(), i18n("General") );
110 dialog.addCollection( m_keyboardActions, i18n("Movement Controls") );
111 dialog.configure();
115 void Killbots::MainWindow::configurePreferences()
117 // An instance of the dialog could be already created and could be cached,
118 // in which case we want to display the cached dialog instead of creating
119 // another one
120 if ( !KConfigDialog::showDialog( "configurePreferencesDialog" ) )
122 // Creating a dialog because KConfigDialog didn't find an instance of it
123 KConfigDialog * configDialog = new KConfigDialog( this, "configurePreferencesDialog", Settings::self() );
125 // Creating setting pages and adding them to the dialog
126 configDialog->addPage( new OptionsPage( this ),
127 i18n("General"),
128 "configure",
129 i18n("Configure general settings")
131 configDialog->addPage( new RulesetSelector( this ),
132 i18n("Game Type"),
133 "games-config-custom",
134 i18n("Select a game type")
136 configDialog->addPage( new KGameThemeSelector( this, Settings::self(), KGameThemeSelector::NewStuffDisableDownload ),
137 i18n("Appearance"),
138 "games-config-theme",
139 i18n("Select a graphical theme")
142 configDialog->setMaximumSize( 800, 600 );
143 configDialog->setInitialSize( QSize( 600, 450 ) );
145 // Update the sprite style if it has changed
146 connect( configDialog, SIGNAL(settingsChanged(QString)), this, SLOT(onSettingsChanged()) );
147 connect( configDialog, SIGNAL(finished()), this, SLOT(onConfigDialogClosed()) );
149 configDialog->show();
154 void Killbots::MainWindow::onSettingsChanged()
156 if ( Render::loadTheme( Settings::theme() ) )
158 m_view->resetCachedContent();
159 m_scene->doLayout();
162 m_scene->setAnimationSpeed( Settings::animationSpeed() );
164 if ( m_engine->ruleset()->fileName() != Settings::ruleset() )
166 kDebug() << "Detected a changed in ruleset. From" << m_engine->ruleset()->fileName() << "to" << Settings::ruleset();
168 // We don't act on the changed ruleset here because the config dialog
169 // is still visible. We want the game in progress to be visible when
170 // we display the message box. We also don't want a new game to start
171 // while hidden behind the config dialog. So we wait until the config
172 // dialog is closed. See onConfigDialogClosed below.
173 m_rulesetChanged = true;
178 void Killbots::MainWindow::onConfigDialogClosed()
180 if ( m_rulesetChanged )
182 if ( !m_engine->gameHasStarted()
183 || KMessageBox::questionYesNo( this,
184 i18n("A new ruleset has been selected, but there is already a game in progress."),
185 i18n("Ruleset Changed"),
186 KGuiItem( i18n("Continue Current Game") ),
187 KGuiItem( i18n("Start a New Game") )
188 ) == KMessageBox::No
191 m_engine->requestNewGame();
194 m_rulesetChanged = false;
199 void Killbots::MainWindow::createScoreDialog()
201 m_scoreDialog = new KScoreDialog( KScoreDialog::Name, this );
202 m_scoreDialog->addField( KScoreDialog::Level, i18n("Round"), "round" );
203 m_scoreDialog->setModal( false );
205 QStringList fileList;
206 KGlobal::dirs()->findAllResources ( "ruleset", "*.desktop", KStandardDirs::NoDuplicates, fileList );
207 foreach ( const QString & fileName, fileList )
209 Ruleset * ruleset = Ruleset::load( fileName );
210 if ( ruleset )
211 m_scoreDialog->addLocalizedConfigGroupName( qMakePair( ruleset->scoreGroupKey(), ruleset->name() ) );
212 delete ruleset;
217 void Killbots::MainWindow::onGameOver( int score, int round )
219 if ( score && m_engine->ruleset() )
221 if ( !m_scoreDialog )
222 createScoreDialog();
224 m_scoreDialog->setConfigGroup( qMakePair( m_engine->ruleset()->scoreGroupKey(), m_engine->ruleset()->name() ) );
226 KScoreDialog::FieldInfo scoreEntry;
227 scoreEntry[ KScoreDialog::Score ].setNum( score );
228 scoreEntry[ KScoreDialog::Level ].setNum( round );
230 if ( m_scoreDialog->addScore( scoreEntry ) )
231 QTimer::singleShot( 1000, this, SLOT(showHighscores()) );
236 void Killbots::MainWindow::showHighscores()
238 if ( !m_scoreDialog )
239 createScoreDialog();
241 m_scoreDialog->exec();
245 KAction * Killbots::MainWindow::createMappedAction( KActionCollection * collection,
246 int mapping,
247 const QString & internalName,
248 const QString & displayName,
249 const QString & translatedShortcut,
250 const QKeySequence & alternateShortcut,
251 const QString & helpText,
252 const QString & icon
255 KAction * action = new KAction( displayName, collection );
256 action->setObjectName( internalName );
257 action->setShortcut( KShortcut( QKeySequence( translatedShortcut ), alternateShortcut ) );
258 if ( !helpText.isEmpty() )
259 action->setHelpText( helpText );
260 if ( !icon.isEmpty() )
261 action->setIcon( KIcon( icon ) );
263 connect( action, SIGNAL(triggered()), m_keyboardMapper, SLOT(map()) );
264 m_keyboardMapper->setMapping( action, mapping );
265 collection->addAction( internalName, action );
267 return action;
271 void Killbots::MainWindow::setupActions()
273 KStandardGameAction::gameNew( m_engine, SLOT(requestNewGame()), actionCollection() );
274 KStandardGameAction::highscores( this, SLOT(showHighscores()), actionCollection() );
275 KStandardGameAction::quit( kapp, SLOT(quit()), actionCollection() );
277 KStandardAction::keyBindings( this, SLOT(configureShortcuts()), actionCollection() );
278 KStandardAction::preferences( this, SLOT(configurePreferences()), actionCollection() );
280 createMappedAction( actionCollection(),
281 TeleportSafely,
282 "teleport_safely",
283 i18n("Teleport Safely"),
284 i18nc("Shortcut for teleport safely. See http://websvn.kde.org/trunk/KDE/kdegames/killbots/README.translators?view=markup", "T"),
285 Qt::Key_Plus,
286 i18n("Teleport to a safe location"),
287 "games-solve"
289 createMappedAction( actionCollection(),
290 Teleport,
291 "teleport",
292 i18n("Teleport"),
293 i18nc("Shortcut for teleport. See http://websvn.kde.org/trunk/KDE/kdegames/killbots/README.translators?view=markup", "R"),
294 Qt::Key_Minus,
295 i18n("Teleport to a random location"),
296 "roll"
298 createMappedAction( actionCollection(),
299 TeleportSafelyIfPossible,
300 "teleport_sip",
301 i18n("Teleport, Safely If Possible"),
302 i18nc("Shortcut for teleport safely if possible. See http://websvn.kde.org/trunk/KDE/kdegames/killbots/README.translators?view=markup", "Space"),
303 Qt::Key_0,
304 i18n("Teleport safely if that action is enabled, otherwise teleport randomly")
306 createMappedAction( actionCollection(),
307 SonicScrewdriver,
308 "screwdriver",
309 i18n("Sonic Screwdriver"),
310 i18nc("Shortcut for sonicscrewdriver. See http://websvn.kde.org/trunk/KDE/kdegames/killbots/README.translators?view=markup", "F"),
311 Qt::Key_Period,
312 i18n("Destroy all enemies in neighboring cells"),
313 "edit-bomb"
315 createMappedAction( actionCollection(),
316 WaitOutRound,
317 "wait_out_round",
318 i18n("Wait Out Round"),
319 i18nc("Shortcut for wait out round. See http://websvn.kde.org/trunk/KDE/kdegames/killbots/README.translators?view=markup", "V"),
320 Qt::Key_Asterisk,
321 i18n("Risk remaining in place until the end of the round for bonuses"),
322 "process-stop"
326 // Keyboard Actions - these are shown in Configure Shortcuts but not in Configure Toolbars
327 createMappedAction( m_keyboardActions,
328 UpLeft,
329 "move_up_left",
330 i18n("Move Up and Left"),
331 i18nc("Shortcut for move up and left. See http://websvn.kde.org/trunk/KDE/kdegames/killbots/README.translators?view=markup", "Q"),
332 Qt::Key_7
334 createMappedAction( m_keyboardActions,
336 "move_up",
337 i18n("Move Up"),
338 i18nc("Shortcut for move up. See http://websvn.kde.org/trunk/KDE/kdegames/killbots/README.translators?view=markup", "W"),
339 Qt::Key_8
341 createMappedAction( m_keyboardActions,
342 UpRight,
343 "move_up_right",
344 i18n("Move Up and Right"),
345 i18nc("Shortcut for move up and right. See http://websvn.kde.org/trunk/KDE/kdegames/killbots/README.translators?view=markup", "E"),
346 Qt::Key_9
348 createMappedAction( m_keyboardActions,
349 Left,
350 "move_left",
351 i18n("Move Left"),
352 i18nc("Shortcut for move left. See http://websvn.kde.org/trunk/KDE/kdegames/killbots/README.translators?view=markup", "A"),
353 Qt::Key_4
355 createMappedAction( m_keyboardActions,
356 Hold,
357 "stand_still",
358 i18n("Stand Still"),
359 i18nc("Shortcut for stand still. See http://websvn.kde.org/trunk/KDE/kdegames/killbots/README.translators?view=markup", "S"),
360 Qt::Key_5
362 createMappedAction( m_keyboardActions,
363 Right,
364 "move_right",
365 i18n("Move Right"),
366 i18nc("Shortcut for move right. See http://websvn.kde.org/trunk/KDE/kdegames/killbots/README.translators?view=markup", "D"),
367 Qt::Key_6
369 createMappedAction( m_keyboardActions,
370 DownLeft,
371 "move_down_left",
372 i18n("Move Down and Left"),
373 i18nc("Shortcut for move down and left. See http://websvn.kde.org/trunk/KDE/kdegames/killbots/README.translators?view=markup", "Z"),
374 Qt::Key_1
376 createMappedAction( m_keyboardActions,
377 Down,
378 "move_down",
379 i18n("Move Down"),
380 i18nc("Shortcut for move down. See http://websvn.kde.org/trunk/KDE/kdegames/killbots/README.translators?view=markup", "X"),
381 Qt::Key_2
383 createMappedAction( m_keyboardActions,
384 DownRight,
385 "move_down_right",
386 i18n("Move Down and Right"),
387 i18nc("Shortcut for move down and right. See http://websvn.kde.org/trunk/KDE/kdegames/killbots/README.translators?view=markup", "C"),
388 Qt::Key_3
391 connect( m_keyboardMapper, SIGNAL(mapped(int)), m_engine, SLOT(requestAction(int)) );
393 m_keyboardActions->associateWidget(this);
396 #include "moc_mainwindow.cpp"