Do not align text widgets by default

This commit is contained in:
Trevor Slocum 2023-09-28 22:43:29 -07:00
parent a3c56d7430
commit 2cf182ad16
3 changed files with 32 additions and 21 deletions

View file

@ -10,7 +10,7 @@ import (
type Button struct {
*Box
label *messeji.TextField
Label *messeji.TextField
onSelected func() error
}
@ -30,7 +30,7 @@ func NewButton(label string, onSelected func() error) *Button {
return &Button{
Box: NewBox(),
label: l,
Label: l,
onSelected: onSelected,
}
}
@ -38,7 +38,7 @@ func NewButton(label string, onSelected func() error) *Button {
func (b *Button) SetRect(r image.Rectangle) {
b.Box.rect = r
b.label.SetRect(r)
b.Label.SetRect(r)
}
func (b *Button) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
@ -67,7 +67,7 @@ func (b *Button) Draw(screen *ebiten.Image) error {
screen.SubImage(b.rect).(*ebiten.Image).Fill(Style.ButtonBgColor)
// Draw label.
b.label.Draw(screen)
b.Label.Draw(screen)
// Draw border.
const borderSize = 4

View file

@ -6,6 +6,8 @@ import (
"fmt"
"log"
"code.rocketnine.space/tslocum/messeji"
"code.rocketnine.space/tslocum/etk"
"github.com/hajimehoshi/ebiten/v2"
)
@ -21,7 +23,10 @@ func main() {
}
newText := func(size int) *etk.Text {
return etk.NewText(fmt.Sprintf("%dpx Text", size))
t := etk.NewText(fmt.Sprintf("%dpx Text", size))
t.SetHorizontal(messeji.AlignCenter)
t.SetVertical(messeji.AlignCenter)
return t
}
g := newGame()

38
text.go
View file

@ -9,8 +9,9 @@ import (
)
type Text struct {
*Box
Field *messeji.TextField
*messeji.TextField
children []Widget
}
func NewText(text string) *Text {
@ -20,33 +21,38 @@ func NewText(text string) *Text {
l.SetText(text)
l.SetForegroundColor(textColor)
l.SetBackgroundColor(Style.TextBgColor)
l.SetHorizontal(messeji.AlignCenter)
l.SetVertical(messeji.AlignCenter)
return &Text{
Box: NewBox(),
Field: l,
TextField: l,
}
}
func (t *Text) Children() []Widget {
t.Lock()
defer t.Unlock()
return t.children
}
func (t *Text) AddChild(w ...Widget) {
t.Lock()
defer t.Unlock()
t.children = append(t.children, w...)
}
// Clear clears the field's buffer.
func (t *Text) Clear() {
t.Field.SetText("")
t.SetText("")
}
// Write writes to the field's buffer.
func (t *Text) Write(p []byte) (n int, err error) {
return t.Field.Write(p)
return t.Write(p)
}
func (t *Text) Text() string {
return t.Field.Text()
}
func (t *Text) SetRect(r image.Rectangle) {
t.Box.rect = r
t.Field.SetRect(r)
return t.Text()
}
func (t *Text) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
@ -59,6 +65,6 @@ func (t *Text) HandleKeyboard() (handled bool, err error) {
func (t *Text) Draw(screen *ebiten.Image) error {
// Draw label.
t.Field.Draw(screen)
t.TextField.Draw(screen)
return nil
}