106 lines
2.3 KiB
C++
106 lines
2.3 KiB
C++
#pragma once
|
|
|
|
#include <array>
|
|
#include <core/types.h>
|
|
#include <core/vec2.h>
|
|
#include <platform/input_codes.h>
|
|
|
|
#include <SDL.h>
|
|
|
|
namespace extra2d {
|
|
|
|
enum class MouseButton {
|
|
Left = 0,
|
|
Right = 1,
|
|
Middle = 2,
|
|
Button4 = 3,
|
|
Button5 = 4,
|
|
Button6 = 5,
|
|
Button7 = 6,
|
|
Button8 = 7,
|
|
Count = 8
|
|
};
|
|
|
|
class Input {
|
|
public:
|
|
Input();
|
|
~Input();
|
|
|
|
void init();
|
|
void shutdown();
|
|
|
|
void update();
|
|
|
|
bool isKeyDown(int keyCode) const;
|
|
bool isKeyPressed(int keyCode) const;
|
|
bool isKeyReleased(int keyCode) const;
|
|
|
|
bool isButtonDown(int button) const;
|
|
bool isButtonPressed(int button) const;
|
|
bool isButtonReleased(int button) const;
|
|
|
|
Vec2 getLeftStick() const;
|
|
Vec2 getRightStick() const;
|
|
|
|
bool isMouseDown(MouseButton button) const;
|
|
bool isMousePressed(MouseButton button) const;
|
|
bool isMouseReleased(MouseButton button) const;
|
|
|
|
Vec2 getMousePosition() const;
|
|
Vec2 getMouseDelta() const;
|
|
float getMouseScroll() const { return mouseScroll_; }
|
|
float getMouseScrollDelta() const { return mouseScroll_ - prevMouseScroll_; }
|
|
|
|
void setMousePosition(const Vec2 &position);
|
|
void setMouseVisible(bool visible);
|
|
void setMouseLocked(bool locked);
|
|
|
|
bool isTouching() const { return touching_; }
|
|
Vec2 getTouchPosition() const { return touchPosition_; }
|
|
int getTouchCount() const { return touchCount_; }
|
|
|
|
bool isAnyKeyDown() const;
|
|
bool isAnyMouseDown() const;
|
|
|
|
void onControllerAdded(int deviceIndex);
|
|
void onControllerRemoved(int instanceId);
|
|
void onMouseWheel(float x, float y);
|
|
|
|
private:
|
|
static constexpr int MAX_BUTTONS = SDL_CONTROLLER_BUTTON_MAX;
|
|
static constexpr int MAX_KEYS = SDL_NUM_SCANCODES;
|
|
|
|
SDL_GameController *controller_;
|
|
|
|
std::array<bool, MAX_KEYS> keysDown_;
|
|
std::array<bool, MAX_KEYS> prevKeysDown_;
|
|
|
|
std::array<bool, MAX_BUTTONS> buttonsDown_;
|
|
std::array<bool, MAX_BUTTONS> prevButtonsDown_;
|
|
|
|
float leftStickX_;
|
|
float leftStickY_;
|
|
float rightStickX_;
|
|
float rightStickY_;
|
|
|
|
Vec2 mousePosition_;
|
|
Vec2 prevMousePosition_;
|
|
float mouseScroll_;
|
|
float prevMouseScroll_;
|
|
std::array<bool, 8> mouseButtonsDown_;
|
|
std::array<bool, 8> prevMouseButtonsDown_;
|
|
|
|
bool touching_;
|
|
bool prevTouching_;
|
|
Vec2 touchPosition_;
|
|
Vec2 prevTouchPosition_;
|
|
int touchCount_;
|
|
|
|
void updateKeyboard();
|
|
void updateMouse();
|
|
void updateGamepad();
|
|
void updateTouch();
|
|
};
|
|
|
|
} // namespace extra2d
|