Commit | Line | Data |
---|---|---|
73496807 GM |
1 | ;;; animate.el --- make text dance |
2 | ||
73b0cd50 | 3 | ;; Copyright (C) 2001-2011 Free Software Foundation, Inc. |
73496807 | 4 | |
5bb97b26 GM |
5 | ;; Maintainer: Richard Stallman <rms@gnu.org> |
6 | ;; Keywords: games | |
7 | ||
73496807 GM |
8 | ;; This file is part of GNU Emacs. |
9 | ||
b1fc2b50 | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
73496807 | 11 | ;; it under the terms of the GNU General Public License as published by |
b1fc2b50 GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
73496807 GM |
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 | |
b1fc2b50 | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
73496807 GM |
22 | |
23 | ;;; Commentary: | |
24 | ||
25 | ;; (animate-string STRING VPOS &optional HPOS) | |
26 | ;; makes the string STRING appear starting at VPOS, HPOS | |
27 | ;; by having each letter swoop into place from random starting position. | |
28 | ||
d1d850d6 PJ |
29 | ;; animate-birthday-present was the first application of this program. |
30 | ||
73496807 GM |
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)) | |
46a048fc | 81 | (let (abbrev-mode) |
73496807 | 82 | (dotimes (i vpos) |
46a048fc KS |
83 | (end-of-line) |
84 | (if (= (forward-line 1) 1) | |
85 | (insert "\n")))) | |
73496807 GM |
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. | |
bde79dc0 JB |
107 | (max 0 (/ (- (window-width) (length string)) 2))))) |
108 | (show-trailing-whitespace nil) | |
109 | ;; Make sure indentation does not use tabs. | |
110 | ;; They would confuse things. | |
111 | (indent-tabs-mode nil)) | |
73496807 GM |
112 | (dotimes (i animate-n-steps) |
113 | ;; Bind buffer-undo-list so it will be unchanged when we are done. | |
114 | ;; (We're going to undo all our changes anyway.) | |
115 | (let (buffer-undo-list | |
116 | list-to-undo) | |
117 | ;; Display the characters at the Ith position. | |
118 | ;; This inserts them in the buffer. | |
119 | (animate-step characters (/ i 1.0 animate-n-steps)) | |
120 | ;; Make sure buffer is displayed starting at the beginning. | |
121 | (set-window-start nil 1) | |
122 | ;; Display it, and wait just a little while. | |
123 | (sit-for .05) | |
124 | ;; Now undo the changes we made in the buffer. | |
125 | (setq list-to-undo buffer-undo-list) | |
126 | (while list-to-undo | |
127 | (let ((undo-in-progress t)) | |
128 | (setq list-to-undo (primitive-undo 1 list-to-undo)))))) | |
129 | ;; Insert the characters in their final positions. | |
130 | (animate-step characters 1) | |
131 | ;; Put the cursor at the end of the text on the line. | |
132 | (end-of-line) | |
133 | ;; Redisplay so they appear on the screen there. | |
134 | (sit-for 0) | |
135 | ;; This is so that the undo command, used afterwards, | |
136 | ;; will undo the "animate" calls one by one. | |
137 | (undo-boundary))) | |
138 | ||
242e5463 GM |
139 | ;;;###autoload |
140 | (defun animate-sequence (list-of-strings space) | |
141 | "Display strings from LIST-OF-STRING with animation in a new buffer. | |
142 | Strings will be separated from each other by SPACE lines." | |
143 | (let ((vpos (/ (- (window-height) | |
144 | 1 ;; For the mode-line | |
145 | (* (1- (length list-of-strings)) space) | |
146 | (length list-of-strings)) | |
147 | 2))) | |
148 | (switch-to-buffer (get-buffer-create "*Animation*")) | |
149 | (erase-buffer) | |
150 | (sit-for 0) | |
242e5463 GM |
151 | (while list-of-strings |
152 | (animate-string (car list-of-strings) vpos) | |
153 | (setq vpos (+ vpos space 1)) | |
154 | (setq list-of-strings (cdr list-of-strings))))) | |
a1506d29 | 155 | |
56c35630 | 156 | ;;;###autoload |
8bcfd13b EZ |
157 | (defun animate-birthday-present (&optional name) |
158 | "Display one's birthday present in a new buffer. | |
159 | You can specify the one's name by NAME; the default value is \"Sarah\"." | |
5b76833f | 160 | (interactive (list (read-string "Name (default Sarah): " |
8bcfd13b | 161 | nil nil "Sarah"))) |
56c35630 | 162 | ;; Make a suitable buffer to display the birthday present in. |
8bcfd13b | 163 | (switch-to-buffer (get-buffer-create (format "*%s*" name))) |
56c35630 GM |
164 | (erase-buffer) |
165 | ;; Display the empty buffer. | |
166 | (sit-for 0) | |
56c35630 GM |
167 | |
168 | (animate-string "Happy Birthday," 6) | |
8bcfd13b | 169 | (animate-string (format "%s" name) 7) |
56c35630 GM |
170 | |
171 | (sit-for 1) | |
172 | ||
173 | (animate-string "You are my sunshine," 10 30) | |
174 | (sit-for .5) | |
175 | (animate-string "My only sunshine." 11 30) | |
176 | (sit-for .5) | |
177 | (animate-string "I'm awful sad that" 12 30) | |
178 | (sit-for .5) | |
179 | (animate-string "You've moved away." 13 30) | |
180 | (sit-for .5) | |
181 | (animate-string "Let's talk together" 15 30) | |
182 | (sit-for .5) | |
183 | (animate-string "And love more deeply." 16 30) | |
184 | (sit-for .5) | |
185 | (animate-string "Please bring back" 17 30) | |
186 | (animate-string "my sunshine" 18 34) | |
187 | (animate-string "to stay!" 19 34)) | |
188 | ||
d2fe6685 GM |
189 | (random t) |
190 | ||
c420eb36 GM |
191 | (provide 'animate) |
192 | ||
73496807 | 193 | ;;; animate.el ends here |