fix Sprite for changing Picture bounds

This commit is contained in:
faiface 2017-03-08 19:19:02 +01:00
parent 0127a8ae21
commit 5a9c43bc6c
1 changed files with 8 additions and 8 deletions

View File

@ -2,8 +2,9 @@ package pixel
// Sprite is a drawable Picture. It's always anchored by the center of it's Picture. // Sprite is a drawable Picture. It's always anchored by the center of it's Picture.
type Sprite struct { type Sprite struct {
tri *TrianglesData tri *TrianglesData
d Drawer bounds Rect
d Drawer
} }
// NewSprite creates a Sprite from the supplied Picture. // NewSprite creates a Sprite from the supplied Picture.
@ -20,18 +21,17 @@ func NewSprite(pic Picture) *Sprite {
// SetPicture changes the Sprite's Picture. The new Picture may have a different size, everything // SetPicture changes the Sprite's Picture. The new Picture may have a different size, everything
// works. // works.
func (s *Sprite) SetPicture(pic Picture) { func (s *Sprite) SetPicture(pic Picture) {
oldPic := s.d.Picture
s.d.Picture = pic s.d.Picture = pic
if oldPic != nil && oldPic.Bounds() == pic.Bounds() { if s.bounds == pic.Bounds() {
return return
} }
s.bounds = pic.Bounds()
var ( var (
bounds = pic.Bounds() center = s.bounds.Center()
center = bounds.Center() horizontal = V(s.bounds.W()/2, 0)
horizontal = V(bounds.W()/2, 0) vertical = V(0, s.bounds.H()/2)
vertical = V(0, bounds.H()/2)
) )
(*s.tri)[0].Position = -horizontal - vertical (*s.tri)[0].Position = -horizontal - vertical