From 6ecb59761ac940e66c9670d23dd65696df3df7cf Mon Sep 17 00:00:00 2001 From: XaviDCR92 Date: Sun, 5 Aug 2018 18:03:30 +0200 Subject: [PATCH] + Added callback for button B released. + Implemented unit selection cancelation. --- HumanPlayerBtn.cpp | 103 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 95 insertions(+), 8 deletions(-) diff --git a/HumanPlayerBtn.cpp b/HumanPlayerBtn.cpp index a21962e..4f8f30f 100644 --- a/HumanPlayerBtn.cpp +++ b/HumanPlayerBtn.cpp @@ -55,7 +55,7 @@ void HumanPlayer::buttonHandler(void) * according to pressed button. */ cameraButtonPressedHandler(szBtn); } - else + else if (gb.buttons.released(static_cast(szBtn))) { /* Key has not been pressed. */ @@ -166,7 +166,8 @@ void HumanPlayer::playerButtonReleasedHandler(const size_t szBtn) [BTN_UP] = NULL, [BTN_RIGHT] = NULL, [BTN_DOWN] = NULL, - [BTN_A] = &HumanPlayer::onABtnReleased + [BTN_A] = &HumanPlayer::onABtnReleased, + [BTN_B] = &HumanPlayer::onBBtnReleased }; /* Key has been pressed. Execute both @@ -228,6 +229,45 @@ void HumanPlayer::onABtnPressed(void) *********************************************************************/ void HumanPlayer::onBBtnPressed(void) { + enum + { + /* Number of frames that B button must + * be pressed in order to cancel unit selection. */ + CANCEL_SELECTION_FRAMES = 10 + }; + + if (++_BBtnFrames >= CANCEL_SELECTION_FRAMES) + { + switch (_eState) + { + case PLAYER_STATE_UNIT_SELECTED: + { + /* Iterator that will be used + * to move along _unitsMap. */ + size_t i; + + for (i = 0; i < (sizeof (_unitsMap) / sizeof (_unitsMap[0])); i++) + { + Unit& u = _unitsMap[i]; + + /* Reset selected flags. */ + u.setSelected(false); + } + + /* Reset to default state. */ + _eState = PLAYER_STATE_IDLE; + } + /* Fall through. */ + case PLAYER_STATE_IDLE: + /* Fall through. */ + default: + /* Reset pressed B button frames counter. */ + _BBtnFrames = 0; + + /* Undefined player state. Exit. */ + break; + } + } } /*****************************************************************//** @@ -260,13 +300,60 @@ void HumanPlayer::onABtnReleased(void) /* Short button press. Exit. */ } } - break; - + /* Fall through. */ + case PLAYER_STATE_IDLE: + /* Fall through. */ default: - /* Undefined state. Exit. */ + /* Undefined player state. Exit. */ + + /* Reset pressed A button frames counter. */ + _ABtnFrames = 0; + break; + } +} + +/*****************************************************************//** + * + * \brief Event handler executed when human player releases + * B button. + * + *********************************************************************/ +void HumanPlayer::onBBtnReleased(void) +{ + switch (_eState) + { + case PLAYER_STATE_UNIT_SELECTED: + { + /* Iterator that will be used + * to move along _unitsMap. */ + size_t i; + + for (i = 0; i < (sizeof (_unitsMap) / sizeof (_unitsMap[0])); i++) + { + Unit& u = _unitsMap[i]; + + if (u.isSelected()) + { + /* Get cursor coordinates. */ + const uint16_t x = _cursor.getX(); + const uint16_t y = _cursor.getY(); + + /* Move unit to cursor position. */ + u.moveTo(x, y); + } + else + { + /* Unit is not selected by the player. Continue. */ + } + } + } + /* Fall through.*/ + case PLAYER_STATE_IDLE: + /* Fall through. */ + default: + /* Undefined player state. Exit. */ + /* Reset pressed B button frames counter. */ + _BBtnFrames = 0; break; } - - /* Reset pressed A button frames counter. */ - _ABtnFrames = 0; }