summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoutade <thegoofist@protonmail.com>2019-09-30 14:40:52 -0500
committerBoutade <thegoofist@protonmail.com>2019-09-30 14:40:52 -0500
commit9efac6ed1d3faa28c4c9235ca044f10731931749 (patch)
treea5c5c2183654b1060db93474e4862fcb5d186833
parentc35bbc9f6c2fa40d677c695ec978bbbad6929565 (diff)
added a tween struct
-rw-r--r--animise.lisp54
-rw-r--r--package.lisp42
2 files changed, 77 insertions, 19 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)))
+
diff --git a/package.lisp b/package.lisp
index 9516b26..fe96911 100644
--- a/package.lisp
+++ b/package.lisp
@@ -3,28 +3,36 @@
(defpackage #:animise
(:use #:cl)
(:export
- #:linear
- #:mirror-linear
- #:quad-in
- #:quad-out
- #:mirror-quad-in
- #:mirror-quad-out
- #:quad-in-out
- #:mirror-quad-in-out
+
+ ;; TWEENS
+ #:tween
+ #:make-tween
+ #:end-time
+ #:tween-finished-p
+
+ ;; EASING FUNCTIONS
+ #:bouce-out
#:cubic-in
+ #:cubic-in-out
#:cubic-out
+ #:elastic-out
+ #:linear
+ #:mirror-bounce-out
#:mirror-cubic-in
- #:mirror-cubic-out
- #:cubic-in-out
#:mirror-cubic-in-out
- #:sinusoidal-in
+ #:mirror-cubic-out
+ #:mirror-elastic-out
+ #:mirror-linear
+ #:mirror-quad-in
+ #:mirror-quad-in-out
+ #:mirror-quad-out
#:mirror-sinusoidal-in
- #:sinusoidal-out
+ #:mirror-sinusoidal-in-out
#:mirror-sinusoidal-out
+ #:quad-in
+ #:quad-in-out
+ #:quad-out
+ #:sinusoidal-in
#:sinusoidal-in-out
- #:mirror-sinusoidal-in-out
- #:elastic-out
- #:mirror-elastic-out
- #:bouce-out
- #:mirror-bounce-out
+ #:sinusoidal-out
))