SDL 2 events and related services

Introduced classes

extern class SDLEvent

sdl2 :: SDLEvent

SDL 2 event, only the data and no metadata
extern class SDLEventBuffer

sdl2 :: SDLEventBuffer

Temporary buffer for an SDL 2 event exposing the pseudo-class hierarchy metadata
extern class SDLKeyboardDownEvent

sdl2 :: SDLKeyboardDownEvent

Keyboard button click event
extern class SDLKeyboardEvent

sdl2 :: SDLKeyboardEvent

Keyboard button event
extern class SDLKeyboardUpEvent

sdl2 :: SDLKeyboardUpEvent

Keyboard button release event
extern class SDLKeysym

sdl2 :: SDLKeysym

Key information
extern class SDLMouseButtonDownEvent

sdl2 :: SDLMouseButtonDownEvent

Mouse button click event
extern class SDLMouseButtonEvent

sdl2 :: SDLMouseButtonEvent

Mouse button event
extern class SDLMouseButtonUpEvent

sdl2 :: SDLMouseButtonUpEvent

Mouse button release event
extern class SDLMouseEvent

sdl2 :: SDLMouseEvent

Mouse event
extern class SDLMouseMotionEvent

sdl2 :: SDLMouseMotionEvent

Mouse motion event
extern class SDLMouseWheelEvent

sdl2 :: SDLMouseWheelEvent

Mouse wheel event
extern class SDLQuitEvent

sdl2 :: SDLQuitEvent

Quit event, usually from the close window button
extern class SDLWindowEvent

sdl2 :: SDLWindowEvent

Window event

All class definitions

extern class SDLEvent

sdl2 $ SDLEvent

SDL 2 event, only the data and no metadata
extern class SDLEventBuffer

sdl2 $ SDLEventBuffer

Temporary buffer for an SDL 2 event exposing the pseudo-class hierarchy metadata
extern class SDLKeyboardDownEvent

sdl2 $ SDLKeyboardDownEvent

Keyboard button click event
extern class SDLKeyboardEvent

sdl2 $ SDLKeyboardEvent

Keyboard button event
extern class SDLKeyboardUpEvent

sdl2 $ SDLKeyboardUpEvent

Keyboard button release event
extern class SDLKeysym

sdl2 $ SDLKeysym

Key information
extern class SDLMouseButtonDownEvent

sdl2 $ SDLMouseButtonDownEvent

Mouse button click event
extern class SDLMouseButtonEvent

sdl2 $ SDLMouseButtonEvent

Mouse button event
extern class SDLMouseButtonUpEvent

sdl2 $ SDLMouseButtonUpEvent

Mouse button release event
extern class SDLMouseEvent

sdl2 $ SDLMouseEvent

Mouse event
extern class SDLMouseMotionEvent

sdl2 $ SDLMouseMotionEvent

Mouse motion event
extern class SDLMouseWheelEvent

sdl2 $ SDLMouseWheelEvent

Mouse wheel event
extern class SDLQuitEvent

sdl2 $ SDLQuitEvent

Quit event, usually from the close window button
extern class SDLWindowEvent

sdl2 $ SDLWindowEvent

Window event
package_diagram sdl2::events events sdl2::sdl2_base sdl2_base sdl2::events->sdl2::sdl2_base core core sdl2::sdl2_base->core ...core ... ...core->core sdl2::sdl2 sdl2 sdl2::sdl2->sdl2::events sdl2::image image sdl2::image->sdl2::sdl2 glesv2::opengles2_hello_triangle opengles2_hello_triangle glesv2::opengles2_hello_triangle->sdl2::sdl2 sdl2::image... ... sdl2::image...->sdl2::image glesv2::opengles2_hello_triangle... ... glesv2::opengles2_hello_triangle...->glesv2::opengles2_hello_triangle

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module array

core :: array

This module introduces the standard array structure.
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module math

core :: math

Mathematical operations
module native

core :: native

Native structures for text and bytes
module numeric

core :: numeric

Advanced services for Numeric types
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O

Parents

module sdl2_base

sdl2 :: sdl2_base

Basic SDL 2 features

Children

module sdl2

sdl2 :: sdl2

Simple DirectMedia Layer (SDL) 2.0 services for easy window creation and 2D drawing

Descendants

module a_star-m

a_star-m

module all

sdl2 :: all

Unites the main sdl2 module and its sister library sdl2::image
module camera_control_linux

gamnit :: camera_control_linux

Mouse wheel and middle mouse button to control camera
module display_linux

gamnit :: display_linux

Gamnit display implementation for GNU/Linux using egl, sdl and x11
module gamnit_linux

gamnit :: gamnit_linux

Support services for Gamnit on GNU/Linux
module image

sdl2 :: image

Services of the SDL_image 2.0 library
module minimal

sdl2 :: minimal

