Added changing the mouse cursor.

This commit is contained in:
Sander Schobers 2019-03-11 17:30:41 +01:00
parent 0387e313de
commit 1d6587adbd
6 changed files with 76 additions and 5 deletions

View File

@ -78,6 +78,10 @@ func (d *Display) SetIcon(i *Bitmap) {
C.al_set_display_icon(d.display, i.bitmap) C.al_set_display_icon(d.display, i.bitmap)
} }
func (d *Display) SetMouseCursor(c MouseCursor) {
C.al_set_system_mouse_cursor(d.display, C.ALLEGRO_SYSTEM_MOUSE_CURSOR(c))
}
func (d *Display) SetMousePosition(x, y int) { func (d *Display) SetMousePosition(x, y int) {
C.al_set_mouse_xy(d.display, C.int(x), C.int(y)) C.al_set_mouse_xy(d.display, C.int(x), C.int(y))
} }

View File

@ -11,6 +11,31 @@ const (
MouseButtonMiddle = 3 MouseButtonMiddle = 3
) )
type MouseCursor uint
const (
MouseCursorNone MouseCursor = 0
MouseCursorDefault = 1
MouseCursorArrow = 2
MouseCursorBusy = 3
MouseCursorQuestion = 4
MouseCursorEdit = 5
MouseCursorMove = 6
MouseCursorResizeN = 7
MouseCursorResizeW = 8
MouseCursorResizeS = 9
MouseCursorResizeE = 10
MouseCursorResizeNW = 11
MouseCursorResizeSW = 12
MouseCursorResizeSE = 13
MouseCursorResizeNE = 14
MouseCursorProgress = 15
MouseCursorPrecision = 16
MouseCursorLink = 17
MouseCursorAltSelect = 18
MouseCursorUnavailable = 19
)
func IsMouseButtonDown(b MouseButton) bool { func IsMouseButtonDown(b MouseButton) bool {
var state C.ALLEGRO_MOUSE_STATE var state C.ALLEGRO_MOUSE_STATE
C.al_get_mouse_state(&state) C.al_get_mouse_state(&state)

View File

@ -34,15 +34,17 @@ func NewRenderer(w, h int, opts allg5.NewDisplayOptions) (*Renderer, error) {
eq.RegisterDisplay(disp) eq.RegisterDisplay(disp)
eq.RegisterUserEvents(user) eq.RegisterUserEvents(user)
return &Renderer{disp, eq, map[string]*font{}, user}, nil return &Renderer{disp, eq, map[string]*font{}, user, ui.MouseCursorDefault, ui.MouseCursorDefault}, nil
} }
// Renderer implements ui.Renderer using Allegro 5. // Renderer implements ui.Renderer using Allegro 5.
type Renderer struct { type Renderer struct {
disp *allg5.Display disp *allg5.Display
eq *allg5.EventQueue eq *allg5.EventQueue
ft map[string]*font ft map[string]*font
user *allg5.UserEventSource user *allg5.UserEventSource
cursor ui.MouseCursor
newCursor ui.MouseCursor
} }
// Renderer implementation (events) // Renderer implementation (events)
@ -50,6 +52,7 @@ type Renderer struct {
func (r *Renderer) PushEvents(t ui.EventTarget, wait bool) { func (r *Renderer) PushEvents(t ui.EventTarget, wait bool) {
r.disp.Flip() r.disp.Flip()
r.newCursor = ui.MouseCursorDefault
var ev = eventWait(r.eq, wait) var ev = eventWait(r.eq, wait)
for nil != ev { for nil != ev {
switch e := ev.(type) { switch e := ev.(type) {
@ -68,6 +71,22 @@ func (r *Renderer) PushEvents(t ui.EventTarget, wait bool) {
} }
ev = r.eq.Get() ev = r.eq.Get()
} }
if r.newCursor != r.cursor {
r.cursor = r.newCursor
switch r.cursor {
case ui.MouseCursorNone:
r.disp.SetMouseCursor(allg5.MouseCursorNone)
case ui.MouseCursorDefault:
r.disp.SetMouseCursor(allg5.MouseCursorDefault)
case ui.MouseCursorNotAllowed:
r.disp.SetMouseCursor(allg5.MouseCursorUnavailable)
case ui.MouseCursorPointer:
r.disp.SetMouseCursor(allg5.MouseCursorLink)
case ui.MouseCursorText:
r.disp.SetMouseCursor(allg5.MouseCursorEdit)
}
}
} }
func (r *Renderer) Refresh() { func (r *Renderer) Refresh() {
@ -183,6 +202,10 @@ func (r *Renderer) SetIcon(im ui.Image) {
r.disp.SetIcon(bmp) r.disp.SetIcon(bmp)
} }
func (r *Renderer) SetMouseCursor(c ui.MouseCursor) {
r.newCursor = c
}
func (r *Renderer) SetWindowTitle(t string) { func (r *Renderer) SetWindowTitle(t string) {
r.disp.SetWindowTitle(t) r.disp.SetWindowTitle(t)
} }

View File

@ -25,6 +25,13 @@ func (b *Button) DesiredSize(ctx Context) geom.PointF32 {
return geom.PtF32(width+pad*2, height+pad*2) return geom.PtF32(width+pad*2, height+pad*2)
} }
func (b *Button) Handle(ctx Context, e Event) {
b.ControlBase.Handle(ctx, e)
if b.over {
ctx.Renderer().SetMouseCursor(MouseCursorPointer)
}
}
func (b *Button) Render(ctx Context) { func (b *Button) Render(ctx Context) {
var fore = b.FontColor var fore = b.FontColor
var style = ctx.Style() var style = ctx.Style()

11
ui/mouse.go Normal file
View File

@ -0,0 +1,11 @@
package ui
type MouseCursor int
const (
MouseCursorNone MouseCursor = iota
MouseCursorDefault
MouseCursorNotAllowed
MouseCursorPointer
MouseCursorText
)

View File

@ -27,6 +27,7 @@ type Renderer interface {
Rectangle(r geom.RectangleF32, c color.Color, thickness float32) Rectangle(r geom.RectangleF32, c color.Color, thickness float32)
RenderTo(Image) RenderTo(Image)
RenderToDisplay() RenderToDisplay()
SetMouseCursor(c MouseCursor)
Size() geom.PointF32 Size() geom.PointF32
Target() Image Target() Image
Text(p geom.PointF32, font string, color color.Color, text string) Text(p geom.PointF32, font string, color color.Color, text string)