Skip to content

Commit

Permalink
Add support for tween delaying
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinmera committed Sep 10, 2024
1 parent 8ac5718 commit 79d5c68
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
15 changes: 8 additions & 7 deletions animation/animation.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
(defgeneric apply-animation (animation animated))

;; TODO: fold tween into animation
(defstruct (tween (:constructor make-tween (setter stops values easings &optional (loop NIL))))
(defstruct (tween (:constructor make-tween (setter stops values easings &optional (loop NIL) (delay 0f0))))
(setter NIL :type function)
(stops NIL :type (simple-array single-float (*)))
(values NIL :type simple-vector)
(easings NIL :type simple-vector)
(idx 0 :type (unsigned-byte 32))
(clock 0f0 :type single-float)
(loop NIL :type T))
(loop NIL :type T)
(delay 0f0 :type single-float))

(defun find-tween-idx (tween clock)
;; TODO: could speed this up with bin search, but not sure if worth it.
Expand All @@ -42,14 +43,14 @@

(defmethod shared-initialize :after ((animated animated) slots &key (tweens NIL tweens-p))
(when tweens-p
(let ((old (tweens animated)))
(let ((old-tweens (tweens animated)))
(loop for i from 0 below (length tweens)
for tween = (aref tweens i)
for setter = (tween-setter tween)
for clock = (loop for tween across old
do (when (eql setter (tween-setter tween))
(return (tween-clock tween)))
finally (return 0f0))
for clock = (loop for old across old-tweens
do (when (eql setter (tween-setter old))
(return (tween-clock old)))
finally (return (- (tween-delay tween))))
do (setf (tween-idx tween) (find-tween-idx tween clock))
(setf (tween-clock tween) clock))
(setf (tweens animated) tweens))))
Expand Down
9 changes: 5 additions & 4 deletions animation/change.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
(comparators ())
(tween-infos ()))
(dolist (tracker trackers (list extractors comparators tween-infos))
(destructuring-bind (extractor &key (duration 1.0) (easing 'linear) (comparison 'equalp)) tracker
(destructuring-bind (extractor &key (delay 0.0) (duration 1.0) (easing 'linear) (comparison 'equalp)) tracker
(push extractor extractors)
(push comparison comparators)
(push (list :duration duration :easing easing) tween-infos)))))
(push (list :delay delay :duration duration :easing easing) tween-infos)))))

(defun compile-tween (extractor old-val new-val info)
(destructuring-bind (&key duration easing) info
(destructuring-bind (&key delay duration easing) info
`(make-tween #'(setf ,extractor)
,(%expand-array (list 0.0 duration) :element-type 'single-float)
,(%expand-array (list old-val new-val))
,(%expand-array (list `(load-time-value (easing ',easing)))))))
,(%expand-array (list `(load-time-value (easing ',easing))))
NIL (float ,delay 0f0))))

(defun compile-change-tracker (animated tracked next-method)
(destructuring-bind (extractors comparators tween-infos) (compile-trackers tracked)
Expand Down

0 comments on commit 79d5c68

Please sign in to comment.