diff --git a/box.go b/box.go index d94abf6..1d206a2 100644 --- a/box.go +++ b/box.go @@ -12,8 +12,6 @@ type Box struct { children []Widget - focus bool - sync.Mutex } @@ -35,12 +33,12 @@ func (b *Box) SetRect(r image.Rectangle) { b.rect = r } -func (b *Box) SetFocus(focus bool) { - b.focus = focus +func (b *Box) SetFocus(focus bool) bool { + return false } func (b *Box) Focus() bool { - return b.focus + return false } func (b *Box) HandleKeyboardEvent(key ebiten.Key, r rune) (handled bool, err error) { diff --git a/game.go b/game.go index 1d8ad4d..85e702b 100644 --- a/game.go +++ b/game.go @@ -30,13 +30,14 @@ func SetRoot(w Widget) { } func SetFocus(w Widget) { - if focusedWidget != nil { - focusedWidget.SetFocus(false) + lastFocused := focusedWidget + if w != nil && !w.SetFocus(true) { + return + } + if lastFocused != nil && lastFocused != w { + lastFocused.SetFocus(false) } focusedWidget = w - if w != nil { - w.SetFocus(true) - } } func Focused() Widget { diff --git a/input.go b/input.go index d41c5d7..2be1aa0 100644 --- a/input.go +++ b/input.go @@ -11,6 +11,7 @@ type Input struct { *Box Field *messeji.InputField Cursor string + focus bool } func NewInput(prefix string, text string, onSelected func(text string) (handled bool)) *Input { @@ -57,7 +58,7 @@ func (i *Input) SetRect(r image.Rectangle) { i.Field.SetRect(r) } -func (i *Input) SetFocus(focus bool) { +func (i *Input) SetFocus(focus bool) bool { i.focus = focus var cursor string @@ -65,6 +66,11 @@ func (i *Input) SetFocus(focus bool) { cursor = i.Cursor } i.Field.SetSuffix(cursor) + return true +} + +func (i *Input) Focused() bool { + return i.focus } func (i *Input) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) { diff --git a/text.go b/text.go index 29582ed..30f7914 100644 --- a/text.go +++ b/text.go @@ -26,8 +26,12 @@ func NewText(text string) *Text { } } -func (t *Text) SetFocus(focus bool) { - // Do nothing. +func (t *Text) SetFocus(focus bool) bool { + return false +} + +func (t *Text) Focus() bool { + return false } func (t *Text) Children() []Widget { diff --git a/widget.go b/widget.go index 896c578..934e56e 100644 --- a/widget.go +++ b/widget.go @@ -9,7 +9,7 @@ import ( type Widget interface { Rect() image.Rectangle SetRect(r image.Rectangle) - SetFocus(focus bool) + SetFocus(focus bool) (accept bool) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) HandleKeyboard() (handled bool, err error) HandleKeyboardEvent(ebiten.Key, rune) (handled bool, err error)