From a5830c982355ac77fec27a92b74f59f4273e9bf4 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sun, 26 May 2024 15:04:58 -0400 Subject: [PATCH] Add SetTextureCenter --- box.go | 55 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/box.go b/box.go index 5c46d92..f96daca 100644 --- a/box.go +++ b/box.go @@ -9,23 +9,29 @@ import "git.tebibyte.media/tomo/tomo/input" import "git.tebibyte.media/tomo/tomo/event" import "git.tebibyte.media/tomo/tomo/canvas" +type textureMode int; const ( + textureModeTile textureMode = iota + textureModeCenter +) + type box struct { backend *Backend parent parent outer anyBox - bounds image.Rectangle - minSize image.Point - userMinSize image.Point + bounds image.Rectangle + minSize image.Point + userMinSize image.Point innerClippingBounds image.Rectangle minSizeQueued bool focusQueued *bool - padding tomo.Inset - border []tomo.Border - color color.Color - texture *xcanvas.Texture + padding tomo.Inset + border []tomo.Border + color color.Color + texture *xcanvas.Texture + textureMode textureMode dndData data.Data dndAccept []data.Mime @@ -116,12 +122,20 @@ func (this *box) SetColor (c color.Color) { this.invalidateDraw() } -func (this *box) SetTexture (texture canvas.Texture) { - if this.texture == texture { return } +func (this *box) SetTextureTile (texture canvas.Texture) { + if this.texture == texture && this.textureMode == textureModeTile { return } + this.textureMode = textureModeTile this.texture = xcanvas.AssertTexture(texture) 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) { previousBorderSum := this.borderSum() previousBorders := this.border @@ -299,13 +313,30 @@ func (this *box) handleKeyUp (key input.Key, numberPad bool) { func (this *box) Draw (can canvas.Canvas) { if can == nil { return } pen := can.Pen() + bounds := this.Bounds() + + // background pen.Fill(this.color) - pen.Texture(this.texture) - + if this.textureMode == textureModeTile { + pen.Texture(this.texture) + } if this.transparent() && this.parent != nil { 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) {