2020-02-08 21:02:37 -05:00
|
|
|
package d2gui
|
|
|
|
|
|
|
|
import (
|
2020-10-28 14:17:42 -04:00
|
|
|
"log"
|
|
|
|
|
2020-07-08 21:57:35 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-06-29 00:41:58 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-09-18 16:10:52 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
2020-02-08 21:02:37 -05:00
|
|
|
)
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// AnimationDirection is a the animation play direction
|
2020-06-25 16:27:16 -04:00
|
|
|
type AnimationDirection int
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// AnimationDirection types
|
2020-06-25 16:27:16 -04:00
|
|
|
const (
|
2020-07-18 18:06:36 -04:00
|
|
|
DirectionForward AnimationDirection = iota
|
|
|
|
DirectionBackward
|
2020-06-25 16:27:16 -04:00
|
|
|
)
|
|
|
|
|
2020-10-28 14:17:42 -04:00
|
|
|
// static check that Sprite implements widget
|
|
|
|
var _ widget = &Sprite{}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// Sprite is an image
|
2020-02-08 21:02:37 -05:00
|
|
|
type Sprite struct {
|
2020-02-17 22:11:52 -05:00
|
|
|
widgetBase
|
2020-02-08 21:02:37 -05:00
|
|
|
|
|
|
|
segmentsX int
|
|
|
|
segmentsY int
|
|
|
|
frameOffset int
|
|
|
|
|
2020-07-05 13:01:44 -04:00
|
|
|
animation d2interface.Animation
|
2020-02-08 21:02:37 -05:00
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// AnimatedSprite is a sprite that has animation
|
2020-06-25 16:27:16 -04:00
|
|
|
type AnimatedSprite struct {
|
|
|
|
*Sprite
|
|
|
|
}
|
|
|
|
|
2020-09-18 16:10:52 -04:00
|
|
|
func createSprite(imagePath, palettePath string, assetManager *d2asset.AssetManager) (*Sprite, error) {
|
|
|
|
animation, err := assetManager.LoadAnimation(imagePath, palettePath)
|
2020-02-24 22:35:21 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sprite := &Sprite{}
|
|
|
|
sprite.animation = animation
|
|
|
|
sprite.SetVisible(true)
|
|
|
|
|
|
|
|
return sprite, nil
|
2020-02-08 21:02:37 -05:00
|
|
|
}
|
|
|
|
|
2020-10-25 21:11:04 -04:00
|
|
|
func createAnimatedSprite(
|
|
|
|
imagePath, palettePath string,
|
|
|
|
direction AnimationDirection,
|
|
|
|
assetManager *d2asset.AssetManager,
|
|
|
|
) (*AnimatedSprite, error) {
|
2020-09-18 16:10:52 -04:00
|
|
|
animation, err := assetManager.LoadAnimation(imagePath, palettePath)
|
2020-06-25 16:27:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-09-18 16:10:52 -04:00
|
|
|
sprite := &AnimatedSprite{Sprite: &Sprite{animation: animation}}
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-06-25 16:27:16 -04:00
|
|
|
if direction == DirectionForward {
|
|
|
|
sprite.animation.PlayForward()
|
|
|
|
} else {
|
|
|
|
sprite.animation.PlayBackward()
|
|
|
|
}
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-06-25 16:27:16 -04:00
|
|
|
sprite.SetVisible(true)
|
|
|
|
|
|
|
|
return sprite, nil
|
|
|
|
}
|
|
|
|
|
2020-10-28 14:17:42 -04:00
|
|
|
func (s *AnimatedSprite) render(target d2interface.Surface) {
|
2020-06-25 16:27:16 -04:00
|
|
|
_, frameHeight := s.animation.GetCurrentFrameSize()
|
|
|
|
|
|
|
|
target.PushTranslation(s.x, s.y-frameHeight)
|
|
|
|
defer target.Pop()
|
2020-07-18 18:06:36 -04:00
|
|
|
|
2020-10-28 14:17:42 -04:00
|
|
|
s.animation.Render(target)
|
2020-06-25 16:27:16 -04:00
|
|
|
}
|
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// SetSegmented sets the segment properties of the sprite
|
2020-02-08 21:02:37 -05:00
|
|
|
func (s *Sprite) SetSegmented(segmentsX, segmentsY, frameOffset int) {
|
|
|
|
s.segmentsX = segmentsX
|
|
|
|
s.segmentsY = segmentsY
|
|
|
|
s.frameOffset = frameOffset
|
|
|
|
}
|
|
|
|
|
2020-10-28 14:17:42 -04:00
|
|
|
func (s *Sprite) render(target d2interface.Surface) {
|
|
|
|
err := renderSegmented(s.animation, s.segmentsX, s.segmentsY, s.frameOffset, target)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2020-02-08 21:02:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sprite) advance(elapsed float64) error {
|
|
|
|
return s.animation.Advance(elapsed)
|
|
|
|
}
|
2020-02-17 22:11:52 -05:00
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
func (s *Sprite) getSize() (width, height int) {
|
2020-02-17 22:11:52 -05:00
|
|
|
return s.animation.GetCurrentFrameSize()
|
|
|
|
}
|
2020-07-08 21:57:35 -04:00
|
|
|
|
2020-07-18 18:06:36 -04:00
|
|
|
// SetEffect sets the draw effect for the sprite
|
2020-07-08 21:57:35 -04:00
|
|
|
func (s *Sprite) SetEffect(e d2enum.DrawEffect) {
|
|
|
|
s.animation.SetEffect(e)
|
|
|
|
}
|