ui: make the reference list argument to Ops.Write variadic

Makes the code nicer.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-06-03 14:07:56 +02:00
parent 402837bbfa
commit 955fe1bc77
6 changed files with 25 additions and 21 deletions
+3 -3
View File
@@ -34,7 +34,7 @@ func (i OpImage) Add(o *ui.Ops) {
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))
o.Write(data, []interface{}{i.Img})
o.Write(data, i.Img)
}
func (i *OpImage) Decode(data []byte, refs []interface{}) {
@@ -65,7 +65,7 @@ func (c OpColor) Add(o *ui.Ops) {
data[2] = c.Col.G
data[3] = c.Col.B
data[4] = c.Col.A
o.Write(data, nil)
o.Write(data)
}
func (c *OpColor) Decode(data []byte, refs []interface{}) {
@@ -90,7 +90,7 @@ func (d OpDraw) Add(o *ui.Ops) {
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))
o.Write(data, nil)
o.Write(data)
}
func (d *OpDraw) Decode(data []byte, refs []interface{}) {
+2 -2
View File
@@ -35,7 +35,7 @@ func (p opClip) Add(o *ui.Ops) {
bo.PutUint32(data[5:], math.Float32bits(p.bounds.Min.Y))
bo.PutUint32(data[9:], math.Float32bits(p.bounds.Max.X))
bo.PutUint32(data[13:], math.Float32bits(p.bounds.Max.Y))
o.Write(data, nil)
o.Write(data)
}
// MoveTo moves the pen to the given position.
@@ -246,7 +246,7 @@ func (p *PathBuilder) vertex(o *ui.Ops, cornerx, cornery int16, ctrl, to f32.Poi
bo.PutUint32(data[21:], math.Float32bits(v.CtrlY))
bo.PutUint32(data[25:], math.Float32bits(v.ToX))
bo.PutUint32(data[29:], math.Float32bits(v.ToY))
o.Write(data, nil)
o.Write(data)
}
func (p *PathBuilder) simpleQuadTo(ops *ui.Ops, ctrl, to f32.Point) {
+2 -2
View File
@@ -74,7 +74,7 @@ func (h OpHandler) Add(o *ui.Ops) {
if h.Focus {
data[1] = 1
}
o.Write(data, []interface{}{h.Key})
o.Write(data, h.Key)
}
func (h *OpHandler) Decode(d []byte, refs []interface{}) {
@@ -90,7 +90,7 @@ func (h *OpHandler) Decode(d []byte, refs []interface{}) {
func (h OpHideInput) Add(o *ui.Ops) {
data := make([]byte, ops.TypeHideInputLen)
data[0] = byte(ops.TypeHideInput)
o.Write(data, nil)
o.Write(data)
}
func (Edit) ImplementsEvent() {}
+13 -9
View File
@@ -77,18 +77,18 @@ type opAux struct {
}
func (p OpPush) Add(o *Ops) {
o.Write([]byte{byte(ops.TypePush)}, nil)
o.Write([]byte{byte(ops.TypePush)})
}
func (p OpPop) Add(o *Ops) {
o.Write([]byte{byte(ops.TypePop)}, nil)
o.Write([]byte{byte(ops.TypePop)})
}
// Begin a block of ops.
func (o *Ops) Begin() {
o.stack = append(o.stack, o.ops.pc())
// Make room for a block definition. Filled out in End.
o.Write(make([]byte, ops.TypeBlockDefLen), nil)
o.Write(make([]byte, ops.TypeBlockDefLen))
}
func (op *opAux) decode(data []byte) {
@@ -152,13 +152,17 @@ func (d *opsData) reset() {
d.version++
}
func (d *opsData) write(op []byte, refs []interface{}) {
func (d *opsData) write(op []byte, refs ...interface{}) {
d.data = append(d.data, op...)
d.refs = append(d.refs, refs...)
}
func (o *Ops) Write(op []byte, refs []interface{}) {
switch ops.OpType(op[0]) {
func (o *Ops) Write(op []byte, refs ...interface{}) {
t := ops.OpType(op[0])
if len(refs) != t.NumRefs() {
panic("invalid ref count")
}
switch t {
case ops.TypeAux:
// Write only the data.
op = op[1:]
@@ -168,7 +172,7 @@ func (o *Ops) Write(op []byte, refs []interface{}) {
o.auxLen = 0
header := make([]byte, ops.TypeAuxLen)
header[0] = byte(ops.TypeAux)
o.ops.write(header, nil)
o.ops.write(header)
}
o.auxLen += len(op)
default:
@@ -178,7 +182,7 @@ func (o *Ops) Write(op []byte, refs []interface{}) {
bo.PutUint32(o.ops.data[o.auxOff+1:], uint32(o.auxLen))
}
}
o.ops.write(op, refs)
o.ops.write(op, refs...)
}
func (d *opsData) pc() pc {
@@ -210,7 +214,7 @@ func (b OpBlock) Add(o *Ops) {
bo.PutUint32(data[1:], uint32(b.pc.data))
bo.PutUint32(data[5:], uint32(b.pc.refs))
bo.PutUint32(data[9:], uint32(b.version))
o.Write(data, []interface{}{b.ops})
o.Write(data, b.ops)
}
// Reset start reading from the op list.
+2 -2
View File
@@ -98,7 +98,7 @@ func (op OpArea) Add(o *ui.Ops) {
bo := binary.LittleEndian
bo.PutUint32(data[2:], uint32(op.size.X))
bo.PutUint32(data[6:], uint32(op.size.Y))
o.Write(data, nil)
o.Write(data)
}
func (op *OpArea) decode(d []byte) {
@@ -148,7 +148,7 @@ func (h OpHandler) Add(o *ui.Ops) {
if h.Grab {
data[1] = 1
}
o.Write(data, []interface{}{h.Key})
o.Write(data, h.Key)
}
func (h *OpHandler) Decode(d []byte, refs []interface{}) {
+3 -3
View File
@@ -77,7 +77,7 @@ func (r OpRedraw) Add(o *Ops) {
bo.PutUint64(data[1:], uint64(nanos))
}
}
o.Write(data, nil)
o.Write(data)
}
func (r *OpRedraw) Decode(d []byte) {
@@ -110,7 +110,7 @@ func (t OpTransform) Add(o *Ops) {
bo := binary.LittleEndian
bo.PutUint32(data[1:], math.Float32bits(t.Transform.offset.X))
bo.PutUint32(data[5:], math.Float32bits(t.Transform.offset.Y))
o.Write(data, nil)
o.Write(data)
}
func (t *OpTransform) Decode(d []byte) {
@@ -129,7 +129,7 @@ func (t *OpTransform) Decode(d []byte) {
func (l OpLayer) Add(o *Ops) {
data := make([]byte, ops.TypeLayerLen)
data[0] = byte(ops.TypeLayer)
o.Write(data, nil)
o.Write(data)
}
func (l *OpLayer) Decode(d []byte) {