2dkg/2dgk_7/2dgk_7/KInput.h
2021-12-09 12:15:49 +01:00

60 lines
2.6 KiB
C++

#pragma once
#include <SDL_events.h>
#include <unordered_map>
#include <vector>
#include "GamePad.h"
#include "KActionBind.h"
#include "KPlayer.h"
namespace KapitanGame {
struct KControllerAxisMapping {
KControllerAxisMapping(Controllers controller, SDL_GameControllerAxis axis, float scale) : Controller(controller), Axis(axis), Scale(scale) {}
Controllers Controller;
SDL_GameControllerAxis Axis;
float Scale;
};
class KInput {
std::vector<SDL_GameController*> ConnectedControllers;
std::vector<GamePad> ControllerInputs;
std::vector<GamePad> LastControllerInputs;
std::vector<Uint8> KeyboardInputs;
std::vector<Uint8> LastKeyboardInputs;
std::vector<KActionBind> Actions;
std::vector<KAxisBind> Axes;
std::unordered_multimap<std::string, Uint8> KeyBinds[static_cast<int>(KPlayer::Player2) + 1];
std::unordered_multimap<std::string, std::pair<Controllers, SDL_GameControllerButton>> ButtonBinds[static_cast<int>(KPlayer::Player2) + 1];
std::unordered_multimap<std::string, KControllerAxisMapping> ControllerAxisBinds[static_cast<int>(KPlayer::Player2) + 1];
std::unordered_multimap<std::string, std::pair<Uint8, float>> KeyAxisBinds[static_cast<int>(KPlayer::Player2) + 1];
int GamePadsCount;
bool Initialized;
bool IsControllerButtonPressed(Controllers controllerId, SDL_GameControllerButton button) const;
bool IsControllerButtonReleased(Controllers controllerId, SDL_GameControllerButton button) const;
bool IsControllerButtonHeld(Controllers controllerId, SDL_GameControllerButton button) const;
float GetControllerAxis(Controllers controllerId, SDL_GameControllerAxis axis) const;
bool IsKeyboardButtonHeld(Uint8 scanCode) const;
bool IsKeyboardButtonPressed(Uint8 scanCode) const;
bool IsKeyboardButtonReleased(Uint8 scanCode) const;
float GetAxisValue(const std::string& name, const KPlayer& player) const;
void ProcessActions();
void ProcessAxes();
public:
KInput();
void Init();
void Free();
void HandleInputPreEvents();
void HandleInputPostEvents();
void HandleEvent(const SDL_Event& event);
void BindAction(const std::string& name, InputState expectedInputState, KPlayerController* controllerObject, KPlayerCommand command, KPlayer player);
void BindAxis(const std::string& name, KPlayerController* controllerObject, KPlayerAxisCommand command, KPlayer player);
bool CheckAction(const std::string& name, InputState expectedInputState, KPlayer player);
void AddAxisMapping(const std::string& name, const float& scale, const std::string& key, const KPlayer& player);
void AddActionMapping(const std::string& name, const std::string& key, const KPlayer& player);
};
}