2020-02-01 18:55:56 -05:00
|
|
|
package d2asset
|
2019-12-21 20:53:18 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"image/color"
|
2019-12-24 01:48:45 -05:00
|
|
|
"math"
|
2019-12-21 20:53:18 -05:00
|
|
|
|
2020-02-01 21:06:22 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
2020-02-26 08:39:38 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dat"
|
2020-01-31 23:18:11 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dc6"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dcc"
|
2020-02-01 18:55:56 -05:00
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2render"
|
2019-12-21 20:53:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type playMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
playModePause playMode = iota
|
|
|
|
playModeForward
|
|
|
|
playModeBackward
|
|
|
|
)
|
|
|
|
|
|
|
|
type animationFrame struct {
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
offsetX int
|
|
|
|
offsetY int
|
|
|
|
|
2020-02-01 20:39:28 -05:00
|
|
|
image d2render.Surface
|
2019-12-21 20:53:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type animationDirection struct {
|
|
|
|
frames []*animationFrame
|
|
|
|
}
|
|
|
|
|
|
|
|
type Animation struct {
|
|
|
|
directions []*animationDirection
|
|
|
|
frameIndex int
|
|
|
|
directionIndex int
|
|
|
|
lastFrameTime float64
|
2019-12-24 01:48:45 -05:00
|
|
|
playedCount int
|
2019-12-21 20:53:18 -05:00
|
|
|
|
2020-02-24 23:04:01 -05:00
|
|
|
compositeMode d2render.CompositeMode
|
|
|
|
colorMod color.Color
|
|
|
|
originAtBottom bool
|
2019-12-21 20:53:18 -05:00
|
|
|
|
2020-02-23 03:23:18 -05:00
|
|
|
playMode playMode
|
|
|
|
playLength float64
|
|
|
|
playLoop bool
|
|
|
|
hasSubLoop bool // runs after first animation ends
|
|
|
|
subStartingFrame int
|
|
|
|
subEndingFrame int
|
2019-12-21 20:53:18 -05:00
|
|
|
}
|
|
|
|
|
2020-06-28 12:10:25 -04:00
|
|
|
func CreateAnimationFromDCC(dcc *d2dcc.DCC, palette *d2dat.DATPalette, transparency int) (*Animation, error) {
|
2019-12-24 01:48:45 -05:00
|
|
|
animation := &Animation{
|
|
|
|
playLength: 1.0,
|
|
|
|
playLoop: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
for directionIndex, dccDirection := range dcc.Directions {
|
|
|
|
for _, dccFrame := range dccDirection.Frames {
|
|
|
|
minX, minY := math.MaxInt32, math.MaxInt32
|
|
|
|
maxX, maxY := math.MinInt32, math.MinInt32
|
|
|
|
for _, dccFrame := range dccDirection.Frames {
|
2020-02-01 21:06:22 -05:00
|
|
|
minX = d2common.MinInt(minX, dccFrame.Box.Left)
|
|
|
|
minY = d2common.MinInt(minY, dccFrame.Box.Top)
|
|
|
|
maxX = d2common.MaxInt(maxX, dccFrame.Box.Right())
|
|
|
|
maxY = d2common.MaxInt(maxY, dccFrame.Box.Bottom())
|
2019-12-24 01:48:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
frameWidth := maxX - minX
|
|
|
|
frameHeight := maxY - minY
|
|
|
|
|
|
|
|
pixels := make([]byte, frameWidth*frameHeight*4)
|
|
|
|
for y := 0; y < frameHeight; y++ {
|
|
|
|
for x := 0; x < frameWidth; x++ {
|
|
|
|
if paletteIndex := dccFrame.PixelData[y*frameWidth+x]; paletteIndex != 0 {
|
2020-02-01 21:51:49 -05:00
|
|
|
palColor := palette.Colors[paletteIndex]
|
2019-12-24 01:48:45 -05:00
|
|
|
offset := (x + y*frameWidth) * 4
|
2020-02-01 21:51:49 -05:00
|
|
|
pixels[offset] = palColor.R
|
|
|
|
pixels[offset+1] = palColor.G
|
|
|
|
pixels[offset+2] = palColor.B
|
2019-12-24 01:48:45 -05:00
|
|
|
pixels[offset+3] = byte(transparency)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-09 14:12:04 -05:00
|
|
|
image, err := d2render.NewSurface(frameWidth, frameHeight, d2render.FilterNearest)
|
2019-12-24 01:48:45 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := image.ReplacePixels(pixels); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if directionIndex >= len(animation.directions) {
|
|
|
|
animation.directions = append(animation.directions, new(animationDirection))
|
|
|
|
}
|
|
|
|
|
|
|
|
direction := animation.directions[directionIndex]
|
|
|
|
direction.frames = append(direction.frames, &animationFrame{
|
2020-02-01 21:51:49 -05:00
|
|
|
width: dccFrame.Width,
|
|
|
|
height: dccFrame.Height,
|
2019-12-24 01:48:45 -05:00
|
|
|
offsetX: minX,
|
|
|
|
offsetY: minY,
|
|
|
|
image: image,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return animation, nil
|
|
|
|
}
|
|
|
|
|
2020-06-28 12:10:25 -04:00
|
|
|
func CreateAnimationFromDC6(dc6 *d2dc6.DC6File, palette *d2dat.DATPalette) (*Animation, error) {
|
2019-12-21 20:53:18 -05:00
|
|
|
animation := &Animation{
|
2020-02-24 23:04:01 -05:00
|
|
|
playLength: 1.0,
|
|
|
|
playLoop: true,
|
|
|
|
originAtBottom: true,
|
2019-12-21 20:53:18 -05:00
|
|
|
}
|
|
|
|
|
2019-12-24 01:48:45 -05:00
|
|
|
for frameIndex, dc6Frame := range dc6.Frames {
|
2020-02-09 14:12:04 -05:00
|
|
|
image, err := d2render.NewSurface(int(dc6Frame.Width), int(dc6Frame.Height), d2render.FilterNearest)
|
2019-12-21 20:53:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-26 08:39:38 -05:00
|
|
|
indexData := make([]int, dc6Frame.Width*dc6Frame.Height)
|
|
|
|
for i := range indexData {
|
|
|
|
indexData[i] = -1
|
|
|
|
}
|
|
|
|
|
|
|
|
x := 0
|
|
|
|
y := int(dc6Frame.Height) - 1
|
|
|
|
offset := 0
|
|
|
|
|
|
|
|
for {
|
|
|
|
b := int(dc6Frame.FrameData[offset])
|
|
|
|
offset++
|
|
|
|
|
|
|
|
if b == 0x80 {
|
|
|
|
if y == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
y--
|
|
|
|
x = 0
|
|
|
|
} else if b&0x80 > 0 {
|
|
|
|
transparentPixels := b & 0x7f
|
|
|
|
for i := 0; i < transparentPixels; i++ {
|
|
|
|
indexData[x+y*int(dc6Frame.Width)+i] = -1
|
|
|
|
}
|
|
|
|
x += transparentPixels
|
|
|
|
} else {
|
|
|
|
for i := 0; i < b; i++ {
|
|
|
|
indexData[x+y*int(dc6Frame.Width)+i] = int(dc6Frame.FrameData[offset])
|
|
|
|
offset++
|
|
|
|
}
|
|
|
|
x += b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
colorData := make([]byte, dc6Frame.Width*dc6Frame.Height*4)
|
|
|
|
for i := 0; i < int(dc6Frame.Width*dc6Frame.Height); i++ {
|
|
|
|
if indexData[i] < 1 { // TODO: Is this == -1 or < 1?
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
colorData[i*4] = palette.Colors[indexData[i]].R
|
|
|
|
colorData[i*4+1] = palette.Colors[indexData[i]].G
|
|
|
|
colorData[i*4+2] = palette.Colors[indexData[i]].B
|
|
|
|
colorData[i*4+3] = 0xff
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := image.ReplacePixels(colorData); err != nil {
|
2019-12-21 20:53:18 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
directionIndex := frameIndex / int(dc6.FramesPerDirection)
|
|
|
|
if directionIndex >= len(animation.directions) {
|
|
|
|
animation.directions = append(animation.directions, new(animationDirection))
|
|
|
|
}
|
|
|
|
|
|
|
|
direction := animation.directions[directionIndex]
|
|
|
|
direction.frames = append(direction.frames, &animationFrame{
|
2019-12-24 01:48:45 -05:00
|
|
|
width: int(dc6Frame.Width),
|
|
|
|
height: int(dc6Frame.Height),
|
|
|
|
offsetX: int(dc6Frame.OffsetX),
|
|
|
|
offsetY: int(dc6Frame.OffsetY),
|
2019-12-21 20:53:18 -05:00
|
|
|
image: image,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return animation, nil
|
|
|
|
}
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
func (a *Animation) Clone() *Animation {
|
2019-12-21 20:53:18 -05:00
|
|
|
animation := *a
|
|
|
|
return &animation
|
|
|
|
}
|
|
|
|
|
2020-02-23 03:23:18 -05:00
|
|
|
func (a *Animation) SetSubLoop(startFrame, EndFrame int) {
|
|
|
|
a.subStartingFrame = startFrame
|
|
|
|
a.subEndingFrame = EndFrame
|
|
|
|
a.hasSubLoop = true
|
|
|
|
}
|
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
func (a *Animation) Advance(elapsed float64) error {
|
|
|
|
if a.playMode == playModePause {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
frameCount := a.GetFrameCount()
|
|
|
|
frameLength := a.playLength / float64(frameCount)
|
|
|
|
a.lastFrameTime += elapsed
|
|
|
|
framesAdvanced := int(a.lastFrameTime / frameLength)
|
|
|
|
a.lastFrameTime -= float64(framesAdvanced) * frameLength
|
|
|
|
|
|
|
|
for i := 0; i < framesAdvanced; i++ {
|
2020-02-23 03:23:18 -05:00
|
|
|
startIndex := 0
|
|
|
|
endIndex := frameCount
|
|
|
|
if a.hasSubLoop && a.playedCount > 0 {
|
|
|
|
startIndex = a.subStartingFrame
|
|
|
|
endIndex = a.subEndingFrame
|
|
|
|
}
|
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
switch a.playMode {
|
|
|
|
case playModeForward:
|
|
|
|
a.frameIndex++
|
2020-02-23 03:23:18 -05:00
|
|
|
if a.frameIndex >= endIndex {
|
2019-12-24 01:48:45 -05:00
|
|
|
a.playedCount++
|
2019-12-21 20:53:18 -05:00
|
|
|
if a.playLoop {
|
2020-02-23 03:23:18 -05:00
|
|
|
a.frameIndex = startIndex
|
2019-12-21 20:53:18 -05:00
|
|
|
} else {
|
2020-02-23 03:23:18 -05:00
|
|
|
a.frameIndex = endIndex - 1
|
2019-12-21 20:53:18 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case playModeBackward:
|
|
|
|
a.frameIndex--
|
2020-02-23 03:23:18 -05:00
|
|
|
if a.frameIndex < startIndex {
|
2019-12-24 01:48:45 -05:00
|
|
|
a.playedCount++
|
2019-12-21 20:53:18 -05:00
|
|
|
if a.playLoop {
|
2020-02-23 03:23:18 -05:00
|
|
|
a.frameIndex = endIndex - 1
|
2019-12-21 20:53:18 -05:00
|
|
|
} else {
|
2020-02-23 03:23:18 -05:00
|
|
|
a.frameIndex = startIndex
|
2019-12-21 20:53:18 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:28 -05:00
|
|
|
func (a *Animation) Render(target d2render.Surface) error {
|
2019-12-21 20:53:18 -05:00
|
|
|
direction := a.directions[a.directionIndex]
|
|
|
|
frame := direction.frames[a.frameIndex]
|
|
|
|
|
2019-12-28 16:46:08 -05:00
|
|
|
target.PushTranslation(frame.offsetX, frame.offsetY)
|
|
|
|
target.PushCompositeMode(a.compositeMode)
|
|
|
|
target.PushColor(a.colorMod)
|
|
|
|
defer target.PopN(3)
|
|
|
|
return target.Render(frame.image)
|
2019-12-21 20:53:18 -05:00
|
|
|
}
|
|
|
|
|
2020-02-24 23:04:01 -05:00
|
|
|
func (a *Animation) RenderFromOrigin(target d2render.Surface) error {
|
|
|
|
if a.originAtBottom {
|
|
|
|
direction := a.directions[a.directionIndex]
|
|
|
|
frame := direction.frames[a.frameIndex]
|
|
|
|
target.PushTranslation(0, -frame.height)
|
|
|
|
defer target.Pop()
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.Render(target)
|
|
|
|
}
|
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
func (a *Animation) GetFrameSize(frameIndex int) (int, int, error) {
|
|
|
|
direction := a.directions[a.directionIndex]
|
|
|
|
if frameIndex >= len(direction.frames) {
|
|
|
|
return 0, 0, errors.New("invalid frame index")
|
|
|
|
}
|
|
|
|
|
|
|
|
frame := direction.frames[frameIndex]
|
|
|
|
return frame.width, frame.height, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animation) GetCurrentFrameSize() (int, int) {
|
|
|
|
width, height, _ := a.GetFrameSize(a.frameIndex)
|
|
|
|
return width, height
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animation) GetFrameBounds() (int, int) {
|
|
|
|
maxWidth, maxHeight := 0, 0
|
|
|
|
|
|
|
|
direction := a.directions[a.directionIndex]
|
|
|
|
for _, frame := range direction.frames {
|
2020-02-01 21:06:22 -05:00
|
|
|
maxWidth = d2common.MaxInt(maxWidth, frame.width)
|
|
|
|
maxHeight = d2common.MaxInt(maxHeight, frame.height)
|
2019-12-21 20:53:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return maxWidth, maxHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animation) GetCurrentFrame() int {
|
|
|
|
return a.frameIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animation) GetFrameCount() int {
|
|
|
|
direction := a.directions[a.directionIndex]
|
|
|
|
return len(direction.frames)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animation) IsOnFirstFrame() bool {
|
|
|
|
return a.frameIndex == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animation) IsOnLastFrame() bool {
|
|
|
|
return a.frameIndex == a.GetFrameCount()-1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animation) GetDirectionCount() int {
|
|
|
|
return len(a.directions)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animation) SetDirection(directionIndex int) error {
|
2020-02-22 22:33:58 -05:00
|
|
|
if directionIndex >= 64 {
|
2019-12-21 20:53:18 -05:00
|
|
|
return errors.New("invalid direction index")
|
|
|
|
}
|
2020-02-22 22:33:58 -05:00
|
|
|
a.directionIndex = d2dcc.Dir64ToDcc(directionIndex, len(a.directions))
|
2019-12-21 20:53:18 -05:00
|
|
|
a.frameIndex = 0
|
2020-02-22 22:33:58 -05:00
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animation) GetDirection() int {
|
|
|
|
return a.directionIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animation) SetCurrentFrame(frameIndex int) error {
|
|
|
|
if frameIndex >= a.GetFrameCount() {
|
|
|
|
return errors.New("invalid frame index")
|
|
|
|
}
|
|
|
|
|
|
|
|
a.frameIndex = frameIndex
|
|
|
|
a.lastFrameTime = 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animation) Rewind() {
|
|
|
|
a.SetCurrentFrame(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animation) PlayForward() {
|
|
|
|
a.playMode = playModeForward
|
|
|
|
a.lastFrameTime = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animation) PlayBackward() {
|
|
|
|
a.playMode = playModeBackward
|
|
|
|
a.lastFrameTime = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animation) Pause() {
|
|
|
|
a.playMode = playModePause
|
|
|
|
a.lastFrameTime = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animation) SetPlayLoop(loop bool) {
|
2020-02-01 21:51:49 -05:00
|
|
|
a.playLoop = loop
|
2019-12-21 20:53:18 -05:00
|
|
|
}
|
|
|
|
|
2019-12-24 01:48:45 -05:00
|
|
|
func (a *Animation) SetPlaySpeed(playSpeed float64) {
|
|
|
|
a.SetPlayLength(playSpeed * float64(a.GetFrameCount()))
|
|
|
|
}
|
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
func (a *Animation) SetPlayLength(playLength float64) {
|
|
|
|
a.playLength = playLength
|
|
|
|
a.lastFrameTime = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animation) SetPlayLengthMs(playLengthMs int) {
|
|
|
|
a.SetPlayLength(float64(playLengthMs) / 1000.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animation) SetColorMod(color color.Color) {
|
|
|
|
a.colorMod = color
|
|
|
|
}
|
|
|
|
|
2019-12-24 01:48:45 -05:00
|
|
|
func (a *Animation) GetPlayedCount() int {
|
|
|
|
return a.playedCount
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Animation) ResetPlayedCount() {
|
|
|
|
a.playedCount = 0
|
|
|
|
}
|
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
func (a *Animation) SetBlend(blend bool) {
|
|
|
|
if blend {
|
2020-02-01 20:39:28 -05:00
|
|
|
a.compositeMode = d2render.CompositeModeLighter
|
2019-12-21 20:53:18 -05:00
|
|
|
} else {
|
2020-02-01 20:39:28 -05:00
|
|
|
a.compositeMode = d2render.CompositeModeSourceOver
|
2019-12-21 20:53:18 -05:00
|
|
|
}
|
|
|
|
}
|