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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
/*
* Copyright 2006-2009 Parker Coates <coates@kde.org>
*
* This file is part of Killbots.
*
* Killbots is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Killbots 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Killbots. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mainwindow.h"
#include "coordinator.h"
#include "engine.h"
#include "optionspage.h"
#include "renderer.h"
#include "rulesetselector.h"
#include "scene.h"
#include "settings.h"
#include "view.h"
#include <KgThemeProvider>
#include <KgThemeSelector>
#include <highscore/kscoredialog.h>
#include <kstandardgameaction.h>
#include <QAction>
#include <KActionCollection>
#include <KConfigDialog>
#include <KLocalizedString>
#include <KMessageBox>
#include <KShortcutsDialog>
#include <KStandardAction>
#include <QIcon>
#include <QDebug>
#include <QSignalMapper>
#include <QTimer>
Killbots::MainWindow::MainWindow(QWidget *parent)
: KXmlGuiWindow(parent),
m_scoreDialog(nullptr),
m_rulesetChanged(false)
{
setAcceptDrops(false);
m_coordinator = new Coordinator(this);
m_coordinator->setAnimationSpeed(Settings::animationSpeed());
m_engine = new Engine(m_coordinator, this);
m_coordinator->setEngine(m_engine);
connect(m_engine, &Engine::gameOver, this, &MainWindow::onGameOver);
m_scene = new Scene(this);
m_coordinator->setScene(m_scene);
connect(m_scene, &Scene::clicked, m_coordinator, &Coordinator::requestAction);
connect(Renderer::self()->themeProvider(), &KgThemeProvider::currentThemeChanged, m_scene, &Scene::doLayout);
m_view = new View(m_scene, this);
m_view->setMinimumSize(400, 280);
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."));
setCentralWidget(m_view);
connect(m_view, &View::sizeChanged, m_scene, &Scene::doLayout);
m_keyboardMapper = new QSignalMapper(this);
connect(m_keyboardMapper, SIGNAL(mapped(int)), m_coordinator, SLOT(requestAction(int)));
setupActions();
connect(m_engine, &Engine::teleportAllowed, actionCollection()->action(QStringLiteral("teleport")), &QAction::setEnabled);
connect(m_engine, &Engine::teleportAllowed, actionCollection()->action(QStringLiteral("teleport_sip")), &QAction::setEnabled);
connect(m_engine, &Engine::teleportSafelyAllowed, actionCollection()->action(QStringLiteral("teleport_safely")), &QAction::setEnabled);
connect(m_engine, &Engine::vaporizerAllowed, actionCollection()->action(QStringLiteral("vaporizer")), &QAction::setEnabled);
connect(m_engine, &Engine::waitOutRoundAllowed, actionCollection()->action(QStringLiteral("wait_out_round")), &QAction::setEnabled);
setupGUI(Save | Create | ToolBar | Keys);
// Delaying the start of the first game by 50ms to avoid resize events after
// the game has been started. Delaying by 0ms doesn't seem to be enough.
QTimer::singleShot(50, m_coordinator, &Coordinator::requestNewGame);
}
Killbots::MainWindow::~MainWindow()
{
Killbots::Renderer::cleanup();
}
void Killbots::MainWindow::configurePreferences()
{
// An instance of the dialog could be already created and could be cached,
// in which case we want to display the cached dialog instead of creating
// another one
if (!KConfigDialog::showDialog(QStringLiteral("configurePreferencesDialog"))) {
// Creating a dialog because KConfigDialog didn't find an instance of it
KConfigDialog *configDialog = new KConfigDialog(this, QStringLiteral("configurePreferencesDialog"), Settings::self());
// Creating setting pages and adding them to the dialog
configDialog->addPage(new OptionsPage(this),
i18n("General"),
QStringLiteral("configure"),
i18n("Configure general settings")
);
configDialog->addPage(new RulesetSelector(this),
i18n("Game Type"),
QStringLiteral("games-config-custom"),
i18n("Select a game type")
);
configDialog->addPage(new KgThemeSelector(Renderer::self()->themeProvider()),
i18n("Appearance"),
QStringLiteral("games-config-theme"),
i18n("Select a graphical theme")
);
configDialog->setMaximumSize(800, 600);
// Update the sprite style if it has changed
connect(configDialog, &KConfigDialog::settingsChanged, this, &MainWindow::onSettingsChanged);
configDialog->show();
}
}
void Killbots::MainWindow::onSettingsChanged()
{
m_coordinator->setAnimationSpeed(Settings::animationSpeed());
if (m_engine->ruleset()->fileName() != Settings::ruleset()) {
qDebug() << "Detected a changed in ruleset. From" << m_engine->ruleset()->fileName() << "to" << Settings::ruleset();
// We don't act on the changed ruleset here because the config dialog
// is still visible. We want the game in progress to be visible when
// we display the message box. We also don't want a new game to start
// while hidden behind the config dialog. So we wait until the config
// dialog is closed. See onConfigDialogClosed below.
m_rulesetChanged = true;
}
onConfigDialogClosed();
}
void Killbots::MainWindow::onConfigDialogClosed()
{
if (m_rulesetChanged) {
if (!m_engine->gameHasStarted()
|| KMessageBox::questionYesNo(this,
i18n("A new game type has been selected, but there is already a game in progress."),
i18n("Game Type Changed"),
KGuiItem(i18n("Continue Current Game")),
KGuiItem(i18n("Start a New Game"))
) == KMessageBox::No
) {
m_coordinator->requestNewGame();
}
m_rulesetChanged = false;
}
}
void Killbots::MainWindow::createScoreDialog()
{
m_scoreDialog = new KScoreDialog(KScoreDialog::Name, this);
m_scoreDialog->addField(KScoreDialog::Level, i18n("Round"), QStringLiteral("round"));
m_scoreDialog->setModal(false);
QStringList fileList;
const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("killbots/ruleset"), QStandardPaths::LocateDirectory);
Q_FOREACH (const QString &dir, dirs) {
const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*.desktop"));
Q_FOREACH (const QString &file, fileNames) {
fileList.append(dir + QLatin1Char('/') + file);
}
}
foreach (const QString &fileName, fileList) {
const Ruleset *ruleset = Ruleset::load(fileName);
if (ruleset) {
m_scoreDialog->addLocalizedConfigGroupName(qMakePair(ruleset->scoreGroupKey(), ruleset->name()));
}
delete ruleset;
}
}
void Killbots::MainWindow::onGameOver(int score, int round)
{
if (score && m_engine->ruleset()) {
if (!m_scoreDialog) {
createScoreDialog();
}
m_scoreDialog->setConfigGroup(qMakePair(m_engine->ruleset()->scoreGroupKey(), m_engine->ruleset()->name()));
KScoreDialog::FieldInfo scoreEntry;
scoreEntry[ KScoreDialog::Score ].setNum(score);
scoreEntry[ KScoreDialog::Level ].setNum(round);
if (m_scoreDialog->addScore(scoreEntry)) {
QTimer::singleShot(1000, this, &MainWindow::showHighscores);
}
}
}
void Killbots::MainWindow::showHighscores()
{
if (!m_scoreDialog) {
createScoreDialog();
}
// TODO: Find out why this line breaks adding high scores.
//m_scoreDialog->setConfigGroup(qMakePair(m_engine->ruleset()->scoreGroupKey(), m_engine->ruleset()->name()));
m_scoreDialog->exec();
}
QAction *Killbots::MainWindow::createMappedAction(int mapping,
const QString &internalName,
const QString &displayName,
const QString &translatedShortcut,
const int alternateShortcut,
const QString &helpText,
const QString &icon
)
{
QAction *action = new QAction(displayName, actionCollection());
action->setObjectName(internalName);
actionCollection()->setDefaultShortcuts(action, QList<QKeySequence>() << QKeySequence(translatedShortcut) << QKeySequence(alternateShortcut) << QKeySequence(alternateShortcut + Qt::KeypadModifier));
if (!helpText.isEmpty()) {
action->setWhatsThis(helpText);
action->setToolTip(helpText);
}
if (!icon.isEmpty()) {
action->setIcon(QIcon::fromTheme(icon));
}
connect(action, SIGNAL(triggered()), m_keyboardMapper, SLOT(map()));
m_keyboardMapper->setMapping(action, mapping);
actionCollection()->addAction(internalName, action);
return action;
}
void Killbots::MainWindow::setupActions()
{
KStandardGameAction::gameNew(m_coordinator, SLOT(requestNewGame()), actionCollection());
KStandardGameAction::highscores(this, SLOT(showHighscores()), actionCollection());
KStandardGameAction::quit(qApp, SLOT(quit()), actionCollection());
KStandardAction::preferences(this, SLOT(configurePreferences()), actionCollection());
createMappedAction(TeleportSafely,
QStringLiteral("teleport_safely"),
i18n("Teleport Safely"),
i18nc("Shortcut for teleport safely. See https://quickgit.kde.org/?p=killbots.git&a=blob&f=README.translators&o=plain", "T"),
Qt::Key_Plus,
i18n("Teleport to a safe location"),
QStringLiteral("games-solve")
);
createMappedAction(Teleport,
QStringLiteral("teleport"),
i18n("Teleport"),
i18nc("Shortcut for teleport. See https://quickgit.kde.org/?p=killbots.git&a=blob&f=README.translators&o=plain", "R"),
Qt::Key_Minus,
i18n("Teleport to a random location"),
QStringLiteral("roll")
);
createMappedAction(TeleportSafelyIfPossible,
QStringLiteral("teleport_sip"),
i18n("Teleport, Safely If Possible"),
i18nc("Shortcut for teleport safely if possible. See https://quickgit.kde.org/?p=killbots.git&a=blob&f=README.translators&o=plain", "Space"),
Qt::Key_0,
i18n("Teleport safely if that action is enabled, otherwise teleport randomly")
);
createMappedAction(Vaporizer,
QStringLiteral("vaporizer"),
i18n("Vaporizer"),
i18nc("Shortcut for vaporizer. See https://quickgit.kde.org/?p=killbots.git&a=blob&f=README.translators&o=plain", "F"),
Qt::Key_Period,
i18n("Destroy all enemies in neighboring cells"),
QStringLiteral("edit-bomb")
);
createMappedAction(WaitOutRound,
QStringLiteral("wait_out_round"),
i18n("Wait Out Round"),
i18nc("Shortcut for wait out round. See https://quickgit.kde.org/?p=killbots.git&a=blob&f=README.translators&o=plain", "V"),
Qt::Key_Asterisk,
i18n("Risk remaining in place until the end of the round for bonuses"),
QStringLiteral("process-stop")
);
createMappedAction(UpLeft,
QStringLiteral("move_up_left"),
i18n("Move Up and Left"),
i18nc("Shortcut for move up and left. https://quickgit.kde.org/?p=killbots.git&a=blob&f=README.translators&o=plain", "Q"),
Qt::Key_7
);
createMappedAction(Up,
QStringLiteral("move_up"),
i18n("Move Up"),
i18nc("Shortcut for move up. See https://quickgit.kde.org/?p=killbots.git&a=blob&f=README.translators&o=plain", "W"),
Qt::Key_8
);
createMappedAction(UpRight,
QStringLiteral("move_up_right"),
i18n("Move Up and Right"),
i18nc("Shortcut for move up and right. https://quickgit.kde.org/?p=killbots.git&a=blob&f=README.translators&o=plain", "E"),
Qt::Key_9
);
createMappedAction(Left,
QStringLiteral("move_left"),
i18n("Move Left"),
i18nc("Shortcut for move left. See https://quickgit.kde.org/?p=killbots.git&a=blob&f=README.translators&o=plain", "A"),
Qt::Key_4
);
createMappedAction(Hold,
QStringLiteral("stand_still"),
i18n("Stand Still"),
i18nc("Shortcut for stand still. See https://quickgit.kde.org/?p=killbots.git&a=blob&f=README.translators&o=plain", "S"),
Qt::Key_5
);
createMappedAction(Right,
QStringLiteral("move_right"),
i18n("Move Right"),
i18nc("Shortcut for move right. See https://quickgit.kde.org/?p=killbots.git&a=blob&f=README.translators&o=plain", "D"),
Qt::Key_6
);
createMappedAction(DownLeft,
QStringLiteral("move_down_left"),
i18n("Move Down and Left"),
i18nc("Shortcut for move down and left. See https://quickgit.kde.org/?p=killbots.git&a=blob&f=README.translators&o=plain", "Z"),
Qt::Key_1
);
createMappedAction(Down,
QStringLiteral("move_down"),
i18n("Move Down"),
i18nc("Shortcut for move down. See https://quickgit.kde.org/?p=killbots.git&a=blob&f=README.translators&o=plain", "X"),
Qt::Key_2
);
createMappedAction(DownRight,
QStringLiteral("move_down_right"),
i18n("Move Down and Right"),
i18nc("Shortcut for move down and right. https://quickgit.kde.org/?p=killbots.git&a=blob&f=README.translators&o=plain", "C"),
Qt::Key_3
);
}
#include "moc_mainwindow.cpp"
|