1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-19 21:55:24 +00:00
OpenDiablo2/d2core/d2gui/sprite.go

87 lines
1.8 KiB
Go
Raw Normal View History

2020-02-09 02:02:37 +00:00
package d2gui
import (
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
2020-02-09 02:02:37 +00:00
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
)
Enhanced escape menu with options - d2gui bug remaining (#459) * First improvements Signed-off-by: William Claude <w.claude@thebeat.co> * Make the menu more generic Signed-off-by: William Claude <w.claude@thebeat.co> * Handle mouse events Signed-off-by: William Claude <w.claude@thebeat.co> * Remove debug statement Signed-off-by: William Claude <w.claude@thebeat.co> * Handle left clicks better Signed-off-by: William Claude <w.claude@thebeat.co> * Remove unused file Signed-off-by: William Claude <w.claude@thebeat.co> * Handle titles in screens Signed-off-by: William Claude <w.claude@thebeat.co> * Improve the menu using layouts Signed-off-by: William Claude <w.claude@thebeat.co> * Add support for onOff labels Signed-off-by: William Claude <w.claude@thebeat.co> * Mutualise title creation Signed-off-by: William Claude <w.claude@thebeat.co> * Add gutter and mutualise things Signed-off-by: William Claude <w.claude@thebeat.co> * Improve menu, mutualise a lot of things and support animated sprites Signed-off-by: William Claude <w.claude@thebeat.co> * Use a cfg struct instead of independent handlers Signed-off-by: William Claude <w.claude@thebeat.co> * Fix hardcoded value Signed-off-by: William Claude <w.claude@thebeat.co> * Clean things a bit Signed-off-by: William Claude <w.claude@thebeat.co> * Remove unused property Signed-off-by: William Claude <w.claude@thebeat.co> * First support for hoverable elements Signed-off-by: William Claude <w.claude@thebeat.co> * Add support for label selection feedback Signed-off-by: William Claude <w.claude@thebeat.co> * Add support options Signed-off-by: William Claude <w.claude@thebeat.co> * Update print statement Signed-off-by: William Claude <w.claude@thebeat.co> * Update rendering and clean code Signed-off-by: William Claude <w.claude@thebeat.co> * Remove debug things Signed-off-by: William Claude <w.claude@thebeat.co> * Handle hovering Signed-off-by: William Claude <w.claude@thebeat.co> * Support enter key for labels Signed-off-by: William Claude <w.claude@thebeat.co> * Attach methods to layout Signed-off-by: William Claude <w.claude@thebeat.co> * Move things under EscapeMenu Signed-off-by: William Claude <w.claude@thebeat.co> * Some renaming Signed-off-by: William Claude <w.claude@thebeat.co> * Clean Signed-off-by: William Claude <w.claude@thebeat.co> * Set hovered element ID when using the mouse Signed-off-by: William Claude <w.claude@thebeat.co> * Clean Signed-off-by: William Claude <w.claude@thebeat.co> * Delete unused file Signed-off-by: William Claude <w.claude@thebeat.co> * Wire save & exit with a nasty hack Signed-off-by: William Claude <w.claude@thebeat.co> * Remove unused file Signed-off-by: William Claude <w.claude@thebeat.co> * Remove dead code Signed-off-by: William Claude <w.claude@thebeat.co> * Reorder the code a bit Signed-off-by: William Claude <w.claude@thebeat.co> * Rename hoverableElement into actionableElement Signed-off-by: William Claude <w.claude@thebeat.co> * Prevent regenerating the label if the text didn't change Signed-off-by: William Claude <w.claude@thebeat.co>
2020-06-25 20:27:16 +00:00
type AnimationDirection int
const (
DirectionForward AnimationDirection = 0
DirectionBackward = 1
)
2020-02-09 02:02:37 +00:00
type Sprite struct {
widgetBase
2020-02-09 02:02:37 +00:00
segmentsX int
segmentsY int
frameOffset int
animation *d2asset.Animation
}
Enhanced escape menu with options - d2gui bug remaining (#459) * First improvements Signed-off-by: William Claude <w.claude@thebeat.co> * Make the menu more generic Signed-off-by: William Claude <w.claude@thebeat.co> * Handle mouse events Signed-off-by: William Claude <w.claude@thebeat.co> * Remove debug statement Signed-off-by: William Claude <w.claude@thebeat.co> * Handle left clicks better Signed-off-by: William Claude <w.claude@thebeat.co> * Remove unused file Signed-off-by: William Claude <w.claude@thebeat.co> * Handle titles in screens Signed-off-by: William Claude <w.claude@thebeat.co> * Improve the menu using layouts Signed-off-by: William Claude <w.claude@thebeat.co> * Add support for onOff labels Signed-off-by: William Claude <w.claude@thebeat.co> * Mutualise title creation Signed-off-by: William Claude <w.claude@thebeat.co> * Add gutter and mutualise things Signed-off-by: William Claude <w.claude@thebeat.co> * Improve menu, mutualise a lot of things and support animated sprites Signed-off-by: William Claude <w.claude@thebeat.co> * Use a cfg struct instead of independent handlers Signed-off-by: William Claude <w.claude@thebeat.co> * Fix hardcoded value Signed-off-by: William Claude <w.claude@thebeat.co> * Clean things a bit Signed-off-by: William Claude <w.claude@thebeat.co> * Remove unused property Signed-off-by: William Claude <w.claude@thebeat.co> * First support for hoverable elements Signed-off-by: William Claude <w.claude@thebeat.co> * Add support for label selection feedback Signed-off-by: William Claude <w.claude@thebeat.co> * Add support options Signed-off-by: William Claude <w.claude@thebeat.co> * Update print statement Signed-off-by: William Claude <w.claude@thebeat.co> * Update rendering and clean code Signed-off-by: William Claude <w.claude@thebeat.co> * Remove debug things Signed-off-by: William Claude <w.claude@thebeat.co> * Handle hovering Signed-off-by: William Claude <w.claude@thebeat.co> * Support enter key for labels Signed-off-by: William Claude <w.claude@thebeat.co> * Attach methods to layout Signed-off-by: William Claude <w.claude@thebeat.co> * Move things under EscapeMenu Signed-off-by: William Claude <w.claude@thebeat.co> * Some renaming Signed-off-by: William Claude <w.claude@thebeat.co> * Clean Signed-off-by: William Claude <w.claude@thebeat.co> * Set hovered element ID when using the mouse Signed-off-by: William Claude <w.claude@thebeat.co> * Clean Signed-off-by: William Claude <w.claude@thebeat.co> * Delete unused file Signed-off-by: William Claude <w.claude@thebeat.co> * Wire save & exit with a nasty hack Signed-off-by: William Claude <w.claude@thebeat.co> * Remove unused file Signed-off-by: William Claude <w.claude@thebeat.co> * Remove dead code Signed-off-by: William Claude <w.claude@thebeat.co> * Reorder the code a bit Signed-off-by: William Claude <w.claude@thebeat.co> * Rename hoverableElement into actionableElement Signed-off-by: William Claude <w.claude@thebeat.co> * Prevent regenerating the label if the text didn't change Signed-off-by: William Claude <w.claude@thebeat.co>
2020-06-25 20:27:16 +00:00
type AnimatedSprite struct {
*Sprite
}
func createSprite(imagePath, palettePath string) (*Sprite, error) {
animation, err := d2asset.LoadAnimation(imagePath, palettePath)
if err != nil {
return nil, err
}
sprite := &Sprite{}
sprite.animation = animation
sprite.SetVisible(true)
return sprite, nil
2020-02-09 02:02:37 +00:00
}
Enhanced escape menu with options - d2gui bug remaining (#459) * First improvements Signed-off-by: William Claude <w.claude@thebeat.co> * Make the menu more generic Signed-off-by: William Claude <w.claude@thebeat.co> * Handle mouse events Signed-off-by: William Claude <w.claude@thebeat.co> * Remove debug statement Signed-off-by: William Claude <w.claude@thebeat.co> * Handle left clicks better Signed-off-by: William Claude <w.claude@thebeat.co> * Remove unused file Signed-off-by: William Claude <w.claude@thebeat.co> * Handle titles in screens Signed-off-by: William Claude <w.claude@thebeat.co> * Improve the menu using layouts Signed-off-by: William Claude <w.claude@thebeat.co> * Add support for onOff labels Signed-off-by: William Claude <w.claude@thebeat.co> * Mutualise title creation Signed-off-by: William Claude <w.claude@thebeat.co> * Add gutter and mutualise things Signed-off-by: William Claude <w.claude@thebeat.co> * Improve menu, mutualise a lot of things and support animated sprites Signed-off-by: William Claude <w.claude@thebeat.co> * Use a cfg struct instead of independent handlers Signed-off-by: William Claude <w.claude@thebeat.co> * Fix hardcoded value Signed-off-by: William Claude <w.claude@thebeat.co> * Clean things a bit Signed-off-by: William Claude <w.claude@thebeat.co> * Remove unused property Signed-off-by: William Claude <w.claude@thebeat.co> * First support for hoverable elements Signed-off-by: William Claude <w.claude@thebeat.co> * Add support for label selection feedback Signed-off-by: William Claude <w.claude@thebeat.co> * Add support options Signed-off-by: William Claude <w.claude@thebeat.co> * Update print statement Signed-off-by: William Claude <w.claude@thebeat.co> * Update rendering and clean code Signed-off-by: William Claude <w.claude@thebeat.co> * Remove debug things Signed-off-by: William Claude <w.claude@thebeat.co> * Handle hovering Signed-off-by: William Claude <w.claude@thebeat.co> * Support enter key for labels Signed-off-by: William Claude <w.claude@thebeat.co> * Attach methods to layout Signed-off-by: William Claude <w.claude@thebeat.co> * Move things under EscapeMenu Signed-off-by: William Claude <w.claude@thebeat.co> * Some renaming Signed-off-by: William Claude <w.claude@thebeat.co> * Clean Signed-off-by: William Claude <w.claude@thebeat.co> * Set hovered element ID when using the mouse Signed-off-by: William Claude <w.claude@thebeat.co> * Clean Signed-off-by: William Claude <w.claude@thebeat.co> * Delete unused file Signed-off-by: William Claude <w.claude@thebeat.co> * Wire save & exit with a nasty hack Signed-off-by: William Claude <w.claude@thebeat.co> * Remove unused file Signed-off-by: William Claude <w.claude@thebeat.co> * Remove dead code Signed-off-by: William Claude <w.claude@thebeat.co> * Reorder the code a bit Signed-off-by: William Claude <w.claude@thebeat.co> * Rename hoverableElement into actionableElement Signed-off-by: William Claude <w.claude@thebeat.co> * Prevent regenerating the label if the text didn't change Signed-off-by: William Claude <w.claude@thebeat.co>
2020-06-25 20:27:16 +00:00
func createAnimatedSprite(imagePath, palettePath string, direction AnimationDirection) (*AnimatedSprite, error) {
animation, err := d2asset.LoadAnimation(imagePath, palettePath)
if err != nil {
return nil, err
}
sprite := &AnimatedSprite{
&Sprite{},
}
sprite.animation = animation
if direction == DirectionForward {
sprite.animation.PlayForward()
} else {
sprite.animation.PlayBackward()
}
sprite.animation.SetBlend(false)
sprite.SetVisible(true)
return sprite, nil
}
func (s *AnimatedSprite) render(target d2interface.Surface) error {
Enhanced escape menu with options - d2gui bug remaining (#459) * First improvements Signed-off-by: William Claude <w.claude@thebeat.co> * Make the menu more generic Signed-off-by: William Claude <w.claude@thebeat.co> * Handle mouse events Signed-off-by: William Claude <w.claude@thebeat.co> * Remove debug statement Signed-off-by: William Claude <w.claude@thebeat.co> * Handle left clicks better Signed-off-by: William Claude <w.claude@thebeat.co> * Remove unused file Signed-off-by: William Claude <w.claude@thebeat.co> * Handle titles in screens Signed-off-by: William Claude <w.claude@thebeat.co> * Improve the menu using layouts Signed-off-by: William Claude <w.claude@thebeat.co> * Add support for onOff labels Signed-off-by: William Claude <w.claude@thebeat.co> * Mutualise title creation Signed-off-by: William Claude <w.claude@thebeat.co> * Add gutter and mutualise things Signed-off-by: William Claude <w.claude@thebeat.co> * Improve menu, mutualise a lot of things and support animated sprites Signed-off-by: William Claude <w.claude@thebeat.co> * Use a cfg struct instead of independent handlers Signed-off-by: William Claude <w.claude@thebeat.co> * Fix hardcoded value Signed-off-by: William Claude <w.claude@thebeat.co> * Clean things a bit Signed-off-by: William Claude <w.claude@thebeat.co> * Remove unused property Signed-off-by: William Claude <w.claude@thebeat.co> * First support for hoverable elements Signed-off-by: William Claude <w.claude@thebeat.co> * Add support for label selection feedback Signed-off-by: William Claude <w.claude@thebeat.co> * Add support options Signed-off-by: William Claude <w.claude@thebeat.co> * Update print statement Signed-off-by: William Claude <w.claude@thebeat.co> * Update rendering and clean code Signed-off-by: William Claude <w.claude@thebeat.co> * Remove debug things Signed-off-by: William Claude <w.claude@thebeat.co> * Handle hovering Signed-off-by: William Claude <w.claude@thebeat.co> * Support enter key for labels Signed-off-by: William Claude <w.claude@thebeat.co> * Attach methods to layout Signed-off-by: William Claude <w.claude@thebeat.co> * Move things under EscapeMenu Signed-off-by: William Claude <w.claude@thebeat.co> * Some renaming Signed-off-by: William Claude <w.claude@thebeat.co> * Clean Signed-off-by: William Claude <w.claude@thebeat.co> * Set hovered element ID when using the mouse Signed-off-by: William Claude <w.claude@thebeat.co> * Clean Signed-off-by: William Claude <w.claude@thebeat.co> * Delete unused file Signed-off-by: William Claude <w.claude@thebeat.co> * Wire save & exit with a nasty hack Signed-off-by: William Claude <w.claude@thebeat.co> * Remove unused file Signed-off-by: William Claude <w.claude@thebeat.co> * Remove dead code Signed-off-by: William Claude <w.claude@thebeat.co> * Reorder the code a bit Signed-off-by: William Claude <w.claude@thebeat.co> * Rename hoverableElement into actionableElement Signed-off-by: William Claude <w.claude@thebeat.co> * Prevent regenerating the label if the text didn't change Signed-off-by: William Claude <w.claude@thebeat.co>
2020-06-25 20:27:16 +00:00
_, frameHeight := s.animation.GetCurrentFrameSize()
target.PushTranslation(s.x, s.y-frameHeight)
defer target.Pop()
return s.animation.Render(target)
}
2020-02-09 02:02:37 +00:00
func (s *Sprite) SetSegmented(segmentsX, segmentsY, frameOffset int) {
s.segmentsX = segmentsX
s.segmentsY = segmentsY
s.frameOffset = frameOffset
}
func (s *Sprite) render(target d2interface.Surface) error {
return renderSegmented(s.animation, s.segmentsX, s.segmentsY, s.frameOffset, target)
2020-02-09 02:02:37 +00:00
}
func (s *Sprite) advance(elapsed float64) error {
return s.animation.Advance(elapsed)
}
func (s *Sprite) getSize() (int, int) {
return s.animation.GetCurrentFrameSize()
}