From b1b41487c2ec3a1522144b544fcd3e46f5e07a5b Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Sun, 28 Jan 2024 20:44:19 -0800 Subject: [PATCH] Draw shaded button when pressed --- button.go | 29 +++++++++++++++++++++-------- doc.go | 6 ++++++ select.go | 25 ++++++++++++++++++++----- style.go | 12 ++++-------- 4 files changed, 51 insertions(+), 21 deletions(-) diff --git a/button.go b/button.go index 33f2520..b554700 100644 --- a/button.go +++ b/button.go @@ -20,6 +20,7 @@ type Button struct { borderBottom color.RGBA borderLeft color.RGBA onSelected func() error + pressed bool } // NewButton returns a new Button widget. @@ -35,7 +36,7 @@ func NewButton(label string, onSelected func() error) *Button { f.SetVertical(messeji.AlignCenter) f.SetScrollBarVisible(false) - return &Button{ + b := &Button{ Box: NewBox(), field: f, onSelected: onSelected, @@ -45,6 +46,8 @@ func NewButton(label string, onSelected func() error) *Button { borderBottom: Style.BorderColorBottom, borderLeft: Style.BorderColorLeft, } + b.SetBackground(Style.ButtonBgColor) + return b } // SetRect sets the position and size of the Button. @@ -109,10 +112,16 @@ func (b *Button) HandleKeyboard(ebiten.Key, rune) (handled bool, err error) { // HandleMouse is called when a mouse event occurs. func (b *Button) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) { if !clicked { + if b.pressed && !pressed { + b.pressed = false + b.SetBackground(Style.ButtonBgColor) + } return true, nil } b.Lock() + b.pressed = true + b.background = color.RGBA{uint8(float64(Style.ButtonBgColor.R) * 0.95), uint8(float64(Style.ButtonBgColor.G) * 0.95), uint8(float64(Style.ButtonBgColor.B) * 0.95), 255} onSelected := b.onSelected if onSelected == nil { b.Unlock() @@ -127,18 +136,22 @@ func (b *Button) HandleMouse(cursor image.Point, pressed bool, clicked bool) (ha func (b *Button) Draw(screen *ebiten.Image) error { r := b.rect - // Draw background. - screen.SubImage(r).(*ebiten.Image).Fill(Style.ButtonBgColor) - // Draw label. b.field.Draw(screen) // Draw border. if b.borderSize != 0 { - screen.SubImage(image.Rect(r.Min.X, r.Min.Y, r.Min.X+b.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+b.borderSize)).(*ebiten.Image).Fill(b.borderTop) - screen.SubImage(image.Rect(r.Max.X-b.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-b.borderSize, r.Max.X, r.Max.Y)).(*ebiten.Image).Fill(b.borderBottom) + if !b.pressed { + screen.SubImage(image.Rect(r.Min.X, r.Min.Y, r.Min.X+b.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+b.borderSize)).(*ebiten.Image).Fill(b.borderTop) + screen.SubImage(image.Rect(r.Max.X-b.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-b.borderSize, r.Max.X, r.Max.Y)).(*ebiten.Image).Fill(b.borderBottom) + } else { + screen.SubImage(image.Rect(r.Max.X-b.borderSize, r.Min.Y, r.Max.X, r.Max.Y)).(*ebiten.Image).Fill(b.borderLeft) + screen.SubImage(image.Rect(r.Min.X, r.Max.Y-b.borderSize, r.Max.X, r.Max.Y)).(*ebiten.Image).Fill(b.borderTop) + screen.SubImage(image.Rect(r.Min.X, r.Min.Y, r.Min.X+b.borderSize, r.Max.Y)).(*ebiten.Image).Fill(b.borderRight) + screen.SubImage(image.Rect(r.Min.X, r.Min.Y, r.Max.X, r.Min.Y+b.borderSize)).(*ebiten.Image).Fill(b.borderBottom) + } } return nil diff --git a/doc.go b/doc.go index 8f58ec2..4db8a76 100644 --- a/doc.go +++ b/doc.go @@ -50,5 +50,11 @@ clicked or tapped always receives a final event where clicked and pressed are bo Each time etk draws a widget it subsequently draws all of the widget's children in the order they are returned. + +# Subpackages + +There are two subpackages in etk: messeji and kibodo. These are available for +use without requiring etk. Usually you will not reference any subpackages, as +etk wraps them to provide widgets with additional features. */ package etk diff --git a/select.go b/select.go index 9ae0de6..918a579 100644 --- a/select.go +++ b/select.go @@ -155,6 +155,12 @@ func (s *Select) _setMenuVisible(visible bool) { s.open = visible s.list.SetVisible(visible) s.updateLabel() + + if !visible { + s.background = Style.ButtonBgColor + } else { + s.background = color.RGBA{uint8(float64(Style.ButtonBgColor.R) * 0.95), uint8(float64(Style.ButtonBgColor.G) * 0.95), uint8(float64(Style.ButtonBgColor.B) * 0.95), 255} + } } // HandleKeyboard is called when a keyboard event occurs. @@ -184,10 +190,19 @@ func (s *Select) Draw(screen *ebiten.Image) error { // Draw border. r := s.rect borderSize := Scale(Style.BorderSize) - 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) - + borderLeft, borderTop := Style.BorderColorLeft, Style.BorderColorTop + borderRight, borderBottom := Style.BorderColorRight, Style.BorderColorBottom + 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) + screen.SubImage(image.Rect(r.Max.X-borderSize, r.Min.Y, r.Max.X, r.Max.Y)).(*ebiten.Image).Fill(borderRight) + screen.SubImage(image.Rect(r.Min.X, r.Max.Y-borderSize, r.Max.X, r.Max.Y)).(*ebiten.Image).Fill(borderBottom) + } else { + borderLeft, borderTop, borderRight, borderBottom = borderRight, borderBottom, borderLeft, borderTop + screen.SubImage(image.Rect(r.Max.X-borderSize, r.Min.Y, r.Max.X, r.Max.Y)).(*ebiten.Image).Fill(borderRight) + screen.SubImage(image.Rect(r.Min.X, r.Max.Y-borderSize, r.Max.X, r.Max.Y)).(*ebiten.Image).Fill(borderBottom) + 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) + } return nil } diff --git a/style.go b/style.go index 9c3e445..94c36d4 100644 --- a/style.go +++ b/style.go @@ -39,8 +39,7 @@ type Attributes struct { TextBgColor color.RGBA - BorderSize int - + BorderSize int BorderColorTop color.RGBA BorderColorRight color.RGBA BorderColorBottom color.RGBA @@ -49,8 +48,7 @@ type Attributes struct { ScrollAreaColor color.RGBA ScrollHandleColor color.RGBA - ScrollBorderSize int - + ScrollBorderSize int ScrollBorderColorTop color.RGBA ScrollBorderColorRight color.RGBA ScrollBorderColorBottom color.RGBA @@ -73,8 +71,7 @@ var Style = &Attributes{ TextBgColor: transparent, - BorderSize: 4, - + BorderSize: 4, BorderColorTop: color.RGBA{220, 220, 220, 255}, BorderColorRight: color.RGBA{0, 0, 0, 255}, BorderColorBottom: color.RGBA{0, 0, 0, 255}, @@ -83,8 +80,7 @@ var Style = &Attributes{ ScrollAreaColor: color.RGBA{200, 200, 200, 255}, ScrollHandleColor: color.RGBA{108, 108, 108, 255}, - ScrollBorderSize: 2, - + ScrollBorderSize: 2, ScrollBorderColorTop: color.RGBA{240, 240, 240, 255}, ScrollBorderColorRight: color.RGBA{0, 0, 0, 255}, ScrollBorderColorBottom: color.RGBA{0, 0, 0, 255},