From b5f67c65b0dcc88697b66e1a2b1fea1b70d07f36 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Fri, 25 Aug 2023 02:51:07 -0400 Subject: [PATCH] Cleaned up code relating to transparency --- box.go | 32 ++++++++++++-------------------- containerbox.go | 24 +++++++----------------- textbox.go | 5 +++++ util.go | 7 +++++++ 4 files changed, 31 insertions(+), 37 deletions(-) diff --git a/box.go b/box.go index 634f3c2..776a159 100644 --- a/box.go +++ b/box.go @@ -25,8 +25,6 @@ type box struct { border []tomo.Border color color.Color texture *xcanvas.Texture - - fillTransparent bool dndData data.Data dndAccept []data.Mime @@ -56,7 +54,7 @@ type box struct { func (backend *Backend) newBox (outer anyBox) *box { box := &box { backend: backend, - color: color.White, + color: color.Transparent, outer: outer, drawer: outer, } @@ -118,19 +116,16 @@ func (this *box) SetColor (c color.Color) { if c == nil { c = color.Transparent } if this.color == c { return } this.color = c - this.determineFillTransparency() this.invalidateDraw() } func (this *box) SetTexture (texture canvas.Texture) { this.texture = xcanvas.AssertTexture(texture) - this.determineFillTransparency() this.invalidateDraw() } func (this *box) SetBorder (border ...tomo.Border) { this.border = border - this.determineFillTransparency() this.invalidateLayout() this.invalidateMinimum() } @@ -283,12 +278,12 @@ func (this *box) Draw (can canvas.Canvas) { if can == nil { return } pen := can.Pen() pen.Fill(this.color) - if this.texture == nil || !this.texture.Opaque() { - pen.Rectangle(this.bounds) - } - if this.texture != nil { - // TODO drawR texture + pen.Texture(this.texture) + + if this.transparent() && this.parent != nil { + this.parent.drawBackgroundPart(can) } + pen.Rectangle(can.Bounds()) } func (this *box) drawBorders (can canvas.Canvas) { @@ -298,8 +293,7 @@ func (this *box) drawBorders (can canvas.Canvas) { rectangle := func (x0, y0, x1, y1 int, c color.Color) { area := image.Rect(x0, y0, x1, y1) - _, _, _, a := c.RGBA() - if a != 0xFFFF && this.parent != nil { + if transparent(c) && this.parent != nil { this.parent.drawBackgroundPart(can.Clip(area)) } pen.Fill(c) @@ -428,13 +422,6 @@ func (this *box) boxUnder (point image.Point) anyBox { } } -func (this *box) determineFillTransparency () { - _, _, _, a := this.color.RGBA() - this.fillTransparent = - a != 0xFFFF && - !(this.texture != nil && this.texture.Opaque()) -} - func (this *box) propagate (callback func (anyBox) bool) bool { return callback(this.outer) } @@ -442,3 +429,8 @@ func (this *box) propagate (callback func (anyBox) bool) bool { func (this *box) propagateAlt (callback func (anyBox) bool) bool { return callback(this.outer) } + +func (this *box) transparent () bool { + return transparent(this.color) && + (this.texture == nil || !this.texture.Opaque()) +} diff --git a/containerbox.go b/containerbox.go index 5d8937f..70670c3 100644 --- a/containerbox.go +++ b/containerbox.go @@ -134,40 +134,30 @@ func (this *containerBox) Draw (can canvas.Canvas) { if can == nil { return } pen := can.Pen() pen.Fill(this.color) + pen.Texture(this.texture) rocks := make([]image.Rectangle, len(this.children)) for index, box := range this.children { rocks[index] = box.Bounds() } for _, tile := range canvas.Shatter(this.bounds, rocks...) { - if this.fillTransparent && this.parent != nil { + if this.transparent() && this.parent != nil { this.parent.drawBackgroundPart(can.Clip(tile)) } - if this.texture == nil || !this.texture.Opaque() { - pen.Rectangle(tile) - } - if this.texture != nil { - // TODO draw texture - } + pen.Rectangle(tile) } } func (this *containerBox) drawBackgroundPart (can canvas.Canvas) { if can == nil { return } - - if this.fillTransparent && this.parent != nil { - this.parent.drawBackgroundPart(can) - } - pen := can.Pen() pen.Fill(this.color) + pen.Texture(this.texture) - if this.texture == nil || !this.texture.Opaque() { - pen.Rectangle(can.Bounds()) - } - if this.texture != nil { - // TODO draw texture + if this.transparent() && this.parent != nil { + this.parent.drawBackgroundPart(can) } + pen.Rectangle(can.Bounds()) } func (this *containerBox) flushActionQueue () { diff --git a/textbox.go b/textbox.go index b98a1c4..7d36dd7 100644 --- a/textbox.go +++ b/textbox.go @@ -146,6 +146,11 @@ func (this *textBox) Draw (can canvas.Canvas) { this.drawBorders(can) pen := can.Pen() pen.Fill(this.color) + pen.Texture(this.texture) + + if this.transparent() && this.parent != nil { + this.parent.drawBackgroundPart(can) + } pen.Rectangle(can.Bounds()) if this.selectable && this.Focused() { diff --git a/util.go b/util.go index 86e1143..9294ce5 100644 --- a/util.go +++ b/util.go @@ -1,5 +1,7 @@ package x +import "image/color" + func indexOf[T comparable] (haystack []T, needle T) int { for index, test := range haystack { if test == needle { @@ -18,3 +20,8 @@ func insert[T any] (slice []T, index int, element T) []T { slice[index] = element return slice } + +func transparent (c color.Color) bool { + _, _, _, a := c.RGBA() + return a != 0xFFFF +}