File indexing completed on 2024-12-01 03:46:17

0001 /*
0002     SPDX-FileCopyrightText: 2009 Mathias Kraus <k.hias@gmx.de>
0003     SPDX-FileCopyrightText: 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "gameview.h"
0009 #include "gamescene.h"
0010 #include "game.h"
0011 
0012 #include <QKeyEvent>
0013 #include <QTimer>
0014 
0015 GameView::GameView(GameScene* p_scene, Game * p_game) : QGraphicsView(p_scene)
0016 {
0017     setFrameStyle(QFrame::NoFrame);
0018     setFocusPolicy(Qt::StrongFocus);
0019     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
0020     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
0021 
0022     // Forward the key press events to the Game instance
0023     connect(this, &GameView::keyPressed, p_game, &Game::keyPressEvent);
0024     connect(this, &GameView::keyReleased, p_game, &Game::keyReleaseEvent);
0025 }
0026 
0027 GameView::~GameView()
0028 = default;
0029 
0030 void GameView::resizeEvent(QResizeEvent*)
0031 {
0032     dynamic_cast <GameScene*> (scene())->resizeSprites(250);
0033 }
0034 
0035 void GameView::focusOutEvent(QFocusEvent*)
0036 {
0037     // Pause the game if it is not already paused
0038     if (((GameScene*)scene())->getGame()->getTimer()->isActive())
0039     {
0040         ((GameScene*)scene())->getGame()->switchPause();
0041     }
0042 }
0043 
0044 void GameView::keyPressEvent(QKeyEvent* p_event)
0045 {
0046     if(p_event->isAutoRepeat())
0047     {
0048         return;
0049     }
0050     Q_EMIT keyPressed(p_event);
0051 }
0052 
0053 void GameView::keyReleaseEvent(QKeyEvent* p_event)
0054 {
0055     if(p_event->isAutoRepeat())
0056     {
0057         return;
0058     }
0059     Q_EMIT keyReleased(p_event);
0060 }
0061 
0062 #include "moc_gameview.cpp"