Compare commits

..

3 Commits

Author SHA1 Message Date
451f389ef6 Extended Display.SetIcon to allow multiple icons. 2020-02-20 18:15:29 +01:00
9002c2b24d Added missing display events.
Added Display parameter to display events.
2020-02-20 18:08:01 +01:00
6468ff1b33 Added methods to query the current mouse position. 2020-02-20 18:07:05 +01:00
3 changed files with 102 additions and 14 deletions

View File

@ -115,8 +115,20 @@ func (d *Display) SetAsTarget() {
C.al_set_target_backbuffer(d.display) C.al_set_target_backbuffer(d.display)
} }
func (d *Display) SetIcon(i *Bitmap) { func (d *Display) SetIcon(i ...*Bitmap) {
C.al_set_display_icon(d.display, i.bitmap) if len(i) == 0 {
return
}
if len(i) == 1 {
C.al_set_display_icon(d.display, i[0].bitmap)
return
}
icons := make([]*C.ALLEGRO_BITMAP, len(i))
for j := range icons {
icons[j] = i[j].bitmap
}
C.al_set_display_icons(d.display, C.int(len(icons)), &(icons[0]))
} }
func (d *Display) SetMouseCursor(c MouseCursor) { func (d *Display) SetMouseCursor(c MouseCursor) {

View File

@ -22,6 +22,7 @@ import (
type EventQueue struct { type EventQueue struct {
queue *C.ALLEGRO_EVENT_QUEUE queue *C.ALLEGRO_EVENT_QUEUE
displays map[*C.ALLEGRO_DISPLAY]*Display
} }
type Event interface { type Event interface {
@ -37,16 +38,33 @@ func (eb EventBase) Stamp() float64 {
} }
type DisplayCloseEvent struct { type DisplayCloseEvent struct {
EventBase DisplayEvent
} }
type DisplayResizeEvent struct { type DisplayConnectedEvent struct {
EventBase DisplayEvent
X, Y int
Width int
Height int
} }
type DisplayDisconnectedEvent struct {
DisplayEvent
}
type DisplayEvent struct {
EventBase
Display *Display
}
type DisplayFoundEvent struct {
DisplayEvent
}
type DisplayHaltDrawingEvent struct {
DisplayEvent
}
type DisplayLostEvent struct {
DisplayEvent
}
type DisplayOrientation int type DisplayOrientation int
const ( const (
@ -78,10 +96,29 @@ func toDisplayOrientation(o C.int) DisplayOrientation {
} }
type DisplayOrientationEvent struct { type DisplayOrientationEvent struct {
EventBase DisplayEvent
Orientation DisplayOrientation Orientation DisplayOrientation
} }
type DisplayResizeEvent struct {
DisplayEvent
X, Y int
Width int
Height int
}
type DisplayResumeDrawingEvent struct {
DisplayEvent
}
type DisplaySwitchInEvent struct {
DisplayEvent
}
type DisplaySwitchOutEvent struct {
DisplayEvent
}
type KeyEvent struct { type KeyEvent struct {
EventBase EventBase
KeyCode Key KeyCode Key
@ -125,6 +162,7 @@ type MouseEvent struct {
Z, W int Z, W int
Display *Display Display *Display
} }
type MouseLeaveEvent struct { type MouseLeaveEvent struct {
MouseEvent MouseEvent
} }
@ -155,7 +193,7 @@ func NewEventQueue() (*EventQueue, error) {
if q == nil { if q == nil {
return nil, errors.New("unable to create event queue") return nil, errors.New("unable to create event queue")
} }
return &EventQueue{q}, nil return &EventQueue{q, map[*C.ALLEGRO_DISPLAY]*Display{}}, nil
} }
func (eq *EventQueue) register(source *C.ALLEGRO_EVENT_SOURCE) { func (eq *EventQueue) register(source *C.ALLEGRO_EVENT_SOURCE) {
@ -164,6 +202,7 @@ func (eq *EventQueue) register(source *C.ALLEGRO_EVENT_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))
eq.displays[d.display] = d
} }
func (eq *EventQueue) RegisterMouse() { func (eq *EventQueue) RegisterMouse() {
@ -189,15 +228,40 @@ func (eq *EventQueue) mapEvent(e *C.ALLEGRO_EVENT) Event {
case C.ALLEGRO_EVENT_AUDIO_RECORDER_FRAGMENT: case C.ALLEGRO_EVENT_AUDIO_RECORDER_FRAGMENT:
recorder := (*C.ALLEGRO_AUDIO_RECORDER_EVENT)(unsafe.Pointer(e)) recorder := (*C.ALLEGRO_AUDIO_RECORDER_EVENT)(unsafe.Pointer(e))
return &RecorderFragmentEvent{eb, unsafe.Pointer(recorder.buffer), int(recorder.samples)} return &RecorderFragmentEvent{eb, unsafe.Pointer(recorder.buffer), int(recorder.samples)}
case C.ALLEGRO_EVENT_DISPLAY_CONNECTED:
display := (*C.ALLEGRO_DISPLAY_EVENT)(unsafe.Pointer(e))
return &DisplayConnectedEvent{DisplayEvent{eb, eq.displays[display.source]}}
case C.ALLEGRO_EVENT_DISPLAY_CLOSE: case C.ALLEGRO_EVENT_DISPLAY_CLOSE:
return &DisplayCloseEvent{eb} display := (*C.ALLEGRO_DISPLAY_EVENT)(unsafe.Pointer(e))
return &DisplayCloseEvent{DisplayEvent{eb, eq.displays[display.source]}}
case C.ALLEGRO_EVENT_DISPLAY_DISCONNECTED:
display := (*C.ALLEGRO_DISPLAY_EVENT)(unsafe.Pointer(e))
return &DisplayDisconnectedEvent{DisplayEvent{eb, eq.displays[display.source]}}
case C.ALLEGRO_EVENT_DISPLAY_FOUND:
display := (*C.ALLEGRO_DISPLAY_EVENT)(unsafe.Pointer(e))
return &DisplayFoundEvent{DisplayEvent{eb, eq.displays[display.source]}}
case C.ALLEGRO_EVENT_DISPLAY_HALT_DRAWING:
display := (*C.ALLEGRO_DISPLAY_EVENT)(unsafe.Pointer(e))
return &DisplayHaltDrawingEvent{DisplayEvent{eb, eq.displays[display.source]}}
case C.ALLEGRO_EVENT_DISPLAY_LOST:
display := (*C.ALLEGRO_DISPLAY_EVENT)(unsafe.Pointer(e))
return &DisplayLostEvent{DisplayEvent{eb, eq.displays[display.source]}}
case C.ALLEGRO_EVENT_DISPLAY_ORIENTATION: case C.ALLEGRO_EVENT_DISPLAY_ORIENTATION:
display := (*C.ALLEGRO_DISPLAY_EVENT)(unsafe.Pointer(e)) display := (*C.ALLEGRO_DISPLAY_EVENT)(unsafe.Pointer(e))
return &DisplayOrientationEvent{eb, toDisplayOrientation(display.orientation)} return &DisplayOrientationEvent{DisplayEvent{eb, eq.displays[display.source]}, toDisplayOrientation(display.orientation)}
case C.ALLEGRO_EVENT_DISPLAY_RESIZE: case C.ALLEGRO_EVENT_DISPLAY_RESIZE:
display := (*C.ALLEGRO_DISPLAY_EVENT)(unsafe.Pointer(e)) display := (*C.ALLEGRO_DISPLAY_EVENT)(unsafe.Pointer(e))
C.al_acknowledge_resize(display.source) C.al_acknowledge_resize(display.source)
return &DisplayResizeEvent{eb, int(display.x), int(display.y), int(display.width), int(display.height)} return &DisplayResizeEvent{DisplayEvent{eb, eq.displays[display.source]}, int(display.x), int(display.y), int(display.width), int(display.height)}
case C.ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING:
display := (*C.ALLEGRO_DISPLAY_EVENT)(unsafe.Pointer(e))
return &DisplayResumeDrawingEvent{DisplayEvent{eb, eq.displays[display.source]}}
case C.ALLEGRO_EVENT_DISPLAY_SWITCH_IN:
display := (*C.ALLEGRO_DISPLAY_EVENT)(unsafe.Pointer(e))
return &DisplaySwitchInEvent{DisplayEvent{eb, eq.displays[display.source]}}
case C.ALLEGRO_EVENT_DISPLAY_SWITCH_OUT:
display := (*C.ALLEGRO_DISPLAY_EVENT)(unsafe.Pointer(e))
return &DisplaySwitchOutEvent{DisplayEvent{eb, eq.displays[display.source]}}
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)}

View File

@ -52,3 +52,15 @@ func IsAnyMouseButtonDown(buttons ...MouseButton) bool {
} }
return false return false
} }
func MousePosition() (int, int) {
var state C.ALLEGRO_MOUSE_STATE
C.al_get_mouse_state(&state)
return int(state.x), int(state.y)
}
func MousePositionScreen() (int, int, bool) {
var x, y C.int
var result = C.al_get_mouse_cursor_position(&x, &y)
return int(x), int(y), bool(result)
}