2020-02-01 18:55:56 -05:00
|
|
|
package d2ui
|
2019-10-24 09:31:59 -04:00
|
|
|
|
|
|
|
import (
|
2020-08-06 10:30:23 -04:00
|
|
|
"fmt"
|
2020-06-30 12:43:13 -04:00
|
|
|
"image"
|
2019-10-24 19:13:30 -04:00
|
|
|
"image/color"
|
2020-09-23 13:30:54 -04:00
|
|
|
"log"
|
2019-10-24 09:31:59 -04:00
|
|
|
|
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-08-06 10:30:23 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
|
2019-10-24 09:31:59 -04:00
|
|
|
)
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// Sprite is a positioned visual object.
|
2019-10-24 09:31:59 -04:00
|
|
|
type Sprite struct {
|
2020-11-06 06:38:20 -05:00
|
|
|
*BaseWidget
|
2020-07-05 13:01:44 -04:00
|
|
|
animation d2interface.Animation
|
2019-12-21 20:53:18 -05:00
|
|
|
}
|
2019-11-24 17:58:23 -05:00
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
const (
|
|
|
|
errNoAnimation = "no animation was specified"
|
2020-01-31 23:18:11 -05:00
|
|
|
)
|
2019-12-15 14:07:57 -05:00
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// 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 {
|
2020-08-06 10:30:23 -04:00
|
|
|
return nil, fmt.Errorf(errNoAnimation)
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
2020-08-06 10:30:23 -04:00
|
|
|
|
2020-10-28 14:17:42 -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
|
|
|
|
2020-11-06 06:38:20 -05:00
|
|
|
base := NewBaseWidget(ui)
|
|
|
|
|
|
|
|
return &Sprite{
|
|
|
|
BaseWidget: base,
|
|
|
|
animation: animation}, nil
|
2019-11-10 10:44:13 -05:00
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// Render renders the sprite on the given surface
|
2020-11-11 08:55:59 -05:00
|
|
|
func (s *Sprite) Render(target d2interface.Surface) {
|
2019-12-21 20:53:18 -05:00
|
|
|
_, frameHeight := s.animation.GetCurrentFrameSize()
|
|
|
|
|
2019-12-28 16:46:08 -05:00
|
|
|
target.PushTranslation(s.x, s.y-frameHeight)
|
|
|
|
defer target.Pop()
|
2020-06-30 12:43:13 -04:00
|
|
|
|
2020-10-28 14:17:42 -04:00
|
|
|
s.animation.Render(target)
|
2019-12-21 20:53:18 -05:00
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// RenderSection renders the section of the sprite enclosed by bounds
|
2020-10-28 14:17:42 -04:00
|
|
|
func (s *Sprite) RenderSection(sfc d2interface.Surface, bound image.Rectangle) {
|
2020-06-30 12:43:13 -04:00
|
|
|
sfc.PushTranslation(s.x, s.y-bound.Dy())
|
|
|
|
defer sfc.Pop()
|
|
|
|
|
2020-10-28 14:17:42 -04:00
|
|
|
s.animation.RenderSection(sfc, bound)
|
2020-06-30 12:43:13 -04:00
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// RenderSegmented renders a sprite that is internally segmented as frames
|
2020-11-11 08:55:59 -05:00
|
|
|
func (s *Sprite) RenderSegmented(target d2interface.Surface, segmentsX, segmentsY, frameOffset int) {
|
2019-12-21 20:53:18 -05:00
|
|
|
var currentY int
|
2020-06-30 12:43:13 -04:00
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
for y := 0; y < segmentsY; y++ {
|
2020-08-06 10:30:23 -04:00
|
|
|
var currentX, maxFrameHeight int
|
2020-06-30 12:43:13 -04:00
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
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 {
|
2020-11-11 08:55:59 -05:00
|
|
|
log.Printf("SetCurrentFrame error %e", err)
|
2019-11-13 00:31:52 -05:00
|
|
|
}
|
2019-12-21 20:53:18 -05:00
|
|
|
|
2019-12-28 16:46:08 -05:00
|
|
|
target.PushTranslation(s.x+currentX, s.y+currentY)
|
2020-10-28 14:17:42 -04:00
|
|
|
s.animation.Render(target)
|
2019-12-28 16:46:08 -05:00
|
|
|
target.Pop()
|
2020-06-30 12:43:13 -04:00
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
frameWidth, frameHeight := s.GetCurrentFrameSize()
|
2020-08-05 00:03:33 -04:00
|
|
|
maxFrameHeight = d2math.MaxInt(maxFrameHeight, frameHeight)
|
2019-12-21 20:53:18 -05:00
|
|
|
currentX += frameWidth
|
2019-10-27 02:58:37 -04:00
|
|
|
}
|
2019-12-21 20:53:18 -05:00
|
|
|
|
|
|
|
currentY += maxFrameHeight
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 06:38:20 -05: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
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// GetFrameSize gets the Size(width, height) of a indexed frame.
|
2020-08-06 10:30:23 -04:00
|
|
|
func (s *Sprite) GetFrameSize(frameIndex int) (x, y int, err error) {
|
2019-12-21 20:53:18 -05:00
|
|
|
return s.animation.GetFrameSize(frameIndex)
|
2019-10-25 23:41:54 -04:00
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// GetCurrentFrameSize gets the Size(width, height) of the current frame.
|
2020-08-06 10:30:23 -04:00
|
|
|
func (s *Sprite) GetCurrentFrameSize() (width, height int) {
|
2019-12-21 20:53:18 -05:00
|
|
|
return s.animation.GetCurrentFrameSize()
|
2019-10-25 23:41:54 -04:00
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// GetFrameBounds gets maximum Size(width, height) of all frame.
|
2020-08-06 10:30:23 -04:00
|
|
|
func (s *Sprite) GetFrameBounds() (width, height int) {
|
2019-12-21 20:53:18 -05:00
|
|
|
return s.animation.GetFrameBounds()
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// GetCurrentFrame gets index of current frame in animation
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) GetCurrentFrame() int {
|
|
|
|
return s.animation.GetCurrentFrame()
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// GetFrameCount gets number of frames in animation
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) GetFrameCount() int {
|
|
|
|
return s.animation.GetFrameCount()
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// IsOnFirstFrame gets if the animation on its first frame
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) IsOnFirstFrame() bool {
|
|
|
|
return s.animation.IsOnFirstFrame()
|
2019-10-24 09:31:59 -04:00
|
|
|
}
|
2019-10-25 18:40:27 -04:00
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// IsOnLastFrame gets if the animation on its last frame
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) IsOnLastFrame() bool {
|
|
|
|
return s.animation.IsOnLastFrame()
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// GetDirectionCount gets the number of animation direction
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) GetDirectionCount() int {
|
|
|
|
return s.animation.GetDirectionCount()
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// SetDirection places the animation in the direction of an animation
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) SetDirection(directionIndex int) error {
|
|
|
|
return s.animation.SetDirection(directionIndex)
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// GetDirection get the current animation direction
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) GetDirection() int {
|
|
|
|
return s.animation.GetDirection()
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// SetCurrentFrame sets animation at a specific frame
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) SetCurrentFrame(frameIndex int) error {
|
|
|
|
return s.animation.SetCurrentFrame(frameIndex)
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// Rewind sprite to beginning
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) Rewind() {
|
2020-09-23 13:30:54 -04:00
|
|
|
err := s.animation.SetCurrentFrame(0)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2019-12-21 20:53:18 -05:00
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// PlayForward plays sprite forward
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) PlayForward() {
|
|
|
|
s.animation.PlayForward()
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// PlayBackward play sprites backward
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) PlayBackward() {
|
|
|
|
s.animation.PlayBackward()
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// Pause animation
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) Pause() {
|
|
|
|
s.animation.Pause()
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// SetPlayLoop sets whether to loop the animation
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) SetPlayLoop(loop bool) {
|
|
|
|
s.animation.SetPlayLoop(loop)
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// SetPlayLength sets the play length of the sprite animation
|
2019-12-21 20:53:18 -05:00
|
|
|
func (s *Sprite) SetPlayLength(playLength float64) {
|
|
|
|
s.animation.SetPlayLength(playLength)
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// SetColorMod sets the color modifier
|
|
|
|
func (s *Sprite) SetColorMod(c color.Color) {
|
|
|
|
s.animation.SetColorMod(c)
|
2019-12-21 20:53:18 -05:00
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// Advance advances the animation
|
2019-12-28 23:32:24 -05:00
|
|
|
func (s *Sprite) Advance(elapsed float64) error {
|
|
|
|
return s.animation.Advance(elapsed)
|
2019-10-25 18:40:27 -04:00
|
|
|
}
|
2020-07-08 21:57:35 -04:00
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
// SetEffect sets the draw effect type
|
2020-07-08 21:57:35 -04:00
|
|
|
func (s *Sprite) SetEffect(e d2enum.DrawEffect) {
|
|
|
|
s.animation.SetEffect(e)
|
|
|
|
}
|