It displays things on a screen

This commit is contained in:
2023-07-05 00:44:56 -04:00
parent 208c50a66b
commit 4c79d6772c
5 changed files with 92 additions and 49 deletions

View File

@@ -14,18 +14,19 @@ type Canvas struct {
}
// New creates a new canvas from a bounding rectangle.
func New (x *xgbutil.XUtil, bounds image.Rectangle) Canvas {
return Canvas { xgraphics.New(x, bounds) }
func New (x *xgbutil.XUtil, bounds image.Rectangle) *Canvas {
return NewFrom(xgraphics.New(x, bounds))
}
// NewFrom creates a new canvas from an existing xgraphics.Image.
func NewFrom (image *xgraphics.Image) Canvas {
return Canvas { image }
func NewFrom (image *xgraphics.Image) *Canvas {
if image == nil { return nil }
return &Canvas { image }
}
// Pen returns a new drawing context.
func (this Canvas) Pen () canvas.Pen {
return pen {
func (this *Canvas) Pen () canvas.Pen {
return &pen {
image: this.Image,
gfx: ggfx.Image[uint8] {
Pix: this.Image.Pix,
@@ -37,16 +38,25 @@ func (this Canvas) Pen () canvas.Pen {
}
// Clip returns a sub-canvas of this canvas.
func (this Canvas) Clip (bounds image.Rectangle) canvas.Canvas {
return Canvas { this.Image.SubImage(bounds).(*xgraphics.Image) }
func (this *Canvas) Clip (bounds image.Rectangle) canvas.Canvas {
this.assert()
subImage := this.Image.SubImage(bounds)
if subImage == nil { return nil }
xImage := subImage.(*xgraphics.Image)
return &Canvas { xImage }
}
// Push pushes this canvas to the screen.
func (this Canvas) Push (window xproto.Window) {
func (this *Canvas) Push (window xproto.Window) {
this.assert()
this.XDraw()
this.XExpPaint(window, this.Bounds().Min.X, this.Bounds().Min.Y)
}
func (this *Canvas) assert () {
if this == nil { panic("nil canvas") }
}
// TODO: we need to implement:
// - cap
// - joint
@@ -65,7 +75,7 @@ type pen struct {
fill [4]uint8
}
func (this pen) Rectangle (bounds image.Rectangle) {
func (this *pen) Rectangle (bounds image.Rectangle) {
if this.weight == 0 {
this.gfx.FillRectangle(this.fill[:], bounds)
} else {
@@ -73,7 +83,7 @@ func (this pen) Rectangle (bounds image.Rectangle) {
}
}
func (this pen) Path (points ...image.Point) {
func (this *pen) Path (points ...image.Point) {
if this.weight == 0 {
this.gfx.FillPolygon(this.fill[:], points...)
} else if this.closed {
@@ -83,21 +93,21 @@ func (this pen) Path (points ...image.Point) {
}
}
func (this pen) Closed (closed bool) { this.closed = closed }
func (this pen) Cap (endCap canvas.Cap) { this.endCap = endCap }
func (this pen) Joint (joint canvas.Joint) { this.joint = joint }
func (this pen) StrokeWeight (weight int) { this.weight = weight }
func (this pen) StrokeAlign (align canvas.StrokeAlign) { this.align = align }
func (this *pen) Closed (closed bool) { this.closed = closed }
func (this *pen) Cap (endCap canvas.Cap) { this.endCap = endCap }
func (this *pen) Joint (joint canvas.Joint) { this.joint = joint }
func (this *pen) StrokeWeight (weight int) { this.weight = weight }
func (this *pen) StrokeAlign (align canvas.StrokeAlign) { this.align = align }
func (this pen) Stroke (stroke color.Color) { this.stroke = convertColor(stroke) }
func (this pen) Fill (fill color.Color) { this.fill = convertColor(fill) }
func (this *pen) Stroke (stroke color.Color) { this.stroke = convertColor(stroke) }
func (this *pen) Fill (fill color.Color) { this.fill = convertColor(fill) }
func convertColor (c color.Color) [4]uint8 {
r, g, b, a := c.RGBA()
return [4]uint8 {
uint8(b >> 8),
uint8(g >> 8),
uint8(a >> 8),
uint8(r >> 8),
uint8(a >> 8),
}
}