1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-18 18:36:12 -05:00
OpenDiablo2/d2core/d2ui/sprite.go

191 lines
4.5 KiB
Go
Raw Normal View History

package d2ui
2019-10-24 09:31:59 -04:00
import (
"errors"
"image"
"image/color"
2019-10-24 09:31:59 -04:00
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2common"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
2019-10-24 09:31:59 -04:00
)
// Sprite is a positioned visual object.
2019-10-24 09:31:59 -04:00
type Sprite struct {
x int
y int
animation *d2asset.Animation
}
var (
ErrNoAnimation = errors.New("no animation was specified")
)
func LoadSprite(animation *d2asset.Animation) (*Sprite, error) {
if animation == nil {
return nil, ErrNoAnimation
2019-10-24 09:31:59 -04:00
}
return &Sprite{animation: animation}, nil
2019-11-10 10:44:13 -05:00
}
func (s *Sprite) Render(target d2interface.Surface) error {
_, frameHeight := s.animation.GetCurrentFrameSize()
target.PushTranslation(s.x, s.y-frameHeight)
defer target.Pop()
return s.animation.Render(target)
}
// RenderSection renders the section of the sprite enclosed by bounds
func (s *Sprite) RenderSection(sfc d2interface.Surface, bound image.Rectangle) error {
sfc.PushTranslation(s.x, s.y-bound.Dy())
defer sfc.Pop()
return s.animation.RenderSection(sfc, bound)
}
func (s *Sprite) RenderSegmented(target d2interface.Surface, segmentsX, segmentsY, frameOffset int) error {
var currentY int
for y := 0; y < segmentsY; y++ {
var currentX int
var maxFrameHeight int
for x := 0; x < segmentsX; x++ {
if err := s.animation.SetCurrentFrame(x + y*segmentsX + frameOffset*segmentsX*segmentsY); err != nil {
return err
2019-11-13 00:31:52 -05:00
}
target.PushTranslation(s.x+currentX, s.y+currentY)
err := s.animation.Render(target)
target.Pop()
if err != nil {
return err
2019-10-27 02:58:37 -04:00
}
frameWidth, frameHeight := s.GetCurrentFrameSize()
maxFrameHeight = d2common.MaxInt(maxFrameHeight, frameHeight)
currentX += frameWidth
2019-10-27 02:58:37 -04:00
}
currentY += maxFrameHeight
2019-10-24 09:31:59 -04:00
}
return nil
2019-10-24 09:31:59 -04:00
}
// SetPosition places the sprite in 2D
func (s *Sprite) SetPosition(x, y int) {
s.x = x
s.y = y
2019-10-27 02:58:37 -04:00
}
// GetPosition retrieves the 2D position of the sprite
func (s *Sprite) GetPosition() (int, int) {
return s.x, s.y
2019-10-27 02:58:37 -04:00
}
// GetFrameSize gets the Size(width, height) of a indexed frame.
func (s *Sprite) GetFrameSize(frameIndex int) (int, int, error) {
return s.animation.GetFrameSize(frameIndex)
2019-10-25 23:41:54 -04:00
}
// GetCurrentFrameSize gets the Size(width, height) of the current frame.
func (s *Sprite) GetCurrentFrameSize() (int, int) {
return s.animation.GetCurrentFrameSize()
2019-10-25 23:41:54 -04:00
}
// GetFrameBounds gets maximum Size(width, height) of all frame.
func (s *Sprite) GetFrameBounds() (int, int) {
return s.animation.GetFrameBounds()
2019-10-24 09:31:59 -04:00
}
// GetCurrentFrame gets index of current frame in animation
func (s *Sprite) GetCurrentFrame() int {
return s.animation.GetCurrentFrame()
}
// GetFrameCount gets number of frames in animation
func (s *Sprite) GetFrameCount() int {
return s.animation.GetFrameCount()
2019-10-24 09:31:59 -04:00
}
// IsOnFirstFrame gets if the animation on its first frame
func (s *Sprite) IsOnFirstFrame() bool {
return s.animation.IsOnFirstFrame()
2019-10-24 09:31:59 -04:00
}
// IsOnLastFrame gets if the animation on its last frame
func (s *Sprite) IsOnLastFrame() bool {
return s.animation.IsOnLastFrame()
}
// GetDirectionCount gets the number of animation direction
func (s *Sprite) GetDirectionCount() int {
return s.animation.GetDirectionCount()
}
// SetDirection places the animation in the direction of an animation
func (s *Sprite) SetDirection(directionIndex int) error {
return s.animation.SetDirection(directionIndex)
}
// GetDirection get the current animation direction
func (s *Sprite) GetDirection() int {
return s.animation.GetDirection()
}
// SetCurrentFrame sets animation at a specific frame
func (s *Sprite) SetCurrentFrame(frameIndex int) error {
return s.animation.SetCurrentFrame(frameIndex)
}
// Rewind sprite to beginning
func (s *Sprite) Rewind() {
s.animation.SetCurrentFrame(0)
}
// PlayForward plays sprite forward
func (s *Sprite) PlayForward() {
s.animation.PlayForward()
}
// PlayBackward play sprites backward
func (s *Sprite) PlayBackward() {
s.animation.PlayBackward()
}
// Pause animation
func (s *Sprite) Pause() {
s.animation.Pause()
}
// SetPlayLoop sets whether to loop the animation
func (s *Sprite) SetPlayLoop(loop bool) {
s.animation.SetPlayLoop(loop)
}
func (s *Sprite) SetPlayLength(playLength float64) {
s.animation.SetPlayLength(playLength)
}
func (s *Sprite) SetPlayLengthMs(playLengthMs int) {
s.animation.SetPlayLengthMs(playLengthMs)
}
func (s *Sprite) SetColorMod(color color.Color) {
s.animation.SetColorMod(color)
}
// SetBlend sets the animation alpha blending status
func (s *Sprite) SetBlend(blend bool) {
s.animation.SetBlend(blend)
}
func (s *Sprite) Advance(elapsed float64) error {
return s.animation.Advance(elapsed)
}