Skip to content

Commit

Permalink
Game controller controls menus as well
Browse files Browse the repository at this point in the history
  • Loading branch information
wivlaro committed Dec 19, 2024
1 parent 3f93afb commit dc45ec6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 36 deletions.
1 change: 1 addition & 0 deletions src/MagnumGameApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ namespace MagnumGame {

void keyPressEvent(KeyEvent &event) override;
void keyReleaseEvent(KeyEvent &event) override;
bool handleKeyPress(Key key, Modifiers modifiers);

void pointerPressEvent(PointerEvent &event) override;

Expand Down
67 changes: 38 additions & 29 deletions src/MagnumGameApp_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,43 +59,54 @@ namespace MagnumGame {

void MagnumGameApp::keyPressEvent(KeyEvent &event) {
auto key = event.key();
if (_debugScreen && _debugScreen->handleKeyPress(key, event.modifiers())) {
auto modifiers = event.modifiers();

if (handleKeyPress(key, modifiers)) {
event.setAccepted();
return;
}
if (_currentScreen && _currentScreen->handleKeyPress(key, event.modifiers())) {
event.setAccepted();
return;
else {
auto keyBit = getKeyBit(key);
if (keyBit) {
_controllerKeysHeld |= keyBit;
event.setAccepted();
}
}
}

auto keyBit = getKeyBit(key);
void MagnumGameApp::keyReleaseEvent(KeyEvent &event) {
auto keyBit = getKeyBit(event.key());
if (keyBit) {
_controllerKeysHeld |= keyBit;
_controllerKeysHeld &= ~keyBit;
event.setAccepted();
}
else switch (key) {
}

bool MagnumGameApp::handleKeyPress(Key key, Modifiers modifiers) {
if (_debugScreen && _debugScreen->handleKeyPress(key, modifiers)) {
return true;
}
if (_currentScreen && _currentScreen->handleKeyPress(key, modifiers)) {
return true;
}

switch (key) {
case Key::Esc:
toPauseScreen();
event.setAccepted();
break;
if (isPlaying()) {
toPauseScreen();
}
else {
startGame();
}
return true;
case Key::Space:
_gameState->getPlayer()->tryJump();
event.setAccepted();
break;
return true;
case Key::X:
_drawDebug = !_drawDebug;
event.setAccepted();
break;
return true;
default: break;
}
}

void MagnumGameApp::keyReleaseEvent(KeyEvent &event) {
auto keyBit = getKeyBit(event.key());
if (keyBit) {
_controllerKeysHeld &= ~keyBit;
event.setAccepted();
}
return false;
}

void MagnumGameApp::pointerPressEvent(PointerEvent &event) {
Expand Down Expand Up @@ -174,18 +185,16 @@ namespace MagnumGame {
event.setAccepted();
}

#ifdef MAGNUM_SDL2APPLICATION_MAIN
#ifdef MAGNUMGAME_SDL
void MagnumGameApp::anyEvent(SDL_Event &event) {
if (event.type == SDL_WINDOWEVENT_FOCUS_LOST) {
_controllerKeysHeld = 0;
return ;
}
if (_gameController) {
if (_gameController->handleEvent(event)) {
return;
}
if (_gameController && _gameController->handleEvent(event, [this](Key key, Modifiers modifiers) { return handleKeyPress(key, modifiers); } )) {
return;
}
Debug{} << "Unhandled SDL event" << event.type;
// Debug{} << "Unhandled SDL event" << event.type;
}
#endif
}
19 changes: 14 additions & 5 deletions src/SdlGameController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace MagnumGame {
}


bool SdlGameController::handleEvent(const SDL_Event &event) {
bool SdlGameController::handleEvent(const SDL_Event &event, const std::function<bool(MagnumGameApp::Key, MagnumGameApp::Modifiers)> &emulateKeyPress) {
switch (event.type) {
case SDL_CONTROLLERDEVICEADDED:
if (!_controller) {
Expand All @@ -76,11 +76,20 @@ namespace MagnumGame {
}
break;
case SDL_CONTROLLERBUTTONDOWN:
if (event.cbutton.button == SDL_CONTROLLER_BUTTON_A) {
_gameState->getPlayer()->tryJump();
return true;
switch (event.cbutton.button) {
case SDL_CONTROLLER_BUTTON_DPAD_UP:
return emulateKeyPress(MagnumGameApp::Key::Up, MagnumGameApp::Modifiers{});
case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
return emulateKeyPress(MagnumGameApp::Key::Down, MagnumGameApp::Modifiers{});
case SDL_CONTROLLER_BUTTON_DPAD_LEFT:
return emulateKeyPress(MagnumGameApp::Key::Left, MagnumGameApp::Modifiers{});
case SDL_CONTROLLER_BUTTON_DPAD_RIGHT:
return emulateKeyPress(MagnumGameApp::Key::Right, MagnumGameApp::Modifiers{});
case SDL_CONTROLLER_BUTTON_A:
return emulateKeyPress(MagnumGameApp::Key::Space, MagnumGameApp::Modifiers{});
case SDL_CONTROLLER_BUTTON_START:
return emulateKeyPress(MagnumGameApp::Key::Esc, MagnumGameApp::Modifiers{});
}
break;
}
return false;
}
Expand Down
4 changes: 3 additions & 1 deletion src/SdlGameController.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <Magnum/Math/Vector2.h>
#include <SDL2/SDL_gamecontroller.h>

#include "MagnumGameApp.h"

namespace MagnumGame {

class GameState;
Expand All @@ -22,7 +24,7 @@ namespace MagnumGame {

Vector2 getCameraDirectionalControlVector() const;

bool handleEvent(const SDL_Event &event);
bool handleEvent(const SDL_Event &event, const std::function<bool(MagnumGameApp::Key, MagnumGameApp::Modifiers)> &emulateKeyPress);

static inline float JoystickDeadzone = 0.05f;

Expand Down
2 changes: 1 addition & 1 deletion src/UserInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace MagnumGame {


bool UITextButton::handleKeyPress(MagnumGameApp::Key key, MagnumGameApp::Modifiers ) {
if (key == MagnumGameApp::Key::Enter) {
if (key == MagnumGameApp::Key::Enter || key == MagnumGameApp::Key::Space) {
if (_onClick) {
_onClick();
return true;
Expand Down

0 comments on commit dc45ec6

Please sign in to comment.