1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-19 10:56:07 -05:00
OpenDiablo2/d2core/d2ui/sprite.go

191 lines
4.8 KiB
Go
Raw Normal View History

package d2ui
2019-10-24 09:31:59 -04:00
import (
"fmt"
"image"
"image/color"
"log"
2019-10-24 09:31:59 -04:00
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
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 {
*BaseWidget
animation d2interface.Animation
}
const (
errNoAnimation = "no animation was specified"
)
// NewSprite creates a new Sprite
Decouple asset manager from renderer (#730) * improve AssetManager implementation Notable changes are: * removed the individual managers inside of d2asset, only one asset manager * AssetManager now has caches for the types of files it loads * created a type for TextDictionary (the txt file structs) * fixed a file path bug in d2loader Source * fixed a asset stream bug in d2loader Asset * d2loader.Loader now needs a d2config.Config on creation (for resolving locale files) * updated the mpq file in d2asset test data, added test case for "sub-directory" * added a Data method to d2asset.Asset. The data is cached on first full read. * renamed ArchiveDataStream to DataStream in d2interface * moved palette utility func out of d2asset and into d2util * bugfix for MacOS mpq loader issue * lint fixes, added data caching to filesystem asset * adding comment for mpq asset close * Decouple d2asset from d2render Notable changes in d2common: * d2dcc.Load now fully decodes the dcc and stores the directions/frames in the dcc struct * un-exported dcc.decodeDirection, it is only used in d2dcc * removed font interface from d2interface, we only have one font implementation * added `Renderer` method to d2interface.Surface, animations use this to bind to a renderer and create surfaces as they need * added `BindRenderer` method to animation interface Notable changes in d2common/d2asset: * **d2asset.NewAssetManager only needs to be passed a d2config.Config**, it is decoupled from d2render * exported Animation * Animation implementation binds to the renderer to create surfaces only on the first time it is rendered * font, dcc, dc6 initialization logic moved out of asset_manager.go * for dc6 and dcc animations, the process of decoding and creating render surfaces has been broken into different methods * the d2asset.Font struct now stores font table data for initialization purposes Notable changes in d2core/d2render: * Surfaces store a renderer reference, this allows animations to bind to the renderer and create a surface just-in-time **These last changes should have been a separate PR, sorry.** Notable changes in d2core/d2ui: * ui.NewSprite now handles creating an animation internally, only needs image and palette path as arguments Notable Changes in d2game: Because of the change in d2ui, all instances of this code pattern... ```golang animation, err := screen.asset.LoadAnimation(imgPath, palettePath) sprite, err := screen.ui.NewSprite(animation) ``` ... becomes this ... ```golang sprite, err := screen.ui.NewSprite(imgPath, palettePath) ```
2020-09-14 17:31:45 -04:00
func (ui *UIManager) NewSprite(animationPath, palettePath string) (*Sprite, error) {
animation, err := ui.asset.LoadAnimation(animationPath, palettePath)
if animation == nil || err != nil {
return nil, fmt.Errorf(errNoAnimation)
2019-10-24 09:31:59 -04:00
}
animation.BindRenderer(ui.renderer)
Decouple asset manager from renderer (#730) * improve AssetManager implementation Notable changes are: * removed the individual managers inside of d2asset, only one asset manager * AssetManager now has caches for the types of files it loads * created a type for TextDictionary (the txt file structs) * fixed a file path bug in d2loader Source * fixed a asset stream bug in d2loader Asset * d2loader.Loader now needs a d2config.Config on creation (for resolving locale files) * updated the mpq file in d2asset test data, added test case for "sub-directory" * added a Data method to d2asset.Asset. The data is cached on first full read. * renamed ArchiveDataStream to DataStream in d2interface * moved palette utility func out of d2asset and into d2util * bugfix for MacOS mpq loader issue * lint fixes, added data caching to filesystem asset * adding comment for mpq asset close * Decouple d2asset from d2render Notable changes in d2common: * d2dcc.Load now fully decodes the dcc and stores the directions/frames in the dcc struct * un-exported dcc.decodeDirection, it is only used in d2dcc * removed font interface from d2interface, we only have one font implementation * added `Renderer` method to d2interface.Surface, animations use this to bind to a renderer and create surfaces as they need * added `BindRenderer` method to animation interface Notable changes in d2common/d2asset: * **d2asset.NewAssetManager only needs to be passed a d2config.Config**, it is decoupled from d2render * exported Animation * Animation implementation binds to the renderer to create surfaces only on the first time it is rendered * font, dcc, dc6 initialization logic moved out of asset_manager.go * for dc6 and dcc animations, the process of decoding and creating render surfaces has been broken into different methods * the d2asset.Font struct now stores font table data for initialization purposes Notable changes in d2core/d2render: * Surfaces store a renderer reference, this allows animations to bind to the renderer and create a surface just-in-time **These last changes should have been a separate PR, sorry.** Notable changes in d2core/d2ui: * ui.NewSprite now handles creating an animation internally, only needs image and palette path as arguments Notable Changes in d2game: Because of the change in d2ui, all instances of this code pattern... ```golang animation, err := screen.asset.LoadAnimation(imgPath, palettePath) sprite, err := screen.ui.NewSprite(animation) ``` ... becomes this ... ```golang sprite, err := screen.ui.NewSprite(imgPath, palettePath) ```
2020-09-14 17:31:45 -04:00
base := NewBaseWidget(ui)
return &Sprite{
BaseWidget: base,
animation: animation}, nil
2019-11-10 10:44:13 -05:00
}
// Render renders the sprite on the given surface
func (s *Sprite) Render(target d2interface.Surface) {
_, frameHeight := s.animation.GetCurrentFrameSize()
target.PushTranslation(s.x, s.y-frameHeight)
defer target.Pop()
s.animation.Render(target)
}
// RenderSection renders the section of the sprite enclosed by bounds
func (s *Sprite) RenderSection(sfc d2interface.Surface, bound image.Rectangle) {
sfc.PushTranslation(s.x, s.y-bound.Dy())
defer sfc.Pop()
s.animation.RenderSection(sfc, bound)
}
// RenderSegmented renders a sprite that is internally segmented as frames
func (s *Sprite) RenderSegmented(target d2interface.Surface, segmentsX, segmentsY, frameOffset int) {
var currentY int
for y := 0; y < segmentsY; y++ {
var currentX, maxFrameHeight int
for x := 0; x < segmentsX; x++ {
Decouple asset manager from renderer (#730) * improve AssetManager implementation Notable changes are: * removed the individual managers inside of d2asset, only one asset manager * AssetManager now has caches for the types of files it loads * created a type for TextDictionary (the txt file structs) * fixed a file path bug in d2loader Source * fixed a asset stream bug in d2loader Asset * d2loader.Loader now needs a d2config.Config on creation (for resolving locale files) * updated the mpq file in d2asset test data, added test case for "sub-directory" * added a Data method to d2asset.Asset. The data is cached on first full read. * renamed ArchiveDataStream to DataStream in d2interface * moved palette utility func out of d2asset and into d2util * bugfix for MacOS mpq loader issue * lint fixes, added data caching to filesystem asset * adding comment for mpq asset close * Decouple d2asset from d2render Notable changes in d2common: * d2dcc.Load now fully decodes the dcc and stores the directions/frames in the dcc struct * un-exported dcc.decodeDirection, it is only used in d2dcc * removed font interface from d2interface, we only have one font implementation * added `Renderer` method to d2interface.Surface, animations use this to bind to a renderer and create surfaces as they need * added `BindRenderer` method to animation interface Notable changes in d2common/d2asset: * **d2asset.NewAssetManager only needs to be passed a d2config.Config**, it is decoupled from d2render * exported Animation * Animation implementation binds to the renderer to create surfaces only on the first time it is rendered * font, dcc, dc6 initialization logic moved out of asset_manager.go * for dc6 and dcc animations, the process of decoding and creating render surfaces has been broken into different methods * the d2asset.Font struct now stores font table data for initialization purposes Notable changes in d2core/d2render: * Surfaces store a renderer reference, this allows animations to bind to the renderer and create a surface just-in-time **These last changes should have been a separate PR, sorry.** Notable changes in d2core/d2ui: * ui.NewSprite now handles creating an animation internally, only needs image and palette path as arguments Notable Changes in d2game: Because of the change in d2ui, all instances of this code pattern... ```golang animation, err := screen.asset.LoadAnimation(imgPath, palettePath) sprite, err := screen.ui.NewSprite(animation) ``` ... becomes this ... ```golang sprite, err := screen.ui.NewSprite(imgPath, palettePath) ```
2020-09-14 17:31:45 -04:00
idx := x + y*segmentsX + frameOffset*segmentsX*segmentsY
if err := s.animation.SetCurrentFrame(idx); err != nil {
log.Printf("SetCurrentFrame error %e", err)
2019-11-13 00:31:52 -05:00
}
target.PushTranslation(s.x+currentX, s.y+currentY)
s.animation.Render(target)
target.Pop()
frameWidth, frameHeight := s.GetCurrentFrameSize()
maxFrameHeight = d2math.MaxInt(maxFrameHeight, frameHeight)
currentX += frameWidth
2019-10-27 02:58:37 -04:00
}
currentY += maxFrameHeight
2019-10-24 09:31:59 -04:00
}
}
// GetSize returns the size of the current frame
func (s *Sprite) GetSize() (width, height int) {
return s.GetCurrentFrameSize()
2019-10-27 02:58:37 -04:00
}
// GetFrameSize gets the Size(width, height) of a indexed frame.
func (s *Sprite) GetFrameSize(frameIndex int) (x, y int, err 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() (width, height 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() (width, height 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() {
err := s.animation.SetCurrentFrame(0)
if err != nil {
log.Print(err)
}
}
// 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)
}
// SetPlayLength sets the play length of the sprite animation
func (s *Sprite) SetPlayLength(playLength float64) {
s.animation.SetPlayLength(playLength)
}
// SetColorMod sets the color modifier
func (s *Sprite) SetColorMod(c color.Color) {
s.animation.SetColorMod(c)
}
// Advance advances the animation
func (s *Sprite) Advance(elapsed float64) error {
return s.animation.Advance(elapsed)
}
// SetEffect sets the draw effect type
func (s *Sprite) SetEffect(e d2enum.DrawEffect) {
s.animation.SetEffect(e)
}