andlabs-ui/draw.go

455 lines
11 KiB
Go
Raw Normal View History

2015-12-13 11:52:16 -06:00
// 13 december 2015
2015-12-13 11:53:37 -06:00
package ui
2015-12-13 11:52:16 -06:00
// #include "pkgui.h"
2015-12-13 11:52:16 -06:00
import "C"
// DrawPath represents a geometric path in a drawing context.
2015-12-13 11:52:16 -06:00
// This is the basic unit of drawing: all drawing operations consist of
// forming a path, then stroking, filling, or clipping to that path.
// A path is an OS resource; you must explicitly free it when finished.
// Paths consist of multiple figures. Once you have added all the
// figures to a path, you must "end" the path to make it ready to draw
// with.
// TODO rewrite all that
//
// Or more visually, the lifecycle of a Path is
// p := DrawNewPath()
2015-12-13 11:52:16 -06:00
// for every figure {
// p.NewFigure(...) // or NewFigureWithArc
// p.LineTo(...) // any number of these in any order
// p.ArcTo(...)
// p.BezierTo(...)
// if figure should be closed {
// p.CloseFigure()
// }
// }
// p.End()
// // ...
// dp.Context.Stroke(p, ...) // any number of these in any order
// dp.Context.Fill(p, ...)
// dp.Context.Clip(p)
// // ...
// p.Free() // when done with the path
2015-12-16 13:03:47 -06:00
//
// A DrawPath also defines its fill mode. (This should ideally be a fill
2015-12-16 13:03:47 -06:00
// parameter, but some implementations prevent it.)
// TODO talk about fill modes
type DrawPath struct {
2015-12-13 11:52:16 -06:00
p *C.uiDrawPath
}
2015-12-16 13:03:47 -06:00
// TODO
//
// TODO disclaimer
type DrawFillMode uint
2015-12-16 13:03:47 -06:00
const (
DrawFillModeWinding DrawFillMode = iota
DrawFillModeAlternate
2015-12-16 13:03:47 -06:00
)
// DrawNewPath creates a new DrawPath with the given fill mode.
func DrawNewPath(fillMode DrawFillMode) *DrawPath {
2015-12-16 13:03:47 -06:00
var fm C.uiDrawFillMode
switch fillMode {
case DrawFillModeWinding:
2015-12-16 13:03:47 -06:00
fm = C.uiDrawFillModeWinding
case DrawFillModeAlternate:
2015-12-16 13:03:47 -06:00
fm = C.uiDrawFillModeAlternate
default:
panic("invalid fill mode passed to ui.NewPath()")
}
return &DrawPath{
2015-12-16 13:03:47 -06:00
p: C.uiDrawNewPath(fm),
2015-12-13 11:52:16 -06:00
}
}
// Free destroys a DrawPath. After calling Free the DrawPath cannot
// be used.
func (p *DrawPath) Free() {
2015-12-13 11:52:16 -06:00
C.uiDrawFreePath(p.p)
}
// NewFigure starts a new figure in the DrawPath. The current point
2015-12-13 11:52:16 -06:00
// is set to the given point.
func (p *DrawPath) NewFigure(x float64, y float64) {
2015-12-13 11:52:16 -06:00
C.uiDrawPathNewFigure(p.p, C.double(x), C.double(y))
}
// NewFigureWithArc starts a new figure in the DrawPath and adds
// an arc as the first element of the figure. Unlike ArcTo,
// NewFigureWithArc does not draw an initial line segment.
// Otherwise, see ArcTo.
func (p *DrawPath) NewFigureWithArc(xCenter float64, yCenter float64, radius float64, startAngle float64, sweep float64, isNegative bool) {
2015-12-13 11:52:16 -06:00
C.uiDrawPathNewFigureWithArc(p.p,
C.double(xCenter), C.double(yCenter),
C.double(radius),
C.double(startAngle), C.double(sweep),
frombool(isNegative))
}
// LineTo adds a line to the current figure of the DrawPath starting
// from the current point and ending at the given point. The current
// point is set to the ending point.
func (p *DrawPath) LineTo(x float64, y float64) {
2015-12-13 11:52:16 -06:00
C.uiDrawPathLineTo(p.p, C.double(x), C.double(y))
}
// ArcTo adds a circular arc to the current figure of the DrawPath.
2015-12-13 11:52:16 -06:00
// You pass it the center of the arc, its radius in radians, the starting
// angle (couterclockwise) in radians, and the number of radians the
// arc should sweep (counterclockwise). A line segment is drawn from
// the current point to the start of the arc. The current point is set to
// the end of the arc.
func (p *DrawPath) ArcTo(xCenter float64, yCenter float64, radius float64, startAngle float64, sweep float64, isNegative bool) {
2015-12-13 11:52:16 -06:00
C.uiDrawPathArcTo(p.p,
C.double(xCenter), C.double(yCenter),
C.double(radius),
C.double(startAngle), C.double(sweep),
frombool(isNegative))
}
// BezierTo adds a cubic Bezier curve to the current figure of the
// DrawPath. Its start point is the current point. c1x and c1y are the
// first control point. c2x and c2y are the second control point. endX
// and endY are the end point. The current point is set to the end
// point.
func (p *DrawPath) BezierTo(c1x float64, c1y float64, c2x float64, c2y float64, endX float64, endY float64) {
2015-12-13 11:52:16 -06:00
C.uiDrawPathBezierTo(p.p,
C.double(c1x), C.double(c1y),
C.double(c2x), C.double(c2y),
C.double(endX), C.double(endY))
}
// CloseFigure draws a line segment from the current point of the
// current figure of the DrawPath back to its initial point. After calling
// this, the current figure is over and you must either start a new
// figure or end the DrawPath. If this is not called and you start a
// new figure or end the DrawPath, then the current figure will not
// have this closing line segment added to it (but the figure will still
// be over).
func (p *DrawPath) CloseFigure() {
2015-12-13 11:52:16 -06:00
C.uiDrawPathCloseFigure(p.p)
}
// AddRectangle creates a new figure in the DrawPath that consists
// entirely of a rectangle whose top-left corner is at the given point
// and whose size is the given size. The rectangle is a closed figure;
// you must either start a new figure or end the Path after calling
// this method.
func (p *DrawPath) AddRectangle(x float64, y float64, width float64, height float64) {
2015-12-13 11:52:16 -06:00
C.uiDrawPathAddRectangle(p.p, C.double(x), C.double(y), C.double(width), C.double(height))
}
// End ends the current DrawPath. You cannot add figures to a
// DrawPath that has been ended. You cannot draw with a
// DrawPath that has not been ended.
func (p *DrawPath) End() {
2015-12-13 11:52:16 -06:00
C.uiDrawPathEnd(p.p)
}
// DrawContext represents a drawing surface that you can draw to.
// At present the only DrawContexts are surfaces associated with
// Areas and are provided by package ui; see AreaDrawParams.
type DrawContext struct {
c *C.uiDrawContext
}
2015-12-20 11:24:10 -06:00
// DrawBrushType defines the various types of brushes.
2015-12-20 11:24:10 -06:00
//
// TODO disclaimer
type DrawBrushType int
2015-12-20 11:24:10 -06:00
const (
DrawBrushTypeSolid DrawBrushType = iota
DrawBrushTypeLinearGradient
DrawBrushTypeRadialGradient
DrawBrushTypeImage // presently unimplemented
2015-12-20 11:24:10 -06:00
)
// TODO
//
// TODO disclaimer
2016-01-16 18:47:36 -06:00
// TODO rename these to put LineCap at the beginning? or just Cap?
type DrawLineCap int
2015-12-20 11:24:10 -06:00
const (
DrawLineCapFlat DrawLineCap = iota
DrawLineCapRound
DrawLineCapSquare
2015-12-20 11:24:10 -06:00
)
// TODO
//
// TODO disclaimer
type DrawLineJoin int
2015-12-20 11:24:10 -06:00
const (
DrawLineJoinMiter DrawLineJoin = iota
DrawLineJoinRound
DrawLineJoinBevel
2015-12-20 11:24:10 -06:00
)
// TODO document
const DrawDefaultMiterLimit = 10.0
2015-12-20 11:24:10 -06:00
// TODO
type DrawBrush struct {
Type DrawBrushType
2015-12-20 11:24:10 -06:00
// If Type is Solid.
// TODO
R float64
G float64
B float64
A float64
// If Type is LinearGradient or RadialGradient.
// TODO
X0 float64 // start point for both
Y0 float64
X1 float64 // linear: end point; radial: circle center
Y1 float64
OuterRadius float64 // for radial gradients only
Stops []DrawGradientStop
2015-12-20 11:24:10 -06:00
}
// TODO
type DrawGradientStop struct {
2015-12-20 11:24:10 -06:00
Pos float64 // between 0 and 1 inclusive
R float64
G float64
B float64
A float64
}
func (b *DrawBrush) toLibui() *C.uiDrawBrush {
cb := C.pkguiAllocBrush()
2015-12-20 11:24:10 -06:00
cb.Type = C.uiDrawBrushType(b.Type)
switch b.Type {
case DrawBrushTypeSolid:
2015-12-20 11:24:10 -06:00
cb.R = C.double(b.R)
cb.G = C.double(b.G)
cb.B = C.double(b.B)
cb.A = C.double(b.A)
case DrawBrushTypeLinearGradient, DrawBrushTypeRadialGradient:
2015-12-20 11:24:10 -06:00
cb.X0 = C.double(b.X0)
cb.Y0 = C.double(b.Y0)
cb.X1 = C.double(b.X1)
cb.Y1 = C.double(b.Y1)
cb.OuterRadius = C.double(b.OuterRadius)
cb.NumStops = C.size_t(len(b.Stops))
cb.Stops = C.pkguiAllocGradientStops(cb.NumStops)
2015-12-20 11:24:10 -06:00
for i, s := range b.Stops {
C.pkguiSetGradientStop(cb.Stops, C.size_t(i),
2015-12-20 11:24:10 -06:00
C.double(s.Pos),
C.double(s.R),
C.double(s.G),
C.double(s.B),
C.double(s.A))
}
case DrawBrushTypeImage:
2015-12-20 11:24:10 -06:00
panic("unimplemented")
default:
panic("invalid brush type in Brush.toLibui()")
2015-12-20 11:24:10 -06:00
}
return cb
}
func freeBrush(cb *C.uiDrawBrush) {
if cb.Type == C.uiDrawBrushTypeLinearGradient || cb.Type == C.uiDrawBrushTypeRadialGradient {
C.pkguiFreeGradientStops(cb.Stops)
}
C.pkguiFreeBrush(cb)
}
2015-12-20 11:24:10 -06:00
// TODO
type DrawStrokeParams struct {
Cap DrawLineCap
Join DrawLineJoin
2015-12-20 11:24:10 -06:00
Thickness float64
MiterLimit float64
Dashes []float64
DashPhase float64
}
func (sp *DrawStrokeParams) toLibui() *C.uiDrawStrokeParams {
csp := C.pkguiAllocStrokeParams()
2015-12-20 11:24:10 -06:00
csp.Cap = C.uiDrawLineCap(sp.Cap)
csp.Join = C.uiDrawLineJoin(sp.Join)
csp.Thickness = C.double(sp.Thickness)
csp.MiterLimit = C.double(sp.MiterLimit)
csp.Dashes = nil
csp.NumDashes = C.size_t(len(sp.Dashes))
if csp.NumDashes != 0 {
csp.Dashes = C.pkguiAllocDashes(csp.NumDashes)
2015-12-20 11:24:10 -06:00
for i, d := range sp.Dashes {
C.pkguiSetDash(csp.Dashes, C.size_t(i), C.double(d))
2015-12-20 11:24:10 -06:00
}
}
csp.DashPhase = C.double(sp.DashPhase)
return csp
}
func freeStrokeParams(csp *C.uiDrawStrokeParams) {
if csp.Dashes != nil {
C.pkguiFreeDashes(csp.Dashes)
}
C.pkguiFreeStrokeParams(csp)
}
2015-12-20 11:24:10 -06:00
// TODO
func (c *DrawContext) Stroke(p *DrawPath, b *DrawBrush, sp *DrawStrokeParams) {
cb := b.toLibui()
defer freeBrush(cb)
csp := sp.toLibui()
defer freeStrokeParams(csp)
2015-12-20 11:24:10 -06:00
C.uiDrawStroke(c.c, p.p, cb, csp)
}
// TODO
func (c *DrawContext) Fill(p *DrawPath, b *DrawBrush) {
cb := b.toLibui()
defer freeBrush(cb)
2015-12-20 11:24:10 -06:00
C.uiDrawFill(c.c, p.p, cb)
}
// TODO
2016-01-24 14:37:03 -06:00
// TODO should the methods of these return self for chaining?
type DrawMatrix struct {
2015-12-20 11:24:10 -06:00
M11 float64
M12 float64
M21 float64
M22 float64
M31 float64
M32 float64
}
// TODO identity matrix
func DrawNewMatrix() *DrawMatrix {
m := new(DrawMatrix)
2015-12-21 22:36:09 -06:00
m.SetIdentity()
return m
}
// TODO
func (m *DrawMatrix) SetIdentity() {
2015-12-20 11:24:10 -06:00
m.M11 = 1
2015-12-21 22:36:09 -06:00
m.M12 = 0
m.M21 = 0
2015-12-20 11:24:10 -06:00
m.M22 = 1
2015-12-21 22:36:09 -06:00
m.M31 = 0
m.M32 = 0
}
func (m *DrawMatrix) toLibui() *C.uiDrawMatrix {
cm := C.pkguiAllocMatrix()
2015-12-21 22:36:09 -06:00
cm.M11 = C.double(m.M11)
cm.M12 = C.double(m.M12)
cm.M21 = C.double(m.M21)
cm.M22 = C.double(m.M22)
cm.M31 = C.double(m.M31)
cm.M32 = C.double(m.M32)
return cm
}
func (m *DrawMatrix) fromLibui(cm *C.uiDrawMatrix) {
2015-12-21 22:36:09 -06:00
m.M11 = float64(cm.M11)
m.M12 = float64(cm.M12)
m.M21 = float64(cm.M21)
m.M22 = float64(cm.M22)
m.M31 = float64(cm.M31)
m.M32 = float64(cm.M32)
C.pkguiFreeMatrix(cm)
2015-12-21 22:36:09 -06:00
}
// TODO
func (m *DrawMatrix) Translate(x float64, y float64) {
cm := m.toLibui()
2015-12-21 22:36:09 -06:00
C.uiDrawMatrixTranslate(cm, C.double(x), C.double(y))
m.fromLibui(cm)
2015-12-21 22:36:09 -06:00
}
// TODO
func (m *DrawMatrix) Scale(xCenter float64, yCenter float64, x float64, y float64) {
cm := m.toLibui()
2015-12-21 22:36:09 -06:00
C.uiDrawMatrixScale(cm,
C.double(xCenter), C.double(yCenter),
C.double(x), C.double(y))
m.fromLibui(cm)
2015-12-21 22:36:09 -06:00
}
// TODO
func (m *DrawMatrix) Rotate(x float64, y float64, amount float64) {
cm := m.toLibui()
2015-12-21 22:36:09 -06:00
C.uiDrawMatrixRotate(cm, C.double(x), C.double(y), C.double(amount))
m.fromLibui(cm)
2015-12-21 22:36:09 -06:00
}
// TODO
func (m *DrawMatrix) Skew(x float64, y float64, xamount float64, yamount float64) {
cm := m.toLibui()
2015-12-21 22:36:09 -06:00
C.uiDrawMatrixSkew(cm,
C.double(x), C.double(y),
C.double(xamount), C.double(yamount))
m.fromLibui(cm)
2015-12-21 22:36:09 -06:00
}
// TODO
func (m *DrawMatrix) Multiply(m2 *DrawMatrix) {
cm := m.toLibui()
cm2 := m2.toLibui()
2015-12-21 22:36:09 -06:00
C.uiDrawMatrixMultiply(cm, cm2)
C.pkguiFreeMatrix(cm2)
m.fromLibui(cm)
2015-12-21 22:36:09 -06:00
}
// TODO
func (m *DrawMatrix) Invertible() bool {
cm := m.toLibui()
2015-12-21 22:36:09 -06:00
res := C.uiDrawMatrixInvertible(cm)
C.pkguiFreeMatrix(cm)
2015-12-21 22:36:09 -06:00
return tobool(res)
}
// TODO
//
// If m is not invertible, false is returned and m is left unchanged.
func (m *DrawMatrix) Invert() bool {
cm := m.toLibui()
2015-12-21 22:36:09 -06:00
res := C.uiDrawMatrixInvert(cm)
m.fromLibui(cm)
2015-12-21 22:36:09 -06:00
return tobool(res)
}
// TODO unimplemented
func (m *DrawMatrix) TransformPoint(x float64, y float64) (xout float64, yout float64) {
2015-12-21 22:36:09 -06:00
panic("TODO")
}
// TODO unimplemented
func (m *DrawMatrix) TransformSize(x float64, y float64) (xout float64, yout float64) {
2015-12-21 22:36:09 -06:00
panic("TODO")
2015-12-20 11:24:10 -06:00
}
// TODO
func (c *DrawContext) Transform(m *DrawMatrix) {
cm := m.toLibui()
2015-12-21 22:36:09 -06:00
C.uiDrawTransform(c.c, cm)
C.pkguiFreeMatrix(cm)
2015-12-21 22:36:09 -06:00
}
// TODO
func (c *DrawContext) Clip(p *DrawPath) {
2015-12-21 22:36:09 -06:00
C.uiDrawClip(c.c, p.p)
}
// TODO
func (c *DrawContext) Save() {
C.uiDrawSave(c.c)
}
// TODO
func (c *DrawContext) Restore() {
C.uiDrawRestore(c.c)
}