2024-07-05 16:41:07 +03:00
|
|
|
package rui
|
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
func (animation *animationData) Start(view View, listener func(view View, animation Animation, event PropertyName)) bool {
|
2024-07-05 16:41:07 +03:00
|
|
|
if view == nil {
|
|
|
|
ErrorLog("nil View in animation.Start() function")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !animation.hasAnimatedProperty() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
animation.view = view
|
|
|
|
animation.listener = listener
|
|
|
|
|
|
|
|
animation.oldAnimation = nil
|
|
|
|
if value := view.Get(AnimationTag); value != nil {
|
|
|
|
if oldAnimation, ok := value.([]Animation); ok && len(oldAnimation) > 0 {
|
|
|
|
animation.oldAnimation = oldAnimation
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
animation.oldListeners = map[PropertyName][]func(View, PropertyName){}
|
2024-07-05 16:41:07 +03:00
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
setListeners := func(event PropertyName, listener func(View, PropertyName)) {
|
|
|
|
var listeners []func(View, PropertyName) = nil
|
2024-07-05 16:41:07 +03:00
|
|
|
if value := view.Get(event); value != nil {
|
2024-11-13 12:56:39 +03:00
|
|
|
if oldListeners, ok := value.([]func(View, PropertyName)); ok && len(oldListeners) > 0 {
|
2024-07-05 16:41:07 +03:00
|
|
|
listeners = oldListeners
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if listeners == nil {
|
|
|
|
view.Set(event, listener)
|
|
|
|
} else {
|
|
|
|
animation.oldListeners[event] = listeners
|
|
|
|
view.Set(event, append(listeners, listener))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setListeners(AnimationStartEvent, animation.onAnimationStart)
|
|
|
|
setListeners(AnimationEndEvent, animation.onAnimationEnd)
|
|
|
|
setListeners(AnimationCancelEvent, animation.onAnimationCancel)
|
|
|
|
setListeners(AnimationIterationEvent, animation.onAnimationIteration)
|
|
|
|
|
|
|
|
view.Set(AnimationTag, animation)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (animation *animationData) finish() {
|
|
|
|
if animation.view != nil {
|
2024-11-13 12:56:39 +03:00
|
|
|
for _, event := range []PropertyName{AnimationStartEvent, AnimationEndEvent, AnimationCancelEvent, AnimationIterationEvent} {
|
2024-07-05 16:41:07 +03:00
|
|
|
if listeners, ok := animation.oldListeners[event]; ok {
|
|
|
|
animation.view.Set(event, listeners)
|
|
|
|
} else {
|
|
|
|
animation.view.Remove(event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-17 16:30:43 +03:00
|
|
|
if animation.oldAnimation != nil {
|
|
|
|
animation.view.Set(AnimationTag, animation.oldAnimation)
|
|
|
|
animation.oldAnimation = nil
|
|
|
|
} else {
|
|
|
|
animation.view.Set(AnimationTag, "")
|
|
|
|
}
|
2024-07-05 16:41:07 +03:00
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
animation.oldListeners = map[PropertyName][]func(View, PropertyName){}
|
2024-07-17 16:30:43 +03:00
|
|
|
|
2024-07-05 16:41:07 +03:00
|
|
|
animation.view = nil
|
|
|
|
animation.listener = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (animation *animationData) Stop() {
|
2024-07-17 16:30:43 +03:00
|
|
|
animation.onAnimationCancel(animation.view, "")
|
2024-07-05 16:41:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (animation *animationData) Pause() {
|
|
|
|
if animation.view != nil {
|
|
|
|
animation.view.Set(AnimationPaused, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (animation *animationData) Resume() {
|
|
|
|
if animation.view != nil {
|
|
|
|
animation.view.Remove(AnimationPaused)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
func (animation *animationData) onAnimationStart(view View, _ PropertyName) {
|
2024-07-05 16:41:07 +03:00
|
|
|
if animation.view != nil && animation.listener != nil {
|
|
|
|
animation.listener(animation.view, animation, AnimationStartEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
func (animation *animationData) onAnimationEnd(view View, _ PropertyName) {
|
2024-07-05 16:41:07 +03:00
|
|
|
if animation.view != nil {
|
2024-07-17 16:30:43 +03:00
|
|
|
animationView := animation.view
|
|
|
|
listener := animation.listener
|
2024-08-01 22:32:31 +03:00
|
|
|
|
|
|
|
if value, ok := animation.properties[PropertyTag]; ok {
|
|
|
|
if props, ok := value.([]AnimatedProperty); ok {
|
|
|
|
for _, prop := range props {
|
|
|
|
animationView.setRaw(prop.Tag, prop.To)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-05 16:41:07 +03:00
|
|
|
animation.finish()
|
2024-07-17 16:30:43 +03:00
|
|
|
if listener != nil {
|
|
|
|
listener(animationView, animation, AnimationEndEvent)
|
|
|
|
}
|
2024-07-05 16:41:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
func (animation *animationData) onAnimationIteration(view View, _ PropertyName) {
|
2024-07-05 16:41:07 +03:00
|
|
|
if animation.view != nil && animation.listener != nil {
|
|
|
|
animation.listener(animation.view, animation, AnimationIterationEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-13 12:56:39 +03:00
|
|
|
func (animation *animationData) onAnimationCancel(view View, _ PropertyName) {
|
2024-07-17 16:30:43 +03:00
|
|
|
if animation.view != nil {
|
|
|
|
animationView := animation.view
|
|
|
|
listener := animation.listener
|
2024-08-01 22:32:31 +03:00
|
|
|
|
|
|
|
if value, ok := animation.properties[PropertyTag]; ok {
|
|
|
|
if props, ok := value.([]AnimatedProperty); ok {
|
|
|
|
for _, prop := range props {
|
|
|
|
animationView.Set(prop.Tag, prop.To)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-17 16:30:43 +03:00
|
|
|
animation.finish()
|
|
|
|
if listener != nil {
|
|
|
|
listener(animationView, animation, AnimationCancelEvent)
|
|
|
|
}
|
2024-07-05 16:41:07 +03:00
|
|
|
}
|
|
|
|
}
|