examples/ui: use a sub-image instead of a buffer image

This commit is contained in:
Hajime Hoshi 2023-11-15 23:05:43 +09:00
parent 2641f65863
commit bf338eb76b

View File

@ -290,7 +290,6 @@ type TextBox struct {
Rect image.Rectangle Rect image.Rectangle
Text string Text string
contentBuf *ebiten.Image
vScrollBar *VScrollBar vScrollBar *VScrollBar
offsetX int offsetX int
offsetY int offsetY int
@ -335,37 +334,18 @@ func (t *TextBox) contentOffset() (int, int) {
func (t *TextBox) Draw(dst *ebiten.Image) { func (t *TextBox) Draw(dst *ebiten.Image) {
drawNinePatches(dst, t.Rect, imageSrcRects[imageTypeTextBox]) drawNinePatches(dst, t.Rect, imageSrcRects[imageTypeTextBox])
// TODO: Use a sub-image of dst instead of an offscreen contentBuf.
// Using a sub-image is better in terms of performance.
if t.contentBuf != nil {
vw, vh := t.viewSize()
w, h := t.contentBuf.Bounds().Dx(), t.contentBuf.Bounds().Dy()
if vw > w || vh > h {
t.contentBuf.Deallocate()
t.contentBuf = nil
}
}
if t.contentBuf == nil {
w, h := t.viewSize()
t.contentBuf = ebiten.NewImage(w, h)
}
t.contentBuf.Clear()
textOp := &text.DrawOptions{} textOp := &text.DrawOptions{}
x := -float64(t.offsetX) + textBoxPaddingLeft x := -float64(t.offsetX) + textBoxPaddingLeft
y := -float64(t.offsetY) + textBoxPaddingTop y := -float64(t.offsetY) + textBoxPaddingTop
textOp.GeoM.Translate(x, y) textOp.GeoM.Translate(x, y)
textOp.GeoM.Translate(float64(t.Rect.Min.X), float64(t.Rect.Min.Y))
textOp.ColorScale.ScaleWithColor(color.Black) textOp.ColorScale.ScaleWithColor(color.Black)
textOp.LineHeight = lineHeight textOp.LineHeight = lineHeight
text.Draw(t.contentBuf, t.Text, &text.GoTextFace{ text.Draw(dst.SubImage(t.Rect).(*ebiten.Image), t.Text, &text.GoTextFace{
Source: uiFaceSource, Source: uiFaceSource,
Size: uiFontSize, Size: uiFontSize,
}, textOp) }, textOp)
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(t.Rect.Min.X), float64(t.Rect.Min.Y))
dst.DrawImage(t.contentBuf, op)
t.vScrollBar.Draw(dst) t.vScrollBar.Draw(dst)
} }