Skip to content
This repository has been archived by the owner on Jun 6, 2020. It is now read-only.

Accomodate new pan event changes #56

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
49 changes: 30 additions & 19 deletions src/sdl2.re
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,12 @@ module Keycode = {
let left = 1073741904;
};

module Axis = {
type t =
| Horizontal
| Vertical;
};

module WheelType = {
type t =
| Last
Expand Down Expand Up @@ -391,6 +397,21 @@ module Event = {
isFlipped: bool,
};

module PanElements = {
type t =
| Interrupt
| Fling
| Pan(float);
}

type panEvent = {
windowID: int,
timestamp: int,
source: WheelType.t,
axis: Axis.t,
action: PanElements.t,
}

type mousePan = {
windowID: int,
deltaX: int,
Expand Down Expand Up @@ -471,7 +492,7 @@ module Event = {
| WindowClosed(windowEvent)
| WindowTakeFocus(windowEvent)
| WindowHitTest(windowEvent)
| MousePan(mousePan)
| Pan(panEvent)
Copy link
Member

Choose a reason for hiding this comment

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

Is mousePan still used ?

// An event that hasn't been implemented yet
| Unknown;

Expand All @@ -488,26 +509,16 @@ module Event = {
deltaY,
isFlipped ? 1 : 0,
)
| MousePan({
| Pan({
windowID,
deltaX,
deltaY,
containsX,
containsY,
isFling,
isInterrupt,
timestamp,
action,
_,
}) =>
Printf.sprintf(
"Pan event: %d %d %d %d %d %d %d",
windowID,
deltaX,
deltaY,
if (containsX) {1} else {0},
if (containsY) {1} else {0},
if (isFling) {1} else {0},
if (isInterrupt) {1} else {0},
)
}) => switch (action) {
| Interrupt => Printf.sprintf("Interrupt event at timestamp %d", timestamp)
| Fling => Printf.sprintf("Fling event at timestamp %d", timestamp)
| Pan(delta) => Printf.sprintf("Pan event at timestamp %d with delta %f", timestamp, delta);
}
| MouseButtonUp({windowID, button, _}) =>
Printf.sprintf(
"MouseButtonUp windowId: %d button: %s",
Expand Down
40 changes: 30 additions & 10 deletions src/sdl2_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -717,17 +717,37 @@ CAMLprim value Val_SDL_Event(SDL_Event *event) {
case SDL_PANEVENT:
v = caml_alloc(1, 24);

vInner = caml_alloc(9, 0);
vInner = caml_alloc(5 * 8, 0);
Copy link
Member

Choose a reason for hiding this comment

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

5 * 8 ???? why.

Copy link
Author

Choose a reason for hiding this comment

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

me being dumb, was adjusting number of fields and forgot to change to constant

thanks for the catch

Store_field(vInner, 0, Val_int(event->window.windowID));
Store_field(vInner, 1, Val_int(event->pan.x));
Store_field(vInner, 2, Val_int(event->pan.y));
Store_field(vInner, 3, Val_bool(event->pan.contains_x));
Store_field(vInner, 4, Val_bool(event->pan.contains_y));
Store_field(vInner, 5, Val_bool(event->pan.fling));
Store_field(vInner, 6, Val_bool(event->pan.interrupt));
// verify this is the correct way of representing a ref to some WheelType.t
Store_field(vInner, 7, Val_int(event->pan.source_type));
Store_field(vInner, 8, Val_int(event->pan.timestamp));
Store_field(vInner, 1, Val_int(event->pan.timestamp));

Store_field(vInner, 2, Val_int(event->pan.source));
int axis;
if( event->pan.axis == SDL_PAN_AXIS_VERTICAL ) {
Copy link
Member

Choose a reason for hiding this comment

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

I would suggest: switch in case we add diagonal pan. And we could get exhaustivity checking with -Wswitch-enum flag.

Copy link
Author

Choose a reason for hiding this comment

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

thankfully I think a diagonal pan is always sum of vertical and horizontal, though agree a switch is more semantically clear here

axis = 0;
} else {
axis = 1;
}

Store_field(vInner, 3, Val_int(axis));

if( event->pan.pantype == SDL_PANEVENTTYPE_PAN ) {
// tagged 0 for only non-constant constructor Pan(float)
CAMLlocal2(panElement, floatElement);
panElement = caml_alloc(1, 0);

floatElement = caml_copy_double(event->pan.contents.pan.delta);

Store_field(panElement, 0, floatElement);

Store_field(vInner, 4, panElement);
} else {
if( event->pan.pantype == SDL_PANEVENTTYPE_INTERRUPT ) {
Copy link
Member

Choose a reason for hiding this comment

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

Here again a switch is better: reduce nesting. Don't omit break;

Store_field(vInner, 4, Val_int(0));
} else if( event->pan.pantype == SDL_PANEVENTTYPE_FLING ) {
Store_field(vInner, 4, Val_int(1));
}
}

Store_field(v, 0, vInner);
break;
Expand Down