Add Button.SetBorderColor

This commit is contained in:
Trevor Slocum 2024-01-23 00:53:47 -08:00
parent 9e17ce6319
commit fe95e8209c
8 changed files with 55 additions and 26 deletions

View file

@ -2,6 +2,7 @@ package etk
import (
"image"
"image/color"
"code.rocket9labs.com/tslocum/etk/messeji"
"github.com/hajimehoshi/ebiten/v2"
@ -13,7 +14,11 @@ type Button struct {
Label *messeji.TextField
onSelected func() error
borderTop color.RGBA
borderRight color.RGBA
borderBottom color.RGBA
borderLeft color.RGBA
onSelected func() error
}
// NewButton returns a new Button widget.
@ -32,9 +37,13 @@ func NewButton(label string, onSelected func() error) *Button {
l.SetScrollBarVisible(false)
return &Button{
Box: NewBox(),
Label: l,
onSelected: onSelected,
Box: NewBox(),
Label: l,
onSelected: onSelected,
borderTop: Style.BorderColorTop,
borderRight: Style.BorderColorRight,
borderBottom: Style.BorderColorBottom,
borderLeft: Style.BorderColorLeft,
}
}
@ -49,6 +58,17 @@ func (b *Button) SetRect(r image.Rectangle) {
}
}
// SetBorderColor sets the color of the top, right, bottom and left border.
func (b *Button) SetBorderColor(top color.RGBA, right color.RGBA, bottom color.RGBA, left color.RGBA) {
b.Lock()
defer b.Unlock()
b.borderTop = top
b.borderRight = right
b.borderBottom = bottom
b.borderLeft = left
}
// HandleKeyboard is called when a keyboard event occurs.
func (b *Button) HandleKeyboard(ebiten.Key, rune) (handled bool, err error) {
return false, nil
@ -73,18 +93,20 @@ func (b *Button) HandleMouse(cursor image.Point, pressed bool, clicked bool) (ha
// Draw draws the button on the screen.
func (b *Button) Draw(screen *ebiten.Image) error {
r := b.rect
// Draw background.
screen.SubImage(b.rect).(*ebiten.Image).Fill(Style.ButtonBgColor)
screen.SubImage(r).(*ebiten.Image).Fill(Style.ButtonBgColor)
// Draw label.
b.Label.Draw(screen)
// Draw border.
const borderSize = 4
screen.SubImage(image.Rect(b.rect.Min.X, b.rect.Min.Y, b.rect.Max.X, b.rect.Min.Y+borderSize)).(*ebiten.Image).Fill(Style.BorderColor)
screen.SubImage(image.Rect(b.rect.Min.X, b.rect.Max.Y-borderSize, b.rect.Max.X, b.rect.Max.Y)).(*ebiten.Image).Fill(Style.BorderColor)
screen.SubImage(image.Rect(b.rect.Min.X, b.rect.Min.Y, b.rect.Min.X+borderSize, b.rect.Max.Y)).(*ebiten.Image).Fill(Style.BorderColor)
screen.SubImage(image.Rect(b.rect.Max.X-borderSize, b.rect.Min.Y, b.rect.Max.X, b.rect.Max.Y)).(*ebiten.Image).Fill(Style.BorderColor)
screen.SubImage(image.Rect(r.Min.X, r.Min.Y, r.Min.X+borderSize, r.Max.Y)).(*ebiten.Image).Fill(b.borderLeft)
screen.SubImage(image.Rect(r.Min.X, r.Min.Y, r.Max.X, r.Min.Y+borderSize)).(*ebiten.Image).Fill(b.borderTop)
screen.SubImage(image.Rect(r.Max.X-borderSize, r.Min.Y, r.Max.X, r.Max.Y)).(*ebiten.Image).Fill(b.borderRight)
screen.SubImage(image.Rect(r.Min.X, r.Max.Y-borderSize, r.Max.X, r.Max.Y)).(*ebiten.Image).Fill(b.borderBottom)
return nil
}

View file

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

View file

@ -44,12 +44,12 @@ const (
backspaceRepeatTime = 75 * time.Millisecond
)
var deviceScale = -1.0
var deviceScale float64
// ScaleFactor returns the device scale factor. When running on Android, this function
// may only be called during or after the first Layout call made by Ebitengine.
func ScaleFactor() float64 {
if deviceScale == -1 {
if deviceScale == 0 {
deviceScale = ebiten.DeviceScaleFactor()
}
return deviceScale
@ -59,7 +59,7 @@ func ScaleFactor() float64 {
// When running on Android, this function may only be called during or after the first
// Layout call made by Ebitengine.
func Scale(v int) int {
if deviceScale == -1 {
if deviceScale == 0 {
deviceScale = ebiten.DeviceScaleFactor()
}
return int(float64(v) * deviceScale)

2
go.mod
View file

@ -3,7 +3,7 @@ module code.rocket9labs.com/tslocum/etk
go 1.18
require (
github.com/hajimehoshi/ebiten/v2 v2.6.3
github.com/hajimehoshi/ebiten/v2 v2.6.4
github.com/llgcode/draw2d v0.0.0-20231212091825-f55e0c776b44
golang.org/x/image v0.15.0
)

4
go.sum
View file

@ -3,8 +3,8 @@ github.com/ebitengine/purego v0.5.2/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/hajimehoshi/bitmapfont/v3 v3.0.0 h1:r2+6gYK38nfztS/et50gHAswb9hXgxXECYgE8Nczmi4=
github.com/hajimehoshi/ebiten/v2 v2.6.3 h1:xJ5klESxhflZbPUx3GdIPoITzgPgamsyv8aZCVguXGI=
github.com/hajimehoshi/ebiten/v2 v2.6.3/go.mod h1:TZtorL713an00UW4LyvMeKD8uXWnuIuCPtlH11b0pgI=
github.com/hajimehoshi/ebiten/v2 v2.6.4 h1:G6tABZ4/njmi8Qn/l4Bqq49UrONrWW7TKcMMOSjPcpk=
github.com/hajimehoshi/ebiten/v2 v2.6.4/go.mod h1:TZtorL713an00UW4LyvMeKD8uXWnuIuCPtlH11b0pgI=
github.com/jezek/xgb v1.1.1 h1:bE/r8ZZtSv7l9gk6nU0mYx51aXrvnyb44892TwSaqS4=
github.com/jezek/xgb v1.1.1/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk=
github.com/llgcode/draw2d v0.0.0-20231212091825-f55e0c776b44 h1:1ad70i0s40IpMtRm2ST+Nvr03X7mlHWtdALYkFNrlxk=

View file

@ -410,10 +410,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.BorderColor)
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.BorderColor)
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.BorderColor)
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.BorderColor)
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)
}
// Draw scroll bar.

View file

@ -185,10 +185,11 @@ func (s *Select) Draw(screen *ebiten.Image) error {
// Draw border.
const borderSize = 4
screen.SubImage(image.Rect(s.rect.Min.X, s.rect.Min.Y, s.rect.Max.X, s.rect.Min.Y+borderSize)).(*ebiten.Image).Fill(Style.BorderColor)
screen.SubImage(image.Rect(s.rect.Min.X, s.rect.Max.Y-borderSize, s.rect.Max.X, s.rect.Max.Y)).(*ebiten.Image).Fill(Style.BorderColor)
screen.SubImage(image.Rect(s.rect.Min.X, s.rect.Min.Y, s.rect.Min.X+borderSize, s.rect.Max.Y)).(*ebiten.Image).Fill(Style.BorderColor)
screen.SubImage(image.Rect(s.rect.Max.X-borderSize, s.rect.Min.Y, s.rect.Max.X, s.rect.Max.Y)).(*ebiten.Image).Fill(Style.BorderColor)
r := s.rect
screen.SubImage(image.Rect(r.Min.X, r.Min.Y, r.Min.X+borderSize, r.Max.Y)).(*ebiten.Image).Fill(Style.BorderColorLeft)
screen.SubImage(image.Rect(r.Min.X, r.Min.Y, r.Max.X, r.Min.Y+borderSize)).(*ebiten.Image).Fill(Style.BorderColorTop)
screen.SubImage(image.Rect(r.Max.X-borderSize, r.Min.Y, r.Max.X, r.Max.Y)).(*ebiten.Image).Fill(Style.BorderColorRight)
screen.SubImage(image.Rect(r.Min.X, r.Max.Y-borderSize, r.Max.X, r.Max.Y)).(*ebiten.Image).Fill(Style.BorderColorBottom)
return nil
}

View file

@ -42,7 +42,10 @@ type Attributes struct {
ScrollAreaColor color.RGBA
ScrollHandleColor color.RGBA
BorderColor color.RGBA
BorderColorTop color.RGBA
BorderColorRight color.RGBA
BorderColorBottom color.RGBA
BorderColorLeft color.RGBA
InputBgColor color.RGBA
@ -64,7 +67,10 @@ var Style = &Attributes{
ScrollAreaColor: color.RGBA{200, 200, 200, 255},
ScrollHandleColor: color.RGBA{108, 108, 108, 255},
BorderColor: color.RGBA{0, 0, 0, 255},
BorderColorTop: color.RGBA{255, 255, 255, 255},
BorderColorRight: color.RGBA{0, 0, 0, 255},
BorderColorBottom: color.RGBA{0, 0, 0, 255},
BorderColorLeft: color.RGBA{255, 255, 255, 255},
InputBgColor: color.RGBA{0, 128, 0, 255},