summaryrefslogtreecommitdiff
path: root/HumanPlayerBtn.cpp
diff options
context:
space:
mode:
authorXaviDCR92 <xavi.dcr@gmail.com>2018-08-05 18:03:30 +0200
committerXaviDCR92 <xavi.dcr@gmail.com>2018-08-05 18:03:30 +0200
commit6ecb59761ac940e66c9670d23dd65696df3df7cf (patch)
treedb4cf3034c9a11cd017c309223e1a26a9868a12d /HumanPlayerBtn.cpp
parent37070c559d37069ddd05f77b803ab92c5266e80c (diff)
downloadpocketempires-6ecb59761ac940e66c9670d23dd65696df3df7cf.tar.gz
+ Added callback for button B released.
+ Implemented unit selection cancelation.
Diffstat (limited to 'HumanPlayerBtn.cpp')
-rw-r--r--HumanPlayerBtn.cpp99
1 files changed, 93 insertions, 6 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<uint8_t>(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. */
}
}
+ /* Fall through. */
+ case PLAYER_STATE_IDLE:
+ /* Fall through. */
+ default:
+ /* 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 state. Exit. */
+ /* Undefined player state. Exit. */
+ /* Reset pressed B button frames counter. */
+ _BBtnFrames = 0;
break;
}
-
- /* Reset pressed A button frames counter. */
- _ABtnFrames = 0;
}