2018-09-22 14:23:26 +00:00
|
|
|
package allg5
|
2017-10-03 18:38:09 +00:00
|
|
|
|
|
|
|
// #include <allegro5/allegro.h>
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
2018-02-13 19:59:55 +00:00
|
|
|
"errors"
|
2018-02-10 08:14:19 +00:00
|
|
|
"unsafe"
|
2017-10-03 18:38:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type EventQueue struct {
|
2018-02-10 08:14:19 +00:00
|
|
|
queue *C.ALLEGRO_EVENT_QUEUE
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Event interface {
|
2018-02-10 08:14:19 +00:00
|
|
|
Stamp() float64
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type EventBase struct {
|
2018-02-10 08:14:19 +00:00
|
|
|
stamp float64
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (eb EventBase) Stamp() float64 {
|
2018-02-10 08:14:19 +00:00
|
|
|
return eb.stamp
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DisplayCloseEvent struct {
|
2018-02-10 08:14:19 +00:00
|
|
|
EventBase
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 18:49:10 +00:00
|
|
|
type DisplayResizeEvent struct {
|
|
|
|
EventBase
|
|
|
|
X, Y int
|
|
|
|
Width int
|
|
|
|
Height int
|
|
|
|
}
|
|
|
|
|
|
|
|
type DisplayOrientation int
|
|
|
|
|
|
|
|
const (
|
|
|
|
DisplayOrientation0Degrees DisplayOrientation = iota
|
|
|
|
DisplayOrientation90Degrees
|
|
|
|
DisplayOrientation180Degrees
|
|
|
|
DisplayOrientation270Degrees
|
|
|
|
DisplayOrientationFaceUp
|
|
|
|
DisplayOrientationFaceDown
|
|
|
|
)
|
|
|
|
|
|
|
|
func toDisplayOrientation(o C.int) DisplayOrientation {
|
|
|
|
switch o {
|
|
|
|
case C.ALLEGRO_DISPLAY_ORIENTATION_0_DEGREES:
|
|
|
|
return DisplayOrientation0Degrees
|
|
|
|
case C.ALLEGRO_DISPLAY_ORIENTATION_90_DEGREES:
|
|
|
|
return DisplayOrientation90Degrees
|
|
|
|
case C.ALLEGRO_DISPLAY_ORIENTATION_180_DEGREES:
|
|
|
|
return DisplayOrientation180Degrees
|
|
|
|
case C.ALLEGRO_DISPLAY_ORIENTATION_270_DEGREES:
|
|
|
|
return DisplayOrientation270Degrees
|
|
|
|
case C.ALLEGRO_DISPLAY_ORIENTATION_FACE_UP:
|
|
|
|
return DisplayOrientationFaceUp
|
|
|
|
case C.ALLEGRO_DISPLAY_ORIENTATION_FACE_DOWN:
|
|
|
|
return DisplayOrientationFaceDown
|
|
|
|
default:
|
|
|
|
panic("not supported")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type DisplayOrientationEvent struct {
|
|
|
|
EventBase
|
|
|
|
Orientation DisplayOrientation
|
|
|
|
}
|
|
|
|
|
2017-10-03 18:38:09 +00:00
|
|
|
type KeyEvent struct {
|
2018-02-10 08:14:19 +00:00
|
|
|
EventBase
|
2018-02-15 20:06:04 +00:00
|
|
|
KeyCode Key
|
2018-02-10 08:14:19 +00:00
|
|
|
Display *Display
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type KeyCharEvent struct {
|
2018-02-10 08:14:19 +00:00
|
|
|
KeyEvent
|
|
|
|
UnicodeCharacter rune
|
2018-02-15 20:06:04 +00:00
|
|
|
Modifiers KeyMod
|
2018-02-10 08:14:19 +00:00
|
|
|
Repeat bool
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type KeyDownEvent struct {
|
2018-02-10 08:14:19 +00:00
|
|
|
KeyEvent
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type KeyUpEvent struct {
|
2018-02-10 08:14:19 +00:00
|
|
|
KeyEvent
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MouseButtonDownEvent struct {
|
2018-02-10 08:14:19 +00:00
|
|
|
MouseEvent
|
2018-03-29 15:21:01 +00:00
|
|
|
Button MouseButton
|
2018-02-10 08:14:19 +00:00
|
|
|
Pressure float32
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MouseButtonUpEvent struct {
|
2018-02-10 08:14:19 +00:00
|
|
|
MouseEvent
|
2018-03-29 15:21:01 +00:00
|
|
|
Button MouseButton
|
2018-02-10 08:14:19 +00:00
|
|
|
Pressure float32
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MouseEvent struct {
|
2018-02-10 08:14:19 +00:00
|
|
|
EventBase
|
|
|
|
X, Y int
|
|
|
|
Z, W int
|
|
|
|
Display *Display
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MouseMoveEvent struct {
|
2018-02-10 08:14:19 +00:00
|
|
|
MouseEvent
|
|
|
|
DeltaX, DeltaY int
|
|
|
|
DeltaZ, DeltaW int
|
|
|
|
Pressure float32
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewEventQueue() (*EventQueue, error) {
|
2018-02-10 08:14:19 +00:00
|
|
|
q := C.al_create_event_queue()
|
|
|
|
if nil == q {
|
2018-02-13 19:59:55 +00:00
|
|
|
return nil, errors.New("unable to create event queue")
|
2018-02-10 08:14:19 +00:00
|
|
|
}
|
|
|
|
return &EventQueue{q}, nil
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (eq *EventQueue) register(source *C.ALLEGRO_EVENT_SOURCE) {
|
2018-02-10 08:14:19 +00:00
|
|
|
C.al_register_event_source(eq.queue, source)
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (eq *EventQueue) RegisterDisplay(d *Display) {
|
2018-02-10 08:14:19 +00:00
|
|
|
eq.register(C.al_get_display_event_source(d.display))
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (eq *EventQueue) RegisterMouse() {
|
2018-02-10 08:14:19 +00:00
|
|
|
eq.register(C.al_get_mouse_event_source())
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (eq *EventQueue) RegisterKeyboard() {
|
2018-02-10 08:14:19 +00:00
|
|
|
eq.register(C.al_get_keyboard_event_source())
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (eq *EventQueue) mapEvent(e *C.ALLEGRO_EVENT) Event {
|
2018-02-10 08:14:19 +00:00
|
|
|
any := (*C.ALLEGRO_ANY_EVENT)(unsafe.Pointer(e))
|
|
|
|
eb := EventBase{float64(any.timestamp)}
|
|
|
|
switch any._type {
|
|
|
|
case C.ALLEGRO_EVENT_DISPLAY_CLOSE:
|
2018-02-12 18:49:10 +00:00
|
|
|
return &DisplayCloseEvent{eb}
|
|
|
|
case C.ALLEGRO_EVENT_DISPLAY_ORIENTATION:
|
|
|
|
display := (*C.ALLEGRO_DISPLAY_EVENT)(unsafe.Pointer(e))
|
|
|
|
return &DisplayOrientationEvent{eb, toDisplayOrientation(display.orientation)}
|
|
|
|
case C.ALLEGRO_EVENT_DISPLAY_RESIZE:
|
|
|
|
display := (*C.ALLEGRO_DISPLAY_EVENT)(unsafe.Pointer(e))
|
2018-02-13 19:59:55 +00:00
|
|
|
C.al_acknowledge_resize(display.source)
|
2018-02-12 18:49:10 +00:00
|
|
|
return &DisplayResizeEvent{eb, int(display.x), int(display.y), int(display.width), int(display.height)}
|
2018-02-10 08:14:19 +00:00
|
|
|
case C.ALLEGRO_EVENT_MOUSE_AXES:
|
|
|
|
mouse := (*C.ALLEGRO_MOUSE_EVENT)(unsafe.Pointer(e))
|
|
|
|
return &MouseMoveEvent{MouseEvent{eb, int(mouse.x), int(mouse.y), int(mouse.z), int(mouse.w), nil}, int(mouse.dx), int(mouse.dy), int(mouse.dz), int(mouse.dw), float32(mouse.pressure)}
|
|
|
|
case C.ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
|
|
|
|
mouse := (*C.ALLEGRO_MOUSE_EVENT)(unsafe.Pointer(e))
|
2018-03-29 15:21:01 +00:00
|
|
|
return &MouseButtonDownEvent{MouseEvent{eb, int(mouse.x), int(mouse.y), int(mouse.z), int(mouse.w), nil}, MouseButton(mouse.button), float32(mouse.pressure)}
|
2018-02-10 08:14:19 +00:00
|
|
|
case C.ALLEGRO_EVENT_MOUSE_BUTTON_UP:
|
|
|
|
mouse := (*C.ALLEGRO_MOUSE_EVENT)(unsafe.Pointer(e))
|
2018-03-29 15:21:01 +00:00
|
|
|
return &MouseButtonUpEvent{MouseEvent{eb, int(mouse.x), int(mouse.y), int(mouse.z), int(mouse.w), nil}, MouseButton(mouse.button), float32(mouse.pressure)}
|
2018-02-10 08:14:19 +00:00
|
|
|
case C.ALLEGRO_EVENT_KEY_DOWN:
|
|
|
|
key := (*C.ALLEGRO_KEYBOARD_EVENT)(unsafe.Pointer(e))
|
2018-02-15 20:06:04 +00:00
|
|
|
return &KeyDownEvent{KeyEvent{eb, Key(key.keycode), nil}}
|
2018-02-10 08:14:19 +00:00
|
|
|
case C.ALLEGRO_EVENT_KEY_UP:
|
|
|
|
key := (*C.ALLEGRO_KEYBOARD_EVENT)(unsafe.Pointer(e))
|
2018-02-15 20:06:04 +00:00
|
|
|
return &KeyUpEvent{KeyEvent{eb, Key(key.keycode), nil}}
|
2018-02-10 08:14:19 +00:00
|
|
|
case C.ALLEGRO_EVENT_KEY_CHAR:
|
|
|
|
key := (*C.ALLEGRO_KEYBOARD_EVENT)(unsafe.Pointer(e))
|
2018-02-15 20:06:04 +00:00
|
|
|
return &KeyCharEvent{KeyEvent{eb, Key(key.keycode), nil}, rune(key.unichar), KeyMod(key.modifiers), bool(key.repeat)}
|
2018-02-10 08:14:19 +00:00
|
|
|
}
|
|
|
|
return nil
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (eq *EventQueue) Get() Event {
|
2018-02-10 08:14:19 +00:00
|
|
|
var event C.ALLEGRO_EVENT
|
|
|
|
if !bool(C.al_get_next_event(eq.queue, &event)) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return eq.mapEvent(&event)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (eq *EventQueue) GetWait() Event {
|
|
|
|
var event C.ALLEGRO_EVENT
|
|
|
|
C.al_wait_for_event(eq.queue, &event)
|
|
|
|
return eq.mapEvent(&event)
|
2017-10-03 18:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (eq *EventQueue) Destroy() {
|
2018-02-10 08:14:19 +00:00
|
|
|
C.al_destroy_event_queue(eq.queue)
|
|
|
|
}
|