This commit is contained in:
elushaX 2024-03-20 00:47:46 -07:00
parent 752243c7e9
commit b2f5c0193a
13 changed files with 182 additions and 90 deletions

View file

@ -37,6 +37,8 @@ DebugGUI::DebugGUI(Window* window) {
ImGuiIO& io = ImGui::GetIO();
io.Fonts->AddFontFromFileTTF("Font.ttf", 20.f);
io.ConfigInputTrickleEventQueue = false;
}
DebugGUI::~DebugGUI() {

View file

@ -4,4 +4,87 @@
using namespace tp;
EventHandler::EventHandler() = default;
EventHandler::~EventHandler() = default;
EventHandler::~EventHandler() = default;
void EventHandler::postEvent(InputID inputID, InputEvent inputEvent) {
mMutex.lock();
mEventQueue.pushBack({ inputID, inputEvent });
mMutex.unlock();
}
bool EventHandler::isEvents() {
mMutex.lock();
auto res = mEventQueue.length();
mMutex.unlock();
return res;
}
InputState::State transitions[4][4] = {
{ InputState::State::NONE, InputState::State::PRESSED, InputState::State::PRESSED, InputState::State::NONE },
{ InputState::State::PRESSED, InputState::State::PRESSED, InputState::State::HOLD, InputState::State::PRESSED },
{ InputState::State::HOLD, InputState::State::RELEASED, InputState::State::RELEASED, InputState::State::HOLD },
{ InputState::State::RELEASED, InputState::State::NONE, InputState::State::RELEASED, InputState::State::RELEASED },
};
bool transitionsReduce[4][4] = {
{ true, true, false, true },
{ true, true, false, true },
{ true, false, true, true },
{ true, false, true, true },
};
void EventHandler::processEvent() {
mMutex.lock();
auto lastEvent = mEventQueue.last();
const auto& eventData = lastEvent->data.second;
const auto& inputId = lastEvent->data.first;
switch (eventData.type) {
case InputEvent::Type::MOUSE_POS:
{
mPointer = eventData.mouseEvent;
mEventQueue.popFront();
break;
}
case InputEvent::Type::BUTTON_ACTION:
{
auto currentState = (int) mInputStates[(int) inputId].mCurrentState;
auto reportedEvent = (int) eventData.buttonAction;
mInputStates[(int) inputId].mCurrentState = transitions[currentState][reportedEvent];
if (transitionsReduce[currentState][reportedEvent]) {
mEventQueue.popFront();
}
break;
}
default:
{
mEventQueue.popFront();
}
}
mMutex.unlock();
}
const Vec2F& EventHandler::getPointer() const { return mPointer; }
bool EventHandler::isPressed(InputID id) const {
return mInputStates[(int) id].mCurrentState == InputState::State::PRESSED;
}
bool EventHandler::isReleased(InputID id) const {
return mInputStates[(int) id].mCurrentState == InputState::State::RELEASED;
}
bool EventHandler::isDown(InputID id) const {
return mInputStates[(int) id].mCurrentState == InputState::State::PRESSED ||
mInputStates[(int) id].mCurrentState == InputState::State::HOLD;
}
halnf EventHandler::getScrollY() const { return 0; }

View file

@ -96,6 +96,8 @@ bool Window::shouldClose() const { return glfwWindowShouldClose(mContext->window
void Window::processEvents() {
glfwWaitEvents();
checkAxisUpdates();
glViewport(0, 0, mSize.x, mSize.y);
}
void Window::draw() {
@ -134,7 +136,7 @@ void Window::checkAxisUpdates() {
RectF windowRec = { { 0, 0 }, mSize };
if (windowRec.isInside(mPointerPos)) {
mEventHandler->postEvent(InputID::MOUSE1, { {}, {}, mPointerPos });
mEventHandler->postEvent(InputID::MOUSE1, { InputEvent::Type::MOUSE_POS, {}, mPointerPos });
}
}
}
@ -149,15 +151,11 @@ static void keyCallback(GLFWwindow* window, int key, int scancode, int action, i
self->checkAxisUpdates();
// post key event
/*
if (action == GLFW_PRESS) {
inputManager->keyStates[key] = GLFW_PRESS;
eventHandler->postEvent((InputID) key, { InputEvent::Type::BUTTON_ACTION, InputEvent::ButtonAction::PRESS, {} });
} else if (action == GLFW_RELEASE) {
inputManager->keyStates[key] = GLFW_RELEASE;
eventHandler->postEvent((InputID) key, { InputEvent::Type::BUTTON_ACTION, InputEvent::ButtonAction::RELEASE, {} });
}
inputManager->isEvent = true;
*/
}
static void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods) {
@ -169,9 +167,14 @@ static void mouseButtonCallback(GLFWwindow* window, int button, int action, int
self->checkAxisUpdates();
// post mouse button event
// inputManager->mouseButtonStates[button] = action;
// inputManager->isEvent = true;
auto id = (InputID) ((int) InputID::MOUSE1 + button);
if (action == GLFW_PRESS) {
eventHandler->postEvent(id, { InputEvent::Type::BUTTON_ACTION, InputEvent::ButtonAction::PRESS, {} }
);
} else if (action == GLFW_RELEASE) {
eventHandler->postEvent(id, { InputEvent::Type::BUTTON_ACTION, InputEvent::ButtonAction::RELEASE, {} });
}
}
static void scrollCallback(GLFWwindow* window, double xOffset, double yOffset) {

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]{};
};
}

View file

@ -142,8 +142,6 @@ namespace tp {
MOUSE3 = 503,
MOUSE4 = 504,
MOUSE5 = 505,
MOUSE_UP = 506,
MOUSE_DOWN = 507,
LAST_KEY_CODE = 508,