etk/box.go

134 lines
2.7 KiB
Go
Raw Normal View History

2022-06-08 23:35:42 +00:00
package etk
import (
"image"
"image/color"
2022-06-08 23:35:42 +00:00
"sync"
"github.com/hajimehoshi/ebiten/v2"
2022-06-08 23:35:42 +00:00
)
2023-10-29 06:04:32 +00:00
// Box is a building block for other widgets. It may also be used as a spacer
// in layout widgets.
2022-06-08 23:35:42 +00:00
type Box struct {
rect image.Rectangle
children []Widget
background color.RGBA
visible bool
2022-06-08 23:35:42 +00:00
sync.Mutex
}
2023-10-29 06:04:32 +00:00
// NewBox returns a new Box widget.
2022-06-08 23:35:42 +00:00
func NewBox() *Box {
2023-10-25 02:35:38 +00:00
return &Box{
2024-01-23 20:39:08 +00:00
background: transparent,
visible: true,
2023-10-25 02:35:38 +00:00
}
2022-06-08 23:35:42 +00:00
}
2023-10-29 06:04:32 +00:00
// Rect returns the position and size of the widget.
2022-06-08 23:35:42 +00:00
func (b *Box) Rect() image.Rectangle {
b.Lock()
defer b.Unlock()
return b.rect
}
2024-01-12 01:07:01 +00:00
// SetRect sets the position and size of the widget.
2022-06-08 23:35:42 +00:00
func (b *Box) SetRect(r image.Rectangle) {
b.Lock()
b.rect = r
2023-11-08 22:11:36 +00:00
b.Unlock()
for _, w := range b.children {
w.SetRect(r)
}
2022-06-08 23:35:42 +00:00
}
2023-10-29 06:04:32 +00:00
// Background returns the background color of the widget.
func (b *Box) Background() color.RGBA {
b.Lock()
defer b.Unlock()
return b.background
}
2023-10-29 06:04:32 +00:00
// SetBackground sets the background color of the widget.
func (b *Box) SetBackground(background color.RGBA) {
b.Lock()
defer b.Unlock()
b.background = background
}
2023-10-29 06:04:32 +00:00
// Focus returns the focus state of the widget.
2023-10-29 05:23:18 +00:00
func (b *Box) Focus() bool {
return false
2023-10-21 22:30:09 +00:00
}
2023-10-29 06:04:32 +00:00
// SetFocus sets the focus state of the widget.
2023-10-29 05:23:18 +00:00
func (b *Box) SetFocus(focus bool) bool {
return false
2023-10-25 02:35:38 +00:00
}
2023-10-29 05:23:18 +00:00
2023-10-29 06:04:32 +00:00
// Visible returns the visibility of the widget.
2023-10-25 02:35:38 +00:00
func (b *Box) Visible() bool {
return b.visible
}
2023-10-29 06:04:32 +00:00
// SetVisible sets the visibility of the widget.
2023-10-29 05:23:18 +00:00
func (b *Box) SetVisible(visible bool) {
b.visible = visible
2023-10-21 22:30:09 +00:00
}
2024-09-12 05:05:01 +00:00
// Cursor returns the cursor shape shown when a mouse cursor hovers over the
// widget, or -1 to let widgets beneath determine the cursor shape.
func (b *Box) Cursor() ebiten.CursorShapeType {
return -1
}
2023-10-29 06:04:32 +00:00
// HandleKeyboard is called when a keyboard event occurs.
func (b *Box) HandleKeyboard(key ebiten.Key, r rune) (handled bool, err error) {
2023-10-28 05:29:30 +00:00
return false, nil
}
2023-10-29 06:04:32 +00:00
// HandleMouse is called when a mouse event occurs. Only mouse events that
// are on top of the widget are passed to the widget.
func (b *Box) HandleMouse(cursor image.Point, pressed bool, clicked bool) (handled bool, err error) {
return false, nil
}
2023-10-29 06:04:32 +00:00
// Draw draws the widget on the screen.
func (b *Box) Draw(screen *ebiten.Image) error {
return nil
}
// Children returns the children of the widget. Children are drawn in the
// order they are returned. Keyboard and mouse events are passed to children
// in reverse order.
2022-06-08 23:35:42 +00:00
func (b *Box) Children() []Widget {
b.Lock()
defer b.Unlock()
return b.children
}
2023-10-29 06:04:32 +00:00
// AddChild adds a child to the widget.
2022-06-08 23:35:42 +00:00
func (b *Box) AddChild(w ...Widget) {
b.Lock()
defer b.Unlock()
b.children = append(b.children, w...)
}
2023-10-24 19:50:41 +00:00
2023-12-25 08:19:46 +00:00
// Clear removes all children from the widget.
func (b *Box) Clear() {
2023-11-20 18:49:16 +00:00
b.Lock()
defer b.Unlock()
b.children = b.children[:0]
}
var _ Widget = &Box{}