An example to test and demonstrate the sdl2 lib with image and events
module opengles2_hello_triangle

glesv2 :: opengles2_hello_triangle

Basic example of OpenGL ES 2.0 usage using SDL 2
# SDL 2 events and related services
module events is pkgconfig "sdl2"

import sdl2_base

in "C Header" `{
	#include <SDL2/SDL_events.h>
`}

# Temporary buffer for an SDL 2 event exposing the pseudo-class hierarchy metadata
#
# An instance of this class should be used to call `poll_event` and `to_event`.
extern class SDLEventBuffer `{SDL_Event *`}
	# Allocate memory for a new `SDLEventBuffer`
	new malloc `{ return malloc(sizeof(SDL_Event)); `}

	# Poll and event into `self`
	#
	# Returns `true` if an event was available.
	fun poll_event: Bool `{ return SDL_PollEvent(self); `}

	# Get a reference to the data at `self` as a precise `SDLEvent`
	#
	# Returns `null` if the event is unknown.
	#
	# Note: The returned `SDLEvent` is just a different Nit instance pointing to the same data.
	# A call to `poll_event` will invalidate any instances returned previously by `to_event`.
	fun to_event: SDLEvent
	do
		if is_quit then return to_quit
		if is_mouse_motion then return to_mouse_motion
		if is_mouse_button_down then return to_mouse_button_down
		if is_mouse_button_up then return to_mouse_button_up
		if is_mouse_wheel then return to_mouse_wheel
		if is_keydown then return to_keydown
		if is_keyup then return to_keyup
		if is_window then return to_window
		return to_event_direct
	end

	private fun to_event_direct: SDLEvent `{ return self; `}

	# Is this a quit event?
	fun is_quit: Bool `{ return self->type == SDL_QUIT; `}

	# Get a reference to data at `self` as a `SDLQuitEvent`
	#
	# Require: `is_quit`
	fun to_quit: SDLQuitEvent `{ return self; `}

	# Is this a mouse motion event?
	fun is_mouse_motion: Bool `{ return self->type == SDL_MOUSEMOTION; `}

	# Get a reference to data at `self` as a `SDLMouseMotionEvent`
	#
	# Require: `is_mouse_motion`
	fun to_mouse_motion: SDLMouseMotionEvent `{ return self; `}

	# Is this a mouse button down event?
	fun is_mouse_button_down: Bool `{ return self->type == SDL_MOUSEBUTTONDOWN; `}

	# Get a reference to data at `self` as a `SDLMouseButtonDownEvent`
	#
	# Require: `is_mouse_button_down`
	fun to_mouse_button_down: SDLMouseButtonDownEvent `{ return self; `}

	# Is this a mouse button up event?
	fun is_mouse_button_up: Bool `{ return self->type == SDL_MOUSEBUTTONUP; `}

	# Get a reference to data at `self` as a `SDLMouseButtonUpEvent`
	#
	# Require: `is_mouse_button_up`
	fun to_mouse_button_up: SDLMouseButtonUpEvent `{ return self; `}

	# Is this a mouse wheel event?
	fun is_mouse_wheel: Bool `{ return self->type == SDL_MOUSEWHEEL; `}

	# Get a reference to data at `self` as a `SDLMouseWheelEvent`
	#
	# Require: `is_mouse_wheel`
	fun to_mouse_wheel: SDLMouseWheelEvent `{ return self; `}

	# Is this a key presse event?
	fun is_keydown: Bool `{ return self->type == SDL_KEYDOWN; `}

	# Get a reference to data at `self` as a `SDLKeyboardDownEvent`
	#
	# Require: `is_keydown`
	fun to_keydown: SDLKeyboardDownEvent `{ return self; `}

	#  Is this a key release event?
	fun is_keyup: Bool `{ return self->type == SDL_KEYUP; `}

	# Get a reference to data at `self` as a `SDLKeyboardUpEvent`
	#
	# Require: `is_keyup`
	fun to_keyup: SDLKeyboardUpEvent `{ return self; `}

	#  Is this a window event?
	fun is_window: Bool `{ return self->type == SDL_WINDOWEVENT; `}

	# Get a reference to data at `self` as a `SDLWindowEvent`
	#
	# Require: `is_window`
	fun to_window: SDLWindowEvent `{ return self; `}

	# TODO other SDL events:
	#
	# SDL_CommonEvent common
	# SDL_WindowEvent window
	# SDL_TextEditingEvent edit
	# SDL_TextInputEvent text
	# SDL_JoyAxisEvent jaxis
	# SDL_JoyBallEvent jball
	# SDL_JoyHatEvent jhat;
	# SDL_JoyButtonEvent jbutton
	# SDL_JoyDeviceEvent jdevice
	# SDL_ControllerAxisEvent caxis
	# SDL_ControllerButtonEvent cbutton
	# SDL_ControllerDeviceEvent cdevice
	# SDL_UserEvent user
	# SDL_SysWMEvent syswm
	# SDL_TouchFingerEvent tfinger
	# SDL_MultiGestureEvent mgesture
	# SDL_DollarGestureEvent dgesture
	# SDL_DropEvent drop
