File indexing completed on 2024-04-21 04:02:07
0001 /*************************************************************************** 0002 * KBlocks, a falling blocks game by KDE * 0003 * SPDX-FileCopyrightText: 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com> * 0004 * * 0005 * SPDX-License-Identifier: GPL-2.0-or-later 0006 ***************************************************************************/ 0007 #include "KBlocksKeyboardPlayer.h" 0008 #include <QIcon> 0009 KBlocksKeyboardPlayer::KBlocksKeyboardPlayer(KXmlGuiWindow *parent, const string &name, bool netMode) 0010 { 0011 mpGame = nullptr; 0012 mPauseFlag = false; 0013 mpKeyWindow = nullptr; 0014 0015 mPlayerName = name; 0016 mNetMode = netMode; 0017 0018 if (parent) { 0019 mpKeyShortcuts = parent->actionCollection(); 0020 } else { 0021 mpKeyWindow = new KXmlGuiWindow(); 0022 mpKeyWindow->setupGUI(); 0023 mpKeyWindow->setFixedWidth(192); 0024 mpKeyWindow->setFixedHeight(32); 0025 mpKeyWindow->show(); 0026 0027 mpKeyShortcuts = mpKeyWindow->actionCollection(); 0028 } 0029 0030 bindKeys(); 0031 0032 mActionList.clear(); 0033 } 0034 0035 KBlocksKeyboardPlayer::~KBlocksKeyboardPlayer() 0036 { 0037 delete mpKeyWindow; 0038 } 0039 0040 void KBlocksKeyboardPlayer::startGame(SingleGameInterface *p) 0041 { 0042 mpGame = p; 0043 mPauseFlag = false; 0044 0045 mActionList.clear(); 0046 } 0047 0048 void KBlocksKeyboardPlayer::stopGame() 0049 { 0050 mpGame = nullptr; 0051 0052 mActionList.clear(); 0053 } 0054 0055 void KBlocksKeyboardPlayer::pauseGame(bool flag) 0056 { 0057 mPauseFlag = flag; 0058 } 0059 0060 void KBlocksKeyboardPlayer::think(GamePlayer_ActionList *actionList) 0061 { 0062 if (mNetMode) { 0063 *actionList = mActionList; 0064 mActionList.clear(); 0065 } 0066 } 0067 0068 string KBlocksKeyboardPlayer::getName() 0069 { 0070 return mPlayerName; 0071 } 0072 0073 void KBlocksKeyboardPlayer::bindKeys() 0074 { 0075 rotatecw = mpKeyShortcuts->addAction(QStringLiteral("rotate_cw")); 0076 rotatecw->setText(i18n("Rotate Piece Clockwise")); 0077 rotatecw->setIcon(QIcon::fromTheme(QStringLiteral("object-rotate-right"))); 0078 KActionCollection::setDefaultShortcut(rotatecw, Qt::Key_Z); 0079 connect(rotatecw, &QAction::triggered, this, &KBlocksKeyboardPlayer::rotateCW); 0080 0081 rotateccw = mpKeyShortcuts->addAction(QStringLiteral("rotate_ccw")); 0082 rotateccw->setText(i18n("Rotate Piece Counter Clockwise")); 0083 rotateccw->setIcon(QIcon::fromTheme(QStringLiteral("object-rotate-left"))); 0084 KActionCollection::setDefaultShortcut(rotateccw, Qt::Key_Up); 0085 connect(rotateccw, &QAction::triggered, this, &KBlocksKeyboardPlayer::rotateCCW); 0086 0087 moveleft = mpKeyShortcuts->addAction(QStringLiteral("move_left")); 0088 moveleft->setText(i18n("Move Piece Left")); 0089 moveleft->setIcon(QIcon::fromTheme(QStringLiteral("arrow-left"))); 0090 KActionCollection::setDefaultShortcut(moveleft, Qt::Key_Left); 0091 connect(moveleft, &QAction::triggered, this, &KBlocksKeyboardPlayer::moveLeft); 0092 0093 moveright = mpKeyShortcuts->addAction(QStringLiteral("move_right")); 0094 moveright->setText(i18n("Move Piece Right")); 0095 moveright->setIcon(QIcon::fromTheme(QStringLiteral("arrow-right"))); 0096 KActionCollection::setDefaultShortcut(moveright, Qt::Key_Right); 0097 connect(moveright, &QAction::triggered, this, &KBlocksKeyboardPlayer::moveRight); 0098 0099 movedown = mpKeyShortcuts->addAction(QStringLiteral("move_down")); 0100 movedown->setText(i18n("Move Piece Down")); 0101 movedown->setIcon(QIcon::fromTheme(QStringLiteral("arrow-down"))); 0102 KActionCollection::setDefaultShortcut(movedown, Qt::Key_Down); 0103 connect(movedown, &QAction::triggered, this, &KBlocksKeyboardPlayer::moveDown); 0104 0105 pushdown = mpKeyShortcuts->addAction(QStringLiteral("push_down")); 0106 pushdown->setText(i18n("Drop the Piece")); 0107 pushdown->setIcon(QIcon::fromTheme(QStringLiteral("arrow-down"))); 0108 KActionCollection::setDefaultShortcut(pushdown, Qt::Key_Space); 0109 connect(pushdown, &QAction::triggered, this, &KBlocksKeyboardPlayer::pushDown); 0110 } 0111 0112 void KBlocksKeyboardPlayer::moveLeft() 0113 { 0114 if ((!mpGame) || (mPauseFlag)) { 0115 return; 0116 } 0117 if (mNetMode) { 0118 mActionList.push_back(PlayerAction_Move_Left); 0119 } else { 0120 mpGame->setCurrentPiece(-1, 0, 0); 0121 } 0122 Q_EMIT blockMoved(); 0123 } 0124 0125 void KBlocksKeyboardPlayer::moveRight() 0126 { 0127 if ((!mpGame) || (mPauseFlag)) { 0128 return; 0129 } 0130 if (mNetMode) { 0131 mActionList.push_back(PlayerAction_Move_Right); 0132 } else { 0133 mpGame->setCurrentPiece(1, 0, 0); 0134 } 0135 Q_EMIT blockMoved(); 0136 } 0137 0138 void KBlocksKeyboardPlayer::moveDown() 0139 { 0140 if ((!mpGame) || (mPauseFlag)) { 0141 return; 0142 } 0143 if (mNetMode) { 0144 mActionList.push_back(PlayerAction_Move_Down); 0145 } else { 0146 mpGame->setCurrentPiece(0, 1, 0); 0147 } 0148 Q_EMIT blockMoved(); 0149 } 0150 0151 void KBlocksKeyboardPlayer::pushDown() 0152 { 0153 if ((!mpGame) || (mPauseFlag)) { 0154 return; 0155 } 0156 if (mNetMode) { 0157 mActionList.push_back(PlayerAction_Push_Down); 0158 } else { 0159 while (mpGame->setCurrentPiece(0, 1, 0)) ; 0160 mpGame->forceUpdateGame(); 0161 } 0162 Q_EMIT blockDropped(); 0163 } 0164 0165 void KBlocksKeyboardPlayer::rotateCW() 0166 { 0167 if ((!mpGame) || (mPauseFlag)) { 0168 return; 0169 } 0170 if (mNetMode) { 0171 mActionList.push_back(PlayerAction_Rotate_CW); 0172 } else { 0173 mpGame->setCurrentPiece(0, 0, -1); 0174 } 0175 Q_EMIT blockMoved(); 0176 } 0177 0178 void KBlocksKeyboardPlayer::rotateCCW() 0179 { 0180 if ((!mpGame) || (mPauseFlag)) { 0181 return; 0182 } 0183 if (mNetMode) { 0184 mActionList.push_back(PlayerAction_Rotate_CCW); 0185 } else { 0186 mpGame->setCurrentPiece(0, 0, 1); 0187 } 0188 Q_EMIT blockMoved(); 0189 } 0190 0191 #include "moc_KBlocksKeyboardPlayer.cpp"