Created
December 20, 2014 08:43
-
-
Save triplefox/e9331fc62e09df948f04 to your computer and use it in GitHub Desktop.
Slactuate, a one file tween library for Haxe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Slactuate | |
{ | |
/* how to use: build your own abstraction on m() and p() to ease the setting of properties. call u() each frame you update, then call c() to cleanup. */ | |
public var td /*tween datas*/ = new Array<SlacTween>(); | |
public function new(){} | |
public function u/*update to the given time*/(t : Float) { for (n in td) n.u(t); } | |
public function c/*clear tweens updated to past their end time, and return the removed ones*/() { | |
var i = td.length - 1; var r = new Array<SlacTween>(); if (td.length < 1) return r; | |
while (i >= 0) { var n = td[i]; if (n.ci > 1.) {r.push(td.splice(i, 1)[0]); } i -= 1; } | |
return r; } | |
public function m/*make generic tween*/(tt : Float->Void, eg : Easing, st : Float, gt : Float, lo : Float, ho : Float, id : Int) { | |
var r = new SlacTween(tt, eg, st, gt, lo, ho, id); td.push(r); return r; } | |
public function p/*property-setting tween*/(tt : Dynamic, py : String, eg : Easing, st : Float, gt : Float, lo : Float, ho : Float, id : Int) { | |
return m(function(v) { Reflect.setProperty(tt, py, v); }, eg, st, gt, lo, ho, id); } | |
private static inline function clamp(pct) { return Math.min(1., Math.max(0.,pct)); } | |
private static inline function lerp/*unclamped*/(pct : Float, lo : Float, ho : Float) : Float { return (ho - lo) * pct + lo; } | |
public static function linear(pct : Float, lo : Float, ho : Float) : Float { return lerp(clamp(pct), lo, ho); } | |
public static function smoothstep(pct : Float, lo : Float, ho : Float) : Float { var x = clamp(pct); return lerp(x * x * x * (x * (x * 6 - 15) + 10), lo, ho); } | |
public static function lutnearest(a : Array<Float>) : Easing { return function(pct : Float, lo : Float, ho : Float):Float { | |
return lerp(a[Math.round(clamp(pct) * (a.length - 1))], lo, ho); }} | |
public static function lutlinear(a : Array<Float>) : Easing { return function(pct : Float, lo : Float, ho : Float):Float { | |
var k = clamp(pct) * (a.length - 1); var i = Std.int(k); (i>=a.length-1) ? return ho : return lerp(lerp(k - i, a[i], a[i + 1]), lo, ho); }} | |
} | |
typedef Easing = Float->Float->Float->Float; | |
class SlacTween | |
{ | |
public var tt /*target*/ : Float->Void; | |
public var eg /*easing*/ : Easing; | |
public var st /*start time*/ : Float; | |
public var gt /*goal time*/ : Float; | |
public var lo /*low output*/ : Float; | |
public var ho /*high output*/ : Float; | |
public var ci /*current input*/ : Float = 0.; | |
public var co /*current output*/ : Float = 0.; | |
public var id /*user id number*/ : Int; | |
public function new(tt, eg, st, gt, lo, ho, id) { | |
this.tt = tt; this.eg = eg; this.st = st; this.gt = gt; | |
this.lo = lo; this.ho = ho; this.id = id; } | |
public function u/*update*/(t : Float) { ci = (t - st) / (gt - st); co = eg(ci, lo, ho); tt(co); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment