Add Frame.SetPadding

This commit is contained in:
Trevor Slocum 2024-01-18 00:08:59 -08:00
parent fef5f103ff
commit 853a537feb
3 changed files with 50 additions and 38 deletions

View File

@ -6,7 +6,7 @@ import (
"github.com/hajimehoshi/ebiten/v2"
)
// Flex is a flexbox layout which may be oriented horizontally or vertically.
// Flex is a flexible stack-based layout which may be oriented horizontally or vertically.
type Flex struct {
*Box
vertical bool
@ -33,8 +33,8 @@ func (f *Flex) SetRect(r image.Rectangle) {
f.modified = true
}
// SetGapSize sets the gap between each child in the Flex.
func (f *Flex) SetGapSize(columnGap int, rowGap int) {
// SetGaps sets the gaps between each child in the Flex.
func (f *Flex) SetGaps(columnGap int, rowGap int) {
f.Lock()
defer f.Unlock()

View File

@ -6,6 +6,7 @@ import "image"
// not repositioned by default. Repositioning may be enabled via SetPositionChildren.
type Frame struct {
*Box
padding int
positionChildren bool
}
@ -18,18 +19,22 @@ func NewFrame(w ...Widget) *Frame {
return f
}
// SetPadding sets the amount of padding around widgets in the frame.
func (f *Frame) SetPadding(padding int) {
f.Lock()
defer f.Unlock()
f.padding = padding
f.reposition()
}
// SetRect sets the position and size of the widget.
func (f *Frame) SetRect(r image.Rectangle) {
f.Lock()
defer f.Unlock()
f.rect = r
if f.positionChildren {
for _, w := range f.children {
w.SetRect(f.rect)
}
}
f.reposition()
}
// SetPositionChildren sets a flag that determines whether child widgets are
@ -39,12 +44,7 @@ func (f *Frame) SetPositionChildren(position bool) {
defer f.Unlock()
f.positionChildren = position
if f.positionChildren {
for _, w := range f.children {
w.SetRect(f.rect)
}
}
f.reposition()
}
// AddChild adds a child to the widget.
@ -55,8 +55,19 @@ func (f *Frame) AddChild(w ...Widget) {
f.children = append(f.children, w...)
if f.positionChildren {
r := f.rect.Inset(f.padding)
for _, wgt := range w {
wgt.SetRect(f.rect)
wgt.SetRect(r)
}
}
}
func (f *Frame) reposition() {
if !f.positionChildren {
return
}
r := f.rect.Inset(f.padding)
for _, w := range f.children {
w.SetRect(r)
}
}

45
game.go
View File

@ -213,34 +213,35 @@ func Update() error {
// Handle keyboard input.
if focusedWidget != nil {
if ebiten.IsKeyPressed(ebiten.KeyBackspace) {
if inpututil.IsKeyJustPressed(ebiten.KeyBackspace) {
lastBackspaceRepeat = time.Now().Add(backspaceRepeatWait)
} else if time.Since(lastBackspaceRepeat) >= backspaceRepeatTime {
lastBackspaceRepeat = time.Now()
if focusedWidget == nil {
return nil
}
if ebiten.IsKeyPressed(ebiten.KeyBackspace) {
if inpututil.IsKeyJustPressed(ebiten.KeyBackspace) {
lastBackspaceRepeat = time.Now().Add(backspaceRepeatWait)
} else if time.Since(lastBackspaceRepeat) >= backspaceRepeatTime {
lastBackspaceRepeat = time.Now()
_, err := focusedWidget.HandleKeyboard(ebiten.KeyBackspace, 0)
if err != nil {
return err
}
_, err := focusedWidget.HandleKeyboard(ebiten.KeyBackspace, 0)
if err != nil {
return err
}
}
}
keyBuffer = inpututil.AppendJustPressedKeys(keyBuffer[:0])
for _, key := range keyBuffer {
_, err := focusedWidget.HandleKeyboard(key, 0)
if err != nil {
return fmt.Errorf("failed to handle widget keyboard input: %s", err)
}
keyBuffer = inpututil.AppendJustPressedKeys(keyBuffer[:0])
for _, key := range keyBuffer {
_, err := focusedWidget.HandleKeyboard(key, 0)
if err != nil {
return fmt.Errorf("failed to handle widget keyboard input: %s", err)
}
}
runeBuffer = ebiten.AppendInputChars(runeBuffer[:0])
for _, r := range runeBuffer {
_, err := focusedWidget.HandleKeyboard(-1, r)
if err != nil {
return fmt.Errorf("failed to handle widget keyboard input: %s", err)
}
runeBuffer = ebiten.AppendInputChars(runeBuffer[:0])
for _, r := range runeBuffer {
_, err := focusedWidget.HandleKeyboard(-1, r)
if err != nil {
return fmt.Errorf("failed to handle widget keyboard input: %s", err)
}
}
return nil