diff options
Diffstat (limited to 'vendor/gioui.org/op/paint/paint.go')
-rw-r--r-- | vendor/gioui.org/op/paint/paint.go | 104 |
1 files changed, 62 insertions, 42 deletions
diff --git a/vendor/gioui.org/op/paint/paint.go b/vendor/gioui.org/op/paint/paint.go index b4a9a56..1c99297 100644 --- a/vendor/gioui.org/op/paint/paint.go +++ b/vendor/gioui.org/op/paint/paint.go @@ -10,20 +10,15 @@ import ( "math" "gioui.org/f32" - "gioui.org/internal/opconst" + "gioui.org/internal/ops" "gioui.org/op" + "gioui.org/op/clip" ) // ImageOp sets the brush to an image. -// -// Note: the ImageOp may keep a reference to the backing image. -// See NewImageOp for details. type ImageOp struct { - // Rect is the section if the backing image to use. - Rect image.Rectangle - uniform bool - color color.RGBA + color color.NRGBA src *image.RGBA // handle is a key to uniquely identify this ImageOp @@ -33,20 +28,23 @@ type ImageOp struct { // ColorOp sets the brush to a constant color. type ColorOp struct { - Color color.RGBA + Color color.NRGBA } -// PaintOp fills an area with the current brush, respecting the -// current clip path and transformation. +// LinearGradientOp sets the brush to a gradient starting at stop1 with color1 and +// ending at stop2 with color2. +type LinearGradientOp struct { + Stop1 f32.Point + Color1 color.NRGBA + Stop2 f32.Point + Color2 color.NRGBA +} + +// PaintOp fills the current clip area with the current brush. type PaintOp struct { - // Rect is the destination area to paint. If necessary, the brush is - // scaled to cover the rectangle area. - Rect f32.Rectangle } -// NewImageOp creates an ImageOp backed by src. See -// gioui.org/io/system.FrameEvent for a description of when data -// referenced by operations is safe to re-use. +// NewImageOp creates an ImageOp backed by src. // // NewImageOp assumes the backing image is immutable, and may cache a // copy of its contents in a GPU-friendly way. Create new ImageOps to @@ -55,19 +53,15 @@ type PaintOp struct { func NewImageOp(src image.Image) ImageOp { switch src := src.(type) { case *image.Uniform: - col := color.RGBAModel.Convert(src.C).(color.RGBA) + col := color.NRGBAModel.Convert(src.C).(color.NRGBA) return ImageOp{ uniform: true, color: col, } case *image.RGBA: - bounds := src.Bounds() - if bounds.Min == (image.Point{}) && src.Stride == bounds.Dx()*4 { - return ImageOp{ - Rect: src.Bounds(), - src: src, - handle: new(int), - } + return ImageOp{ + src: src, + handle: new(int), } } @@ -78,7 +72,6 @@ func NewImageOp(src image.Image) ImageOp { }) draw.Draw(dst, dst.Bounds(), src, src.Bounds().Min, draw.Src) return ImageOp{ - Rect: dst.Bounds(), src: dst, handle: new(int), } @@ -97,31 +90,58 @@ func (i ImageOp) Add(o *op.Ops) { Color: i.color, }.Add(o) return + } else if i.src == nil || i.src.Bounds().Empty() { + return } - data := o.Write(opconst.TypeImageLen, i.src, i.handle) - data[0] = byte(opconst.TypeImage) - bo := binary.LittleEndian - bo.PutUint32(data[1:], uint32(i.Rect.Min.X)) - bo.PutUint32(data[5:], uint32(i.Rect.Min.Y)) - bo.PutUint32(data[9:], uint32(i.Rect.Max.X)) - bo.PutUint32(data[13:], uint32(i.Rect.Max.Y)) + data := ops.Write2(&o.Internal, ops.TypeImageLen, i.src, i.handle) + data[0] = byte(ops.TypeImage) } func (c ColorOp) Add(o *op.Ops) { - data := o.Write(opconst.TypeColorLen) - data[0] = byte(opconst.TypeColor) + data := ops.Write(&o.Internal, ops.TypeColorLen) + data[0] = byte(ops.TypeColor) data[1] = c.Color.R data[2] = c.Color.G data[3] = c.Color.B data[4] = c.Color.A } -func (d PaintOp) Add(o *op.Ops) { - data := o.Write(opconst.TypePaintLen) - data[0] = byte(opconst.TypePaint) +func (c LinearGradientOp) Add(o *op.Ops) { + data := ops.Write(&o.Internal, ops.TypeLinearGradientLen) + data[0] = byte(ops.TypeLinearGradient) + bo := binary.LittleEndian - bo.PutUint32(data[1:], math.Float32bits(d.Rect.Min.X)) - bo.PutUint32(data[5:], math.Float32bits(d.Rect.Min.Y)) - bo.PutUint32(data[9:], math.Float32bits(d.Rect.Max.X)) - bo.PutUint32(data[13:], math.Float32bits(d.Rect.Max.Y)) + bo.PutUint32(data[1:], math.Float32bits(c.Stop1.X)) + bo.PutUint32(data[5:], math.Float32bits(c.Stop1.Y)) + bo.PutUint32(data[9:], math.Float32bits(c.Stop2.X)) + bo.PutUint32(data[13:], math.Float32bits(c.Stop2.Y)) + + data[17+0] = c.Color1.R + data[17+1] = c.Color1.G + data[17+2] = c.Color1.B + data[17+3] = c.Color1.A + data[21+0] = c.Color2.R + data[21+1] = c.Color2.G + data[21+2] = c.Color2.B + data[21+3] = c.Color2.A +} + +func (d PaintOp) Add(o *op.Ops) { + data := ops.Write(&o.Internal, ops.TypePaintLen) + data[0] = byte(ops.TypePaint) +} + +// FillShape fills the clip shape with a color. +func FillShape(ops *op.Ops, c color.NRGBA, shape clip.Op) { + defer shape.Push(ops).Pop() + Fill(ops, c) +} + +// Fill paints an infinitely large plane with the provided color. It +// is intended to be used with a clip.Op already in place to limit +// the painted area. Use FillShape unless you need to paint several +// times within the same clip.Op. +func Fill(ops *op.Ops, c color.NRGBA) { + ColorOp{Color: c}.Add(ops) + PaintOp{}.Add(ops) } |