1 (in-package #:clnl-interface)
3 (defvar *turtle-lists* nil)
4 (defvar *patch-list* nil)
6 (defvar *glut-window-opened* nil)
7 (defvar *dimensions* nil) ; This is a useful placeholder for other view properties
8 (defvar *window-width* 1024)
9 (defvar *window-height* 768)
11 (defvar *default-shapes* nil)
13 (defvar *textbox* nil)
14 (defvar *inputbox* nil)
16 (defvar *current-globals* nil)
18 (defvar *widgets* nil) ; this is going to be pairs to save the original definition
20 ; This is the thread that does the work of querying the currently running system to update
21 ; the interface state. We keep it seperate from the main system display thread for two reasons:
22 ; 1) It should run even if there's no active display
23 ; 2) We want it to run slower than the current display thread
24 (defvar *interface-thread* nil)
26 (defgeneric update-widget (type def widget extra-info))
27 (defmethod update-widget (type def widget extra-info))
29 (defmethod update-widget ((type (eql :button)) button-def button idx)
30 (when (getf button-def :forever)
31 (clnl-gltk:toggle button (clnl-model:forever-button-on (getf button-def :display) idx))))
33 (defmethod update-widget ((type (eql :switch)) switch-def switch nothing)
35 ((global (find (getf switch-def :var) *current-globals* :key (lambda (def) (getf def :name)))))
36 (clnl-gltk:toggle switch (getf global :value))))
38 (defun update-interface ()
40 (lambda (widget) (apply #'update-widget widget))
43 (defun boot-interface-thread ()
44 (when (not *interface-thread*)
45 (setf *interface-thread*
46 (sb-thread:make-thread (lambda () (loop (update-interface) (sleep .1))) :name "Interface Thread"))))
48 ; For now, shapes can live in here
50 ; * name <like default>
51 ; * rotatable (equal to "true" if yes)
53 ; then after, the elements are like so:
55 ; filled == filled in (always for now, ha)
56 ; marked == use the turtle color instead of a color
57 ; polygon -> Polygon <color> <filled> <marked> <alternating x y coords>
58 ; circle -> Circle <color> <filled> <marked> <left> <top> <diameter> ; here, the left and top are NOT the center
59 ; rectangle -> Rectangle <color> <filled> <marked> <left> <top> <right> <bottom>
61 ; then ends with an empty string
63 (defun parse-circle (sections)
65 :color (parse-integer (car sections))
66 :filled (string= (nth 1 sections) "true")
67 :marked (string= (nth 2 sections) "true")
68 :left (parse-integer (nth 3 sections))
69 :top (parse-integer (nth 4 sections))
70 :diameter (parse-integer (nth 5 sections))))
72 (defun parse-rectangle (sections)
75 :color (parse-integer (car sections))
76 :filled (string= (nth 1 sections) "true")
77 :marked (string= (nth 2 sections) "true")
78 :left (parse-integer (nth 3 sections))
79 :top (parse-integer (nth 4 sections))
80 :right (parse-integer (nth 5 sections))
81 :bottom (parse-integer (nth 6 sections))))
83 (defun parse-polygon (sections)
85 ((parse-points (sections)
88 (list (- 300 (parse-integer (car sections))) (parse-integer (cadr sections)))
89 (parse-points (cddr sections))))))
92 :color (parse-integer (car sections))
93 :filled (string= (nth 1 sections) "true")
94 :marked (string= (nth 2 sections) "true")
95 :coords (parse-points (nthcdr 3 sections)))))
97 (defun parse-shape (str)
99 ((parse-element (line)
101 ((sections (cl-ppcre:split " " line)))
103 ((string= (car sections) "Circle") (parse-circle (cdr sections)))
104 ((string= (car sections) "Rectangle") (parse-rectangle (cdr sections)))
105 ((string= (car sections) "Polygon") (parse-polygon (cdr sections))))))
108 ((line (read-line str nil)))
109 (when (and line (string/= line ""))
112 (parse-elements))))))
114 ((next-line (read-line str nil)))
118 :rotatable (string= "true" (read-line str))
119 :rgb (read-line str) ; this is ignored for now, I think
120 :elements (parse-elements))))))
122 ; Clipping ears algorithm. This can be slow due to the fact that it will only be run once.
123 (defun triangulate (points &optional (ccw :unknown))
126 (< 0 (- (* (- (car y) (car x)) (- (cadr z) (cadr x))) (* (- (car z) (car x)) (- (cadr y) (cadr x))))))
127 (tri-is-concave (x y z) (if (tri-is-ccw x y z) (not ccw) ccw))
128 (poly-is-ccw (points &optional cur-tri)
131 (poly-is-ccw (append points (list (car points))) (list (car (last points)) (car points) (cadr points))))
132 ((eql (length points) 2)
133 (apply #'tri-is-ccw cur-tri))
135 (< (car (cadr points)) (car (cadr cur-tri)))
137 (= (car (cadr points)) (car (cadr cur-tri)))
138 (< (cadr (cadr points)) (cadr (cadr cur-tri)))))
139 (poly-is-ccw (cdr points) (subseq points 0 3)))
140 (t (poly-is-ccw (cdr points) cur-tri))))
141 (point-in-tri (x y z p)
142 ; Barycentric system test
144 ((denom (+ (* (- (cadr y) (cadr z)) (- (car x) (car z))) (* (- (car z) (car y)) (- (cadr x) (cadr z)))))
145 (a (/ (+ (* (- (cadr y) (cadr z)) (- (car p) (car z))) (* (- (car z) (car y)) (- (cadr p) (cadr z)))) denom))
146 (b (/ (+ (* (- (cadr z) (cadr x)) (- (car p) (car z))) (* (- (car x) (car z)) (- (cadr p) (cadr z)))) denom))
148 (and (<= 0 a 1) (<= 0 b 1) (<= 0 c 1))))
149 (no-points-in-tri (tri points)
150 (every (lambda (point) (not (point-in-tri (car tri) (cadr tri) (caddr tri) point))) points))
151 (tri-is-actually-line (x y z)
152 (zerop (+ (* (- (cadr y) (cadr z)) (- (car x) (car z))) (* (- (car z) (car y)) (- (cadr x) (cadr z)))))))
154 ((not (find :end points)) (triangulate (append points (list :end)) ccw))
155 ((< (length points) 4) (error "Must have at least 3 points..."))
156 ((= (length points) 4) (list (remove :end points)))
157 ((eql ccw :unknown) (triangulate points (poly-is-ccw (remove :end points))))
158 ((eql :end (car points)) (error "This polygon may not be triangulateable"))
161 ((endless (remove :end points))
162 (tri (subseq endless 0 3)))
164 ((apply #'tri-is-actually-line tri)
165 (triangulate (cons (car endless) (cddr endless)) ccw))
166 ((apply #'tri-is-concave tri)
167 (triangulate (append (cdr points) (list (car points))) ccw))
168 ((no-points-in-tri tri (nthcdr 3 endless))
169 (cons tri (triangulate (cons (car endless) (cddr endless)) ccw)))
170 (t (triangulate (append (cdr points) (list (car points))) ccw))))))))
172 (defun element->gl-list (shape)
174 (when (not (getf (cdr shape) :marked))
175 (gl:push-attrib :all-attrib-bits)
177 (/ (ash (ldb (byte 24 0) (getf (cdr shape) :color)) -16) 255)
178 (/ (ash (ldb (byte 16 0) (getf (cdr shape) :color)) -8) 255)
179 (/ (ldb (byte 8 0) (getf (cdr shape) :color)) 255)))
180 (gl:begin :triangles)
184 (lambda (point) (gl:vertex (car point) (cadr point) 0))
185 (apply #'append (triangulate (getf (cdr shape) :coords)))))
188 (lambda (point) (gl:vertex (car point) (cadr point) 0))
192 (list (- 300 (getf (cdr shape) :left)) (getf (cdr shape) :top))
193 (list (- 300 (getf (cdr shape) :right)) (getf (cdr shape) :top))
194 (list (- 300 (getf (cdr shape) :right)) (getf (cdr shape) :bottom))
195 (list (- 300 (getf (cdr shape) :left)) (getf (cdr shape) :bottom)))))))
198 (lambda (point) (gl:vertex (car point) (cadr point) 0))
203 :with c := (strictmath:cos (strictmath:to-radians 1))
204 :with s := (strictmath:sin (strictmath:to-radians 1))
205 :with r := (/ (getf (cdr shape) :diameter) 2)
206 :with left := (getf (cdr shape) :left)
207 :with top := (getf (cdr shape) :top)
209 :for x := r :then (- (* c x) (* s y))
210 :for y := 0 :then (+ (* s n) (* c y))
211 :collect (list (- 300 (+ (+ x left) r)) (+ (+ y top) r))))))))
213 (when (not (getf (cdr shape) :marked))
216 (defun parse-shapes (str)
218 ((shape (parse-shape str)))
219 (when shape (cons shape (parse-shapes str)))))
221 (defun default-shapes ()
222 (with-open-file (str "resources/defaultshapes") (parse-shapes str)))
224 (eval-when (:load-toplevel)
225 (when (probe-file "resources/defaultshapes")
226 (setf *default-shapes* (default-shapes))))
229 '((140 140 140) ; gray (5)
230 (215 48 39) ; red (15)
231 (241 105 19) ; orange (25)
232 (156 109 70) ; brown (35)
233 (237 237 47) ; yellow (45)
234 (87 176 58) ; green (55)
235 (42 209 57) ; lime (65)
236 (27 158 119) ; turquoise (75)
237 (82 196 196) ; cyan (85)
238 (43 140 190) ; sky (95)
239 (50 92 168) ; blue (105)
240 (123 78 163) ; violet (115)
241 (166 25 105) ; magenta (125)
242 (224 126 149) ; pink (135)
244 (255 255 255))) ; white
246 (defun nl-color->rgb (color)
248 ((step (+ (/ (- (mod (floor (* color 10)) 100) 50) 50.48) 0.012)))
250 (lambda (x) (/ (+ x (floor (* (if (< step 0d0) x (- 255 x)) step))) 255))
251 (nth (floor color 10) *colors*))))
253 (defun render-scene ()
254 (gl:matrix-mode :projection)
255 (gl:with-pushed-matrix
258 (floor (* (- (getf *dimensions* :xmin) 0.5) (patch-size)))
259 (floor (* (+ (getf *dimensions* :xmax) 0.5) (patch-size)))
260 (floor (* (- (getf *dimensions* :ymin) 0.5) (patch-size)))
261 (floor (* (+ (getf *dimensions* :ymax) 0.5) (patch-size)))
263 (gl:matrix-mode :modelview)
264 (gl:with-pushed-matrix
266 (destructuring-bind (turtles patches globals) (clnl-nvm:current-state)
267 (setf *current-globals* globals)
271 ((color (nl-color->rgb (getf patch :color))))
272 (gl:color (car color) (cadr color) (caddr color)))
273 (gl:with-pushed-matrix
274 (gl:translate (* (getf patch :xcor) (patch-size)) (* (getf patch :ycor) (patch-size)) 0)
275 (gl:translate (floor (* -.5d0 (patch-size))) (floor (* -.5d0 (patch-size))) 0)
276 (gl:scale (patch-size) (patch-size) 1)
277 (gl:call-list *patch-list*)))
282 ((color (nl-color->rgb (getf turtle :color))))
283 (gl:color (car color) (cadr color) (caddr color)))
285 (lambda (x-modification y-modification)
286 (gl:with-pushed-matrix
287 (gl:translate (* (getf turtle :xcor) (patch-size)) (* (getf turtle :ycor) (patch-size)) 0)
288 (gl:translate x-modification y-modification 0)
290 ((turtle-list (find (getf turtle :shape) *turtle-lists* :test #'string= :key #'car)))
292 (when (second turtle-list)
293 (gl:rotate (getf turtle :heading) 0 0 -1))
294 (gl:scale (patch-size) (patch-size) 1)
295 (gl:scale (getf turtle :size) (getf turtle :size) 1)
296 (gl:call-list (third turtle-list))))))
297 (list 0 (1- (world-width-in-pixels)) (- (1- (world-width-in-pixels))) 0 0)
298 (list 0 0 0 (1- (world-height-in-pixels)) (- (1- (world-height-in-pixels))))))
300 (gl:matrix-mode :projection))
303 (defun render-widgets ()
304 (clnl-gltk:render *textbox*)
305 (clnl-gltk:render *inputbox*)
306 (mapcar #'clnl-gltk:render (mapcar #'third *widgets*)))
309 (gl:clear :color-buffer-bit :depth-buffer-bit)
311 ((width (world-width-in-pixels))
312 (height (world-height-in-pixels)))
313 (gl:viewport 0 0 *window-width* *window-height*)
315 (gl:matrix-mode :projection)
317 ((left (getf *dimensions* :left))
318 (top (getf *dimensions* :top))
320 (view-x2 (+ view-x1 width))
321 (view-y1 (- *window-height* height top))
322 (view-y2 (- *window-height* top)))
324 (gl:with-pushed-matrix
326 (gl:ortho 0 *window-width* 0 *window-height* 0 5000)
331 (gl:vertex view-x1 view-y1)
332 (gl:vertex view-x1 (+ view-y2 1))
334 (gl:vertex view-x1 (+ view-y2 1))
335 (gl:vertex (+ view-x2 1) (+ view-y2 1))
337 (gl:vertex (+ view-x2 1) (+ view-y2 1))
338 (gl:vertex (+ view-x2 1) (- view-y1 1))
340 (gl:vertex (+ view-x2 1) view-y1)
341 (gl:vertex (- view-x1 1) view-y1)
344 (gl:viewport view-x1 view-y1 width height)
349 (cl-glut:swap-buffers))
352 (cl-glut:post-redisplay))
355 ;(glut:leave-main-loop)
356 (sb-ext:exit :abort t))
358 (defun reshape (width height)
359 (when (and (/= 0 width) (/= 0 height))
360 (setf *window-width* width)
361 (setf *window-height* height)
363 ((box-width (truncate (- width 12) clnl-gltk:*font-width*)))
364 (clnl-gltk:resize *textbox* box-width 12)
365 (clnl-gltk:resize *inputbox* box-width 1))
368 (clnl-gltk:reposition (third widget)
369 (getf (second widget) :left)
370 (- *window-height* (getf (second widget) :height) (getf (second widget) :top))))
375 (clnl-gltk:textbox-text *textbox*)
376 (format nil "> ~A~%~%~A" str
378 (with-output-to-string (*standard-output*)
379 (clnl:run-commands str))
380 (error (e) (format nil "Ok, something went wrong: ~A~%Try :help" e))))))
382 (defun key-pressed (key x y)
383 (declare (ignore x y))
386 (execute (clnl-gltk:value *inputbox*))
387 (clnl-gltk:clear *inputbox*))
388 (clnl-gltk:key-pressed *inputbox* key)))
390 (defun mouse (button state x y)
391 (declare (ignore button))
394 (when (eql state :down) (clnl-gltk:mousedown w x (- *window-height* y)))
395 (when (eql state :up) (clnl-gltk:mouseup w x (- *window-height* y))))
396 (mapcar #'third *widgets*)))
400 (lambda (w) (clnl-gltk:mousemove w x (- *window-height* y)))
401 (mapcar #'third *widgets*)))
403 (cffi:defcallback display :void () (display))
404 (cffi:defcallback idle :void () (idle))
405 (cffi:defcallback close-func :void () (close-func))
406 (cffi:defcallback reshape :void ((width :int) (height :int)) (reshape width height))
407 (cffi:defcallback key-pressed :void ((key :uchar) (x :int) (y :int)) (key-pressed key x y))
408 (cffi:defcallback special-key-pressed :void ((key glut:special-keys) (x :int) (y :int)) (key-pressed key x y))
409 (cffi:defcallback mouse :void ((button cl-glut:mouse-button) (state cl-glut:mouse-button-state) (x :int) (y :int))
410 (mouse button state x y))
412 (cffi:defcallback motion :void ((x :int) (y :int)) (motion x y))
414 (defun set-turtle-lists ()
423 (getf shape :rotatable)
425 (gl:with-new-list ((third turtle-list) :compile)
426 (gl:rotate 180d0 0d0 0d0 -1d0)
427 (gl:scale (/ 1d0 300d0) (/ 1d0 300d0) 1)
428 (gl:translate -150d0 -150d0 -0.0d0)
429 (mapcar #'element->gl-list (getf shape :elements)))
431 (or *default-shapes* (default-shapes)))))
433 (defun set-patch-list ()
434 (setf *patch-list* (gl:gen-lists 1))
435 (gl:with-new-list (*patch-list* :compile)
443 (defvar *initial-banner*
446 / \\ Welcome to CLNL version ~A!
450 CLNL is an experiment at creating an alternate implementation of NetLogo.
452 You can enter in various netlogo commands below, or use :q to quit the program.
454 See http://github.com/frankduncan/clnl for more information about CLNL and to
455 keep apprised of any updates that may happen.")
457 (defun button-defs->buttons (button-defs)
459 ((known-button-names nil))
463 ((idx (length (remove (getf button-def :display) known-button-names :test-not #'equal)))
467 (getf button-def :left)
468 (- *window-height* (getf button-def :height) (getf button-def :top))
469 (getf button-def :width)
470 (getf button-def :height)
471 (getf button-def :display)
473 (when toggle-button (funcall toggle-button))
475 (format nil ":button \"~A\"~A"
476 (getf button-def :display)
477 (if (zerop idx) "" (format nil " ~A" idx)))))
478 :forever (getf button-def :forever))))
479 (push (getf button-def :display) known-button-names)
480 (when (getf button-def :forever) (setf toggle-button (lambda () (clnl-gltk:toggle button))))
481 (list :button button-def button idx)))
484 (defun switch-defs->switches (switch-defs)
490 (getf switch-def :left)
491 (- *window-height* clnl-gltk:*switch-height* (getf switch-def :top))
492 (getf switch-def :width)
493 (getf switch-def :display)
494 (lambda (state) (execute (format nil "set ~A ~A" (getf switch-def :display) (if state "true" "false"))))
495 (getf switch-def :initial-value))))
496 (list :switch (append switch-def (list :height clnl-gltk:*switch-height*)) switch nil)))
499 (defun initialize (&key dims view buttons switches)
500 "INITIALIZE &key DIMS VIEW BUTTONS SWITCHES => RESULT
502 DIMS: (:xmin XMIN :xmax XMAX :ymin YMIN :ymax YMAX :patch-size PATCH-SIZE)
503 VIEW: (:left LEFT :top TOP)
505 SWITCHES: SWITCH-DEF*
506 BUTTON-DEF: (:left LEFT :top TOP :height HEIGHT :width WIDTH :forever FOREVER :display DISPLAY)
507 SWITCH-DEF: (:left LEFT :top TOP :width WIDTH :var VAR :display DISPLAY :initial-value INITIAL-VALUE)
509 ARGUMENTS AND VALUES:
512 XMIN: An integer representing the minimum patch coord in X
513 XMAX: An integer representing the maximum patch coord in X
514 YMIN: An integer representing the minimum patch coord in Y
515 YMAX: An integer representing the maximum patch coord in Y
516 PATCH-SIZE: A double representing the size of the patches in pixels
517 HEIGHT: An integer representing height
518 FOREVER: A boolean representing the forever status
519 LEFT: An integer representing the left position
520 TOP: An integer representing the top position
521 WIDTH: An integer representing width
522 VAR: A string representing the variable name
523 DISPLAY: A string representing display name
524 INITIAL-VALUE: The initial value
528 This is where the initialization of the interface that sits behind
529 the interface lives. From here, one can go into headless or running
530 mode, but for certain things this interface will still need to act,
531 and also allows for bringing up and taking down of visual elements."
532 (boot-interface-thread)
533 (setf *dimensions* (append dims view))
536 (button-defs->buttons buttons)
537 (switch-defs->switches switches))))
542 ARGUMENTS AND VALUES:
544 RESULT: undefined, should never get here
548 RUN runs the view in an external window.
550 This should be run inside another thread as it starts the glut main-loop.
551 Closing this window will then cause the entire program to terminate."
552 ; I do this because I don't know who or what in the many layers
553 ; is causing the floating point errors, but I definitely don't
554 ; want to investigate until simply ignoring them becomes a problem.
555 (sb-int:with-float-traps-masked (:invalid :inexact :divide-by-zero :overflow :underflow)
557 (cl-glut:init-window-size *window-width* *window-height*)
558 (cl-glut:init-display-mode :double :rgba)
559 (cl-glut:create-window "CLNL Test Window")
560 (setf *glut-window-opened* t)
561 (gl:clear-color 0 0 0 1)
562 (cl-glut:display-func (cffi:get-callback 'display))
563 (glut:reshape-func (cffi:callback reshape))
564 (cl-glut:idle-func (cffi:get-callback 'idle))
565 (cl-glut:close-func (cffi:get-callback 'close-func))
566 (cl-glut:keyboard-func (cffi:get-callback 'key-pressed))
567 (cl-glut:special-func (cffi:get-callback 'special-key-pressed))
568 (cl-glut:motion-func (cffi:get-callback 'motion)) ; while mouse is down
569 (cl-glut:passive-motion-func (cffi:get-callback 'motion)) ; while mouse is up
570 (cl-glut:mouse-func (cffi:get-callback 'mouse)) ; state is up/down, button is button
571 (gl:depth-func :lequal)
572 (gl:blend-func :src-alpha :one-minus-src-alpha)
579 5 (+ clnl-gltk:*font-height* 14)
581 (format nil *initial-banner* (asdf:component-version (asdf:find-system :clnl)))))
582 (setf *inputbox* (clnl-gltk:inputbox 5 5 10))
583 (cl-glut:main-loop)))
585 (defun patch-size () (getf *dimensions* :patch-size))
587 (defun world-width-in-pixels ()
588 (floor (* (patch-size) (1+ (- (getf *dimensions* :xmax) (getf *dimensions* :xmin))))))
590 (defun world-height-in-pixels ()
591 (floor (* (patch-size) (1+ (- (getf *dimensions* :ymax) (getf *dimensions* :ymin))))))
593 (defun export-view ()
594 "EXPORT-VIEW => IMAGE-DATA
596 ARGUMENTS AND VALUES:
598 IMAGE-DATA: A vector, pixel data as returned by opengls readPixels
602 EXPORT-VIEW returns the current view in raw data of RGBA pixels.
604 Each pixel is made up of 4 bytes of data, which an be walked over. The number
605 of pixels is the current width x height. Converting to some other image format
606 is a matter of pulling that information out and putting it into whatever format
609 This requires opengl to run, but can be used with xvfb in a headless mode."
610 (sb-int:with-float-traps-masked (:invalid)
611 (when (not *glut-window-opened*)
613 (cl-glut:init-window-size 1 1)
614 (cl-glut:create-window "CLNL Test Window")
615 (gl:clear-color 0 0 0 1)
618 (setf *glut-window-opened* t))
620 ((fbo (first (gl:gen-framebuffers 1)))
621 (render-buf (first (gl:gen-renderbuffers 1)))
622 (width (world-width-in-pixels))
623 (height (world-height-in-pixels)))
624 (gl:bind-framebuffer :framebuffer fbo)
625 (gl:bind-renderbuffer :renderbuffer render-buf)
626 (gl:renderbuffer-storage :renderbuffer :rgba8 width height)
627 (gl:framebuffer-renderbuffer :draw-framebuffer :color-attachment0 :renderbuffer render-buf)
628 (gl:viewport 0 0 width height)
630 (gl:read-pixels 0 0 width height :rgba :unsigned-byte))))