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