Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions latebit/core/events/EventInput.cpp

This file was deleted.

26 changes: 16 additions & 10 deletions latebit/core/events/EventInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,29 @@ class EventInput : public Event {
const InputAction::InputAction action = InputAction::UNDEFINED_ACTION;
// The event type
const string type = INPUT_EVENT;
// If true, the event is a repeat of a previous event. Used to detect long
// pressed, for example
const bool repeat = false;

public:
EventInput()
: Event(INPUT_EVENT),
key(InputKey::UNDEFINED_KEY),
action(InputAction::UNDEFINED_ACTION) {};
EventInput() : Event(INPUT_EVENT){};

// Create input event with an InputKey representing the control being acted
// on, and an action representing the action on the given control
EventInput(InputKey::InputKey k, InputAction::InputAction a)
: Event(INPUT_EVENT), key(k), action(a) {};
// Create input event with an InputKey representing the button being pressed,
// whether it's a press or a release, and if it's repeated over time
EventInput(InputKey::InputKey k, InputAction::InputAction a,
bool repeat = false)
: Event(INPUT_EVENT), key(k), action(a), repeat(repeat){};

// Return the key value for this event
[[nodiscard]] auto getKey() const -> InputKey::InputKey;
[[nodiscard]] auto getKey() const -> InputKey::InputKey { return key; }

// Return the action for this event
[[nodiscard]] auto getAction() const -> InputAction::InputAction;
[[nodiscard]] auto getAction() const -> InputAction::InputAction {
return action;
}

// Return true if the event is repeated
[[nodiscard]] auto getRepeat() const -> bool { return repeat; }
};

} // namespace lb
3 changes: 2 additions & 1 deletion latebit/core/events/EventInput.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ auto main() -> int {
});

test("constructor with parameters", []() {
EventInput event(InputKey::A, InputAction::PRESSED);
EventInput event(InputKey::A, InputAction::PRESSED, true);
assertEq("sets correct key", event.getKey(), InputKey::A);
assertEq("sets correct keyboard action", event.getAction(),
InputAction::PRESSED);
assertEq("sets correct repeat", event.getRepeat(), true);
});

return report();
Expand Down
11 changes: 7 additions & 4 deletions latebit/core/input/InputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,26 @@ auto InputManager::isValid(string eventType) const -> bool {

void InputManager::getInput() const {
SDL_Event event;
auto lastKey = InputKey::UNDEFINED_KEY;

while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN: {
const auto key = fromSDLKeyCode(event.key.keysym.sym);
if (key == InputKey::UNDEFINED_KEY) return;
if (key == InputKey::UNDEFINED_KEY) break;

const auto evt = EventInput(key, InputAction::PRESSED);
const auto evt = EventInput(key, InputAction::PRESSED, event.key.repeat != 0 && key == lastKey);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be too opinionated. It does not seem to yield the expected results in test game, needs revisiting

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifically, the key == lastKey seems brittle

broadcast(&evt);
lastKey = key;
break;
}
case SDL_KEYUP: {
const auto key = fromSDLKeyCode(event.key.keysym.sym);
if (key == InputKey::UNDEFINED_KEY) return;
if (key == InputKey::UNDEFINED_KEY) break;

const auto evt = EventInput(key, InputAction::RELEASED);
const auto evt = EventInput(key, InputAction::RELEASED, event.key.repeat != 0 && key == lastKey);
broadcast(&evt);
lastKey = key;
break;
}
case SDL_QUIT:
Expand Down