end

# SDL 2 event, only the data and no metadata
extern class SDLEvent `{SDL_Event *`}
end

# Quit event, usually from the close window button
extern class SDLQuitEvent
	super SDLEvent
end

# Mouse event
extern class SDLMouseEvent
	super SDLEvent

	# Implementation note
	#
	# Even if the structures are different between the mouse events, the first
	# four fields of each events are common to all of them.

	# Which mouse, pointer or finger raised this event
	fun which: Int `{ return self->motion.which; `}

	# X coordinate on screen of this event
	fun x: Int is abstract

	# Y coordinate on screen of this event
	fun y: Int is abstract
end

# Mouse motion event
extern class SDLMouseMotionEvent
	super SDLMouseEvent

	redef fun x `{ return self->motion.x; `}

	redef fun y `{ return self->motion.y; `}

	# State of the buttons
	#
	# ~~~raw
	# state & 1 == 1 -> left button down
	# state & 2 == 2 -> middle button down
	# state & 4 == 4 -> right button down
	# ~~~
	fun state: Int `{ return self->motion.state; `}

	# Difference on the X axis between this event and the previous one
	fun xrel: Int `{ return self->motion.xrel; `}

	# Difference on the Y axis between this event and the previous one
	fun yrel: Int `{ return self->motion.yrel; `}
end

# Mouse button event
#
# This could as well be an abstract class. All instances of `SDLMouseButtonEvent`
# is either a `SDLMouseButtonUpEvent` or a `SDLMouseButtonDownEvent`.
extern class SDLMouseButtonEvent
	super SDLMouseEvent

	# Index of the button
	#
	# ~~~raw
	# 1 -> left button
	# 2 -> center button
	# 3 -> right button
	# ~~~
	fun button: Int `{ return self->button.button; `}

	# Is the button currently pressed down?
	fun pressed: Bool `{ return self->button.state == SDL_PRESSED; `}

	# Number of clicks (1 or 2)
	fun clicks: Int `{ return self->button.clicks; `}

	redef fun x `{ return self->button.x; `}

	redef fun y `{ return self->button.y; `}
end

# Mouse button release event
extern class SDLMouseButtonUpEvent
	super SDLMouseButtonEvent
end

# Mouse button click event
extern class SDLMouseButtonDownEvent
	super SDLMouseButtonEvent
end

# Mouse wheel event
extern class SDLMouseWheelEvent
	super SDLEvent

	# Horizontal scroll amount
	fun x: Int `{ return self->wheel.x; `}

	# Vertical scroll amount
	fun y: Int `{ return self->wheel.y; `}
end

# Keyboard button event
extern class SDLKeyboardEvent
	super SDLEvent

	# Is this is a key repeat?
	fun repeat: Bool `{ return self->key.repeat; `}

	# The key that was pressed or released
	fun keysym: SDLKeysym `{ return &self->key.keysym; `}

	redef fun to_s do return native_to_s.to_s
	private fun native_to_s: CString `{
		return (char*)SDL_GetKeyName(self->key.keysym.sym);
	`}
end

# Keyboard button release event
extern class SDLKeyboardUpEvent
	super SDLKeyboardEvent
end

# Keyboard button click event
extern class SDLKeyboardDownEvent
	super SDLKeyboardEvent
end

# Key information
extern class SDLKeysym `{ SDL_Keysym * `}
	# Is this the arrow right key?
	fun is_right: Bool `{ return self->sym == SDLK_RIGHT; `}

	# Is this the arrow left key?
	fun is_left: Bool `{ return self->sym == SDLK_LEFT; `}

	# Is this the arrow down key?
	fun is_down: Bool `{ return self->sym == SDLK_DOWN; `}

	# Is this the arrow up key?
	fun is_up: Bool `{ return self->sym == SDLK_UP; `}

	# Modification keys
	fun mod: Int `{ return self->mod; `}
	# TODO related masks
end

# Window event
extern class SDLWindowEvent
	super SDLEvent

	# Window identifier
	fun id: Int `{ return self->window.windowID; `}

	# Is `self` a resized event?
	fun is_resized: Bool `{ return self->window.event == SDL_WINDOWEVENT_RESIZED; `}

	# Is `self` a size changed event?
	fun is_size_changed: Bool `{ return self->window.event == SDL_WINDOWEVENT_SIZE_CHANGED; `}

	# `data1` field, depends on the event kind
	fun data1: Int `{ return self->window.data1; `}

	# `data2` field, depends on the event kind
	fun data2: Int `{ return self->window.data2; `}
end
lib/sdl2/events.nit:17,1--308,3