Skip to content
This repository has been archived by the owner on Oct 13, 2021. It is now read-only.

Commit

Permalink
Fix mask issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Olsson committed Jul 16, 2017
1 parent f2d3d29 commit ebebe5e
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 14 deletions.
10 changes: 7 additions & 3 deletions box.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ func (b *Box) Draw(p *Painter) {
if b.border {
p.WithStyle(style, func(p *Painter) {
p.DrawRect(0, 0, sz.X, sz.Y)
p.WithMask(image.Rect(2, 0, sz.X-3, 0)).DrawText(2, 0, b.title)
p.WithMask(image.Rect(2, 0, sz.X-3, 0), func(p *Painter) {
p.DrawText(2, 0, b.title)
})
})

p.Translate(1, 1)
Expand All @@ -104,10 +106,12 @@ func (b *Box) Draw(p *Painter) {
p.Translate(0, off.Y)
}

child.Draw(p.WithMask(image.Rectangle{
p.WithMask(image.Rectangle{
Min: image.ZP,
Max: child.Size().Sub(image.Point{1, 1}),
}))
}, func(p *Painter) {
child.Draw(p)
})

p.Restore()

Expand Down
2 changes: 1 addition & 1 deletion example/audioplayer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func main() {
library := tui.NewTable(0, 0)
library.SetColumnStretch(0, 1)
library.SetColumnStretch(1, 1)
library.SetColumnStretch(2, 2)
library.SetColumnStretch(2, 4)

library.AppendRow(
tui.NewLabel("ARTIST"),
Expand Down
6 changes: 4 additions & 2 deletions grid.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,12 @@ func (g *Grid) Draw(p *Painter) {

if w, ok := g.cells[pos]; ok {
p.Translate(wp.X, wp.Y)
w.Draw(p.WithMask(image.Rectangle{
p.WithMask(image.Rectangle{
Min: image.ZP,
Max: w.Size(),
}.Sub(image.Point{1, 1})))
}.Sub(image.Point{1, 1}), func(p *Painter) {
w.Draw(p)
})
p.Restore()
}
}
Expand Down
5 changes: 3 additions & 2 deletions painter.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,10 @@ func (p *Painter) WithStyle(n string, fn func(*Painter)) {
p.RestoreStyle()
}

func (p *Painter) WithMask(r image.Rectangle) *Painter {
func (p *Painter) WithMask(r image.Rectangle, fn func(*Painter)) {
p.mask = r
return p
fn(p)
p.mask = image.ZR
}

func (p *Painter) mapLocalToWorld(point image.Point) image.Point {
Expand Down
11 changes: 6 additions & 5 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,22 @@ func (t *Table) Draw(p *Painter) {
p.WithStyle(style, func(p *Painter) {
pos := image.Point{i, j}
wp := t.mapCellToLocal(pos)
width := t.colWidths[i]

p.Translate(wp.X, wp.Y)
defer p.Restore()

if w, ok := t.cells[pos]; ok {
size := w.Size()
size.X = width
size.X = t.colWidths[i]

p.FillRect(0, 0, size.X, size.Y)

w.Draw(p.WithMask(image.Rectangle{
p.WithMask(image.Rectangle{
Min: image.ZP,
Max: size.Sub(image.Point{1, 1}),
}))
Max: size,
}.Sub(image.Point{1, 1}), func(p *Painter) {
w.Draw(p)
})
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var drawTableTests = []struct {
┌──────────────────────────────┐
│┌───────────┬──────────┐┌────┐│
││ABC123 │test ││test││
││ │..........││....││
││ │ ││....││
│├───────────┼──────────┤│....││
││DEF456 │testing a ││....││
│├───────────┼──────────┤│....││
Expand Down

0 comments on commit ebebe5e

Please sign in to comment.