add Canvas.Draw and remove SetTransform/MaskColor from Sprite
This commit is contained in:
parent
f6e713ade0
commit
cb699e7057
19
canvas.go
19
canvas.go
|
@ -19,6 +19,8 @@ type Canvas struct {
|
||||||
copyVs *pixelgl.VertexSlice
|
copyVs *pixelgl.VertexSlice
|
||||||
smooth bool
|
smooth bool
|
||||||
|
|
||||||
|
drawTd TrianglesDrawer
|
||||||
|
|
||||||
pic *Picture
|
pic *Picture
|
||||||
mat mgl32.Mat3
|
mat mgl32.Mat3
|
||||||
col mgl32.Vec4
|
col mgl32.Vec4
|
||||||
|
@ -53,6 +55,16 @@ func NewCanvas(width, height float64, smooth bool) *Canvas {
|
||||||
})
|
})
|
||||||
c.copyVs.End()
|
c.copyVs.End()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
c.drawTd = TrianglesDrawer{Triangles: &TrianglesData{
|
||||||
|
{Position: V(-1, -1), Color: NRGBA{1, 1, 1, 1}, Texture: V(0, 0)},
|
||||||
|
{Position: V(1, -1), Color: NRGBA{1, 1, 1, 1}, Texture: V(1, 0)},
|
||||||
|
{Position: V(1, 1), Color: NRGBA{1, 1, 1, 1}, Texture: V(1, 1)},
|
||||||
|
{Position: V(-1, -1), Color: NRGBA{1, 1, 1, 1}, Texture: V(0, 0)},
|
||||||
|
{Position: V(1, 1), Color: NRGBA{1, 1, 1, 1}, Texture: V(1, 1)},
|
||||||
|
{Position: V(-1, 1), Color: NRGBA{1, 1, 1, 1}, Texture: V(0, 1)},
|
||||||
|
}}
|
||||||
|
|
||||||
c.pic = nil
|
c.pic = nil
|
||||||
c.mat = mgl32.Ident3()
|
c.mat = mgl32.Ident3()
|
||||||
c.col = mgl32.Vec4{1, 1, 1, 1}
|
c.col = mgl32.Vec4{1, 1, 1, 1}
|
||||||
|
@ -112,6 +124,13 @@ func (c *Canvas) Clear(col color.Color) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Draw draws the content of the Canvas onto another Target. If no transform is applied, the content
|
||||||
|
// is fully stretched to fit the Target.
|
||||||
|
func (c *Canvas) Draw(t Target) {
|
||||||
|
t.SetPicture(c.Content())
|
||||||
|
c.drawTd.Draw(t)
|
||||||
|
}
|
||||||
|
|
||||||
// MakeTriangles returns Triangles that draw onto this Canvas.
|
// MakeTriangles returns Triangles that draw onto this Canvas.
|
||||||
func (c *Canvas) MakeTriangles(t Triangles) Triangles {
|
func (c *Canvas) MakeTriangles(t Triangles) Triangles {
|
||||||
tpcs := NewGLTriangles(c.s, t).(trianglesPositionColorTexture)
|
tpcs := NewGLTriangles(c.s, t).(trianglesPositionColorTexture)
|
||||||
|
|
36
graphics.go
36
graphics.go
|
@ -1,9 +1,6 @@
|
||||||
package pixel
|
package pixel
|
||||||
|
|
||||||
import (
|
import "fmt"
|
||||||
"fmt"
|
|
||||||
"image/color"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TrianglesData specifies a list of Triangles vertices with three common properties: Position,
|
// TrianglesData specifies a list of Triangles vertices with three common properties: Position,
|
||||||
// Color and Texture.
|
// Color and Texture.
|
||||||
|
@ -156,12 +153,11 @@ func (td *TrianglesDrawer) Append(t Triangles) {
|
||||||
td.Triangles.Append(t)
|
td.Triangles.Append(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sprite is a picture, positioned somewhere, with an optional mask color.
|
// Sprite is a picture that can be drawn onto a Target. To change the position/rotation/scale of
|
||||||
|
// the Sprite, use Target's SetTransform method.
|
||||||
type Sprite struct {
|
type Sprite struct {
|
||||||
td TrianglesDrawer
|
td TrianglesDrawer
|
||||||
pic *Picture
|
pic *Picture
|
||||||
transform []Transform
|
|
||||||
maskColor color.Color
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSprite creates a Sprite with the supplied Picture. The dimensions of the returned Sprite match
|
// NewSprite creates a Sprite with the supplied Picture. The dimensions of the returned Sprite match
|
||||||
|
@ -193,30 +189,8 @@ func (s *Sprite) Picture() *Picture {
|
||||||
return s.pic
|
return s.pic
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetTransform sets a chain of Transforms that will be applied to this Sprite in reverse order.
|
|
||||||
func (s *Sprite) SetTransform(t ...Transform) {
|
|
||||||
s.transform = t
|
|
||||||
}
|
|
||||||
|
|
||||||
// Transform returns the current chain of Transforms that this Sprite is transformed by.
|
|
||||||
func (s *Sprite) Transform() []Transform {
|
|
||||||
return s.transform
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetMaskColor changes the mask color of the Sprite.
|
|
||||||
func (s *Sprite) SetMaskColor(c color.Color) {
|
|
||||||
s.maskColor = c
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaskColor returns the current mask color of the Sprite.
|
|
||||||
func (s *Sprite) MaskColor() color.Color {
|
|
||||||
return s.maskColor
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw draws the Sprite onto the provided Target.
|
// Draw draws the Sprite onto the provided Target.
|
||||||
func (s *Sprite) Draw(target Target) {
|
func (s *Sprite) Draw(target Target) {
|
||||||
target.SetPicture(s.pic)
|
target.SetPicture(s.pic)
|
||||||
target.SetTransform(s.transform...)
|
|
||||||
target.SetMaskColor(s.maskColor)
|
|
||||||
s.td.Draw(target)
|
s.td.Draw(target)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue