Normalized names in draw.go; started migrating zz_histogram.go to boot.

This commit is contained in:
Pietro Gagliardi 2018-08-26 14:42:43 -04:00
parent c90187046a
commit ba8f53fe3e
2 changed files with 121 additions and 115 deletions

View File

@ -31,7 +31,7 @@ const (
// helper to quickly set a brush color
func mkSolidBrush(color uint32, alpha float64) *ui.Brush {
brush := new(ui.Brush)
brush.Type = ui.BrushTypeSolid
brush.Type = ui.DrawBrushTypeSolid
component := uint8((color >> 16) & 0xFF)
brush.R = float64(component) / 255
component = uint8((color >> 8) & 0xFF)
@ -66,7 +66,7 @@ func pointLocations(width, height float64) (xs, ys [10]float64) {
func constructGraph(width, height float64, extend bool) *ui.Path {
xs, ys := pointLocations(width, height)
path := ui.NewPath(ui.Winding)
path := ui.NewPath(ui.DrawFillModeWinding)
path.NewFigure(xs[0], ys[0])
for i := 1; i < 10; i++ {
@ -93,7 +93,7 @@ type areaHandler struct{}
func (areaHandler) Draw(a *ui.Area, p *ui.AreaDrawParams) {
// fill the area with white
brush := mkSolidBrush(colorWhite, 1.0)
path := ui.NewPath(ui.Winding)
path := ui.NewPath(ui.DrawFillModeWinding)
path.AddRectangle(0, 0, p.AreaWidth, p.AreaHeight)
path.End()
p.Context.Fill(path, brush)
@ -110,7 +110,7 @@ func (areaHandler) Draw(a *ui.Area, p *ui.AreaDrawParams) {
// draw the axes
brush = mkSolidBrush(colorBlack, 1.0)
path = ui.NewPath(ui.Winding)
path = ui.NewPath(ui.DrawFillModeWinding)
path.NewFigure(xoffLeft, yoffTop)
path.LineTo(xoffLeft, yoffTop + graphHeight)
path.LineTo(xoffLeft + graphWidth, yoffTop + graphHeight)
@ -125,7 +125,7 @@ func (areaHandler) Draw(a *ui.Area, p *ui.AreaDrawParams) {
// now get the color for the graph itself and set up the brush
graphR, graphG, graphB, graphA := colorButton.Color()
brush.Type = ui.BrushTypeSolid
brush.Type = ui.DrawBrushTypeSolid
brush.R = graphR
brush.G = graphG
brush.B = graphB
@ -146,7 +146,7 @@ func (areaHandler) Draw(a *ui.Area, p *ui.AreaDrawParams) {
// now draw the point being hovered over
if currentPoint != -1 {
xs, ys := pointLocations(graphWidth, graphHeight)
path = ui.NewPath(ui.Winding)
path = ui.NewPath(ui.DrawFillModeWinding)
path.NewFigureWithArc(
xs[currentPoint], ys[currentPoint],
pointRadius,

224
draw.go
View File

@ -5,7 +5,7 @@ package ui
// #include "pkgui.h"
import "C"
// Path represents a geometric path in a drawing context.
// DrawPath represents a geometric path in a drawing context.
// 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.
@ -15,7 +15,7 @@ import "C"
// TODO rewrite all that
//
// Or more visually, the lifecycle of a Path is
// p := NewPath()
// p := DrawNewPath()
// for every figure {
// p.NewFigure(...) // or NewFigureWithArc
// p.LineTo(...) // any number of these in any order
@ -33,54 +33,56 @@ import "C"
// // ...
// p.Free() // when done with the path
//
// A Path also defines its fill mode. (This should ideally be a fill
// A DrawPath also defines its fill mode. (This should ideally be a fill
// parameter, but some implementations prevent it.)
// TODO talk about fill modes
type Path struct {
type DrawPath struct {
p *C.uiDrawPath
}
// TODO
//
// TODO disclaimer
type FillMode uint
type DrawFillMode uint
const (
Winding FillMode = iota
Alternate
DrawFillModeWinding DrawFillMode = iota
DrawFillModeAlternate
)
// NewPath creates a new Path with the given fill mode.
func NewPath(fillMode FillMode) *Path {
// DrawNewPath creates a new DrawPath with the given fill mode.
func DrawNewPath(fillMode DrawFillMode) *DrawPath {
var fm C.uiDrawFillMode
switch fillMode {
case Winding:
case DrawFillModeWinding:
fm = C.uiDrawFillModeWinding
case Alternate:
case DrawFillModeAlternate:
fm = C.uiDrawFillModeAlternate
default:
panic("invalid fill mode passed to ui.NewPath()")
}
return &Path{
return &DrawPath{
p: C.uiDrawNewPath(fm),
}
}
// Free destroys a Path. After calling Free the Path cannot be used.
func (p *Path) Free() {
// Free destroys a DrawPath. After calling Free the DrawPath cannot
// be used.
func (p *DrawPath) Free() {
C.uiDrawFreePath(p.p)
}
// NewFigure starts a new figure in the Path. The current point
// NewFigure starts a new figure in the DrawPath. The current point
// is set to the given point.
func (p *Path) NewFigure(x float64, y float64) {
func (p *DrawPath) NewFigure(x float64, y float64) {
C.uiDrawPathNewFigure(p.p, C.double(x), C.double(y))
}
// NewFigureWithArc starts a new figure in the Path 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 *Path) NewFigureWithArc(xCenter float64, yCenter float64, radius float64, startAngle float64, sweep float64, isNegative bool) {
// 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) {
C.uiDrawPathNewFigureWithArc(p.p,
C.double(xCenter), C.double(yCenter),
C.double(radius),
@ -88,20 +90,20 @@ func (p *Path) NewFigureWithArc(xCenter float64, yCenter float64, radius float64
frombool(isNegative))
}
// LineTo adds a line to the current figure of the Path starting from
// the current point and ending at the given point. The current point
// is set to the ending point.
func (p *Path) LineTo(x float64, y float64) {
// 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) {
C.uiDrawPathLineTo(p.p, C.double(x), C.double(y))
}
// ArcTo adds a circular arc to the current figure of the Path.
// ArcTo adds a circular arc to the current figure of the DrawPath.
// 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 *Path) ArcTo(xCenter float64, yCenter float64, radius float64, startAngle float64, sweep float64, isNegative bool) {
func (p *DrawPath) ArcTo(xCenter float64, yCenter float64, radius float64, startAngle float64, sweep float64, isNegative bool) {
C.uiDrawPathArcTo(p.p,
C.double(xCenter), C.double(yCenter),
C.double(radius),
@ -109,11 +111,12 @@ func (p *Path) ArcTo(xCenter float64, yCenter float64, radius float64, startAngl
frombool(isNegative))
}
// BezierTo adds a cubic Bezier curve to the current figure of the Path.
// 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 *Path) BezierTo(c1x float64, c1y float64, c2x float64, c2y float64, endX float64, endY float64) {
// 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) {
C.uiDrawPathBezierTo(p.p,
C.double(c1x), C.double(c1y),
C.double(c2x), C.double(c2y),
@ -121,26 +124,29 @@ func (p *Path) BezierTo(c1x float64, c1y float64, c2x float64, c2y float64, endX
}
// CloseFigure draws a line segment from the current point of the
// current figure of the Path back to its initial point. After calling this,
// the current figure is over and you must either start a new figure
// or end the Path. If this is not called and you start a new figure or
// end the Path, then the current figure will not have this closing line
// segment added to it (but the figure will still be over).
func (p *Path) CloseFigure() {
// 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() {
C.uiDrawPathCloseFigure(p.p)
}
// AddRectangle creates a new figure in the Path 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 *Path) AddRectangle(x float64, y float64, width float64, height float64) {
// 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) {
C.uiDrawPathAddRectangle(p.p, C.double(x), C.double(y), C.double(width), C.double(height))
}
// End ends the current Path. You cannot add figures to a Path that has
// been ended. You cannot draw with a Path that has not been ended.
func (p *Path) End() {
// 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() {
C.uiDrawPathEnd(p.p)
}
@ -151,44 +157,44 @@ type DrawContext struct {
c *C.uiDrawContext
}
// BrushType defines the various types of brushes.
// DrawBrushType defines the various types of brushes.
//
// TODO disclaimer
type BrushType int
type DrawBrushType int
const (
BrushTypeSolid BrushType = iota
BrushTypeLinearGradient
BrushTypeRadialGradient
BrushTypeImage // presently unimplemented
DrawBrushTypeSolid DrawBrushType = iota
DrawBrushTypeLinearGradient
DrawBrushTypeRadialGradient
DrawBrushTypeImage // presently unimplemented
)
// TODO
//
// TODO disclaimer
// TODO rename these to put LineCap at the beginning? or just Cap?
type LineCap int
type DrawLineCap int
const (
FlatCap LineCap = iota
RoundCap
SquareCap
DrawLineCapFlat DrawLineCap = iota
DrawLineCapRound
DrawLineCapSquare
)
// TODO
//
// TODO disclaimer
type LineJoin int
type DrawLineJoin int
const (
MiterJoin LineJoin = iota
RoundJoin
BevelJoin
DrawLineJoinMiter DrawLineJoin = iota
DrawLineJoinRound
DrawLineJoinBevel
)
// TODO document
const DefaultMiterLimit = 10.0
// TODO
type Brush struct {
Type BrushType
type DrawBrush struct {
Type DrawBrushType
// If Type is Solid.
// TODO
@ -204,11 +210,11 @@ type Brush struct {
X1 float64 // linear: end point; radial: circle center
Y1 float64
OuterRadius float64 // for radial gradients only
Stops []GradientStop
Stops []DrawGradientStop
}
// TODO
type GradientStop struct {
type DrawGradientStop struct {
Pos float64 // between 0 and 1 inclusive
R float64
G float64
@ -216,16 +222,16 @@ type GradientStop struct {
A float64
}
func (b *Brush) toC() *C.uiDrawBrush {
func (b *DrawBrush) toLibui() *C.uiDrawBrush {
cb := C.pkguiAllocBrush()
cb.Type = C.uiDrawBrushType(b.Type)
switch b.Type {
case BrushTypeSolid:
case DrawBrushTypeSolid:
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 BrushTypeLinearGradient, BrushTypeRadialGradient:
case DrawBrushTypeLinearGradient, DrawBrushTypeRadialGradient:
cb.X0 = C.double(b.X0)
cb.Y0 = C.double(b.Y0)
cb.X1 = C.double(b.X1)
@ -241,10 +247,10 @@ func (b *Brush) toC() *C.uiDrawBrush {
C.double(s.B),
C.double(s.A))
}
case BrushTypeImage:
case DrawBrushTypeImage:
panic("unimplemented")
default:
panic("invalid brush type in Brush.toC()")
panic("invalid brush type in Brush.toLibui()")
}
return cb
}
@ -257,16 +263,16 @@ func freeBrush(cb *C.uiDrawBrush) {
}
// TODO
type StrokeParams struct {
Cap LineCap
Join LineJoin
type DrawStrokeParams struct {
Cap DrawLineCap
Join DrawLineJoin
Thickness float64
MiterLimit float64
Dashes []float64
DashPhase float64
}
func (sp *StrokeParams) toC() *C.uiDrawStrokeParams {
func (sp *DrawStrokeParams) toLibui() *C.uiDrawStrokeParams {
csp := C.pkguiAllocStrokeParams()
csp.Cap = C.uiDrawLineCap(sp.Cap)
csp.Join = C.uiDrawLineJoin(sp.Join)
@ -292,24 +298,24 @@ func freeStrokeParams(csp *C.uiDrawStrokeParams) {
}
// TODO
func (c *DrawContext) Stroke(p *Path, b *Brush, sp *StrokeParams) {
cb := b.toC()
func (c *DrawContext) Stroke(p *DrawPath, b *DrawBrush, sp *DrawStrokeParams) {
cb := b.toLibui()
defer freeBrush(cb)
csp := sp.toC()
csp := sp.toLibui()
defer freeStrokeParams(csp)
C.uiDrawStroke(c.c, p.p, cb, csp)
}
// TODO
func (c *DrawContext) Fill(p *Path, b *Brush) {
cb := b.toC()
func (c *DrawContext) Fill(p *DrawPath, b *DrawBrush) {
cb := b.toLibui()
defer freeBrush(cb)
C.uiDrawFill(c.c, p.p, cb)
}
// TODO
// TODO should the methods of these return self for chaining?
type Matrix struct {
type DrawMatrix struct {
M11 float64
M12 float64
M21 float64
@ -319,14 +325,14 @@ type Matrix struct {
}
// TODO identity matrix
func NewMatrix() *Matrix {
m := new(Matrix)
func DrawNewMatrix() *DrawMatrix {
m := new(DrawMatrix)
m.SetIdentity()
return m
}
// TODO
func (m *Matrix) SetIdentity() {
func (m *DrawMatrix) SetIdentity() {
m.M11 = 1
m.M12 = 0
m.M21 = 0
@ -335,7 +341,7 @@ func (m *Matrix) SetIdentity() {
m.M32 = 0
}
func (m *Matrix) toC() *C.uiDrawMatrix {
func (m *DrawMatrix) toLibui() *C.uiDrawMatrix {
cm := C.pkguiAllocMatrix()
cm.M11 = C.double(m.M11)
cm.M12 = C.double(m.M12)
@ -346,7 +352,7 @@ func (m *Matrix) toC() *C.uiDrawMatrix {
return cm
}
func (m *Matrix) fromC(cm *C.uiDrawMatrix) {
func (m *DrawMatrix) fromLibui(cm *C.uiDrawMatrix) {
m.M11 = float64(cm.M11)
m.M12 = float64(cm.M12)
m.M21 = float64(cm.M21)
@ -357,49 +363,49 @@ func (m *Matrix) fromC(cm *C.uiDrawMatrix) {
}
// TODO
func (m *Matrix) Translate(x float64, y float64) {
cm := m.toC()
func (m *DrawMatrix) Translate(x float64, y float64) {
cm := m.toLibui()
C.uiDrawMatrixTranslate(cm, C.double(x), C.double(y))
m.fromC(cm)
m.fromLibui(cm)
}
// TODO
func (m *Matrix) Scale(xCenter float64, yCenter float64, x float64, y float64) {
cm := m.toC()
func (m *DrawMatrix) Scale(xCenter float64, yCenter float64, x float64, y float64) {
cm := m.toLibui()
C.uiDrawMatrixScale(cm,
C.double(xCenter), C.double(yCenter),
C.double(x), C.double(y))
m.fromC(cm)
m.fromLibui(cm)
}
// TODO
func (m *Matrix) Rotate(x float64, y float64, amount float64) {
cm := m.toC()
func (m *DrawMatrix) Rotate(x float64, y float64, amount float64) {
cm := m.toLibui()
C.uiDrawMatrixRotate(cm, C.double(x), C.double(y), C.double(amount))
m.fromC(cm)
m.fromLibui(cm)
}
// TODO
func (m *Matrix) Skew(x float64, y float64, xamount float64, yamount float64) {
cm := m.toC()
func (m *DrawMatrix) Skew(x float64, y float64, xamount float64, yamount float64) {
cm := m.toLibui()
C.uiDrawMatrixSkew(cm,
C.double(x), C.double(y),
C.double(xamount), C.double(yamount))
m.fromC(cm)
m.fromLibui(cm)
}
// TODO
func (m *Matrix) Multiply(m2 *Matrix) {
cm := m.toC()
cm2 := m2.toC()
func (m *DrawMatrix) Multiply(m2 *DrawMatrix) {
cm := m.toLibui()
cm2 := m2.toLibui()
C.uiDrawMatrixMultiply(cm, cm2)
C.pkguiFreeMatrix(cm2)
m.fromC(cm)
m.fromLibui(cm)
}
// TODO
func (m *Matrix) Invertible() bool {
cm := m.toC()
func (m *DrawMatrix) Invertible() bool {
cm := m.toLibui()
res := C.uiDrawMatrixInvertible(cm)
C.pkguiFreeMatrix(cm)
return tobool(res)
@ -408,32 +414,32 @@ func (m *Matrix) Invertible() bool {
// TODO
//
// If m is not invertible, false is returned and m is left unchanged.
func (m *Matrix) Invert() bool {
cm := m.toC()
func (m *DrawMatrix) Invert() bool {
cm := m.toLibui()
res := C.uiDrawMatrixInvert(cm)
m.fromC(cm)
m.fromLibui(cm)
return tobool(res)
}
// TODO unimplemented
func (m *Matrix) TransformPoint(x float64, y float64) (xout float64, yout float64) {
func (m *DrawMatrix) TransformPoint(x float64, y float64) (xout float64, yout float64) {
panic("TODO")
}
// TODO unimplemented
func (m *Matrix) TransformSize(x float64, y float64) (xout float64, yout float64) {
func (m *DrawMatrix) TransformSize(x float64, y float64) (xout float64, yout float64) {
panic("TODO")
}
// TODO
func (c *DrawContext) Transform(m *Matrix) {
cm := m.toC()
func (c *DrawContext) Transform(m *DrawMatrix) {
cm := m.toLibui()
C.uiDrawTransform(c.c, cm)
C.pkguiFreeMatrix(cm)
}
// TODO
func (c *DrawContext) Clip(p *Path) {
func (c *DrawContext) Clip(p *DrawPath) {
C.uiDrawClip(c.c, p.p)
}