Add SetDebug

This commit is contained in:
Trevor Slocum 2023-11-10 22:17:33 -08:00
parent 702b49df24
commit ffdef73ac8

23
game.go
View file

@ -3,6 +3,7 @@ package etk
import (
"fmt"
"image"
"image/color"
"math"
"runtime/debug"
"strings"
@ -16,6 +17,8 @@ import (
var root Widget
var drawDebug bool
var (
lastWidth, lastHeight int
@ -34,6 +37,8 @@ var (
runeBuffer []rune
)
var debugColor = color.RGBA{0, 0, 255, 255}
const (
backspaceRepeatWait = 500 * time.Millisecond
backspaceRepeatTime = 75 * time.Millisecond
@ -103,6 +108,12 @@ func BoundString(f font.Face, s string) image.Rectangle {
return int26ToRect(bounds)
}
// SetDebug sets whether debug information is drawn on screen. When enabled,
// all visible widgets are outlined.
func SetDebug(debug bool) {
drawDebug = debug
}
// Layout sets the current screen size and resizes the root widget.
func Layout(outsideWidth, outsideHeight int) {
if outsideWidth != lastWidth || outsideHeight != lastHeight {
@ -318,6 +329,18 @@ func draw(w Widget, screen *ebiten.Image) error {
return fmt.Errorf("failed to draw widget: %s", err)
}
if drawDebug {
r := w.Rect()
if !r.Empty() {
x, y := r.Min.X, r.Min.Y
w, h := r.Dx(), r.Dy()
screen.SubImage(image.Rect(x, y, x+w, y+1)).(*ebiten.Image).Fill(debugColor)
screen.SubImage(image.Rect(x, y+h-1, x+w, y+h)).(*ebiten.Image).Fill(debugColor)
screen.SubImage(image.Rect(x, y, x+1, y+h)).(*ebiten.Image).Fill(debugColor)
screen.SubImage(image.Rect(x+w-1, y, x+w, y+h)).(*ebiten.Image).Fill(debugColor)
}
}
children := w.Children()
for _, child := range children {
err = draw(child, screen)