98 lines
No EOL
2 KiB
C++
98 lines
No EOL
2 KiB
C++
#ifndef _H_INPUT_H
|
|
#define _H_INPUT_H
|
|
|
|
#include <unordered_map>
|
|
#include <SDL_keycode.h>
|
|
#include <SDL_events.h>
|
|
|
|
#include "mousestate.h"
|
|
|
|
class Command;
|
|
class MouseCommand;
|
|
class GameActor;
|
|
|
|
enum {
|
|
MOUSE_BUTTON_LEFT = 1,
|
|
MOUSE_BUTTON_RIGHT = 2,
|
|
MOUSE_BUTTON_MIDDLE = 4,
|
|
MOUSE_MOTION = 8
|
|
};
|
|
|
|
class Keyboard
|
|
{
|
|
protected:
|
|
Keyboard() {}
|
|
|
|
void captureKeyboard(const SDL_Event& e)
|
|
{
|
|
if (e.type == SDL_KEYDOWN)
|
|
keys[e.key.keysym.sym] = true;
|
|
if (e.type == SDL_KEYUP)
|
|
keys[e.key.keysym.sym] = false;
|
|
}
|
|
std::unordered_map<SDL_Keycode, bool> keys;
|
|
};
|
|
|
|
class Mouse
|
|
{
|
|
protected:
|
|
Mouse() {}
|
|
|
|
void captureMouse(const SDL_Event& e)
|
|
{
|
|
if (e.type == SDL_MOUSEBUTTONDOWN)
|
|
mouseButtons[MOUSE_BUTTON_LEFT] = true;
|
|
if (e.type == SDL_MOUSEBUTTONUP)
|
|
mouseButtons[MOUSE_BUTTON_LEFT] = false;
|
|
if (e.type == SDL_MOUSEMOTION)
|
|
{
|
|
mouse_state.x = static_cast<float>(e.motion.x);
|
|
mouse_state.y = static_cast<float>(e.motion.y);
|
|
}
|
|
}
|
|
MouseState mouse_state;
|
|
std::unordered_map<Uint8, bool> mouseButtons;
|
|
};
|
|
|
|
class InputHandler : public Keyboard, public Mouse
|
|
{
|
|
private:
|
|
struct CommandWithDelay{
|
|
Command* cmd;
|
|
Uint32 lastExecution;
|
|
Uint32 delay;
|
|
};
|
|
struct MouseCommandWithDelay{
|
|
MouseCommand* cmd;
|
|
Uint32 lastExecution;
|
|
Uint32 delay;
|
|
};
|
|
public:
|
|
InputHandler() {}
|
|
void captureInput(const SDL_Event& e) {
|
|
Keyboard::captureKeyboard(e); Mouse::captureMouse(e);
|
|
}
|
|
void setActor(GameActor* actor) {
|
|
this->actor = actor;
|
|
}
|
|
|
|
void bindKeyCommand(SDL_Keycode key, Uint32 delay, Command* command) {
|
|
keyCommands[key] = { command, 0, delay };
|
|
}
|
|
void bindMouseCommand(Uint8 mouse, Uint32 delay, MouseCommand* command) {
|
|
mouseCommands[mouse] = { command, 0, delay };
|
|
}
|
|
void bindMouseMotion(MouseCommand* command) {
|
|
mouseMotionCommand = command;
|
|
}
|
|
void handleInput();
|
|
|
|
private:
|
|
GameActor* actor = nullptr;
|
|
// final int is delay
|
|
std::unordered_map<SDL_Keycode, CommandWithDelay> keyCommands;
|
|
std::unordered_map<Uint8, MouseCommandWithDelay> mouseCommands;
|
|
MouseCommand* mouseMotionCommand = nullptr;
|
|
};
|
|
|
|
#endif // _H_INPUT_H
|