Add Widget.Clip

This commit is contained in:
Trevor Slocum 2024-11-19 00:49:53 -08:00
parent 53b248a771
commit e8f49c5641
6 changed files with 42 additions and 2 deletions

7
box.go
View file

@ -82,6 +82,13 @@ func (b *Box) SetVisible(visible bool) {
b.visible = visible
}
// Clip returns whether the widget and its children are restricted to drawing
// within the widget's rect area of the screen. For best performance, Clip
// should return false unless clipping is actually needed.
func (b *Box) Clip() bool {
return false
}
// 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 {

11
game.go
View file

@ -429,11 +429,18 @@ func draw(w Widget, screen *ebiten.Image) error {
}
r := w.Rect()
subScreen := screen.SubImage(r).(*ebiten.Image)
subScreen := screen
if w.Clip() {
subScreen = screen.SubImage(r).(*ebiten.Image)
}
background := w.Background()
if background.A > 0 {
subScreen.Fill(background)
if subScreen == screen {
screen.SubImage(r).(*ebiten.Image).Fill(background)
} else {
subScreen.Fill(background)
}
}
err := w.Draw(subScreen)

View file

@ -161,6 +161,13 @@ func (l *List) SetVisible(visible bool) {
l.grid.SetVisible(visible)
}
// Clip returns whether the widget and its children are restricted to drawing
// within the widget's rect area of the screen. For best performance, Clip
// should return false unless clipping is actually needed.
func (l *List) Clip() bool {
return true
}
// SetColumnSizes sets the size of each column. A size of -1 represents an equal
// proportion of the available space.
func (l *List) SetColumnSizes(size ...int) {

View file

@ -58,6 +58,13 @@ func (s *Select) SetRect(r image.Rectangle) {
s.list.SetRect(listRect)
}
// Clip returns whether the widget and its children are restricted to drawing
// within the widget's rect area of the screen. For best performance, Clip
// should return false unless clipping is actually needed.
func (s *Select) Clip() bool {
return false
}
// SetHighlightColor sets the color used to highlight the currently selected item.
func (s *Select) SetHighlightColor(c color.RGBA) {
s.list.SetHighlightColor(c)

View file

@ -34,6 +34,11 @@ type Widget interface {
// SetVisible sets the visibility of the widget.
SetVisible(visible bool)
// Clip returns whether the widget and its children are restricted to drawing
// within the widget's rect area of the screen. For best performance, Clip
// should return false unless clipping is actually needed.
Clip() bool
// Cursor returns the cursor shape shown when a mouse cursor hovers over
// the widget, or -1 to let widgets beneath determine the cursor shape.
Cursor() ebiten.CursorShapeType

View file

@ -55,6 +55,13 @@ func (w *Window) SetRect(r image.Rectangle) {
w.modified = true
}
// Clip returns whether the widget and its children are restricted to drawing
// within the widget's rect area of the screen. For best performance, Clip
// should return false unless clipping is actually needed.
func (w *Window) Clip() bool {
return false
}
// SetFont sets the font and text size of the window titles. Scaling is not applied.
func (w *Window) SetFont(fnt *text.GoTextFaceSource, size int) {
w.Lock()