Fix handling touch input press

This commit is contained in:
Trevor Slocum 2023-11-08 14:11:36 -08:00
parent c8545de5b3
commit e826b3e7fc
5 changed files with 54 additions and 8 deletions

7
box.go
View file

@ -37,9 +37,12 @@ func (b *Box) Rect() image.Rectangle {
// SetRect returns the position and size of the widget.
func (b *Box) SetRect(r image.Rectangle) {
b.Lock()
defer b.Unlock()
b.rect = r
b.Unlock()
for _, w := range b.children {
w.SetRect(r)
}
}
// Background returns the background color of the widget.

View file

@ -43,6 +43,10 @@ func (b *Button) SetRect(r image.Rectangle) {
b.Box.rect = r
b.Label.SetRect(r)
for _, w := range b.children {
w.SetRect(r)
}
}
// HandleKeyboard is called when a keyboard event occurs.

View file

@ -54,6 +54,10 @@ func (c *Checkbox) SetRect(r image.Rectangle) {
c.Box.rect = r
c.updateImage()
for _, w := range c.children {
w.SetRect(r)
}
}
// SetCheckColor sets the check mark color of the Checkbox.

43
game.go
View file

@ -21,10 +21,13 @@ var (
lastX, lastY = -math.MaxInt, -math.MaxInt
touchIDs []ebiten.TouchID
touchIDs []ebiten.TouchID
activeTouchID = ebiten.TouchID(-1)
focusedWidget Widget
pressedWidget Widget
lastBackspaceRepeat time.Time
keyBuffer []ebiten.Key
@ -126,15 +129,32 @@ func Update() error {
var clicked bool
var touchInput bool
touchIDs = inpututil.AppendJustPressedTouchIDs(touchIDs[:0])
for _, id := range touchIDs {
x, y := ebiten.TouchPosition(id)
if activeTouchID != -1 {
x, y := ebiten.TouchPosition(activeTouchID)
if x != 0 || y != 0 {
cursor = image.Point{x, y}
pressed = true
clicked = true
touchInput = true
} else {
activeTouchID = -1
}
}
if activeTouchID == -1 {
touchIDs = inpututil.AppendJustPressedTouchIDs(touchIDs[:0])
for _, id := range touchIDs {
x, y := ebiten.TouchPosition(id)
if x != 0 || y != 0 {
cursor = image.Point{x, y}
pressed = true
clicked = true
touchInput = true
activeTouchID = id
break
}
}
}
@ -155,13 +175,17 @@ func Update() error {
}
for _, binding := range Bindings.ConfirmMouse {
clicked = inpututil.IsMouseButtonJustReleased(binding)
clicked = inpututil.IsMouseButtonJustPressed(binding)
if clicked {
break
}
}
}
if !pressed && !clicked {
pressedWidget = nil
}
_, err := update(root, cursor, pressed, clicked, false)
if err != nil {
return fmt.Errorf("failed to handle widget mouse input: %s", err)
@ -252,12 +276,19 @@ func update(w Widget, cursor image.Point, pressed bool, clicked bool, mouseHandl
}
}
if !mouseHandled && cursor.In(w.Rect()) {
if pressed && !clicked && w != pressedWidget {
return mouseHandled, nil
}
mouseHandled, err = w.HandleMouse(cursor, pressed, clicked)
if err != nil {
return false, fmt.Errorf("failed to handle widget mouse input: %s", err)
}
if mouseHandled && !clicked && pressedWidget != nil && (!pressed || pressedWidget != w) {
pressedWidget = nil
}
if clicked && mouseHandled {
SetFocus(w)
pressedWidget = w
}
}
return mouseHandled, nil

View file

@ -46,6 +46,10 @@ func (i *Input) SetRect(r image.Rectangle) {
i.Box.rect = r
i.Field.SetRect(r)
for _, w := range i.children {
w.SetRect(r)
}
}
// Focus returns the focus state of the widget.