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)
}
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) {
C.al_set_mouse_xy(d.display, C.int(x), C.int(y))
}

View File

@ -11,6 +11,31 @@ const (
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 {
var state C.ALLEGRO_MOUSE_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.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.
type Renderer struct {
disp *allg5.Display
eq *allg5.EventQueue
ft map[string]*font
user *allg5.UserEventSource
disp *allg5.Display
eq *allg5.EventQueue
ft map[string]*font
user *allg5.UserEventSource
cursor ui.MouseCursor
newCursor ui.MouseCursor
}
// Renderer implementation (events)
@ -50,6 +52,7 @@ type Renderer struct {
func (r *Renderer) PushEvents(t ui.EventTarget, wait bool) {
r.disp.Flip()
r.newCursor = ui.MouseCursorDefault
var ev = eventWait(r.eq, wait)
for nil != ev {
switch e := ev.(type) {
@ -68,6 +71,22 @@ func (r *Renderer) PushEvents(t ui.EventTarget, wait bool) {
}
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() {
@ -183,6 +202,10 @@ func (r *Renderer) SetIcon(im ui.Image) {
r.disp.SetIcon(bmp)
}
func (r *Renderer) SetMouseCursor(c ui.MouseCursor) {
r.newCursor = c
}
func (r *Renderer) SetWindowTitle(t string) {
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)
}
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) {
var fore = b.FontColor
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)
RenderTo(Image)
RenderToDisplay()
SetMouseCursor(c MouseCursor)
Size() geom.PointF32
Target() Image
Text(p geom.PointF32, font string, color color.Color, text string)