Skip to content

Commit

Permalink
add and fix support for loops
Browse files Browse the repository at this point in the history
  • Loading branch information
ceceppa committed Aug 28, 2024
1 parent d562c16 commit bce606f
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 57 deletions.
1 change: 0 additions & 1 deletion addons/anima/animations/attention_seeker/bounce.gd
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ var KEYFRAMES := {
"scale:y": 1.02,
},
}

9 changes: 8 additions & 1 deletion addons/anima/core/anima_node.gd
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func clear() -> void:
if not is_instance_valid(_anima_tween) or _anima_tween.is_queued_for_deletion():
return

_timer.stop()
stop()

_anima_tween.clear_animations()
Expand Down Expand Up @@ -759,7 +760,10 @@ func _maybe_play() -> void:

if _loop_times > 0 or _should_loop:
if _loop_delay > 0:
await get_tree().create_timer(_loop_delay).timeout
_timer.wait_time = _loop_delay
_timer.start()

await _timer.timeout

_do_play()

Expand All @@ -786,3 +790,6 @@ func get_animation_data() -> Array:

func _play_backwards(time: float) -> void:
_anima_tween.seek(time)

func debug():
print(get_animation_data())
25 changes: 15 additions & 10 deletions addons/anima/core/anima_tween.gd
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,21 @@ func _init(new_name: String = "AnimaTween"):
name = new_name

func _enter_tree():
var tree: SceneTree = get_tree()
_create_tween()

if tree:
_tween = tree.create_tween()
func _create_tween():
var tree = get_tree()

_tween.set_parallel(true)
_tween.pause()
if not tree:
return

_tween = tree.create_tween()

_tween.set_parallel(true)
_tween.pause()

_tween.loop_finished.connect(_on_tween_completed)
_tween.finished.connect(_on_tween_completed)
_tween.loop_finished.connect(_on_tween_completed)
_tween.finished.connect(_on_tween_completed)

func _exit_tree():
for child in get_children():
Expand Down Expand Up @@ -291,16 +296,16 @@ func clear_animations() -> void:
_tween.stop()
_tween.kill()

if is_inside_tree():
_enter_tree()

for child in get_children():
child.queue_free()

_callbacks = {}
_animation_data.clear()
_initial_values.clear()

if is_inside_tree():
_enter_tree()

func set_visibility_strategy(strategy: int) -> void:
for animation_data in _animation_data:
_apply_visibility_strategy(animation_data, strategy)
Expand Down
105 changes: 95 additions & 10 deletions addons/anima/core/declaration/anima_declaration_base.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class_name AnimaDeclarationBase

var _data := {}
var _is_single_shot := true
var _anima_node: AnimaNode

