2016-12-03 09:25:41 -06:00
|
|
|
package pixel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image/color"
|
2016-12-03 12:51:43 -06:00
|
|
|
"math"
|
2016-12-03 09:25:41 -06:00
|
|
|
|
|
|
|
"github.com/faiface/pixel/pixelgl"
|
|
|
|
"github.com/go-gl/mathgl/mgl32"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Drawer is anything that can be drawn. It's by no means a drawer inside your table.
|
|
|
|
//
|
2016-12-30 10:43:26 -06:00
|
|
|
// Drawer consists of a single methods: Draw. Draw methods takes any number of Transform
|
|
|
|
// arguments. It applies these transforms in the reverse order and finally draws something
|
|
|
|
// transformed by these transforms.
|
2016-12-03 09:25:41 -06:00
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// // object is a drawer
|
|
|
|
// object.Draw(pixel.Position(pixel.V(100, 100).Rotate(math.Pi / 2)))
|
|
|
|
// camera := pixel.Camera(pixel.V(0, 0), pixel.V(500, 500), pixel.V(window.Size()))
|
|
|
|
// object.Draw(camera, pixel.Position(0).Scale(0.5))
|
|
|
|
type Drawer interface {
|
|
|
|
Draw(t ...Transform)
|
|
|
|
}
|
|
|
|
|
2016-12-30 10:43:26 -06:00
|
|
|
// Group is used to effeciently handle a collection of objects with a common parent. Usually many
|
|
|
|
// objects share a parent, using a group can significantly increase performance in these cases.
|
2016-12-04 13:28:23 -06:00
|
|
|
//
|
2016-12-30 10:43:26 -06:00
|
|
|
// To use a group, first, create a group and as it's parent use the common parent of the
|
|
|
|
// collection of objects:
|
2016-12-04 13:28:23 -06:00
|
|
|
//
|
|
|
|
// group := pixel.NewGroup(commonParent)
|
|
|
|
//
|
2016-12-30 10:43:26 -06:00
|
|
|
// Then, when creating the objects, use the group as their parent, instead of the original
|
|
|
|
// common parent, but, don't forget to put everything into a With block, like this:
|
2016-12-04 13:28:23 -06:00
|
|
|
//
|
|
|
|
// group.With(func() {
|
|
|
|
// object := newArbitratyObject(group, ...) // group is the parent of the object
|
|
|
|
// })
|
|
|
|
//
|
2016-12-30 10:43:26 -06:00
|
|
|
// When dealing with objects associated with a group, it's always necessary to wrap that into
|
|
|
|
// a With block:
|
2016-12-04 13:28:23 -06:00
|
|
|
//
|
|
|
|
// group.With(func() {
|
|
|
|
// for _, obj := range objectsWithCommonParent {
|
|
|
|
// // do something with obj
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
//
|
|
|
|
// That's all!
|
|
|
|
type Group struct {
|
|
|
|
parent pixelgl.Doer
|
|
|
|
context pixelgl.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGroup creates a new group with the specified parent.
|
|
|
|
func NewGroup(parent pixelgl.Doer) *Group {
|
|
|
|
return &Group{
|
|
|
|
parent: parent,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// With enables the parent of a group and executes sub.
|
|
|
|
func (g *Group) With(sub func()) {
|
|
|
|
g.parent.Do(func(ctx pixelgl.Context) {
|
|
|
|
g.context = ctx
|
|
|
|
sub()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do just passes a cached context to sub.
|
|
|
|
func (g *Group) Do(sub func(pixelgl.Context)) {
|
|
|
|
sub(g.context)
|
|
|
|
}
|
|
|
|
|
2016-12-14 10:53:29 -06:00
|
|
|
// Shape is a general drawable shape constructed from vertices.
|
2016-12-06 10:53:10 -06:00
|
|
|
//
|
2016-12-30 10:43:26 -06:00
|
|
|
// Vertices are specified in the vertex array of a shape. A shape can have a picture, a color
|
|
|
|
// (mask) and a static transform.
|
2016-12-14 10:53:29 -06:00
|
|
|
//
|
2016-12-30 10:43:26 -06:00
|
|
|
// Usually you use this type only indirectly throught other specific shapes (sprites, polygons,
|
|
|
|
// ...) embedding it.
|
2016-12-14 10:53:29 -06:00
|
|
|
type Shape struct {
|
2016-12-06 10:53:10 -06:00
|
|
|
parent pixelgl.Doer
|
2016-12-18 18:11:34 -06:00
|
|
|
picture *Picture
|
2016-12-14 10:53:29 -06:00
|
|
|
color color.Color
|
2016-12-06 10:53:10 -06:00
|
|
|
transform Transform
|
|
|
|
va *pixelgl.VertexArray
|
|
|
|
}
|
|
|
|
|
2016-12-14 10:53:29 -06:00
|
|
|
// NewShape creates a new shape with specified parent, picture, color, transform and vertex array.
|
2016-12-18 18:11:34 -06:00
|
|
|
func NewShape(parent pixelgl.Doer, picture *Picture, c color.Color, transform Transform, va *pixelgl.VertexArray) *Shape {
|
2016-12-14 10:53:29 -06:00
|
|
|
return &Shape{
|
2016-12-06 10:53:10 -06:00
|
|
|
parent: parent,
|
|
|
|
picture: picture,
|
2016-12-14 10:53:29 -06:00
|
|
|
color: c,
|
|
|
|
transform: transform,
|
|
|
|
va: va,
|
2016-12-06 10:53:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 10:53:29 -06:00
|
|
|
// SetPicture changes the picture of a shape.
|
2016-12-18 18:11:34 -06:00
|
|
|
func (s *Shape) SetPicture(picture *Picture) {
|
2016-12-14 10:53:29 -06:00
|
|
|
s.picture = picture
|
|
|
|
}
|
|
|
|
|
|
|
|
// Picture returns the current picture of a shape.
|
2016-12-18 18:11:34 -06:00
|
|
|
func (s *Shape) Picture() *Picture {
|
2016-12-06 10:53:10 -06:00
|
|
|
return s.picture
|
|
|
|
}
|
|
|
|
|
2016-12-14 10:53:29 -06:00
|
|
|
// SetColor changes the color (mask) of a shape.
|
|
|
|
func (s *Shape) SetColor(c color.Color) {
|
2016-12-06 10:53:10 -06:00
|
|
|
s.color = c
|
|
|
|
}
|
|
|
|
|
2016-12-14 10:53:29 -06:00
|
|
|
// Color returns the current color (mask) of a shape.
|
|
|
|
func (s *Shape) Color() color.Color {
|
2016-12-06 10:53:10 -06:00
|
|
|
return s.color
|
|
|
|
}
|
|
|
|
|
2016-12-14 10:53:29 -06:00
|
|
|
// SetTransform changes the ("static") transform of a shape.
|
|
|
|
func (s *Shape) SetTransform(transform Transform) {
|
|
|
|
s.transform = transform
|
2016-12-06 10:53:10 -06:00
|
|
|
}
|
|
|
|
|
2016-12-14 10:53:29 -06:00
|
|
|
// Transform returns the current ("static") transform of a shape.
|
|
|
|
func (s *Shape) Transform() Transform {
|
2016-12-06 10:53:10 -06:00
|
|
|
return s.transform
|
|
|
|
}
|
|
|
|
|
2016-12-14 10:53:29 -06:00
|
|
|
// VertexArray changes the underlying vertex array of a shape.
|
|
|
|
func (s *Shape) VertexArray() *pixelgl.VertexArray {
|
|
|
|
return s.va
|
|
|
|
}
|
|
|
|
|
2016-12-06 10:53:10 -06:00
|
|
|
// Draw draws a sprite transformed by the supplied transforms applied in the reverse order.
|
2016-12-14 10:53:29 -06:00
|
|
|
func (s *Shape) Draw(t ...Transform) {
|
2016-12-06 10:53:10 -06:00
|
|
|
mat := mgl32.Ident3()
|
|
|
|
for i := range t {
|
2016-12-21 13:32:52 -06:00
|
|
|
mat = mat.Mul3(t[i].Mat())
|
2016-12-06 10:53:10 -06:00
|
|
|
}
|
2016-12-21 13:32:52 -06:00
|
|
|
mat = mat.Mul3(s.transform.Mat())
|
2016-12-06 10:53:10 -06:00
|
|
|
|
|
|
|
s.parent.Do(func(ctx pixelgl.Context) {
|
2016-12-23 13:25:47 -06:00
|
|
|
c := NRGBAModel.Convert(s.color).(NRGBA)
|
|
|
|
ctx.Shader().SetUniformAttr(maskColorVec4, mgl32.Vec4{float32(c.R), float32(c.G), float32(c.B), float32(c.A)})
|
2016-12-14 10:53:29 -06:00
|
|
|
ctx.Shader().SetUniformAttr(transformMat3, mat)
|
|
|
|
|
2016-12-18 18:11:34 -06:00
|
|
|
if s.picture != nil {
|
2016-12-14 10:53:29 -06:00
|
|
|
s.picture.Texture().Do(func(pixelgl.Context) {
|
|
|
|
s.va.Draw()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
s.va.Draw()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-30 10:43:26 -06:00
|
|
|
// MultiShape is a shape composed of several other shapes. These shapes cannot be modifies
|
|
|
|
// after combined into a multishape.
|
2016-12-17 19:06:50 -06:00
|
|
|
//
|
2016-12-30 10:43:26 -06:00
|
|
|
// Using a multishape can greatly increase drawing performance. However, it's only usable when
|
|
|
|
// the relative transformations of the shapes don't change (e.g. static blocks in a level).
|
2016-12-17 19:06:50 -06:00
|
|
|
//
|
|
|
|
// All shapes in a multishape must share the same texture (or use no texture).
|
|
|
|
type MultiShape struct {
|
|
|
|
*Shape
|
|
|
|
}
|
|
|
|
|
2016-12-18 13:16:27 -06:00
|
|
|
// NewMultiShape creates a new multishape from several other shapes.
|
2016-12-17 19:06:50 -06:00
|
|
|
//
|
|
|
|
// If two of the supplied shapes have different pictures, this function panics.
|
|
|
|
func NewMultiShape(parent pixelgl.Doer, shapes ...*Shape) *MultiShape {
|
2016-12-18 18:11:34 -06:00
|
|
|
var picture *Picture
|
2016-12-17 19:06:50 -06:00
|
|
|
for _, shape := range shapes {
|
2016-12-20 07:23:50 -06:00
|
|
|
if picture != nil && shape.Picture() != nil && shape.Picture().Texture().ID() != picture.Texture().ID() {
|
|
|
|
panic(errors.New("failed to create multishape: shapes have different pictures"))
|
|
|
|
}
|
|
|
|
if shape.Picture() != nil {
|
2016-12-17 19:06:50 -06:00
|
|
|
picture = shape.Picture()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var va *pixelgl.VertexArray
|
|
|
|
|
2016-12-20 07:23:50 -06:00
|
|
|
var indices []int
|
2016-12-17 19:06:50 -06:00
|
|
|
offset := 0
|
|
|
|
for _, shape := range shapes {
|
|
|
|
for _, i := range shape.va.Indices() {
|
|
|
|
indices = append(indices, offset+i)
|
|
|
|
}
|
2016-12-21 15:29:23 -06:00
|
|
|
offset += shape.VertexArray().NumVertices()
|
2016-12-17 19:06:50 -06:00
|
|
|
}
|
|
|
|
|
2016-12-20 07:23:50 -06:00
|
|
|
var vertices []map[pixelgl.Attr]interface{}
|
|
|
|
for _, shape := range shapes {
|
2016-12-20 07:56:45 -06:00
|
|
|
shapeVertices := shape.VertexArray().Vertices()
|
|
|
|
|
|
|
|
for vertex := range shapeVertices {
|
|
|
|
if pos, ok := shapeVertices[vertex][positionVec2]; ok {
|
|
|
|
pos := pos.(mgl32.Vec2)
|
2016-12-21 13:32:52 -06:00
|
|
|
pos = shape.Transform().Mat().Mul3x1(mgl32.Vec3{pos.X(), pos.Y(), 1}).Vec2()
|
2016-12-20 07:56:45 -06:00
|
|
|
shapeVertices[vertex][positionVec2] = pos
|
|
|
|
}
|
|
|
|
if color, ok := shapeVertices[vertex][colorVec4]; ok {
|
|
|
|
color := color.(mgl32.Vec4)
|
2016-12-23 13:25:47 -06:00
|
|
|
c := NRGBAModel.Convert(shape.Color()).(NRGBA)
|
2016-12-20 07:56:45 -06:00
|
|
|
color = mgl32.Vec4{
|
2016-12-23 13:25:47 -06:00
|
|
|
color[0] * float32(c.R),
|
|
|
|
color[1] * float32(c.G),
|
|
|
|
color[2] * float32(c.B),
|
|
|
|
color[3] * float32(c.A),
|
2016-12-20 07:56:45 -06:00
|
|
|
}
|
|
|
|
shapeVertices[vertex][colorVec4] = color
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vertices = append(vertices, shapeVertices...)
|
2016-12-20 07:23:50 -06:00
|
|
|
}
|
|
|
|
|
2016-12-17 19:06:50 -06:00
|
|
|
parent.Do(func(ctx pixelgl.Context) {
|
|
|
|
var err error
|
|
|
|
va, err = pixelgl.NewVertexArray(
|
|
|
|
pixelgl.ContextHolder{Context: ctx},
|
|
|
|
ctx.Shader().VertexFormat(),
|
2016-12-20 07:23:50 -06:00
|
|
|
len(vertices),
|
2016-12-17 19:06:50 -06:00
|
|
|
indices,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, "failed to create multishape"))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-12-20 07:23:50 -06:00
|
|
|
va.SetVertices(vertices)
|
2016-12-17 19:06:50 -06:00
|
|
|
|
|
|
|
return &MultiShape{NewShape(parent, picture, color.White, Position(0), va)}
|
|
|
|
}
|
|
|
|
|
2016-12-30 10:43:26 -06:00
|
|
|
// Sprite is a picture that can be drawn on the screen. Optionally it can be color masked
|
|
|
|
// or tranformed.
|
2016-12-14 10:53:29 -06:00
|
|
|
//
|
2016-12-30 10:43:26 -06:00
|
|
|
// Usually, you only transform objects when you're drawing them (by passing transforms to the
|
|
|
|
// Draw method). With sprites however, it can be useful to also transform them "statically". For
|
|
|
|
// example, sprites are anchor by their bottom-left corner by default. Setting a transform can
|
|
|
|
// change this anchored to the center, or wherever you want.
|
2016-12-14 10:53:29 -06:00
|
|
|
type Sprite struct {
|
|
|
|
*Shape
|
|
|
|
}
|
2016-12-06 10:53:10 -06:00
|
|
|
|
2016-12-30 10:43:26 -06:00
|
|
|
// NewSprite creates a new sprite with the supplied picture. The sprite's size is the size of
|
|
|
|
// the supplied picture. If you want to change the sprite's size, change it's transform.
|
2016-12-18 18:11:34 -06:00
|
|
|
func NewSprite(parent pixelgl.Doer, picture *Picture) *Sprite {
|
2016-12-14 10:53:29 -06:00
|
|
|
var va *pixelgl.VertexArray
|
|
|
|
|
|
|
|
parent.Do(func(ctx pixelgl.Context) {
|
|
|
|
var err error
|
|
|
|
va, err = pixelgl.NewVertexArray(
|
2016-12-15 17:28:52 -06:00
|
|
|
pixelgl.ContextHolder{Context: ctx},
|
2016-12-14 10:53:29 -06:00
|
|
|
ctx.Shader().VertexFormat(),
|
|
|
|
4,
|
|
|
|
[]int{0, 1, 2, 0, 2, 3},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, "failed to create sprite"))
|
|
|
|
}
|
2016-12-06 10:53:10 -06:00
|
|
|
})
|
2016-12-14 10:53:29 -06:00
|
|
|
|
2016-12-19 18:23:33 -06:00
|
|
|
vertices := make([]map[pixelgl.Attr]interface{}, 4)
|
|
|
|
|
2016-12-14 10:53:29 -06:00
|
|
|
w, h := picture.Bounds().Size.XY()
|
|
|
|
for i, p := range []Vec{V(0, 0), V(w, 0), V(w, h), V(0, h)} {
|
|
|
|
texCoord := V(
|
|
|
|
(picture.Bounds().X()+p.X())/float64(picture.Texture().Width()),
|
|
|
|
(picture.Bounds().Y()+p.Y())/float64(picture.Texture().Height()),
|
|
|
|
)
|
|
|
|
|
2016-12-19 18:23:33 -06:00
|
|
|
vertices[i] = map[pixelgl.Attr]interface{}{
|
|
|
|
positionVec2: mgl32.Vec2{float32(p.X()), float32(p.Y())},
|
|
|
|
colorVec4: mgl32.Vec4{1, 1, 1, 1},
|
|
|
|
texCoordVec2: mgl32.Vec2{float32(texCoord.X()), float32(texCoord.Y())},
|
|
|
|
}
|
2016-12-14 10:53:29 -06:00
|
|
|
}
|
|
|
|
|
2016-12-20 06:56:25 -06:00
|
|
|
va.SetVertices(vertices)
|
2016-12-19 18:23:33 -06:00
|
|
|
|
2016-12-14 10:53:29 -06:00
|
|
|
return &Sprite{NewShape(parent, picture, color.White, Position(0), va)}
|
2016-12-06 10:53:10 -06:00
|
|
|
}
|
|
|
|
|
2016-12-03 13:20:16 -06:00
|
|
|
// LineColor a line shape (with sharp ends) filled with a single color.
|
|
|
|
type LineColor struct {
|
2016-12-14 10:53:29 -06:00
|
|
|
*Shape
|
|
|
|
a, b Vec
|
|
|
|
width float64
|
2016-12-03 13:20:16 -06:00
|
|
|
}
|
|
|
|
|
2016-12-30 10:43:26 -06:00
|
|
|
// NewLineColor creates a new line shape between points A and B filled with a single color. Parent
|
|
|
|
// is an object that this shape belongs to, such as a window, or a graphics effect.
|
2016-12-03 13:20:16 -06:00
|
|
|
func NewLineColor(parent pixelgl.Doer, c color.Color, a, b Vec, width float64) *LineColor {
|
2016-12-14 10:53:29 -06:00
|
|
|
var va *pixelgl.VertexArray
|
2016-12-03 13:20:16 -06:00
|
|
|
|
|
|
|
parent.Do(func(ctx pixelgl.Context) {
|
2016-12-04 16:57:15 -06:00
|
|
|
var err error
|
2016-12-14 10:53:29 -06:00
|
|
|
va, err = pixelgl.NewVertexArray(
|
2016-12-15 17:28:52 -06:00
|
|
|
pixelgl.ContextHolder{Context: ctx},
|
2016-12-04 16:57:15 -06:00
|
|
|
ctx.Shader().VertexFormat(),
|
|
|
|
4,
|
2016-12-08 08:25:00 -06:00
|
|
|
[]int{0, 1, 2, 1, 2, 3},
|
2016-12-04 16:57:15 -06:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, "failed to create line"))
|
|
|
|
}
|
2016-12-03 13:20:16 -06:00
|
|
|
})
|
|
|
|
|
2016-12-19 18:23:33 -06:00
|
|
|
vertices := make([]map[pixelgl.Attr]interface{}, 4)
|
|
|
|
|
2016-12-09 13:13:32 -06:00
|
|
|
for i := 0; i < 4; i++ {
|
2016-12-19 18:23:33 -06:00
|
|
|
vertices[i] = map[pixelgl.Attr]interface{}{
|
|
|
|
colorVec4: mgl32.Vec4{1, 1, 1, 1},
|
|
|
|
texCoordVec2: mgl32.Vec2{-1, -1},
|
|
|
|
}
|
2016-12-09 13:13:32 -06:00
|
|
|
}
|
2016-12-03 13:20:16 -06:00
|
|
|
|
2016-12-20 06:56:25 -06:00
|
|
|
va.SetVertices(vertices)
|
2016-12-19 18:23:33 -06:00
|
|
|
|
2016-12-18 18:11:34 -06:00
|
|
|
lc := &LineColor{NewShape(parent, nil, c, Position(0), va), a, b, width}
|
2016-12-03 13:20:16 -06:00
|
|
|
lc.setPoints()
|
|
|
|
return lc
|
|
|
|
}
|
|
|
|
|
|
|
|
// setPoints updates the vertex array data according to A, B and width.
|
|
|
|
func (lc *LineColor) setPoints() {
|
|
|
|
r := (lc.b - lc.a).Unit().Scaled(lc.width / 2).Rotated(math.Pi / 2)
|
|
|
|
for i, p := range []Vec{lc.a - r, lc.a + r, lc.b - r, lc.b + r} {
|
2016-12-14 10:53:29 -06:00
|
|
|
lc.va.SetVertexAttr(i, positionVec2, mgl32.Vec2{float32(p.X()), float32(p.Y())})
|
2016-12-03 13:20:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetA changes the position of the first endpoint of a line.
|
|
|
|
func (lc *LineColor) SetA(a Vec) {
|
|
|
|
lc.a = a
|
|
|
|
lc.setPoints()
|
|
|
|
}
|
|
|
|
|
|
|
|
// A returns the current position of the first endpoint of a line.
|
|
|
|
func (lc *LineColor) A() Vec {
|
|
|
|
return lc.a
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetB changes the position of the second endpoint of a line.
|
|
|
|
func (lc *LineColor) SetB(b Vec) {
|
|
|
|
lc.b = b
|
|
|
|
lc.setPoints()
|
|
|
|
}
|
|
|
|
|
|
|
|
// B returns the current position of the second endpoint of a line.
|
|
|
|
func (lc *LineColor) B() Vec {
|
|
|
|
return lc.b
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetWidth changes the width of a line.
|
|
|
|
func (lc *LineColor) SetWidth(width float64) {
|
|
|
|
lc.width = width
|
|
|
|
lc.setPoints()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Width returns the current width of a line.
|
|
|
|
func (lc *LineColor) Width() float64 {
|
|
|
|
return lc.width
|
|
|
|
}
|
|
|
|
|
2016-12-14 10:53:29 -06:00
|
|
|
// PolygonColor is a convex polygon shape filled with a single color.
|
2016-12-03 09:25:41 -06:00
|
|
|
type PolygonColor struct {
|
2016-12-14 10:53:29 -06:00
|
|
|
*Shape
|
2016-12-03 09:25:41 -06:00
|
|
|
points []Vec
|
|
|
|
}
|
|
|
|
|
2016-12-30 10:43:26 -06:00
|
|
|
// NewPolygonColor creates a new polygon shape filled with a single color. Parent is an object
|
|
|
|
// that this shape belongs to, such as a window, or a graphics effect.
|
2016-12-03 09:25:41 -06:00
|
|
|
func NewPolygonColor(parent pixelgl.Doer, c color.Color, points ...Vec) *PolygonColor {
|
2016-12-14 10:53:29 -06:00
|
|
|
var va *pixelgl.VertexArray
|
2016-12-03 09:25:41 -06:00
|
|
|
|
2016-12-08 08:25:00 -06:00
|
|
|
var indices []int
|
|
|
|
for i := 2; i < len(points); i++ {
|
|
|
|
indices = append(indices, 0, i-1, i)
|
|
|
|
}
|
|
|
|
|
2016-12-03 09:25:41 -06:00
|
|
|
parent.Do(func(ctx pixelgl.Context) {
|
2016-12-04 16:57:15 -06:00
|
|
|
var err error
|
2016-12-14 10:53:29 -06:00
|
|
|
va, err = pixelgl.NewVertexArray(
|
2016-12-15 17:28:52 -06:00
|
|
|
pixelgl.ContextHolder{Context: ctx},
|
2016-12-04 16:57:15 -06:00
|
|
|
ctx.Shader().VertexFormat(),
|
|
|
|
len(points),
|
2016-12-08 08:25:00 -06:00
|
|
|
indices,
|
2016-12-04 16:57:15 -06:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, "failed to create polygon"))
|
|
|
|
}
|
2016-12-03 09:25:41 -06:00
|
|
|
})
|
|
|
|
|
2016-12-19 18:23:33 -06:00
|
|
|
vertices := make([]map[pixelgl.Attr]interface{}, len(points))
|
|
|
|
|
2016-12-03 09:25:41 -06:00
|
|
|
for i, p := range points {
|
2016-12-19 18:23:33 -06:00
|
|
|
vertices[i] = map[pixelgl.Attr]interface{}{
|
|
|
|
positionVec2: mgl32.Vec2{float32(p.X()), float32(p.Y())},
|
|
|
|
colorVec4: mgl32.Vec4{1, 1, 1, 1},
|
|
|
|
texCoordVec2: mgl32.Vec2{-1, -1},
|
|
|
|
}
|
2016-12-03 09:25:41 -06:00
|
|
|
}
|
|
|
|
|
2016-12-20 06:56:25 -06:00
|
|
|
va.SetVertices(vertices)
|
2016-12-19 18:23:33 -06:00
|
|
|
|
2016-12-18 18:11:34 -06:00
|
|
|
return &PolygonColor{NewShape(parent, nil, c, Position(0), va), points}
|
2016-12-03 09:25:41 -06:00
|
|
|
}
|
|
|
|
|
2016-12-21 15:29:23 -06:00
|
|
|
// NumPoints returns the number of points in a polygon.
|
|
|
|
func (pc *PolygonColor) NumPoints() int {
|
2016-12-03 09:25:41 -06:00
|
|
|
return len(pc.points)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPoint changes the position of a point in a polygon.
|
|
|
|
//
|
|
|
|
// If the index is out of range, this function panics.
|
|
|
|
func (pc *PolygonColor) SetPoint(i int, point Vec) {
|
|
|
|
pc.points[i] = point
|
2016-12-14 10:53:29 -06:00
|
|
|
pc.va.SetVertexAttr(i, positionVec2, mgl32.Vec2{float32(point.X()), float32(point.Y())})
|
2016-12-03 09:25:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Point returns the position of a point in a polygon.
|
|
|
|
//
|
|
|
|
// If the index is out of range, this function panics.
|
|
|
|
func (pc *PolygonColor) Point(i int) Vec {
|
|
|
|
return pc.points[i]
|
|
|
|
}
|
|
|
|
|
2016-12-03 12:51:43 -06:00
|
|
|
// EllipseColor is an ellipse shape filled with a single color.
|
|
|
|
type EllipseColor struct {
|
2016-12-14 10:53:29 -06:00
|
|
|
*Shape
|
2016-12-03 12:51:43 -06:00
|
|
|
radius Vec
|
|
|
|
fill float64
|
|
|
|
}
|
|
|
|
|
2016-12-30 10:43:26 -06:00
|
|
|
// NewEllipseColor creates a new ellipse shape filled with a single color. Parent is an object
|
|
|
|
// that this shape belongs to, such as a window, or a graphics effect. Fill should be a number
|
|
|
|
// between 0 and 1 which specifies how much of the ellipse will be filled (from the outside). The
|
|
|
|
// value of 1 means that the whole ellipse is filled. The value of 0 means that none of the
|
2016-12-03 12:51:43 -06:00
|
|
|
// ellipse is filled (which makes the ellipse invisible).
|
|
|
|
func NewEllipseColor(parent pixelgl.Doer, c color.Color, radius Vec, fill float64) *EllipseColor {
|
2016-12-14 10:53:29 -06:00
|
|
|
var va *pixelgl.VertexArray
|
2016-12-03 12:51:43 -06:00
|
|
|
|
2016-12-14 10:53:29 -06:00
|
|
|
const n = 256
|
2016-12-03 12:51:43 -06:00
|
|
|
|
2016-12-08 08:25:00 -06:00
|
|
|
var indices []int
|
|
|
|
for i := 2; i < (n+1)*2; i++ {
|
|
|
|
indices = append(indices, i-2, i-1, i)
|
|
|
|
}
|
|
|
|
|
2016-12-03 12:51:43 -06:00
|
|
|
parent.Do(func(ctx pixelgl.Context) {
|
2016-12-04 16:57:15 -06:00
|
|
|
var err error
|
2016-12-14 10:53:29 -06:00
|
|
|
va, err = pixelgl.NewVertexArray(
|
2016-12-15 17:28:52 -06:00
|
|
|
pixelgl.ContextHolder{Context: ctx},
|
2016-12-04 16:57:15 -06:00
|
|
|
ctx.Shader().VertexFormat(),
|
|
|
|
(n+1)*2,
|
2016-12-08 08:25:00 -06:00
|
|
|
indices,
|
2016-12-04 16:57:15 -06:00
|
|
|
)
|
|
|
|
if err != nil {
|
2016-12-08 08:25:00 -06:00
|
|
|
panic(errors.Wrap(err, "failed to create ellipse"))
|
2016-12-04 16:57:15 -06:00
|
|
|
}
|
2016-12-03 12:51:43 -06:00
|
|
|
})
|
|
|
|
|
2016-12-19 18:23:33 -06:00
|
|
|
vertices := make([]map[pixelgl.Attr]interface{}, (n+1)*2)
|
|
|
|
|
2016-12-03 12:51:43 -06:00
|
|
|
for k := 0; k < n+1; k++ {
|
|
|
|
i, j := k*2, k*2+1
|
|
|
|
angle := math.Pi * 2 * float64(k%n) / n
|
|
|
|
|
2016-12-19 18:23:33 -06:00
|
|
|
vertices[i] = map[pixelgl.Attr]interface{}{
|
|
|
|
positionVec2: mgl32.Vec2{
|
|
|
|
float32(math.Cos(angle) * radius.X()),
|
|
|
|
float32(math.Sin(angle) * radius.Y()),
|
|
|
|
},
|
|
|
|
colorVec4: mgl32.Vec4{1, 1, 1, 1},
|
|
|
|
texCoordVec2: mgl32.Vec2{-1, -1},
|
|
|
|
}
|
|
|
|
|
|
|
|
vertices[j] = map[pixelgl.Attr]interface{}{
|
|
|
|
positionVec2: mgl32.Vec2{
|
|
|
|
float32(math.Cos(angle) * radius.X() * (1 - fill)),
|
|
|
|
float32(math.Sin(angle) * radius.Y() * (1 - fill)),
|
|
|
|
},
|
|
|
|
colorVec4: mgl32.Vec4{1, 1, 1, 1},
|
|
|
|
texCoordVec2: mgl32.Vec2{-1, -1},
|
|
|
|
}
|
2016-12-14 10:53:29 -06:00
|
|
|
}
|
2016-12-03 12:51:43 -06:00
|
|
|
|
2016-12-20 06:56:25 -06:00
|
|
|
va.SetVertices(vertices)
|
2016-12-19 18:23:33 -06:00
|
|
|
|
2016-12-18 18:11:34 -06:00
|
|
|
return &EllipseColor{NewShape(parent, nil, c, Position(0), va), radius, fill}
|
2016-12-03 12:51:43 -06:00
|
|
|
}
|
|
|
|
|
2016-12-14 10:53:29 -06:00
|
|
|
// Radius returns the radius of an ellipse.
|
2016-12-03 12:51:43 -06:00
|
|
|
func (ec *EllipseColor) Radius() Vec {
|
|
|
|
return ec.radius
|
|
|
|
}
|
|
|
|
|
2016-12-14 10:53:29 -06:00
|
|
|
// Fill returns the fill ratio of an ellipse.
|
|
|
|
func (ec *EllipseColor) Fill() float64 {
|
|
|
|
return ec.fill
|
2016-12-03 13:20:16 -06:00
|
|
|
}
|