gioui.org/ui@v0.0.0-20190926171558-ce74bc0cbaea/ui.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package ui
     4  
     5  import (
     6  	"encoding/binary"
     7  	"math"
     8  	"time"
     9  
    10  	"gioui.org/ui/f32"
    11  	"gioui.org/ui/internal/opconst"
    12  )
    13  
    14  // Config define the essential properties of
    15  // the environment.
    16  type Config interface {
    17  	// Now returns the current animation time.
    18  	Now() time.Time
    19  	// Px converts a Value to pixels.
    20  	Px(v Value) int
    21  }
    22  
    23  // InvalidateOp requests a redraw at the given time. Use
    24  // the zero value to request an immediate redraw.
    25  type InvalidateOp struct {
    26  	At time.Time
    27  }
    28  
    29  // TransformOp applies a transform to the current transform.
    30  type TransformOp struct {
    31  	// TODO: general transformations.
    32  	offset f32.Point
    33  }
    34  
    35  func (r InvalidateOp) Add(o *Ops) {
    36  	data := make([]byte, opconst.TypeRedrawLen)
    37  	data[0] = byte(opconst.TypeInvalidate)
    38  	bo := binary.LittleEndian
    39  	// UnixNano cannot represent the zero time.
    40  	if t := r.At; !t.IsZero() {
    41  		nanos := t.UnixNano()
    42  		if nanos > 0 {
    43  			bo.PutUint64(data[1:], uint64(nanos))
    44  		}
    45  	}
    46  	o.Write(data)
    47  }
    48  
    49  // Offset the transformation.
    50  func (t TransformOp) Offset(o f32.Point) TransformOp {
    51  	return t.Multiply(TransformOp{o})
    52  }
    53  
    54  // Invert the transformation.
    55  func (t TransformOp) Invert() TransformOp {
    56  	return TransformOp{offset: t.offset.Mul(-1)}
    57  }
    58  
    59  // Transform a point.
    60  func (t TransformOp) Transform(p f32.Point) f32.Point {
    61  	return p.Add(t.offset)
    62  }
    63  
    64  // Multiply by a transformation.
    65  func (t TransformOp) Multiply(t2 TransformOp) TransformOp {
    66  	return TransformOp{
    67  		offset: t.offset.Add(t2.offset),
    68  	}
    69  }
    70  
    71  func (t TransformOp) Add(o *Ops) {
    72  	data := make([]byte, opconst.TypeTransformLen)
    73  	data[0] = byte(opconst.TypeTransform)
    74  	bo := binary.LittleEndian
    75  	bo.PutUint32(data[1:], math.Float32bits(t.offset.X))
    76  	bo.PutUint32(data[5:], math.Float32bits(t.offset.Y))
    77  	o.Write(data)
    78  }