mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
936c266b03
The op.Save and Load methods exist to support the need for
transformation, clip, pointer area state to behave as stacks. For
example, layout needs to apply an offset to its children but not
subsequent operations.
Before this change, op.Save and Load were used to save and restore the
state:
ops := new(op.Ops)
// Save state.
state := op.Save(ops)
// Apply offset.
op.Offset(...).Add(ops)
// Draw with offset applied.
draw(ops)
// Restore state.
state.Load()
A drawback with the op.Save mechanism is that there is no direct
connection between the state change and the saving and loading of state.
This causes confusion as to when a Save/Load is needed and who is
responsible for performing them, which leads to subtle bugs and over-use
of Save/Loads.
This change gets rid of the general state stack and replaces it with
per-state stacks. There is now a stack for transformation, clip, pointer
areas, and they can only be restored by the code pushing state to them.
The example above now becomes:
ops := new(op.Ops)
// Push offset to the transformation stack.
stack := op.Offset(...).Push(ops)
// Draw with offset applied.
draw(ops)
// Restore state.
stack.Pop()
For convenience, transformation also be Add'ed if the stack operation is
not required.
Simple state such as the current material no longer has a way to be
restored; it is assumed the client of a PaintOp adds their desired
material operation before it.
API change: replace op.Save/Load with explicit Push/Pop scopes for
op.TransformOps, pointer.AreaOps, clip.Ops.
To ease porting, this change retains a version of op.Save/Load that
saves and restores the transformation and clip stacks. It also retains
an Add method for clip.Op.
Signed-off-by: Elias Naur <mail@eliasnaur.com>
151 lines
3.4 KiB
Go
151 lines
3.4 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
package paint
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"image"
|
|
"image/color"
|
|
"image/draw"
|
|
"math"
|
|
|
|
"gioui.org/f32"
|
|
"gioui.org/internal/opconst"
|
|
"gioui.org/op"
|
|
"gioui.org/op/clip"
|
|
)
|
|
|
|
// ImageOp sets the brush to an image.
|
|
type ImageOp struct {
|
|
uniform bool
|
|
color color.NRGBA
|
|
src *image.RGBA
|
|
|
|
// handle is a key to uniquely identify this ImageOp
|
|
// in a map of cached textures.
|
|
handle interface{}
|
|
}
|
|
|
|
// ColorOp sets the brush to a constant color.
|
|
type ColorOp struct {
|
|
Color color.NRGBA
|
|
}
|
|
|
|
// 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 {
|
|
}
|
|
|
|
// 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
|
|
// ensure that changes to an image is reflected in the display of
|
|
// it.
|
|
func NewImageOp(src image.Image) ImageOp {
|
|
switch src := src.(type) {
|
|
case *image.Uniform:
|
|
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{
|
|
src: src,
|
|
handle: new(int),
|
|
}
|
|
}
|
|
}
|
|
|
|
sz := src.Bounds().Size()
|
|
// Copy the image into a GPU friendly format.
|
|
dst := image.NewRGBA(image.Rectangle{
|
|
Max: sz,
|
|
})
|
|
draw.Draw(dst, dst.Bounds(), src, src.Bounds().Min, draw.Src)
|
|
return ImageOp{
|
|
src: dst,
|
|
handle: new(int),
|
|
}
|
|
}
|
|
|
|
func (i ImageOp) Size() image.Point {
|
|
if i.src == nil {
|
|
return image.Point{}
|
|
}
|
|
return i.src.Bounds().Size()
|
|
}
|
|
|
|
func (i ImageOp) Add(o *op.Ops) {
|
|
if i.uniform {
|
|
ColorOp{
|
|
Color: i.color,
|
|
}.Add(o)
|
|
return
|
|
} else if i.src == nil || i.src.Bounds().Empty() {
|
|
return
|
|
}
|
|
data := o.Write2(opconst.TypeImageLen, i.src, i.handle)
|
|
data[0] = byte(opconst.TypeImage)
|
|
}
|
|
|
|
func (c ColorOp) Add(o *op.Ops) {
|
|
data := o.Write(opconst.TypeColorLen)
|
|
data[0] = byte(opconst.TypeColor)
|
|
data[1] = c.Color.R
|
|
data[2] = c.Color.G
|
|
data[3] = c.Color.B
|
|
data[4] = c.Color.A
|
|
}
|
|
|
|
func (c LinearGradientOp) Add(o *op.Ops) {
|
|
data := o.Write(opconst.TypeLinearGradientLen)
|
|
data[0] = byte(opconst.TypeLinearGradient)
|
|
|
|
bo := binary.LittleEndian
|
|
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 := o.Write(opconst.TypePaintLen)
|
|
data[0] = byte(opconst.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)
|
|
}
|