]> code.delx.au - gnu-emacs/blob - lisp/play/animate.el
(animate-birthday-present): Re-insert.
[gnu-emacs] / lisp / play / animate.el
1 ;;; animate.el --- make text dance
2
3 ;; Copyright (C) 2001 Free Software Foundation, Inc.
4
5 ;; Maintainer: Richard Stallman <rms@gnu.org>
6 ;; Keywords: games
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; (animate-string STRING VPOS &optional HPOS)
28 ;; makes the string STRING appear starting at VPOS, HPOS
29 ;; by having each letter swoop into place from random starting position.
30
31 ;;; Code:
32
33 ;;; STRING is the string to be displayed,
34 ;;; and DEST-X, DEST-Y say where on the screen
35 ;;; it should end up.
36
37 ;;; This function returns a list describing
38 ;;; all the characters and the paths they should take.
39 ;;; Each element has the form
40 ;;; (CHAR START-Y START-X DEST-Y DEST-X).
41
42 ;;; The start position of each character is chosen randomly.
43 ;;; The destination is chosen to put it in the right place
44 ;;; in the string when the whole string finally reaches its
45 ;;; specified position.
46
47 (defun animate-initialize (string vpos hpos)
48 (let ((characters nil))
49 (dotimes (i (length string))
50 (setq characters
51 (cons (list (aref string i)
52 ;; Random starting positions.
53 (random (window-height))
54 (random (1- (window-width)))
55 ;; All the chars should end up
56 ;; on the specified line.
57 vpos
58 ;; The Ith character in the string
59 ;; needs to end up I positions later.
60 (+ hpos i))
61 characters)))
62 characters))
63
64 ;;; Display the characters in CHARACTERS,
65 ;;; each one FRACTION of the way from its start to its destination.
66 ;;; If FRACTION is 0, the characters appear in their starting positions.
67 ;;; If FRACTION is 1, the characters appear in their destinations.
68
69 (defun animate-step (characters fraction)
70 (let ((remains (- 1 fraction)))
71 (dolist (item characters)
72 (let ((vpos (+ (* remains (nth 1 item))
73 (* fraction (nth 3 item))))
74 (hpos (+ (* remains (nth 2 item))
75 (* fraction (nth 4 item)))))
76 (animate-place-char (car item) vpos hpos)))))
77
78 ;;; Place the character CHAR at position VPOS, HPOS in the current buffer.
79 (defun animate-place-char (char vpos hpos)
80 (goto-char (window-start))
81 (let ((next-line-add-newlines t))
82 (dotimes (i vpos)
83 (next-line 1)))
84 (beginning-of-line)
85 (move-to-column (floor hpos) t)
86 (unless (eolp) (delete-char 1))
87 (insert-char char 1))
88
89 (defvar animate-n-steps 10
90 "Number of steps to use `animate-string'.")
91
92 ;;;###autoload
93 (defun animate-string (string vpos &optional hpos)
94 "Display STRING starting at position VPOS, HPOS, using animation.
95 The characters start at randomly chosen places,
96 and all slide in parallel to their final positions,
97 passing through `animate-n-steps' positions before the final ones.
98 If HPOS is nil (or omitted), center the string horizontally
99 in the current window."
100 (let ((characters
101 (animate-initialize string vpos
102 (or hpos
103 ;; HPOS unspecified, so compute
104 ;; it so as to center the string.
105 (max 0 (/ (- (window-width) (length string)) 2))))))
106 (dotimes (i animate-n-steps)
107 ;; Bind buffer-undo-list so it will be unchanged when we are done.
108 ;; (We're going to undo all our changes anyway.)
109 (let (buffer-undo-list
110 list-to-undo)
111 ;; Display the characters at the Ith position.
112 ;; This inserts them in the buffer.
113 (animate-step characters (/ i 1.0 animate-n-steps))
114 ;; Make sure buffer is displayed starting at the beginning.
115 (set-window-start nil 1)
116 ;; Display it, and wait just a little while.
117 (sit-for .05)
118 ;; Now undo the changes we made in the buffer.
119 (setq list-to-undo buffer-undo-list)
120 (while list-to-undo
121 (let ((undo-in-progress t))
122 (setq list-to-undo (primitive-undo 1 list-to-undo))))))
123 ;; Insert the characters in their final positions.
124 (animate-step characters 1)
125 ;; Put the cursor at the end of the text on the line.
126 (end-of-line)
127 ;; Redisplay so they appear on the screen there.
128 (sit-for 0)
129 ;; This is so that the undo command, used afterwards,
130 ;; will undo the "animate" calls one by one.
131 (undo-boundary)))
132
133 ;;;###autoload
134 (defun animate-birthday-present ()
135 "Display Sarah's birthday present."
136 (interactive)
137 ;; Make a suitable buffer to display the birthday present in.
138 (switch-to-buffer (get-buffer-create "Sarah"))
139 (erase-buffer)
140 ;; Display the empty buffer.
141 (sit-for 0)
142 ;; Make sure indentation does not use tabs.
143 ;; They would confuse things.
144 (setq indent-tabs-mode nil)
145
146 (animate-string "Happy Birthday," 6)
147 (animate-string "Sarah" 7)
148
149 (sit-for 1)
150
151 (animate-string "You are my sunshine," 10 30)
152 (sit-for .5)
153 (animate-string "My only sunshine." 11 30)
154 (sit-for .5)
155 (animate-string "I'm awful sad that" 12 30)
156 (sit-for .5)
157 (animate-string "You've moved away." 13 30)
158 (sit-for .5)
159 (animate-string "Let's talk together" 15 30)
160 (sit-for .5)
161 (animate-string "And love more deeply." 16 30)
162 (sit-for .5)
163 (animate-string "Please bring back" 17 30)
164 (animate-string "my sunshine" 18 34)
165 (animate-string "to stay!" 19 34))
166
167 ;;; animate.el ends here
168