Add border to Input

This commit is contained in:
Trevor Slocum 2024-09-19 23:12:49 -07:00
parent b9ddff24b5
commit 0b260674c2
8 changed files with 89 additions and 47 deletions

View file

@ -45,11 +45,11 @@ func NewButton(label string, onSelected func() error) *Button {
textFont: Style.TextFont,
textSize: Scale(Style.TextSize),
onSelected: onSelected,
borderSize: Scale(Style.BorderSize),
borderTop: Style.BorderColorTop,
borderRight: Style.BorderColorRight,
borderBottom: Style.BorderColorBottom,
borderLeft: Style.BorderColorLeft,
borderSize: Scale(Style.ButtonBorderSize),
borderTop: Style.ButtonBorderTop,
borderRight: Style.ButtonBorderRight,
borderBottom: Style.ButtonBorderBottom,
borderLeft: Style.ButtonBorderLeft,
}
b.SetBackground(Style.ButtonBgColor)
b.resizeFont()

View file

@ -27,7 +27,7 @@ func NewCheckbox(onSelect func() error) *Checkbox {
Box: NewBox(),
checkColor: Style.TextColorDark,
borderSize: 2,
borderColor: Style.BorderColorBottom,
borderColor: Style.ButtonBorderBottom,
onSelect: onSelect,
}
}

1
doc.go
View file

@ -15,6 +15,7 @@ The following official widgets are available:
Grid - Highly customizable cell-based layout. Widgets added to the Grid may span multiple cells.
Input - Text input widget. The Input widget is simply a Text widget that also accepts user input.
List - List of widgets as selectable items.
Sprite - Resizable image.
Text - Text display widget.
Window - Widget paging mechanism. Only one widget added to a window is displayed at a time.

View file

@ -454,7 +454,7 @@ func newText() *messeji.TextField {
f.SetBackgroundColor(transparent)
f.SetScrollBarColors(Style.ScrollAreaColor, Style.ScrollHandleColor)
f.SetScrollBorderSize(Scale(Style.ScrollBorderSize))
f.SetScrollBorderColors(Style.ScrollBorderColorTop, Style.ScrollBorderColorRight, Style.ScrollBorderColorBottom, Style.ScrollBorderColorLeft)
f.SetScrollBorderColors(Style.ScrollBorderTop, Style.ScrollBorderRight, Style.ScrollBorderBottom, Style.ScrollBorderLeft)
return f
}

View file

