aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2022-06-28 18:35:18 +0200
committerGitHub <noreply@github.com>2022-06-28 18:35:18 +0200
commit04ae87eaef35860c9c9ef425cc425d29e4caf978 (patch)
tree6d13b0ab9da03c5404bb1024060f2e04387177a5
parentc0d2359a913c9f62969b0b104ffe48c885c9ba6f (diff)
parentb313d09c2cebbe5da02dd9bccca40a5a67f50c3c (diff)
Merge pull request #1868 from Skytrias/skytrias-ease-flux-fixes
math ease fixed flux map key deletion and generic in `flux_to`
-rw-r--r--core/math/ease/ease.odin34
1 files changed, 23 insertions, 11 deletions
diff --git a/core/math/ease/ease.odin b/core/math/ease/ease.odin
index 5a767b5a9..0bd7c3641 100644
--- a/core/math/ease/ease.odin
+++ b/core/math/ease/ease.odin
@@ -325,9 +325,9 @@ ease :: proc "contextless" (type: Ease, p: $T) -> T
// in case type was invalid
return 0
}
-
Flux_Map :: struct($T: typeid) {
values: map[^T]Flux_Tween(T),
+ keys_to_be_deleted: [dynamic]^T,
}
Flux_Tween :: struct($T: typeid) {
@@ -353,15 +353,17 @@ Flux_Tween :: struct($T: typeid) {
}
// init flux map to a float type and a wanted cap
-flux_init :: proc($T: typeid, cap := 8) -> Flux_Map(T) where intrinsics.type_is_float(T) {
+flux_init :: proc($T: typeid, value_capacity := 8) -> Flux_Map(T) where intrinsics.type_is_float(T) {
return {
- make(map[^T]Flux_Tween(T), cap),
+ values = make(map[^T]Flux_Tween(T), value_capacity),
+ keys_to_be_deleted = make([dynamic]^T, 0, value_capacity)
}
}
// delete map content
flux_destroy :: proc(flux: Flux_Map($T)) where intrinsics.type_is_float(T) {
delete(flux.values)
+ delete(flux.keys_to_be_deleted)
}
// clear map content, stops all animations
@@ -374,8 +376,8 @@ flux_clear :: proc(flux: ^Flux_Map($T)) where intrinsics.type_is_float(T) {
// return value can be used to set callbacks
flux_to :: proc(
flux: ^Flux_Map($T),
- value: ^f32,
- goal: f32,
+ value: ^T,
+ goal: T,
type: Ease = .Quadratic_Out,
duration: time.Duration = time.Second,
delay: f64 = 0,
@@ -413,6 +415,8 @@ flux_tween_init :: proc(tween: ^Flux_Tween($T), duration: time.Duration) where i
// calls callbacks in all stages, when they're filled
// deletes tween from the map after completion
flux_update :: proc(flux: ^Flux_Map($T), dt: f64) where intrinsics.type_is_float(T) {
+ clear(&flux.keys_to_be_deleted)
+
for key, tween in &flux.values {
delay_remainder := f64(0)
@@ -451,7 +455,8 @@ flux_update :: proc(flux: ^Flux_Map($T), dt: f64) where intrinsics.type_is_float
}
if tween.progress >= 1 {
- delete_key(&flux.values, key)
+ // append keys to array that will be deleted after the loop
+ append(&flux.keys_to_be_deleted, key)
if tween.on_complete != nil {
tween.on_complete(flux, tween.data)
@@ -459,17 +464,24 @@ flux_update :: proc(flux: ^Flux_Map($T), dt: f64) where intrinsics.type_is_float
}
}
}
+
+ // loop through keys that should be deleted from the map
+ if len(flux.keys_to_be_deleted) != 0 {
+ for key in flux.keys_to_be_deleted {
+ delete_key(&flux.values, key)
+ }
+ }
}
// stop a specific key inside the map
// returns true when it successfully removed the key
flux_stop :: proc(flux: ^Flux_Map($T), key: ^T) -> bool where intrinsics.type_is_float(T) {
- if key in flux.values {
- delete_key(&flux.values, key)
- return true
- }
+ if key in flux.values {
+ delete_key(&flux.values, key)
+ return true
+ }
- return false
+ return false
}
// returns the amount of time left for the tween animation, if the key exists in the map