-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathanimator.el
174 lines (130 loc) · 4.86 KB
/
animator.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
(require 'utils)
(setf *animator-color-stack* nil)
(defun start-animator-process ()
(let ((sh (shell "*animator*")))
(comint-send-strings sh "animator")
(setq *animator* sh)))
(start-animator-process)
(defun animator-send (&rest strings)
(apply #'comint-send-strings *animator* strings))
(defun animator-flush ()
(comint-send-strings *animator* "flush"))
(defun animator-frame ()
(comint-send-strings *animator* "frame"))
(defun animator-color (cc)
(if (listp cc)
(let* ((s (format "%s" cc))
(n (length s)))
(animator-send (concat "color " (substring s 1 (- n 1)))))
(animator-send (format "color %s" cc))))
(defun animator-push-color (cc)
(push cc *animator-color-stack*)
(if (listp cc)
(let* ((s (format "%s" cc))
(n (length s)))
(animator-send (concat "color " (substring s 1 (- n 1)))))
(animator-send (format "color %s" cc))))
(defun animator-pop-color ()
(let ((clr (pop *animator-color-stack*))
(top (car *animator-color-stack*)))
(if top (animator-color top)
(animator-color "black"))))
(defmacro* with-animator-color (color &body body)
(let ((color-sym (gensym "animator-color-")))
`(let ((,color-sym ,color))
(animator-push-color ,color-sym)
,@body
(animator-pop-color))))
(defmacro* with-flush/frame (&body body)
`(progn (animator-frame) ,@body (animator-flush)))
(defun animator-dot (x y)
(comint-send-strings *animator*
(format "dot %f %f" x y)))
(defun animator-dots (pairs)
(loop for pair in pairs do
(apply #'animator-dot pair)))
(defun animator-line (x1 y1 x2 y2)
(animator-send (format "line %f %f %f %f" x1 y1 x2 y2)))
(defun animator-disjoint-lines (&rest lines)
(loop for line in lines do
(apply animator-line line)))
(defun animator-connected-lines (&rest args)
(let ((args (flatten args)))
(animator-send
(concat "lines "
(foldl (lambda (it ac) (concat ac (format " %f" it)))
""
args)))))
(defun animator-poly (&rest args)
(let ((args (flatten args)))
(animator-send
(concat "poly "
(foldl (lambda (it ac) (concat ac (format " %f" it)))
""
args)))))
(defun animator-circle (x y r &optional verts)
(let ((verts (if verts verts 25)))
(animator-send (format "circle %f %f %f %f" x y r verts))))
(defun animator-filled-circle (x y r &optional verts)
(let ((verts (if verts verts 25)))
(animator-send (format "fillcircle %f %f %f %f" x y r verts))))
(defun animator-text (x y text &optional alignment)
(if alignment
(animator-send (format "text %s %f %f \"%s\"" alignment x y text))
(animator-send (format "text %f %f \"%s\"" x y text))))
(defun animator-dup ()
(animator-send "push"))
(defun animator-pop ()
(animator-send "pop"))
(defun animator-ident ()
(animator-send "ident"))
(defun animator-shift (x y)
(animator-send (format "shift %f %f" x y)))
(defun animator-rotate (degrees)
(animator-send (format "rotate %f" degrees)))
(defun animator-scale (x &optional y)
(let ((y (if y y x)))
(animator-send (format "scale %f %f" x y))))
(defun animator-viewport (left bottom w h)
(animator-send (format "viewport %f %f %f %f" left bottom w h)))
(defun create-animator-format-string (name-string args)
(loop for i from 1 to (length args) do
(setq name-string (concat name-string " %f")))
name-string)
(defmacro* def-numeric-animator-primitive (name &optional doc &rest arg-names)
(let ((interface-name (internf "animator-%s" name))
(name-string (format "%s" name))
(doc (if (stringp doc) doc nil))
(arg-names (if (stringp doc) arg-names
(cons doc arg-names))))
`(defun ,interface-name ,arg-names ,doc
(animator-send (format ,(create-animator-format-string name-string arg-names)
,@arg-names)))))
(defmacro* def-numeric-animator-primitive-alt-name (interface-name name &optional doc &rest arg-names)
(let ((name-string (format "%s" name))
(doc (if (stringp doc) doc nil))
(arg-names (if (stringp doc) arg-names
(cons doc arg-names))))
`(defun ,interface-name ,arg-names ,doc
(animator-send (format ,(create-animator-format-string name-string arg-names)
,@arg-names)))))
(def-numeric-animator-primitive thick "Set animator line thickness"
thickness)
(def-numeric-animator-primitive alpha "Set animator transparency." alpha)
(def-numeric-animator-primitive arrow "Draw an arrow pointing towards X2 Y2"
x1 y1 x2 x2)
(def-numeric-animator-primitive-alt-name fill-rect fillrect "Fill an animator rectangle with the current color." x y w h)
(def-numeric-animator-primitive rect "Fill an animator rectangle with the current color." x y w h)
(dont-do
(require 'animator)
(with-flush/frame (animator-text 0 0 "HOLY SHEET!"))
(loop for i from 1 to 1000000 do
(sleep-for 0 10)
(with-flush/frame
(print (* 50.0 (sin (/ i 100.0))))
(animator-text 0
(* 50.0 (sin (/ i 100.0)))
"HOLY SHEEET!!!")))
(with-flush/frame (animator-rect 0 0 .25 .25))
)
(provide 'animator)