@ -13,9 +13,12 @@ import (
// also accepts user input.
type Input struct {
*Box
field *messeji.InputField
cursor string
focus bool
field *messeji.InputField
cursor string
borderSize int
borderFocused color.RGBA
borderUnfocused color.RGBA
focus bool
}
// NewInput returns a new Input widget.
@ -30,7 +33,7 @@ func NewInput(text string, onSelected func(text string) (handled bool)) *Input {
f.SetBackgroundColor(transparent)
f.SetScrollBarColors(Style.ScrollAreaColor, Style.ScrollHandleColor)
f.SetScrollBorderSize(Scale(Style.ScrollBorderSize))
f.SetScrollBorderColors(Style.ScrollBorderColorTop, Style.ScrollBorderColorRight, Style.ScrollBorderColorBottom, Style.ScrollBorderColorLeft)
f.SetScrollBorderColors(Style.ScrollBorderTop, Style.ScrollBorderRight, Style.ScrollBorderBottom, Style.ScrollBorderLeft)
f.SetPrefix("")
f.SetSuffix("")
f.SetText(text)
@ -40,9 +43,12 @@ func NewInput(text string, onSelected func(text string) (handled bool)) *Input {
})
i := &Input{
Box: NewBox(),
field: f,
cursor: "_",
Box: NewBox(),
field: f,
cursor: "_",
borderSize: Scale(Style.InputBorderSize),
borderFocused: Style.InputBorderFocused,
borderUnfocused: Style.InputBorderUnfocused,
}
i.SetBackground(Style.InputBgColor)
return i
@ -59,6 +65,23 @@ func (i *Input) SetRect(r image.Rectangle) {
}
}
// SetBorderSize sets the size of the border around the field.
func (i *Input) SetBorderSize(size int) {
i.Lock()
defer i.Unlock()
i.borderSize = size
}
// SetBorderColors sets the border colors of the field when focused and unfocused.
func (i *Input) SetBorderColors(focused color.RGBA, unfocused color.RGBA) {
i.Lock()
defer i.Unlock()
i.borderFocused = focused
i.borderUnfocused = unfocused
}
// Foreground return the color of the text within the field.
func (i *Input) Foreground() color.RGBA {
i.Lock()
@ -252,5 +275,19 @@ func (i *Input) HandleMouse(cursor image.Point, pressed bool, clicked bool) (han
// Draw draws the widget on the screen.
func (i *Input) Draw(screen *ebiten.Image) error {
i.field.Draw(screen)
// Draw border.
if i.borderSize == 0 {
return nil
}
r := i.rect
c := i.borderUnfocused
if i.focus {
c = i.borderFocused
}
screen.SubImage(image.Rect(r.Min.X, r.Min.Y, r.Min.X+i.borderSize, r.Max.Y)).(*ebiten.Image).Fill(c)
screen.SubImage(image.Rect(r.Min.X, r.Min.Y, r.Max.X, r.Min.Y+i.borderSize)).(*ebiten.Image).Fill(c)
screen.SubImage(image.Rect(r.Max.X-i.borderSize, r.Min.Y, r.Max.X, r.Max.Y)).(*ebiten.Image).Fill(c)
screen.SubImage(image.Rect(r.Min.X, r.Max.Y-i.borderSize, r.Max.X, r.Max.Y)).(*ebiten.Image).Fill(c)
return nil
}

16
list.go
View file

@ -77,10 +77,10 @@ func NewList(itemHeight int, onSelected func(index int) (accept bool)) *List {
scrollAreaColor: initialScrollArea,
scrollHandleColor: initialScrollHandle,
scrollBorderSize: Style.ScrollBorderSize,
scrollBorderTop: Style.ScrollBorderColorTop,
scrollBorderRight: Style.ScrollBorderColorRight,
scrollBorderBottom: Style.ScrollBorderColorBottom,
scrollBorderLeft: Style.ScrollBorderColorLeft,
scrollBorderTop: Style.ScrollBorderTop,
scrollBorderRight: Style.ScrollBorderRight,
scrollBorderBottom: Style.ScrollBorderBottom,
scrollBorderLeft: Style.ScrollBorderLeft,
}
}
@ -461,10 +461,10 @@ func (l *List) Draw(screen *ebiten.Image) error {
// Draw border.
if l.drawBorder {
const borderSize = 4
screen.SubImage(image.Rect(l.grid.rect.Min.X, l.grid.rect.Min.Y, l.grid.rect.Max.X, l.grid.rect.Min.Y+borderSize)).(*ebiten.Image).Fill(Style.BorderColorBottom)
screen.SubImage(image.Rect(l.grid.rect.Min.X, l.grid.rect.Max.Y-borderSize, l.grid.rect.Max.X, l.grid.rect.Max.Y)).(*ebiten.Image).Fill(Style.BorderColorBottom)
screen.SubImage(image.Rect(l.grid.rect.Min.X, l.grid.rect.Min.Y, l.grid.rect.Min.X+borderSize, l.grid.rect.Max.Y)).(*ebiten.Image).Fill(Style.BorderColorBottom)
screen.SubImage(image.Rect(l.grid.rect.Max.X-borderSize, l.grid.rect.Min.Y, l.grid.rect.Max.X, l.grid.rect.Max.Y)).(*ebiten.Image).Fill(Style.BorderColorBottom)
screen.SubImage(image.Rect(l.grid.rect.Min.X, l.grid.rect.Min.Y, l.grid.rect.Max.X, l.grid.rect.Min.Y+borderSize)).(*ebiten.Image).Fill(Style.ButtonBorderBottom)
screen.SubImage(image.Rect(l.grid.rect.Min.X, l.grid.rect.Max.Y-borderSize, l.grid.rect.Max.X, l.grid.rect.Max.Y)).(*ebiten.Image).Fill(Style.ButtonBorderBottom)
screen.SubImage(image.Rect(l.grid.rect.Min.X, l.grid.rect.Min.Y, l.grid.rect.Min.X+borderSize, l.grid.rect.Max.Y)).(*ebiten.Image).Fill(Style.ButtonBorderBottom)
screen.SubImage(image.Rect(l.grid.rect.Max.X-borderSize, l.grid.rect.Min.Y, l.grid.rect.Max.X, l.grid.rect.Max.Y)).(*ebiten.Image).Fill(Style.ButtonBorderBottom)
}
// Draw scroll bar.

View file

@ -195,9 +195,9 @@ func (s *Select) Draw(screen *ebiten.Image) error {
// Draw border.
r := s.rect
borderSize := Scale(Style.BorderSize)
borderLeft, borderTop := Style.BorderColorLeft, Style.BorderColorTop
borderRight, borderBottom := Style.BorderColorRight, Style.BorderColorBottom
borderSize := Scale(Style.ButtonBorderSize)
borderLeft, borderTop := Style.ButtonBorderLeft, Style.ButtonBorderTop
borderRight, borderBottom := Style.ButtonBorderRight, Style.ButtonBorderBottom
if !s.open {
screen.SubImage(image.Rect(r.Min.X, r.Min.Y, r.Min.X+borderSize, r.Max.Y)).(*ebiten.Image).Fill(borderLeft)
screen.SubImage(image.Rect(r.Min.X, r.Min.Y, r.Max.X, r.Min.Y+borderSize)).(*ebiten.Image).Fill(borderTop)

View file

@ -18,22 +18,24 @@ type Attributes struct {
TextBgColor color.RGBA
BorderSize int
ButtonBorderSize int
ButtonBorderTop color.RGBA
ButtonBorderRight color.RGBA
ButtonBorderBottom color.RGBA
ButtonBorderLeft color.RGBA
BorderColorTop color.RGBA
BorderColorRight color.RGBA
BorderColorBottom color.RGBA
BorderColorLeft color.RGBA
InputBorderSize int
InputBorderFocused color.RGBA
InputBorderUnfocused color.RGBA
ScrollAreaColor color.RGBA
ScrollHandleColor color.RGBA
ScrollBorderSize int
ScrollBorderColorTop color.RGBA
ScrollBorderColorRight color.RGBA
ScrollBorderColorBottom color.RGBA
ScrollBorderColorLeft color.RGBA
ScrollBorderSize int
ScrollBorderTop color.RGBA
ScrollBorderRight color.RGBA
ScrollBorderBottom color.RGBA
ScrollBorderLeft color.RGBA
InputBgColor color.RGBA
@ -51,22 +53,24 @@ var Style = &Attributes{
TextBgColor: transparent,
BorderSize: 4,
ButtonBorderSize: 4,
ButtonBorderTop: color.RGBA{220, 220, 220, 255},
ButtonBorderRight: color.RGBA{0, 0, 0, 255},
ButtonBorderBottom: color.RGBA{0, 0, 0, 255},
ButtonBorderLeft: color.RGBA{220, 220, 220, 255},
BorderColorTop: color.RGBA{220, 220, 220, 255},
BorderColorRight: color.RGBA{0, 0, 0, 255},
BorderColorBottom: color.RGBA{0, 0, 0, 255},
BorderColorLeft: color.RGBA{220, 220, 220, 255},
InputBorderSize: 2,
InputBorderFocused: color.RGBA{220, 220, 220, 255},
InputBorderUnfocused: color.RGBA{0, 0, 0, 255},
ScrollAreaColor: color.RGBA{200, 200, 200, 255},
ScrollHandleColor: color.RGBA{108, 108, 108, 255},
ScrollBorderSize: 2,
ScrollBorderColorTop: color.RGBA{240, 240, 240, 255},
ScrollBorderColorRight: color.RGBA{0, 0, 0, 255},
ScrollBorderColorBottom: color.RGBA{0, 0, 0, 255},
ScrollBorderColorLeft: color.RGBA{240, 240, 240, 255},
ScrollBorderSize: 2,
ScrollBorderTop: color.RGBA{240, 240, 240, 255},
ScrollBorderRight: color.RGBA{0, 0, 0, 255},
ScrollBorderBottom: color.RGBA{0, 0, 0, 255},
ScrollBorderLeft: color.RGBA{240, 240, 240, 255},
InputBgColor: color.RGBA{0, 128, 0, 255},