2017-03-07 15:47:55 -06:00
|
|
|
package pixel
|
|
|
|
|
2017-03-23 17:00:42 -05:00
|
|
|
import "image/color"
|
|
|
|
|
2017-03-31 08:00:59 -05:00
|
|
|
// Sprite is a drawable frame of a Picture. It's anchored by the center of it's Picture's frame.
|
|
|
|
//
|
|
|
|
// Frame specifies a rectangular portion of the Picture that will be drawn. For example, this
|
|
|
|
// creates a Sprite that draws the whole Picture:
|
|
|
|
//
|
|
|
|
// sprite := pixel.NewSprite(pic, pic.Bounds())
|
2017-03-23 13:59:03 -05:00
|
|
|
//
|
2017-04-26 07:28:25 -05:00
|
|
|
// Note, that Sprite caches the results of MakePicture from Targets it's drawn to for each Picture
|
|
|
|
// it's set to. What it means is that using a Sprite with an unbounded number of Pictures leads to a
|
|
|
|
// memory leak, since Sprite caches them and never forgets. In such a situation, create a new Sprite
|
|
|
|
// for each Picture.
|
2017-03-07 15:47:55 -06:00
|
|
|
type Sprite struct {
|
2017-03-31 08:00:59 -05:00
|
|
|
tri *TrianglesData
|
|
|
|
frame Rect
|
|
|
|
d Drawer
|
2017-03-23 13:59:03 -05:00
|
|
|
|
|
|
|
matrix Matrix
|
2017-04-09 15:00:26 -05:00
|
|
|
mask RGBA
|
2017-03-07 15:47:55 -06:00
|
|
|
}
|
|
|
|
|
2017-03-31 08:00:59 -05:00
|
|
|
// NewSprite creates a Sprite from the supplied frame of a Picture.
|
|
|
|
func NewSprite(pic Picture, frame Rect) *Sprite {
|
2017-03-07 15:47:55 -06:00
|
|
|
tri := MakeTrianglesData(6)
|
|
|
|
s := &Sprite{
|
|
|
|
tri: tri,
|
2021-10-01 13:31:18 -05:00
|
|
|
d: Drawer{Triangles: tri, Cached: true},
|
2017-03-07 15:47:55 -06:00
|
|
|
}
|
2017-03-23 13:59:03 -05:00
|
|
|
s.matrix = IM
|
2017-04-10 10:25:56 -05:00
|
|
|
s.mask = Alpha(1)
|
2017-03-31 08:00:59 -05:00
|
|
|
s.Set(pic, frame)
|
2017-03-07 15:47:55 -06:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2017-03-31 08:00:59 -05:00
|
|
|
// Set sets a new frame of a Picture for this Sprite.
|
|
|
|
func (s *Sprite) Set(pic Picture, frame Rect) {
|
2017-03-07 17:43:01 -06:00
|
|
|
s.d.Picture = pic
|
2017-03-31 08:00:59 -05:00
|
|
|
if frame != s.frame {
|
|
|
|
s.frame = frame
|
|
|
|
s.calcData()
|
2017-03-07 17:43:01 -06:00
|
|
|
}
|
2017-03-23 13:59:03 -05:00
|
|
|
}
|
|
|
|
|
2021-10-01 13:31:18 -05:00
|
|
|
// SetCached makes the sprite cache all the
|
|
|
|
// incoming pictures if the argument is true, and
|
|
|
|
// doesn't make it do that if the argument is false.
|
|
|
|
func (s *Sprite) SetCached(cached bool) {
|
|
|
|
s.d.Cached = cached
|
|
|
|
}
|
|
|
|
|
2017-03-23 13:59:03 -05:00
|
|
|
// Picture returns the current Sprite's Picture.
|
|
|
|
func (s *Sprite) Picture() Picture {
|
|
|
|
return s.d.Picture
|
|
|
|
}
|
|
|
|
|
2017-03-31 08:00:59 -05:00
|
|
|
// Frame returns the current Sprite's frame.
|
|
|
|
func (s *Sprite) Frame() Rect {
|
|
|
|
return s.frame
|
|
|
|
}
|
|
|
|
|
2017-05-17 16:45:22 -05:00
|
|
|
// Draw draws the Sprite onto the provided Target. The Sprite will be transformed by the given Matrix.
|
2017-03-23 13:59:03 -05:00
|
|
|
//
|
2017-05-17 16:45:22 -05:00
|
|
|
// This method is equivalent to calling DrawColorMask with nil color mask.
|
|
|
|
func (s *Sprite) Draw(t Target, matrix Matrix) {
|
|
|
|
s.DrawColorMask(t, matrix, nil)
|
2017-03-23 13:59:03 -05:00
|
|
|
}
|
|
|
|
|
2017-05-21 11:23:20 -05:00
|
|
|
// DrawColorMask draws the Sprite onto the provided Target. The Sprite will be transformed by the
|
2017-05-17 16:45:22 -05:00
|
|
|
// given Matrix and all of it's color will be multiplied by the given mask.
|
2017-03-23 13:59:03 -05:00
|
|
|
//
|
2017-05-17 16:45:22 -05:00
|
|
|
// If the mask is nil, a fully opaque white mask will be used, which causes no effect.
|
|
|
|
func (s *Sprite) DrawColorMask(t Target, matrix Matrix, mask color.Color) {
|
|
|
|
dirty := false
|
|
|
|
if matrix != s.matrix {
|
|
|
|
s.matrix = matrix
|
|
|
|
dirty = true
|
|
|
|
}
|
|
|
|
if mask == nil {
|
|
|
|
mask = Alpha(1)
|
|
|
|
}
|
2017-05-21 11:23:20 -05:00
|
|
|
rgba := ToRGBA(mask)
|
|
|
|
if rgba != s.mask {
|
|
|
|
s.mask = rgba
|
2017-05-17 16:45:22 -05:00
|
|
|
dirty = true
|
2017-04-12 09:18:25 -05:00
|
|
|
}
|
2017-03-23 13:59:03 -05:00
|
|
|
|
2017-05-17 16:45:22 -05:00
|
|
|
if dirty {
|
|
|
|
s.calcData()
|
|
|
|
}
|
2017-03-23 17:00:42 -05:00
|
|
|
|
2017-03-23 13:59:03 -05:00
|
|
|
s.d.Draw(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprite) calcData() {
|
2017-03-07 15:47:55 -06:00
|
|
|
var (
|
2017-03-31 08:00:59 -05:00
|
|
|
center = s.frame.Center()
|
2017-05-21 12:25:06 -05:00
|
|
|
horizontal = V(s.frame.W()/2, 0)
|
|
|
|
vertical = V(0, s.frame.H()/2)
|
2017-03-07 15:47:55 -06:00
|
|
|
)
|
2017-03-07 15:50:49 -06:00
|
|
|
|
2017-05-21 12:25:06 -05:00
|
|
|
(*s.tri)[0].Position = Vec{}.Sub(horizontal).Sub(vertical)
|
|
|
|
(*s.tri)[1].Position = Vec{}.Add(horizontal).Sub(vertical)
|
|
|
|
(*s.tri)[2].Position = Vec{}.Add(horizontal).Add(vertical)
|
|
|
|
(*s.tri)[3].Position = Vec{}.Sub(horizontal).Sub(vertical)
|
|
|
|
(*s.tri)[4].Position = Vec{}.Add(horizontal).Add(vertical)
|
|
|
|
(*s.tri)[5].Position = Vec{}.Sub(horizontal).Add(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-05-21 12:25:06 -05:00
|
|
|
(*s.tri)[i].Picture = center.Add((*s.tri)[i].Position)
|
2017-03-07 15:47:55 -06:00
|
|
|
(*s.tri)[i].Intensity = 1
|
2019-04-15 02:46:29 -05:00
|
|
|
(*s.tri)[i].Position = s.matrix.Project((*s.tri)[i].Position)
|
2017-03-23 14:27:51 -05:00
|
|
|
}
|
2017-03-23 13:59:03 -05:00
|
|
|
|
2017-03-07 15:47:55 -06:00
|
|
|
s.d.Dirty()
|
|
|
|
}
|