File indexing completed on 2024-04-28 07:51:29

0001 /*
0002     SPDX-FileCopyrightText: 2000-2005 Stefan Schimanski <1Stein@gmx.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "mainwindow.h"
0008 #include "settings.h"
0009 #include "backgroundselector.h"
0010 #include "debug.h"
0011 // KDEGames
0012 #include <KGameStandardAction>
0013 #include <KGameHighScoreDialog>
0014 #include <KGameThemeSelector>
0015 #include <KGameDifficulty>
0016 // KF
0017 #include <KStandardGuiItem>
0018 #include <KToggleAction>
0019 #include <KActionCollection>
0020 #include <KConfigDialog>
0021 #include <KMessageBox>
0022 #include <KLocalizedString>
0023 // Qt
0024 #include <QStatusBar>
0025 #include <QAction>
0026 
0027 KBounceMainWindow::KBounceMainWindow()
0028 {
0029     //setComponentName(QStringLiteral("kbounce"), i18n("KBounce"));
0030     m_statusBar = statusBar();
0031 
0032     levelLabel->setText(i18n("Level: %1", QStringLiteral( "XX" )));
0033     scoreLabel->setText(i18n("Score: %1", QStringLiteral( "XXXXXX" )));
0034     filledLabel->setText(i18n( "Filled: %1%", QStringLiteral( "XX" )));
0035     livesLabel->setText(i18n( "Lives: %1", QStringLiteral( "XX" )));
0036     timeLabel->setText(i18n( "Time: %1", QStringLiteral( "XXX" )));
0037 
0038     m_statusBar->insertPermanentWidget(0, levelLabel, 1);
0039     m_statusBar->insertPermanentWidget(1, scoreLabel, 1);
0040     m_statusBar->insertPermanentWidget(2, filledLabel, 1);
0041     m_statusBar->insertPermanentWidget(3, livesLabel, 1);
0042     m_statusBar->insertPermanentWidget(4, timeLabel, 1);
0043 
0044     m_gameWidget = new KBounceGameWidget( this );
0045     connect( m_gameWidget, &KBounceGameWidget::levelChanged, this, &KBounceMainWindow::displayLevel );
0046     connect( m_gameWidget, &KBounceGameWidget::scoreChanged, this, &KBounceMainWindow::displayScore );
0047     connect( m_gameWidget, &KBounceGameWidget::livesChanged, this, &KBounceMainWindow::displayLives );
0048     connect( m_gameWidget, &KBounceGameWidget::filledChanged, this, &KBounceMainWindow::displayFilled );
0049     connect( m_gameWidget, &KBounceGameWidget::timeChanged, this, &KBounceMainWindow::displayTime );
0050     connect( m_gameWidget, &KBounceGameWidget::stateChanged, this, &KBounceMainWindow::gameStateChanged );
0051     //connect( m_gameWidget, SIGNAL(gameOver()), this, SLOT(gameOverNow()) );
0052     setCentralWidget( m_gameWidget );
0053 
0054     initXMLUI();
0055 
0056     /* Set initial ball and wall velocity according to the difficulty */
0057     m_gameWidget->handleLevelChanged();
0058 
0059     setFocusPolicy(Qt::StrongFocus);
0060     setFocus();
0061     setupGUI();
0062 
0063     readSettings();
0064 }
0065 
0066 KBounceMainWindow::~KBounceMainWindow()
0067 {
0068 }
0069 
0070 /**
0071  * create the action events create the gui.
0072  */
0073 void KBounceMainWindow::initXMLUI()
0074 {
0075     // Game
0076     m_newAction = KGameStandardAction::gameNew(this, &KBounceMainWindow::newGame, actionCollection());
0077     KGameStandardAction::end(this, &KBounceMainWindow::closeGame, actionCollection());
0078     m_pauseAction = KGameStandardAction::pause(this, &KBounceMainWindow::pauseGame, actionCollection());
0079     KGameStandardAction::highscores(this, &KBounceMainWindow::showHighscore, actionCollection());
0080     KGameStandardAction::quit(this, &QWidget::close, actionCollection());
0081 
0082     // Difficulty
0083     KGameDifficulty::global()->addStandardLevelRange(
0084             KGameDifficultyLevel::Easy, KGameDifficultyLevel::Hard
0085             );
0086     KGameDifficultyGUI::init(this);
0087     connect(KGameDifficulty::global(), &KGameDifficulty::currentLevelChanged,
0088             m_gameWidget, &KBounceGameWidget::handleLevelChanged);
0089 
0090     // Settings
0091     KStandardAction::preferences( this, &KBounceMainWindow::configureSettings, actionCollection() );
0092     m_soundAction = new KToggleAction( i18n("&Play Sounds"), this );
0093     actionCollection()->addAction( QStringLiteral(  "toggle_sound" ), m_soundAction );
0094     connect( m_soundAction, &QAction::triggered, this, &KBounceMainWindow::setSounds );
0095 }
0096 
0097 
0098 
0099 void KBounceMainWindow::newGame()
0100 {
0101     // Check for running game
0102     closeGame();
0103     if ( m_gameWidget->state() == KBounceGameWidget::BeforeFirstGame || m_gameWidget->state() == KBounceGameWidget::GameOver )
0104     {
0105         m_gameWidget->newGame();
0106     }
0107 }
0108 
0109 void KBounceMainWindow::pauseGame()
0110 {
0111     if ( m_gameWidget->state() == KBounceGameWidget::Paused )
0112     {
0113         m_gameWidget->setPaused( false );
0114     }
0115     else
0116     {
0117         m_gameWidget->setPaused( true );
0118     }
0119 }
0120 
0121 void KBounceMainWindow::closeGame()
0122 {
0123     if ( m_gameWidget->state() == KBounceGameWidget::BeforeFirstGame || m_gameWidget->state() == KBounceGameWidget::GameOver )
0124     {
0125         return;
0126     }
0127 
0128     KBounceGameWidget::State old_state = m_gameWidget->state();
0129     if ( old_state == KBounceGameWidget::Running )
0130         m_gameWidget->setPaused( true );
0131     int ret = KMessageBox::questionTwoActions(this,
0132                                          i18n("Are you sure you want to quit the current game?"),
0133                                          QString(),
0134                                          KGuiItem(i18nc("@action;button", "Quit Game"), QStringLiteral("window-close")),
0135                                          KStandardGuiItem::cancel() );
0136     if ( ret == KMessageBox::PrimaryAction )
0137     {
0138         m_gameWidget->closeGame();
0139     }
0140     else if ( old_state == KBounceGameWidget::Running )
0141     {
0142         m_gameWidget->setPaused( false );
0143     }
0144 }
0145 
0146 void KBounceMainWindow::gameOverNow()
0147 {
0148     statusBar()->showMessage(  i18n("Game over. Click to start a game") );
0149     highscore();
0150 }
0151 
0152 /**
0153  * Bring up the standard kde high score dialog.
0154  */
0155 void KBounceMainWindow::showHighscore()
0156 {
0157     KGameHighScoreDialog ksdialog( KGameHighScoreDialog::Name | KGameHighScoreDialog::Score, this );
0158     ksdialog.initFromDifficulty(KGameDifficulty::global());
0159     ksdialog.exec();
0160 }
0161 
0162 void KBounceMainWindow::highscore()
0163 {
0164     if ( m_gameWidget->score() == 0 ) {
0165         return;
0166     }
0167 
0168     qCDebug(KBOUNCE_LOG);
0169     KGameHighScoreDialog ksdialog( KGameHighScoreDialog::Name | KGameHighScoreDialog::Score | KGameHighScoreDialog::Level, this );
0170     ksdialog.initFromDifficulty(KGameDifficulty::global());
0171     KGameHighScoreDialog::FieldInfo info;
0172     info[KGameHighScoreDialog::Score].setNum( m_gameWidget->score() );
0173     info[KGameHighScoreDialog::Level].setNum( m_gameWidget->level() );
0174     if ( ksdialog.addScore( info ) )
0175         ksdialog.exec();
0176 }
0177 
0178 void KBounceMainWindow::configureSettings()
0179 {
0180     if ( KConfigDialog::showDialog( QStringLiteral("settings") ) ) return;
0181 
0182     KConfigDialog* dialog = new KConfigDialog( this, QStringLiteral("settings"), KBounceSettings::self());
0183     dialog->addPage( new KGameThemeSelector(m_gameWidget->renderer()->themeProvider(), KGameThemeSelector::DefaultBehavior, dialog), i18n( "Theme" ), QStringLiteral("games-config-theme") );
0184     dialog->addPage( new BackgroundSelector(dialog,KBounceSettings::self() ),i18n("Background"),QStringLiteral("games-config-background"));
0185     dialog->show();
0186     connect( dialog, &KConfigDialog::settingsChanged, this, &KBounceMainWindow::settingsChanged );
0187 }
0188 
0189 void KBounceMainWindow::readSettings()
0190 {
0191     m_soundAction->setChecked( KBounceSettings::playSounds() );
0192     m_gameWidget->settingsChanged();
0193 }
0194 
0195 void KBounceMainWindow::settingsChanged()
0196 {
0197     m_gameWidget->settingsChanged();
0198     KBounceSettings::self()->save(); // Bug 184606
0199 }
0200 
0201 void KBounceMainWindow::setSounds( bool val )
0202 {
0203     KBounceSettings::setPlaySounds( val );
0204     settingsChanged();
0205 }
0206 
0207 void KBounceMainWindow::displayLevel( int level )
0208 {
0209     levelLabel->setText(i18n("Level: %1", level));
0210 }
0211 
0212 void KBounceMainWindow::displayScore( int score )
0213 {
0214     scoreLabel->setText(i18n("Score: %1", score));
0215 }
0216 
0217 void KBounceMainWindow::displayFilled( int filled )
0218 {
0219     filledLabel->setText(i18n("Filled: %1%", filled));
0220 }
0221 
0222 void KBounceMainWindow::displayLives( int lives )
0223 {
0224     livesLabel->setText(i18n("Lives: %1", lives - 1));
0225 }
0226 
0227 void KBounceMainWindow::displayTime( int time )
0228 {
0229     timeLabel->setText(i18n("Time: %1", time));
0230 }
0231 
0232 void KBounceMainWindow::gameStateChanged( KBounceGameWidget::State state )
0233 {
0234     switch ( state )
0235     {
0236         case KBounceGameWidget::BeforeFirstGame :
0237             break;
0238         case KBounceGameWidget::BetweenLevels :
0239             break;
0240         case KBounceGameWidget::Suspended :
0241             break;
0242         case KBounceGameWidget::Paused :
0243             m_pauseAction->setChecked( true );
0244             m_statusBar->clearMessage();
0245             break;
0246         case KBounceGameWidget::Running :
0247             m_pauseAction->setChecked( false );
0248             m_statusBar->clearMessage();
0249             break;
0250         case KBounceGameWidget::GameOver :
0251             statusBar()->showMessage(  i18n("Game over. Click to start a game") );
0252             highscore();
0253             break;
0254     }
0255 }
0256 
0257 void KBounceMainWindow::focusOutEvent( QFocusEvent *ev )
0258 {
0259     if ( m_gameWidget->state() == KBounceGameWidget::Running &&
0260             focusWidget() != m_gameWidget )
0261     {
0262         m_gameWidget->setPaused( true );
0263     }
0264 
0265     KXmlGuiWindow::focusOutEvent( ev );
0266 }
0267 
0268 void KBounceMainWindow::focusInEvent ( QFocusEvent *ev )
0269 {
0270     //m_board->setSuspended( true );
0271     KXmlGuiWindow::focusInEvent( ev );
0272 }
0273 
0274 #include "moc_mainwindow.cpp"