Add windowevent and buttonevent

This commit is contained in:
Sijmen 2020-05-22 22:41:52 +02:00
parent d7b69fafdd
commit a102266ffd
Signed by: vijfhoek
GPG key ID: DAF7821E067D9C48

View file

@ -312,6 +312,52 @@ pub const MouseMotionEvent = struct {
yrel: i32,
};
pub const WindowEventType = extern enum(u8) {
None = c.SDL_WINDOWEVENT_NONE,
Shown = c.SDL_WINDOWEVENT_SHOWN,
Hidden = c.SDL_WINDOWEVENT_HIDDEN,
Exposed = c.SDL_WINDOWEVENT_EXPOSED,
Moved = c.SDL_WINDOWEVENT_MOVED,
Resized = c.SDL_WINDOWEVENT_RESIZED,
SizeChanged = c.SDL_WINDOWEVENT_SIZE_CHANGED,
Minimized = c.SDL_WINDOWEVENT_MINIMIZED,
Maximized = c.SDL_WINDOWEVENT_MAXIMIZED,
Restored = c.SDL_WINDOWEVENT_RESTORED,
Enter = c.SDL_WINDOWEVENT_ENTER,
Leave = c.SDL_WINDOWEVENT_LEAVE,
FocusGained = c.SDL_WINDOWEVENT_FOCUS_GAINED,
FocusLost = c.SDL_WINDOWEVENT_FOCUS_LOST,
Close = c.SDL_WINDOWEVENT_CLOSE,
TakeFocus = c.SDL_WINDOWEVENT_TAKE_FOCUS,
HitTest = c.SDL_WINDOWEVENT_HIT_TEST,
};
pub const WindowEvent = struct {
timestamp: u32,
windowId: u32,
event: WindowEventType,
// TODO Might want to make this more specific
data1: i32,
data2: i32,
};
pub const MouseButtonState = extern enum(u8) {
Pressed = c.SDL_PRESSED,
Released = c.SDL_RELEASED,
};
pub const MouseButtonEvent = struct {
timestamp: u32,
windowId: u32,
which: u32,
button: u8,
state: MouseButtonState,
clicks: u8,
x: i32,
y: i32,
};
pub const Event = struct {
event: c.SDL_Event,
@ -344,6 +390,39 @@ pub const Event = struct {
.yrel = inner.yrel,
};
}
pub fn window(self: Event) ?WindowEvent {
if (self.kind() != .WindowEvent) {
return null;
}
var inner = self.event.window;
return WindowEvent{
.timestamp = inner.timestamp,
.windowId = inner.windowID,
.event = @intToEnum(WindowEventType, inner.event),
.data1 = inner.data1,
.data2 = inner.data2,
};
}
pub fn button(self: Event) ?MouseButtonEvent {
if (self.kind() != .MouseButtonUp and self.kind() != .MouseButtonDown) {
return null;
}
var inner = self.event.button;
return MouseButtonEvent{
.timestamp = inner.timestamp,
.windowId = inner.windowID,
.which = inner.which,
.button = inner.button,
.state = @intToEnum(MouseButtonState, inner.state),
.clicks = inner.clicks,
.x = inner.x,
.y = inner.y,
};
}
};
pub const SystemCursor = extern enum {