Applied formatting & added GetWait().
- GetWait waits for an event in a blocking way.
This commit is contained in:
parent
4d98c63391
commit
ab5572018d
@ -4,137 +4,143 @@ package allegro5
|
|||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
type EventQueue struct {
|
type EventQueue struct {
|
||||||
queue *C.ALLEGRO_EVENT_QUEUE
|
queue *C.ALLEGRO_EVENT_QUEUE
|
||||||
}
|
}
|
||||||
|
|
||||||
type Event interface {
|
type Event interface {
|
||||||
Stamp() float64
|
Stamp() float64
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventBase struct {
|
type EventBase struct {
|
||||||
stamp float64
|
stamp float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (eb EventBase) Stamp() float64 {
|
func (eb EventBase) Stamp() float64 {
|
||||||
return eb.stamp
|
return eb.stamp
|
||||||
}
|
}
|
||||||
|
|
||||||
type DisplayCloseEvent struct {
|
type DisplayCloseEvent struct {
|
||||||
EventBase
|
EventBase
|
||||||
}
|
}
|
||||||
|
|
||||||
type KeyEvent struct {
|
type KeyEvent struct {
|
||||||
EventBase
|
EventBase
|
||||||
KeyCode int
|
KeyCode int
|
||||||
Display *Display
|
Display *Display
|
||||||
}
|
}
|
||||||
|
|
||||||
type KeyCharEvent struct {
|
type KeyCharEvent struct {
|
||||||
KeyEvent
|
KeyEvent
|
||||||
UnicodeCharacter rune
|
UnicodeCharacter rune
|
||||||
Modifiers uint
|
Modifiers uint
|
||||||
Repeat bool
|
Repeat bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type KeyDownEvent struct {
|
type KeyDownEvent struct {
|
||||||
KeyEvent
|
KeyEvent
|
||||||
}
|
}
|
||||||
|
|
||||||
type KeyUpEvent struct {
|
type KeyUpEvent struct {
|
||||||
KeyEvent
|
KeyEvent
|
||||||
}
|
}
|
||||||
|
|
||||||
type MouseButtonDownEvent struct {
|
type MouseButtonDownEvent struct {
|
||||||
MouseEvent
|
MouseEvent
|
||||||
Button uint
|
Button uint
|
||||||
Pressure float32
|
Pressure float32
|
||||||
}
|
}
|
||||||
|
|
||||||
type MouseButtonUpEvent struct {
|
type MouseButtonUpEvent struct {
|
||||||
MouseEvent
|
MouseEvent
|
||||||
Button uint
|
Button uint
|
||||||
Pressure float32
|
Pressure float32
|
||||||
}
|
}
|
||||||
|
|
||||||
type MouseEvent struct {
|
type MouseEvent struct {
|
||||||
EventBase
|
EventBase
|
||||||
X, Y int
|
X, Y int
|
||||||
Z, W int
|
Z, W int
|
||||||
Display *Display
|
Display *Display
|
||||||
}
|
}
|
||||||
|
|
||||||
type MouseMoveEvent struct {
|
type MouseMoveEvent struct {
|
||||||
MouseEvent
|
MouseEvent
|
||||||
DeltaX, DeltaY int
|
DeltaX, DeltaY int
|
||||||
DeltaZ, DeltaW int
|
DeltaZ, DeltaW int
|
||||||
Pressure float32
|
Pressure float32
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEventQueue() (*EventQueue, error) {
|
func NewEventQueue() (*EventQueue, error) {
|
||||||
q := C.al_create_event_queue()
|
q := C.al_create_event_queue()
|
||||||
if nil == q {
|
if nil == q {
|
||||||
return nil, fmt.Errorf("Unable to create event queue")
|
return nil, fmt.Errorf("Unable to create event queue")
|
||||||
}
|
}
|
||||||
return &EventQueue{q}, nil
|
return &EventQueue{q}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (eq *EventQueue) register(source *C.ALLEGRO_EVENT_SOURCE) {
|
func (eq *EventQueue) register(source *C.ALLEGRO_EVENT_SOURCE) {
|
||||||
C.al_register_event_source(eq.queue, source)
|
C.al_register_event_source(eq.queue, source)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (eq *EventQueue) RegisterDisplay(d *Display) {
|
func (eq *EventQueue) RegisterDisplay(d *Display) {
|
||||||
eq.register(C.al_get_display_event_source(d.display))
|
eq.register(C.al_get_display_event_source(d.display))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (eq *EventQueue) RegisterMouse() {
|
func (eq *EventQueue) RegisterMouse() {
|
||||||
eq.register(C.al_get_mouse_event_source())
|
eq.register(C.al_get_mouse_event_source())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (eq *EventQueue) RegisterKeyboard() {
|
func (eq *EventQueue) RegisterKeyboard() {
|
||||||
eq.register(C.al_get_keyboard_event_source())
|
eq.register(C.al_get_keyboard_event_source())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (eq *EventQueue) mapEvent(e *C.ALLEGRO_EVENT) Event {
|
func (eq *EventQueue) mapEvent(e *C.ALLEGRO_EVENT) Event {
|
||||||
any := (*C.ALLEGRO_ANY_EVENT)(unsafe.Pointer(e))
|
any := (*C.ALLEGRO_ANY_EVENT)(unsafe.Pointer(e))
|
||||||
eb := EventBase{float64(any.timestamp)}
|
eb := EventBase{float64(any.timestamp)}
|
||||||
switch any._type {
|
switch any._type {
|
||||||
case C.ALLEGRO_EVENT_DISPLAY_CLOSE:
|
case C.ALLEGRO_EVENT_DISPLAY_CLOSE:
|
||||||
return &DisplayCloseEvent{EventBase{float64(any.timestamp)}}
|
return &DisplayCloseEvent{EventBase{float64(any.timestamp)}}
|
||||||
case C.ALLEGRO_EVENT_MOUSE_AXES:
|
case C.ALLEGRO_EVENT_MOUSE_AXES:
|
||||||
mouse := (*C.ALLEGRO_MOUSE_EVENT)(unsafe.Pointer(e))
|
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)}
|
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:
|
case C.ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
|
||||||
mouse := (*C.ALLEGRO_MOUSE_EVENT)(unsafe.Pointer(e))
|
mouse := (*C.ALLEGRO_MOUSE_EVENT)(unsafe.Pointer(e))
|
||||||
return &MouseButtonDownEvent{MouseEvent{eb, int(mouse.x), int(mouse.y), int(mouse.z), int(mouse.w), nil}, uint(mouse.button), float32(mouse.pressure)}
|
return &MouseButtonDownEvent{MouseEvent{eb, int(mouse.x), int(mouse.y), int(mouse.z), int(mouse.w), nil}, uint(mouse.button), float32(mouse.pressure)}
|
||||||
case C.ALLEGRO_EVENT_MOUSE_BUTTON_UP:
|
case C.ALLEGRO_EVENT_MOUSE_BUTTON_UP:
|
||||||
mouse := (*C.ALLEGRO_MOUSE_EVENT)(unsafe.Pointer(e))
|
mouse := (*C.ALLEGRO_MOUSE_EVENT)(unsafe.Pointer(e))
|
||||||
return &MouseButtonUpEvent{MouseEvent{eb, int(mouse.x), int(mouse.y), int(mouse.z), int(mouse.w), nil}, uint(mouse.button), float32(mouse.pressure)}
|
return &MouseButtonUpEvent{MouseEvent{eb, int(mouse.x), int(mouse.y), int(mouse.z), int(mouse.w), nil}, uint(mouse.button), float32(mouse.pressure)}
|
||||||
case C.ALLEGRO_EVENT_KEY_DOWN:
|
case C.ALLEGRO_EVENT_KEY_DOWN:
|
||||||
key := (*C.ALLEGRO_KEYBOARD_EVENT)(unsafe.Pointer(e))
|
key := (*C.ALLEGRO_KEYBOARD_EVENT)(unsafe.Pointer(e))
|
||||||
return &KeyDownEvent{KeyEvent{eb, int(key.keycode), nil}}
|
return &KeyDownEvent{KeyEvent{eb, int(key.keycode), nil}}
|
||||||
case C.ALLEGRO_EVENT_KEY_UP:
|
case C.ALLEGRO_EVENT_KEY_UP:
|
||||||
key := (*C.ALLEGRO_KEYBOARD_EVENT)(unsafe.Pointer(e))
|
key := (*C.ALLEGRO_KEYBOARD_EVENT)(unsafe.Pointer(e))
|
||||||
return &KeyUpEvent{KeyEvent{eb, int(key.keycode), nil}}
|
return &KeyUpEvent{KeyEvent{eb, int(key.keycode), nil}}
|
||||||
case C.ALLEGRO_EVENT_KEY_CHAR:
|
case C.ALLEGRO_EVENT_KEY_CHAR:
|
||||||
key := (*C.ALLEGRO_KEYBOARD_EVENT)(unsafe.Pointer(e))
|
key := (*C.ALLEGRO_KEYBOARD_EVENT)(unsafe.Pointer(e))
|
||||||
return &KeyCharEvent{KeyEvent{eb, int(key.keycode), nil}, rune(key.unichar), uint(key.modifiers), bool(key.repeat)}
|
return &KeyCharEvent{KeyEvent{eb, int(key.keycode), nil}, rune(key.unichar), uint(key.modifiers), bool(key.repeat)}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (eq *EventQueue) Get() Event {
|
func (eq *EventQueue) Get() Event {
|
||||||
var event C.ALLEGRO_EVENT
|
var event C.ALLEGRO_EVENT
|
||||||
if !bool(C.al_get_next_event(eq.queue, &event)) {
|
if !bool(C.al_get_next_event(eq.queue, &event)) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return eq.mapEvent(&event)
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (eq *EventQueue) Destroy() {
|
func (eq *EventQueue) Destroy() {
|
||||||
C.al_destroy_event_queue(eq.queue)
|
C.al_destroy_event_queue(eq.queue)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user