summaryrefslogtreecommitdiffhomepage
path: root/dragon/easing.rb
diff options
context:
space:
mode:
authorAmir Rajan <[email protected]>2020-09-22 06:27:46 -0500
committerAmir Rajan <[email protected]>2020-09-22 06:27:46 -0500
commit20d5b4057b44ffcf92478b2a8e9476ace2fdc0f5 (patch)
treeb4742e4f9acfd5400a04f314164812606a71df9f /dragon/easing.rb
parent5b2311900072cfff9582bb0296140cfb354cb911 (diff)
downloaddragonruby-game-toolkit-contrib-20d5b4057b44ffcf92478b2a8e9476ace2fdc0f5.tar.gz
dragonruby-game-toolkit-contrib-20d5b4057b44ffcf92478b2a8e9476ace2fdc0f5.zip
synced with 1.22
Diffstat (limited to 'dragon/easing.rb')
-rw-r--r--dragon/easing.rb82
1 files changed, 82 insertions, 0 deletions
diff --git a/dragon/easing.rb b/dragon/easing.rb
new file mode 100644
index 0000000..310e7ed
--- /dev/null
+++ b/dragon/easing.rb
@@ -0,0 +1,82 @@
+# coding: utf-8
+# Copyright 2019 DragonRuby LLC
+# MIT License
+# easing.rb has been released under MIT (*only this file*).
+
+module GTK
+ module Easing
+ def self.ease_extended start_tick, current_tick, end_tick, default_before, default_after, *definitions
+ definitions.flatten!
+ definitions = [:identity] if definitions.length == 0
+ duration = end_tick - start_tick
+ elapsed = current_tick - start_tick
+ y = elapsed.percentage_of(duration).cap_min_max(0, 1)
+
+ definitions.map do |definition|
+ y = Easing.exec_definition(definition, start_tick, duration, y)
+ end
+
+ y
+ end
+
+ def self.ease_spline_extended start_tick, current_tick, end_tick, spline
+ duration = end_tick - start_tick
+ t = (current_tick - start_tick).fdiv duration
+ time_allocation_per_curve = 1.fdiv(spline.length)
+ curve_index, curve_t = t.fdiv(time_allocation_per_curve).let do |spline_t|
+ [spline_t.to_i, spline_t - spline_t.to_i]
+ end
+ Geometry.cubic_bezier curve_t, *spline[curve_index]
+ end
+
+ def self.initial_value *definitions
+ definitions.flatten!
+ return Easing.exec_definition (definitions.value(-1) || :identity), 0, 10, 0
+ end
+
+ def self.final_value *definitions
+ definitions.flatten!
+ return Easing.exec_definition (definitions.value(-1) || :identity), 0, 10, 1.0
+ end
+
+ def self.exec_definition definition, start_tick, duration, x
+ if definition.is_a? Symbol
+ return Easing.send(definition, x).cap_min_max(0, 1)
+ elsif definition.is_a? Proc
+ return definition.call(x, start_tick, duration).cap_min_max(0, 1)
+ end
+
+ raise <<-S
+* ERROR:
+I don't know how to execute easing function with definition #{definition}.
+
+S
+ end
+
+ def self.identity x
+ x
+ end
+
+ def self.flip x
+ 1 - x
+ end
+
+ def self.quad x
+ x * x
+ end
+
+ def self.cube x
+ x * x * x
+ end
+
+ def self.quart x
+ x * x * x * x * x
+ end
+
+ def self.quint x
+ x * x * x * x * x * x
+ end
+ end
+end
+
+Easing = GTK::Easing