Cleaned up code relating to transparency

This commit is contained in:
Sasha Koshka 2023-08-25 02:51:07 -04:00
parent 8ed1352fd4
commit b5f67c65b0
4 changed files with 31 additions and 37 deletions

32
box.go
View File

@ -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())
}

View File

@ -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 () {

View File

@ -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() {

View File

@ -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
}