Mercurial > emacs
annotate lisp/play/animate.el @ 39077:b0080041c3c5
*** empty log message ***
author | Eli Zaretskii <eliz@gnu.org> |
---|---|
date | Sat, 01 Sep 2001 18:22:38 +0000 |
parents | 255503bac5c2 |
children | 0d8b17d428b5 |
rev | line source |
---|---|
38521 | 1 ;;; animate.el --- make text dance |
2 | |
3 ;; Copyright (C) 2001 Free Software Foundation, Inc. | |
4 | |
38626
6b570d0e1cf3
Add Keywords and Maintainer. Remove a
Gerd Moellmann <gerd@gnu.org>
parents:
38621
diff
changeset
|
5 ;; Maintainer: Richard Stallman <rms@gnu.org> |
6b570d0e1cf3
Add Keywords and Maintainer. Remove a
Gerd Moellmann <gerd@gnu.org>
parents:
38621
diff
changeset
|
6 ;; Keywords: games |
6b570d0e1cf3
Add Keywords and Maintainer. Remove a
Gerd Moellmann <gerd@gnu.org>
parents:
38621
diff
changeset
|
7 |
38521 | 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 | |
38708
456acc0ad1ee
Re-insert a reference to re-inserted function animate-birthday-present.
Pavel Janík <Pavel@Janik.cz>
parents:
38702
diff
changeset
|
31 ;; animate-birthday-present was the first application of this program. |
456acc0ad1ee
Re-insert a reference to re-inserted function animate-birthday-present.
Pavel Janík <Pavel@Janik.cz>
parents:
38702
diff
changeset
|
32 |
38521 | 33 ;;; Code: |
34 | |
35 ;;; STRING is the string to be displayed, | |
36 ;;; and DEST-X, DEST-Y say where on the screen | |
37 ;;; it should end up. | |
38 | |
39 ;;; This function returns a list describing | |
40 ;;; all the characters and the paths they should take. | |
41 ;;; Each element has the form | |
42 ;;; (CHAR START-Y START-X DEST-Y DEST-X). | |
43 | |
44 ;;; The start position of each character is chosen randomly. | |
45 ;;; The destination is chosen to put it in the right place | |
46 ;;; in the string when the whole string finally reaches its | |
47 ;;; specified position. | |
48 | |
49 (defun animate-initialize (string vpos hpos) | |
50 (let ((characters nil)) | |
51 (dotimes (i (length string)) | |
52 (setq characters | |
53 (cons (list (aref string i) | |
54 ;; Random starting positions. | |
55 (random (window-height)) | |
56 (random (1- (window-width))) | |
57 ;; All the chars should end up | |
58 ;; on the specified line. | |
59 vpos | |
60 ;; The Ith character in the string | |
61 ;; needs to end up I positions later. | |
62 (+ hpos i)) | |
63 characters))) | |
64 characters)) | |
65 | |
66 ;;; Display the characters in CHARACTERS, | |
67 ;;; each one FRACTION of the way from its start to its destination. | |
68 ;;; If FRACTION is 0, the characters appear in their starting positions. | |
69 ;;; If FRACTION is 1, the characters appear in their destinations. | |
70 | |
71 (defun animate-step (characters fraction) | |
72 (let ((remains (- 1 fraction))) | |
73 (dolist (item characters) | |
74 (let ((vpos (+ (* remains (nth 1 item)) | |
75 (* fraction (nth 3 item)))) | |
76 (hpos (+ (* remains (nth 2 item)) | |
77 (* fraction (nth 4 item))))) | |
78 (animate-place-char (car item) vpos hpos))))) | |
79 | |
80 ;;; Place the character CHAR at position VPOS, HPOS in the current buffer. | |
81 (defun animate-place-char (char vpos hpos) | |
82 (goto-char (window-start)) | |
83 (let ((next-line-add-newlines t)) | |
84 (dotimes (i vpos) | |
85 (next-line 1))) | |
86 (beginning-of-line) | |
87 (move-to-column (floor hpos) t) | |
88 (unless (eolp) (delete-char 1)) | |
89 (insert-char char 1)) | |
90 | |
91 (defvar animate-n-steps 10 | |
92 "Number of steps to use `animate-string'.") | |
93 | |
94 ;;;###autoload | |
95 (defun animate-string (string vpos &optional hpos) | |
96 "Display STRING starting at position VPOS, HPOS, using animation. | |
97 The characters start at randomly chosen places, | |
98 and all slide in parallel to their final positions, | |
99 passing through `animate-n-steps' positions before the final ones. | |
100 If HPOS is nil (or omitted), center the string horizontally | |
101 in the current window." | |
102 (let ((characters | |
103 (animate-initialize string vpos | |
104 (or hpos | |
105 ;; HPOS unspecified, so compute | |
106 ;; it so as to center the string. | |
38621
7f4573a73075
(animate-string): Handle case that the string is
Gerd Moellmann <gerd@gnu.org>
parents:
38522
diff
changeset
|
107 (max 0 (/ (- (window-width) (length string)) 2)))))) |
38521 | 108 (dotimes (i animate-n-steps) |
109 ;; Bind buffer-undo-list so it will be unchanged when we are done. | |
110 ;; (We're going to undo all our changes anyway.) | |
111 (let (buffer-undo-list | |
112 list-to-undo) | |
113 ;; Display the characters at the Ith position. | |
114 ;; This inserts them in the buffer. | |
115 (animate-step characters (/ i 1.0 animate-n-steps)) | |
116 ;; Make sure buffer is displayed starting at the beginning. | |
117 (set-window-start nil 1) | |
118 ;; Display it, and wait just a little while. | |
119 (sit-for .05) | |
120 ;; Now undo the changes we made in the buffer. | |
121 (setq list-to-undo buffer-undo-list) | |
122 (while list-to-undo | |
123 (let ((undo-in-progress t)) | |
124 (setq list-to-undo (primitive-undo 1 list-to-undo)))))) | |
125 ;; Insert the characters in their final positions. | |
126 (animate-step characters 1) | |
127 ;; Put the cursor at the end of the text on the line. | |
128 (end-of-line) | |
129 ;; Redisplay so they appear on the screen there. | |
130 (sit-for 0) | |
131 ;; This is so that the undo command, used afterwards, | |
132 ;; will undo the "animate" calls one by one. | |
133 (undo-boundary))) | |
134 | |
38702
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
135 ;;;###autoload |
38783
255503bac5c2
(animate-string): Doc fix. Use a buffer
Gerd Moellmann <gerd@gnu.org>
parents:
38708
diff
changeset
|
136 (defun animate-sequence (list-of-strings space) |
255503bac5c2
(animate-string): Doc fix. Use a buffer
Gerd Moellmann <gerd@gnu.org>
parents:
38708
diff
changeset
|
137 "Display strings from LIST-OF-STRING with animation in a new buffer. |
255503bac5c2
(animate-string): Doc fix. Use a buffer
Gerd Moellmann <gerd@gnu.org>
parents:
38708
diff
changeset
|
138 Strings will be separated from each other by SPACE lines." |
255503bac5c2
(animate-string): Doc fix. Use a buffer
Gerd Moellmann <gerd@gnu.org>
parents:
38708
diff
changeset
|
139 (let ((vpos (/ (- (window-height) |
255503bac5c2
(animate-string): Doc fix. Use a buffer
Gerd Moellmann <gerd@gnu.org>
parents:
38708
diff
changeset
|
140 1 ;; For the mode-line |
255503bac5c2
(animate-string): Doc fix. Use a buffer
Gerd Moellmann <gerd@gnu.org>
parents:
38708
diff
changeset
|
141 (* (1- (length list-of-strings)) space) |
255503bac5c2
(animate-string): Doc fix. Use a buffer
Gerd Moellmann <gerd@gnu.org>
parents:
38708
diff
changeset
|
142 (length list-of-strings)) |
255503bac5c2
(animate-string): Doc fix. Use a buffer
Gerd Moellmann <gerd@gnu.org>
parents:
38708
diff
changeset
|
143 2))) |
255503bac5c2
(animate-string): Doc fix. Use a buffer
Gerd Moellmann <gerd@gnu.org>
parents:
38708
diff
changeset
|
144 (switch-to-buffer (get-buffer-create "*Animation*")) |
255503bac5c2
(animate-string): Doc fix. Use a buffer
Gerd Moellmann <gerd@gnu.org>
parents:
38708
diff
changeset
|
145 (erase-buffer) |
255503bac5c2
(animate-string): Doc fix. Use a buffer
Gerd Moellmann <gerd@gnu.org>
parents:
38708
diff
changeset
|
146 (sit-for 0) |
255503bac5c2
(animate-string): Doc fix. Use a buffer
Gerd Moellmann <gerd@gnu.org>
parents:
38708
diff
changeset
|
147 (setq indent-tabs-mode nil) |
255503bac5c2
(animate-string): Doc fix. Use a buffer
Gerd Moellmann <gerd@gnu.org>
parents:
38708
diff
changeset
|
148 (while list-of-strings |
255503bac5c2
(animate-string): Doc fix. Use a buffer
Gerd Moellmann <gerd@gnu.org>
parents:
38708
diff
changeset
|
149 (animate-string (car list-of-strings) vpos) |
255503bac5c2
(animate-string): Doc fix. Use a buffer
Gerd Moellmann <gerd@gnu.org>
parents:
38708
diff
changeset
|
150 (setq vpos (+ vpos space 1)) |
255503bac5c2
(animate-string): Doc fix. Use a buffer
Gerd Moellmann <gerd@gnu.org>
parents:
38708
diff
changeset
|
151 (setq list-of-strings (cdr list-of-strings))))) |
255503bac5c2
(animate-string): Doc fix. Use a buffer
Gerd Moellmann <gerd@gnu.org>
parents:
38708
diff
changeset
|
152 |
255503bac5c2
(animate-string): Doc fix. Use a buffer
Gerd Moellmann <gerd@gnu.org>
parents:
38708
diff
changeset
|
153 ;;;###autoload |
38702
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
154 (defun animate-birthday-present () |
38783
255503bac5c2
(animate-string): Doc fix. Use a buffer
Gerd Moellmann <gerd@gnu.org>
parents:
38708
diff
changeset
|
155 "Display Sarah's birthday present in a new buffer." |
38702
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
156 (interactive) |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
157 ;; Make a suitable buffer to display the birthday present in. |
38783
255503bac5c2
(animate-string): Doc fix. Use a buffer
Gerd Moellmann <gerd@gnu.org>
parents:
38708
diff
changeset
|
158 (switch-to-buffer (get-buffer-create "*Sarah*")) |
38702
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
159 (erase-buffer) |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
160 ;; Display the empty buffer. |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
161 (sit-for 0) |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
162 ;; Make sure indentation does not use tabs. |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
163 ;; They would confuse things. |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
164 (setq indent-tabs-mode nil) |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
165 |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
166 (animate-string "Happy Birthday," 6) |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
167 (animate-string "Sarah" 7) |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
168 |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
169 (sit-for 1) |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
170 |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
171 (animate-string "You are my sunshine," 10 30) |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
172 (sit-for .5) |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
173 (animate-string "My only sunshine." 11 30) |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
174 (sit-for .5) |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
175 (animate-string "I'm awful sad that" 12 30) |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
176 (sit-for .5) |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
177 (animate-string "You've moved away." 13 30) |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
178 (sit-for .5) |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
179 (animate-string "Let's talk together" 15 30) |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
180 (sit-for .5) |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
181 (animate-string "And love more deeply." 16 30) |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
182 (sit-for .5) |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
183 (animate-string "Please bring back" 17 30) |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
184 (animate-string "my sunshine" 18 34) |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
185 (animate-string "to stay!" 19 34)) |
dc644b0090f2
(animate-birthday-present): Re-insert.
Gerd Moellmann <gerd@gnu.org>
parents:
38626
diff
changeset
|
186 |
38521 | 187 ;;; animate.el ends here |