Repository navigation
Expand file tree
/
Copy pathpd-structs.lisp
More file actions
277 lines (248 loc) · 8.95 KB
/
Copy pathpd-structs.lisp
File metadata and controls
277 lines (248 loc) · 8.95 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
(defpackage :pd-structs
(:use :common-lisp)
(:export :write-node
:to-string
;; --------------------------------
:node
:object-node
:self-node
:patch-node
:floatatom-node
:symbolatom-node
:ui-node
:bng-node
:tgl-node
:cnv-node
:vsl-node
:hsl-node
;; from node
:node-p
:node-name
:node-init-args
:node-id
:node-index
:node-rank
:node-x
:node-y
;; from ui-node
:node-send-symbol
:node-receive-symbol
:node-label-text
:node-label-x
:node-label-y
:node-font-family
:node-font-size
:node-bg-color
:node-fg-color
:node-label-color
;; --------------------------------
:connection
:make-connection
:connection-p
:connection-out-id
:connection-out-port
:connection-in-id
:connection-in-port
;; --------------------------------
:port
:make-port
:port-p
:port-number
:port-node))
(in-package :pd-structs)
;; --------------------------------------------------------------------------------
;; node classes
;;
(defclass node ()
((id
:accessor node-id
:initform (gensym))
(name
:accessor node-name
:initarg :name)
(index
:accessor node-index)
(x
:initarg :x
:initform nil
:accessor node-x)
(y
:initarg :y
:initform nil
:accessor node-y)
(rank
:initform nil
:accessor node-rank))
)
(defgeneric node-p (n) (:documentation "test wether a thing is a node (or one of its subclasses)"))
(defmethod node-p (n) nil)
(defmethod node-p ((n node)) t)
(defclass object-node (node)
((init-args
:accessor node-init-args
:initarg :init-args
:initform ""
)))
(defclass self-node (object-node)
;; for nodes like "msg" and "text" that are represented as "#X msg" or "#X text"
;; (e.g. representing themselves)
())
(defclass patch-node (object-node) ())
(defclass atom-node (node)
((send-symbol :initarg :send :initform "-")
(receive-symbol :initarg :receive :initform "-")
(label-text :initarg :label :initform "-")
(label-pos :initarg :label-pos :initform 0)
(width :initarg :width :initform 5) ; width means number of digits shown.
(lower-limit :initarg :lower :initform 0)
(upper-limit :initarg :upper :initform 0)))
(defclass floatatom-node (atom-node)
((width :initarg width :initform 5)))
(defclass symbolatom-node (atom-node)
((width :initarg width :initform 10)))
(defclass ui-node (node)
((send-symbol :accessor node-send-symbol :initarg :send :initform "empty")
(receive-symbol :accessor node-receive-symbol :initarg :receive :initform "empty")
(label-text :accessor node-label-text :initarg :label :initform "empty")
(label-x :accessor node-label-x :initarg :label-x :initform 17)
(label-y :accessor node-label-y :initarg :label-y :initform 7)
(font-family :accessor node-font-family :initarg :font :initform 0)
(font-size :accessor node-font-size :initarg :font-size :initform 10)
(bg-color :accessor node-bg-color :initarg :bg-color :initform -262144)
(fg-color :accessor node-fg-color :initarg :fg-color :initform -1)
(label-color :accessor node-label-color :initarg :label-color :initform -1)))
(defclass bng-node (ui-node)
((size :initarg :size :initform 15)
(hold :initarg :hold :initform 250)
(interrupt :initarg :interrupt :initform 50)
(init :initarg :init :initform 0)))
(defclass tgl-node (ui-node)
((size :initarg :size :initform 15)
(init :initarg :init :initform 0)
(init-value :initarg :init-value :initform 0)
(nonzero-value :initarg :nonzero-value :initform 1)))
(defclass cnv-node (ui-node)
((bg-color :initarg :bg-color :initform -233017) ; different default color
(label-color :initarg :label-color :initform -66577) ; different default color
(size :initarg :size :initform 15)
(width :initarg :width :initform 100)
(height :initarg :height :initform 60)
(unknown :initform 0)))
(defclass slider-node (ui-node)
((width :initarg :width :initform nil)
(height :initarg :height :initform nil)
(bottom :initarg :bottom :initform 0)
(top :initarg :top :initform 127)
(log :initarg :log :initform 0)
(init :initarg :init :initform 0)
(default :initarg :default :initform 0) ; default means: saved slider position, in pixels.
(steady-on-click :initarg :steady-on-click :initform 1)))
(defclass vsl-node (slider-node)
((width :initform 15)
(height :initform 127)))
(defclass hsl-node (slider-node)
((width :initform 127)
(height :initform 15)))
;; --------------------------------------------------------------------------------
;; templating
;;
(defun to-string (any)
(format nil "~a" any))
(defun fill-template (template n &optional s)
"fill provided template with node attributes"
(if (null template)
(concatenate 'string s ";" (string #\newline))
(let ((arg (first template)))
(fill-template (rest template)
n
(concatenate 'string
s
(if s " " "")
(to-string
(if (symbolp arg)
(slot-value n arg)
arg)))))))
(defgeneric write-node (n)
(:documentation "create .pd-compliant string representation of node objects"))
(defmethod write-node ((n object-node))
(fill-template '("#X obj" x y name init-args) n))
(defmethod write-node ((n self-node))
(fill-template '("#X" name x y init-args) n))
(defmethod write-node ((n patch-node))
(fill-template '("#X obj" x y init-args) n))
(defmethod write-node ((n atom-node))
(fill-template '("#X"
name ; floatatom or symbolatom
x y
width
lower-limit upper-limit
label-pos
label-text
receive-symbol
send-symbol)
n))
(defmethod write-node ((n bng-node))
(fill-template '("#X obj"
x y
name ; "bng" ; FIXME remove variable if only ever used with "bng"
size
hold interrupt
init
send-symbol receive-symbol
label-text
label-x label-y
font-family font-size
bg-color fg-color label-color)
n))
(defmethod write-node ((n tgl-node))
(fill-template '("#X obj" x y
name ; "tgl"
size
init
send-symbol receive-symbol
label-text
label-x label-y
font-family font-size
bg-color fg-color label-color
init-value nonzero-value)
n))
(defmethod write-node ((n cnv-node))
(fill-template '("#X obj" x y
name ; "cnv"
size
width height
send-symbol receive-symbol
label-text
label-x label-y
font-family font-size
bg-color label-color
unknown)
n))
(defmethod write-node ((n slider-node))
(fill-template '("#X obj" x y
name ; "hsl" or "vsl"
width height
bottom top
log
init
send-symbol
receive-symbol
label-text
label-x label-y
font-family font-size
bg-color fg-color label-color
default steady-on-click
)
n))
;; --------------------------------------------------------------------------------
;; misc
;;
(defstruct connection
(out-id) ; gensym of source node
(out-port) ; index of output port
(in-id) ; gensym of target node
(in-port) ; index of input port
)
(defstruct port
(number)
(node))