enum PlayAction {
PLAY,
Expand All @@ -11,8 +12,24 @@ enum PlayAction {
PLAY_BACKWARDS,
PLAY_BACKWARDS_WITH_DELAY,
PLAY_BACKWARDS_WITH_SPEED,
LOOP,
LOOP_IN_CIRCLE,
LOOP_IN_CIRCLE_WITH_DELAY,
LOOP_IN_CIRCLE_WITH_SPEED,
LOOP_IN_CIRCLE_WITH_DELAY_AND_SPEED,
LOOP_BACKWARDS,
LOOP_BACKWARDS_WITH_SPEED,
LOOP_BACKWARDS_WITH_DELAY,
LOOP_BACKWARDS_WITH_DELAY_AND_SPEED,
LOOP_WITH_DELAY,
LOOP_WITH_SPEED,
LOOP_TIMES_WITH_DELAY,
LOOP_TIMES_WITH_DELAY_AND_SPEED
}

func clear():
_anima_node.clear()

func get_data() -> Dictionary:
return _data

Expand Down Expand Up @@ -108,24 +125,53 @@ func __get_source():
return null

func _do_play(action: PlayAction, param = null) -> AnimaNode:
var anima := Anima.begin(__get_source()).then(_data)
anima.set_single_shot(_is_single_shot)
if _anima_node == null:
_anima_node = Anima.begin(__get_source()).then(_data)

var single_shot = _is_single_shot if action < PlayAction.LOOP else false
_anima_node.set_single_shot(single_shot)

match action:
PlayAction.PLAY:
anima.play()
_anima_node.play()
PlayAction.PLAY_WITH_DELAY:
anima.play_with_delay(param)
_anima_node.play_with_delay(param)
PlayAction.PLAY_WITH_SPEED:
anima.play_with_speed(param)
_anima_node.play_with_speed(param)
PlayAction.PLAY_BACKWARDS:
anima.play_backwards()
_anima_node.play_backwards()
PlayAction.PLAY_BACKWARDS_WITH_DELAY:
anima.play_backwards_with_delay(param)
_anima_node.play_backwards_with_delay(param)
PlayAction.PLAY_BACKWARDS_WITH_SPEED:
anima.play_backwards_with_speed(param)

return anima
_anima_node.play_backwards_with_speed(param)
PlayAction.LOOP:
_anima_node.loop(param)
PlayAction.LOOP_IN_CIRCLE:
_anima_node.loop_in_circle(param)
PlayAction.LOOP_IN_CIRCLE_WITH_DELAY:
_anima_node.loop_in_circle_with_delay(param)
PlayAction.LOOP_IN_CIRCLE_WITH_SPEED:
_anima_node.loop_in_circle_with_speed(param.speed, param.times)
PlayAction.LOOP_IN_CIRCLE_WITH_DELAY_AND_SPEED:
_anima_node.loop_in_circle_with_delay_and_speed(param.delay, param.speed, param.times)
PlayAction.LOOP_BACKWARDS:
_anima_node.loop_backwards(param)
PlayAction.LOOP_BACKWARDS_WITH_SPEED:
_anima_node.loop_backwards_with_speed(param.speed, param.times)
PlayAction.LOOP_BACKWARDS_WITH_DELAY:
_anima_node.loop_with_delay(param.delay, param.times)
PlayAction.LOOP_BACKWARDS_WITH_DELAY_AND_SPEED:
_anima_node.loop_times_with_delay_and_speed(param.times, param.delay, param.speed)
PlayAction.LOOP_WITH_DELAY:
_anima_node.loop_with_delay(param.delay, param.times)
PlayAction.LOOP_WITH_SPEED:
_anima_node.loop_with_speed(param.speed, param.times)
PlayAction.LOOP_TIMES_WITH_DELAY:
_anima_node.loop_times_with_delay(param.times, param.delay)
PlayAction.LOOP_TIMES_WITH_DELAY_AND_SPEED:
_anima_node.loop_times_with_delay_and_speed(param.times, param.delay, param.speed)

return _anima_node

func play() -> AnimaNode:
return _do_play(PlayAction.PLAY)
Expand All @@ -144,3 +190,42 @@ func play_backwards_with_delay(delay: float) -> AnimaNode:

func play_backwards_with_speed(speed: float) -> AnimaNode:
return _do_play(PlayAction.PLAY_BACKWARDS_WITH_SPEED, speed)

func loop(times: int = -1) -> AnimaNode:
return _do_play(PlayAction.LOOP, times)

func loop_in_circle(times: int = -1) -> AnimaNode:
return _do_play(PlayAction.LOOP_IN_CIRCLE, times)

func loop_in_circle_with_delay(delay: float, times: int = -1) -> AnimaNode:
return _do_play(PlayAction.LOOP_IN_CIRCLE_WITH_DELAY, times)

func loop_in_circle_with_speed(speed: float, times: int = -1) -> AnimaNode:
return _do_play(PlayAction.LOOP_IN_CIRCLE_WITH_SPEED, { times = times, speed = speed })

func loop_in_circle_with_delay_and_speed(delay: float, speed: float, times: int = -1) -> AnimaNode:
return _do_play(PlayAction.LOOP_IN_CIRCLE_WITH_DELAY_AND_SPEED, { times = times, delay = delay, speed = speed })

func loop_backwards(times: int = -1) -> AnimaNode:
return _do_play(PlayAction.LOOP_BACKWARDS, times)

func loop_backwards_with_speed(speed: float, times: int = -1) -> AnimaNode:
return _do_play(PlayAction.LOOP_BACKWARDS_WITH_SPEED, { times = times, speed = speed })

func loop_backwards_with_delay(delay: float, times: int = -1) -> AnimaNode:
return _do_play(PlayAction.LOOP_BACKWARDS_WITH_DELAY, { times = times, delay = delay })

func loop_backwards_with_delay_and_speed(delay: float, speed: float, times: int = -1) -> AnimaNode:
return _do_play(PlayAction.LOOP_BACKWARDS_WITH_DELAY_AND_SPEED, { times = times, delay = delay, speed = speed })

func loop_with_delay(delay: float, times: int = -1) -> AnimaNode:
return _do_play(PlayAction.LOOP_WITH_DELAY, { times = times, delay = delay })

func loop_with_speed(speed: float, times: int = -1) -> AnimaNode:
return _do_play(PlayAction.LOOP_WITH_SPEED, { times = times, speed = speed })

func loop_times_with_delay(times: float, delay: float) -> AnimaNode:
return _do_play(PlayAction.LOOP_TIMES_WITH_DELAY, { times = times, delay = delay })

func loop_times_with_delay_and_speed(times: int, delay: float, speed: float) -> AnimaNode:
return _do_play(PlayAction.LOOP_TIMES_WITH_DELAY_AND_SPEED, { times = times, delay = delay, speed = speed })
Loading

0 comments on commit bce606f

Please sign in to comment.