2017-03-07 15:47:55 -06:00
|
|
|
package pixel
|
|
|
|
|
2017-03-23 17:00:42 -05:00
|
|
|
import "image/color"
|
|
|
|
|
2017-03-23 13:59:03 -05:00
|
|
|
// Sprite is a drawable Picture. It's anchored by the center of it's Picture.
|
|
|
|
//
|
|
|
|
// To achieve different anchoring, transformations and color masking, use SetMatrix and SetColorMask
|
|
|
|
// methods.
|
2017-03-07 15:47:55 -06:00
|
|
|
type Sprite struct {
|
2017-03-08 12:19:02 -06:00
|
|
|
tri *TrianglesData
|
|
|
|
bounds Rect
|
|
|
|
d Drawer
|
2017-03-23 13:59:03 -05:00
|
|
|
|
|
|
|
matrix Matrix
|
|
|
|
mask NRGBA
|
2017-03-07 15:47:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSprite creates a Sprite from the supplied Picture.
|
|
|
|
func NewSprite(pic Picture) *Sprite {
|
|
|
|
tri := MakeTrianglesData(6)
|
|
|
|
s := &Sprite{
|
|
|
|
tri: tri,
|
|
|
|
d: Drawer{Triangles: tri},
|
|
|
|
}
|
2017-03-23 13:59:03 -05:00
|
|
|
s.matrix = IM
|
|
|
|
s.mask = NRGBA{1, 1, 1, 1}
|
2017-03-07 15:47:55 -06:00
|
|
|
s.SetPicture(pic)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPicture changes the Sprite's Picture. The new Picture may have a different size, everything
|
|
|
|
// works.
|
|
|
|
func (s *Sprite) SetPicture(pic Picture) {
|
2017-03-07 17:43:01 -06:00
|
|
|
s.d.Picture = pic
|
|
|
|
|
2017-03-08 12:19:02 -06:00
|
|
|
if s.bounds == pic.Bounds() {
|
2017-03-07 17:43:01 -06:00
|
|
|
return
|
|
|
|
}
|
2017-03-08 12:19:02 -06:00
|
|
|
s.bounds = pic.Bounds()
|
2017-03-07 17:43:01 -06:00
|
|
|
|
2017-03-23 13:59:03 -05:00
|
|
|
s.calcData()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Picture returns the current Sprite's Picture.
|
|
|
|
func (s *Sprite) Picture() Picture {
|
|
|
|
return s.d.Picture
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMatrix sets a Matrix that this Sprite will be transformed by. This overrides any previously
|
|
|
|
// set Matrix.
|
|
|
|
//
|
|
|
|
// Note, that this has nothing to do with BasicTarget's SetMatrix method. This only affects this
|
|
|
|
// Sprite and is usable with any Target.
|
|
|
|
func (s *Sprite) SetMatrix(matrix Matrix) {
|
|
|
|
s.matrix = matrix
|
|
|
|
s.calcData()
|
|
|
|
}
|
|
|
|
|
2017-03-23 17:00:42 -05:00
|
|
|
// Matrix returns the currently set Matrix.
|
|
|
|
func (s *Sprite) Matrix() Matrix {
|
|
|
|
return s.matrix
|
|
|
|
}
|
|
|
|
|
2017-03-23 13:59:03 -05:00
|
|
|
// SetColorMask sets a color that this Sprite will be multiplied by. This overrides any previously
|
|
|
|
// set color mask.
|
|
|
|
//
|
|
|
|
// Note, that this has nothing to do with BasicTarget's SetColorMask method. This only affects this
|
|
|
|
// Sprite and is usable with any Target.
|
2017-03-23 17:00:42 -05:00
|
|
|
func (s *Sprite) SetColorMask(mask color.Color) {
|
2017-03-23 17:07:49 -05:00
|
|
|
s.mask = ToNRGBA(mask)
|
2017-03-23 13:59:03 -05:00
|
|
|
s.calcData()
|
|
|
|
}
|
|
|
|
|
2017-03-23 17:00:42 -05:00
|
|
|
// ColorMask returns the currently set color mask.
|
|
|
|
func (s *Sprite) ColorMask() NRGBA {
|
|
|
|
return s.mask
|
|
|
|
}
|
|
|
|
|
2017-03-23 13:59:03 -05:00
|
|
|
// Draw draws the Sprite onto the provided Target.
|
|
|
|
func (s *Sprite) Draw(t Target) {
|
|
|
|
s.d.Draw(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprite) calcData() {
|
2017-03-07 15:47:55 -06:00
|
|
|
var (
|
2017-03-08 12:19:02 -06:00
|
|
|
center = s.bounds.Center()
|
2017-03-14 18:35:14 -05:00
|
|
|
horizontal = X(s.bounds.W() / 2)
|
|
|
|
vertical = Y(s.bounds.H() / 2)
|
2017-03-07 15:47:55 -06:00
|
|
|
)
|
2017-03-07 15:50:49 -06:00
|
|
|
|
2017-03-07 15:47:55 -06:00
|
|
|
(*s.tri)[0].Position = -horizontal - vertical
|
|
|
|
(*s.tri)[1].Position = +horizontal - vertical
|
|
|
|
(*s.tri)[2].Position = +horizontal + vertical
|
|
|
|
(*s.tri)[3].Position = -horizontal - vertical
|
|
|
|
(*s.tri)[4].Position = +horizontal + vertical
|
|
|
|
(*s.tri)[5].Position = -horizontal + vertical
|
2017-03-07 15:50:49 -06:00
|
|
|
|
2017-03-07 15:47:55 -06:00
|
|
|
for i := range *s.tri {
|
2017-03-23 13:59:03 -05:00
|
|
|
(*s.tri)[i].Color = s.mask
|
2017-03-07 15:47:55 -06:00
|
|
|
(*s.tri)[i].Picture = center + (*s.tri)[i].Position
|
|
|
|
(*s.tri)[i].Intensity = 1
|
2017-03-23 14:27:51 -05:00
|
|
|
}
|
2017-03-23 13:59:03 -05:00
|
|
|
|
2017-03-23 14:27:51 -05:00
|
|
|
// matrix and mask
|
|
|
|
for i := range *s.tri {
|
2017-03-23 13:59:03 -05:00
|
|
|
(*s.tri)[i].Position = s.matrix.Project((*s.tri)[i].Position)
|
2017-03-23 14:27:51 -05:00
|
|
|
(*s.tri)[i].Color = s.mask
|
2017-03-07 15:47:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
s.d.Dirty()
|
|
|
|
}
|