;;;; animate.stk 		-- A simple image animation (Demo)
;;;;
;;;; Copyright © 1998-1999 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
;;;; 
;;;; Permission to use, copy, modify, distribute,and license this
;;;; software and its documentation for any purpose is hereby granted,
;;;; provided that existing copyright notices are retained in all
;;;; copies and that this notice is included verbatim in any
;;;; distributions.  No written agreement, license, or royalty fee is
;;;; required for any of the authorized uses.
;;;;
;;;; This software is provided ``AS IS'' without express or implied
;;;; warranty.
;;;;
;;;;
;;;;          Author: Erick Gallesio [eg@unice.fr]
;;;;   Creation date:  9-Mar-1998 18:51
;;;;Last file update:  3-Sep-1999 18:45 (eg)

(define *img* #f)

(define (make-animation pattern parent delay)
  (let* ((files  (sort (glob pattern) string<?)) 
	 (size   (length files)) 
	 (images (make-vector size)) 
	 (name   (gensym (& (if (eq? parent *root*) "" parent) ".l"))) 
	 (widget (label name :bd 0 :relief "flat" :background "white"
			:env (the-environment))))
    ;; Store in the image vector all the components of the animation
    (dotimes (i size)
      (let ((key (list-ref files i)))
	(vector-set! images 
		     i 
		     (make-image key :file key))))
    
    ;; Display the first image of the animation
    (tk-set! widget :image (vector-ref images 0))

    ;; Animate the image 
    (letrec ((anim (let ((i 0))
		     (lambda () 
		       (if (>= delay 0) 
			   (when (winfo (quote exists) widget)
			     (if (>= i size) (set! i 0))
			     (tk-set! widget :image (vector-ref images i))
			     (set! i (+ i 1))
			     (update)
			     (after (abs delay) anim))
			   (after 100 anim))))))
      (after 'idle anim))
    widget))


(define (change-animation-delay w delay)
  (let ((env (tk-get w :env)))
    (eval `(set! delay ,delay) env)))

