diff options
Diffstat (limited to 'animise.lisp')
-rw-r--r-- | animise.lisp | 54 |
1 files changed, 52 insertions, 2 deletions
diff --git a/animise.lisp b/animise.lisp index 8c6e54a..81cea4e 100644 --- a/animise.lisp +++ b/animise.lisp @@ -2,6 +2,8 @@ (in-package #:animise) +;;; Utilities for defining easing functions + (defun time-frac (start duration current) (let* ((end (+ start duration)) (progress (max 0 (- end current)))) @@ -22,6 +24,7 @@ (- current (+ start (* 0.5 duration)))) delta))))) +;;; EASING FUNCTION DEFINITIONS ;;; ;;; The DEF-EASE macro defines a function. the BODY of the function has the ;;; following variables available to it: @@ -94,7 +97,6 @@ (def-mirror-for elastic-out) - (def-ease bounce-out (let ((coeff 7.5627) (step (/ 1 2.75))) @@ -118,7 +120,6 @@ (def-mirror-for bounce-out) - ;;; Some functions to check your intuitions about the output of easing functions (defun make-frames (ease-fn &optional (step 0.1)) @@ -132,3 +133,52 @@ (princ #\Space)) (princ mark) (terpri))) + +;;; TWEENS + +(defstruct tween + "A TWEEN is function to produce a sequence of values over time for the purpose + of animating an object. + + START-TIME and DURATION arguments are a representation of time and must use + the same units. START-TIME must be supplied on TWEEN instantiation, and is + used to record when the tween begins running. + + DELTA-VAL is a number, the amount by which the animated object's target + property should have changed by the time the animation is over. + + Different EASE functions may be supplied to modulate the way that sequence is + produced. EASE is the heart of the TWEEN's behavior. + + Every TWEEN instance must have an EFFECTOR, which should be a closure that + accepts a single argument. The purpose of the EFFECTOR is to apply the values + generated by the TWEEN to some object." + (start-time (error "Must supply a start time.")) + (duration 1.0) + (ease #'linear) + (start-val (error "Must supply a start value.")) + (delta-val (error "Must supply a delta value.")) + (effector (error "Must supply an effector function"))) + +(defun end-time (tween) + (with-slots (start-time duration) tween + (+ start-time duration))) + +(defun tween-finished-p (tween current-time) + (>= current-time (end-time tween))) + +(defun run-tween (tween time) + (with-slots (start-time duration ease start-val delta-val effector) tween + (when (>= time start-time) + (funcall effector + (+ start-val + (funcall ease start-time duration time delta-val)))))) + +(defun reverse-tween (tween &optional start) + (with-slots (start-time duration ease start-val delta-val effector) tween + (make-tween :start-time (if start start (+ duration start-time)) + :duration duration + :start-val (+ start-val delta-val) + :delta-val (* -1 delta-val) + :effector effector))) + |