This commit is contained in:
elushaX 2024-03-20 00:47:46 -07:00 committed by Ilya Shurupov
parent 6d02137dca
commit a46a2167ba
13 changed files with 182 additions and 90 deletions

View file

@ -11,14 +11,14 @@ namespace tp {
struct InputEvent {
enum class Type {
NONE,
NONE = 0,
BUTTON_ACTION,
MOUSE_DELTA,
MOUSE_POS,
} type = Type::NONE;
enum class ButtonAction {
NONE,
NONE = 0,
PRESS,
RELEASE,
REPEAT,
@ -28,18 +28,6 @@ namespace tp {
};
class InputState {
// Transitions from state to state is strict and defined by state order in the enum
// If posted event conflicts with that automata, we need to emulate intermediate states
enum State {
NONE, // Button is inactive
PRESSED, // Button is pressed
HOLD, // Button is still pressed
RELEASED, // Button is released
};
State mCurrentState;
State mPreviousState;
public:
InputState() = default;
@ -49,6 +37,19 @@ namespace tp {
// true if event is no longer needed and false if we need to emulate some transitions further
bool isHandled();
public:
// Transitions from state to state is strict and defined by state order in the enum
// If posted event conflicts with that automata, we need to emulate intermediate states
enum State {
NONE = 0, // Button is inactive
PRESSED, // Button is pressed
HOLD, // Button is still pressed
RELEASED, // Button is released
};
State mCurrentState;
State mPreviousState;
};
// Assumes that user and event poster use different threads
@ -62,32 +63,16 @@ namespace tp {
public: // Event Poster Interface
// Record event
void postEvent(InputID inputID, InputEvent inputEvent) {
mMutex.lock();
mEventQueue.pushBack({ inputID, inputEvent });
mMutex.unlock();
}
void postEvent(InputID inputID, InputEvent inputEvent);
public: // User interface
bool isEvents() {
mMutex.lock();
auto res = mEventQueue.length();
mMutex.unlock();
return res;
}
void processEvent() {
mMutex.lock();
mEventQueue.popFront();
mMutex.unlock();
}
bool isEvents();
void processEvent();
const Vec2F& getPointer() const;
bool isPressed(InputID id) const;
bool isReleased(InputID id) const;
bool isDown(InputID id) const;
halnf getScrollY() const;
private:
@ -100,7 +85,7 @@ namespace tp {
Vec2F mPointer;
halnf pressure = 0;
InputState mInputStates[(int) InputID::LAST_KEY_CODE];
InputState mInputStates[(int) InputID::LAST_KEY_CODE]{};
};
}