Skip to content

Commit

Permalink
Merge pull request #18 from chances/input
Browse files Browse the repository at this point in the history
  • Loading branch information
chances committed Feb 3, 2021
2 parents 44ada4f + 62c1731 commit 926e3d2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
43 changes: 42 additions & 1 deletion source/teraflop/input/event.d
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ enum InputDevice {
abstract class InputEvent {
///
const InputDevice device;
private bool _handled = false;

///
this(const InputDevice device) {
Expand All @@ -40,6 +41,12 @@ abstract class InputEvent {
return other && device == other.device;
}

/// Whether this `InputEvent` has been handled.
/// See_Also: `stopPropagation`
bool handled() @property const {
return _handled;
}

bool isKeyboardEvent() @property const {
return typeid(InputEventKeyboard).isBaseOf(this.classinfo);
}
Expand Down Expand Up @@ -75,6 +82,12 @@ abstract class InputEvent {
}
return null;
}

/// Mark this `InputEvent` as handled, stopping propagation through the input tree.
/// See_Also: `handled`
void stopPropagation() {
_handled = true;
}
}

// TODO: Joypad input enums: https://github.com/BindBC/bindbc-glfw/blob/5bed82e7bdd18afb0e810aeb173e11d38e18075b/source/bindbc/glfw/types.d#L229-L283
Expand All @@ -92,7 +105,7 @@ class InputEventKeyboard : InputEvent {
const int modifiers = 0;

///
this(const KeyboardKey key, bool pressed, bool held, int modifiers) {
this(const KeyboardKey key, bool pressed, bool held, int modifiers = 0) {
super(InputDevice.keyboard);
this.key = key;
this.pressed = pressed && !held;
Expand All @@ -101,6 +114,18 @@ class InputEventKeyboard : InputEvent {
}
}

unittest {
auto event = new InputEventKeyboard(KeyboardKey.enter, true, false);

assert(event.device == InputDevice.keyboard);
assert(!event.handled);
assert(event.isKeyboardEvent);
assert(event.asKeyboardEvent == event);

event.stopPropagation();
assert(event.handled);
}

///
enum MouseButton {
///
Expand Down Expand Up @@ -161,16 +186,23 @@ class InputEventMouse : InputEvent {
unittest {
auto event = new InputEventMouse(vec2d.init, vec2d.init);

assert(event.device == InputDevice.mouse);
assert(event.isMouseEvent);
assert(event.asMouseEvent == event);

assert((event.buttons & MouseButton.LEFT) != MouseButton.LEFT);
assert((event.buttons & MouseButton.RIGHT) != MouseButton.RIGHT);
assert((event.buttons & MouseButton.MIDDLE) != MouseButton.MIDDLE);

event.buttons |= MouseButton.RIGHT;
assert(!event.wasButtonJustClicked(MouseButton.LEFT));
assert(event.wasButtonJustPressed(MouseButton.RIGHT));
assert((event.buttons & MouseButton.LEFT) != MouseButton.LEFT);
assert((event.buttons & MouseButton.RIGHT) == MouseButton.RIGHT);
assert((event.buttons & MouseButton.MIDDLE) != MouseButton.MIDDLE);

event.buttons |= MouseButton.MIDDLE;
assert(event.wasButtonJustPressed(MouseButton.MIDDLE));
assert((event.buttons & MouseButton.LEFT) != MouseButton.LEFT);
assert((event.buttons & MouseButton.RIGHT) == MouseButton.RIGHT);
assert((event.buttons & MouseButton.MIDDLE) == MouseButton.MIDDLE);
Expand Down Expand Up @@ -208,3 +240,12 @@ class InputEventAction : InputEvent {
return other && device == other.device && action == other.action;
}
}

unittest {
auto event = new InputEventAction(InputDevice.keyboard, "cancel");
event.pressed = true;

assert(event.device == InputDevice.keyboard);
assert(event.isActionEvent);
assert(event.asActionEvent == event);
}
19 changes: 9 additions & 10 deletions source/teraflop/input/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ final class Input {

foreach (handlers; nodes) {
if (actionEvents.length) foreach (actionEvent; actionEvents) handlers.actionHandler(actionEvent);
if (handlers.unhandledHandler(event)) break;
if (handlers.unhandledHandler(event) || event.handled) break;
}
}

Expand All @@ -127,15 +127,14 @@ final class Input {
}

/// A node in the input event tree.
abstract class InputNode {
interface InputNode {
///
void actionInput(const InputEventAction event) {
assert(event.action.length);
}
void actionInput(const InputEventAction event);

///
bool unhandledInput(const InputEvent event) {
assert(event.device);
return false;
}
/// Returns: Whether the mark the given input `event` as handled, stopping propagation through the input tree.
bool unhandledInput(const InputEvent event);
}

unittest {
// TODO: Test an event tree
}
2 changes: 1 addition & 1 deletion source/teraflop/platform/window.d
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class Window : InputNode {

override bool unhandledInput(const InputEvent event) {
onUnhandledInput(event);
return false;
return true; // Mark handled
}

extern(C) {
Expand Down

0 comments on commit 926e3d2

Please sign in to comment.