Add SetTextureCenter

This commit is contained in:
Sasha Koshka 2024-05-26 15:04:58 -04:00
parent dd201f1b5f
commit a5830c9823

55
box.go
View File

@ -9,23 +9,29 @@ import "git.tebibyte.media/tomo/tomo/input"
import "git.tebibyte.media/tomo/tomo/event" import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/tomo/canvas" import "git.tebibyte.media/tomo/tomo/canvas"
type textureMode int; const (
textureModeTile textureMode = iota
textureModeCenter
)
type box struct { type box struct {
backend *Backend backend *Backend
parent parent parent parent
outer anyBox outer anyBox
bounds image.Rectangle bounds image.Rectangle
minSize image.Point minSize image.Point
userMinSize image.Point userMinSize image.Point
innerClippingBounds image.Rectangle innerClippingBounds image.Rectangle
minSizeQueued bool minSizeQueued bool
focusQueued *bool focusQueued *bool
padding tomo.Inset padding tomo.Inset
border []tomo.Border border []tomo.Border
color color.Color color color.Color
texture *xcanvas.Texture texture *xcanvas.Texture
textureMode textureMode
dndData data.Data dndData data.Data
dndAccept []data.Mime dndAccept []data.Mime
@ -116,12 +122,20 @@ func (this *box) SetColor (c color.Color) {
this.invalidateDraw() this.invalidateDraw()
} }
func (this *box) SetTexture (texture canvas.Texture) { func (this *box) SetTextureTile (texture canvas.Texture) {
if this.texture == texture { return } if this.texture == texture && this.textureMode == textureModeTile { return }
this.textureMode = textureModeTile
this.texture = xcanvas.AssertTexture(texture) this.texture = xcanvas.AssertTexture(texture)
this.invalidateDraw() this.invalidateDraw()
} }
func (this *box) SetTextureCenter (texture canvas.Texture) {
if this.texture == texture && this.textureMode == textureModeCenter { return }
this.texture = xcanvas.AssertTexture(texture)
this.textureMode = textureModeCenter
this.invalidateDraw()
}
func (this *box) SetBorder (borders ...tomo.Border) { func (this *box) SetBorder (borders ...tomo.Border) {
previousBorderSum := this.borderSum() previousBorderSum := this.borderSum()
previousBorders := this.border previousBorders := this.border
@ -299,13 +313,30 @@ func (this *box) handleKeyUp (key input.Key, numberPad bool) {
func (this *box) Draw (can canvas.Canvas) { func (this *box) Draw (can canvas.Canvas) {
if can == nil { return } if can == nil { return }
pen := can.Pen() pen := can.Pen()
bounds := this.Bounds()
// background
pen.Fill(this.color) pen.Fill(this.color)
pen.Texture(this.texture) if this.textureMode == textureModeTile {
pen.Texture(this.texture)
}
if this.transparent() && this.parent != nil { if this.transparent() && this.parent != nil {
this.parent.drawBackgroundPart(can) this.parent.drawBackgroundPart(can)
} }
pen.Rectangle(this.Bounds()) pen.Rectangle(bounds)
// centered texture
if this.textureMode == textureModeCenter {
textureBounds := this.texture.Bounds()
textureOrigin := image.Pt (
textureBounds.Dx() / -2,
textureBounds.Dy() / -2).
Add(bounds.Min)
pen.Fill(color.Transparent)
pen.Texture(this.texture)
pen.Rectangle(textureBounds.Sub(textureBounds.Min).Add(textureOrigin))
}
} }
func (this *box) drawBorders (can canvas.Canvas) { func (this *box) drawBorders (can canvas.Canvas) {