You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
1.9 KiB
85 lines
1.9 KiB
|
6 years ago
|
#include "controls/controller.h"
|
||
|
|
|
||
|
5 years ago
|
#include <cmath>
|
||
|
|
|
||
|
6 years ago
|
#include "controls/devices/kbcontroller.h"
|
||
|
|
#include "controls/devices/joystick.h"
|
||
|
|
#include "controls/devices/game_controller.h"
|
||
|
|
|
||
|
5 years ago
|
namespace devilution {
|
||
|
6 years ago
|
|
||
|
|
ControllerButtonEvent ToControllerButtonEvent(const SDL_Event &event)
|
||
|
|
{
|
||
|
5 years ago
|
ControllerButtonEvent result { ControllerButton_NONE, false };
|
||
|
6 years ago
|
switch (event.type) {
|
||
|
|
#ifndef USE_SDL1
|
||
|
|
case SDL_CONTROLLERBUTTONUP:
|
||
|
|
#endif
|
||
|
|
case SDL_JOYBUTTONUP:
|
||
|
|
case SDL_KEYUP:
|
||
|
|
result.up = true;
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
break;
|
||
|
|
}
|
||
|
5 years ago
|
#if HAS_KBCTRL == 1
|
||
|
|
result.button = KbCtrlToControllerButton(event);
|
||
|
|
if (result.button != ControllerButton_NONE)
|
||
|
|
return result;
|
||
|
|
#endif
|
||
|
6 years ago
|
#ifndef USE_SDL1
|
||
|
5 years ago
|
GameController *const controller = GameController::Get(event);
|
||
|
|
if (controller != NULL) {
|
||
|
|
result.button = controller->ToControllerButton(event);
|
||
|
|
if (result.button != ControllerButton_NONE)
|
||
|
|
return result;
|
||
|
|
}
|
||
|
6 years ago
|
#endif
|
||
|
|
|
||
|
5 years ago
|
const Joystick *joystick = Joystick::Get(event);
|
||
|
5 years ago
|
if (joystick != NULL)
|
||
|
|
result.button = joystick->ToControllerButton(event);
|
||
|
6 years ago
|
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool IsControllerButtonPressed(ControllerButton button)
|
||
|
|
{
|
||
|
|
#ifndef USE_SDL1
|
||
|
5 years ago
|
if (GameController::IsPressedOnAnyController(button))
|
||
|
|
return true;
|
||
|
6 years ago
|
#endif
|
||
|
|
#if HAS_KBCTRL == 1
|
||
|
5 years ago
|
if (IsKbCtrlButtonPressed(button))
|
||
|
|
return true;
|
||
|
6 years ago
|
#endif
|
||
|
5 years ago
|
return Joystick::IsPressedOnAnyJoystick(button);
|
||
|
6 years ago
|
}
|
||
|
|
|
||
|
5 years ago
|
bool HandleControllerAddedOrRemovedEvent(const SDL_Event &event)
|
||
|
6 years ago
|
{
|
||
|
|
#ifndef USE_SDL1
|
||
|
5 years ago
|
switch (event.type) {
|
||
|
|
case SDL_CONTROLLERDEVICEADDED:
|
||
|
|
GameController::Add(event.cdevice.which);
|
||
|
|
break;
|
||
|
|
case SDL_CONTROLLERDEVICEREMOVED:
|
||
|
|
GameController::Remove(event.cdevice.which);
|
||
|
|
break;
|
||
|
|
case SDL_JOYDEVICEADDED:
|
||
|
|
Joystick::Add(event.jdevice.which);
|
||
|
|
break;
|
||
|
|
case SDL_JOYDEVICEREMOVED:
|
||
|
|
Joystick::Remove(event.jdevice.which);
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
#else
|
||
|
|
return false;
|
||
|
6 years ago
|
#endif
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
} // namespace devilution
|