2dkg/2dkg_zad5/2dgk_zad5/KInput.h
2022-01-20 08:52:14 +01:00

62 lines
2.7 KiB
C++

#pragma once
#include <SDL_events.h>
#include <unordered_map>
#include <vector>
#include "Controllers.h"
#include "GamePad.h"
#include "KActionBind.h"
#include "KPlayer.h"
namespace KapitanGame {
struct KControllerAxisMapping {
KControllerAxisMapping(const Controllers controller, const SDL_GameControllerAxis axis, const 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;
void ProcessActions();
void ProcessAxes();
public:
KInput();
void Init();
void Free();
void HandleInputPreEvents();
void HandleInputPostEvents(bool processInput);
void HandleEvent(const SDL_Event& event);
void BindAction(const std::string& name, InputState expectedInputState, const std::shared_ptr<KPlayerController>& controllerObject, KPlayerCommand command, KPlayer player);
void BindAxis(const std::string& name, const std::shared_ptr<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);
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;
};
}