aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/canvas-language.lisp
blob: b77ad02fb366817a48829ce2b0a502ffff59c363 (plain)
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
;;;; canvas-language.lisp  -- a drawing api for canvas instances

(in-package #:wheelwork)

(defvar *current-canvas* nil
  "Bound by with-canvas")
(defvar *current-pen-color* '(0 0 0 255))
(defvar *current-pen-color-function* nil)

(defvar *current-pen-width* 1)

(defvar *current-pen-position* '(0 0))



(defmacro with-canvas (canvas &body body)
  `(let ((*current-canvas* ,canvas)
         (*current-pen-width* 1)
         (*current-pen-position* (list 0 0))
         (*current-pen-color* (list 0 0 0 255))
         (*current-pen-color-function* nil))
     ,@body))

(defun colfix (c)
  (round (clamp 0 c 255)))

(defun canvas-pen-color (r g b a)
  (setf *current-pen-color* (mapcar #'colfix (list r g b a))))

(defun canvas-pen-color-function (fn)
  (setf *current-pen-color-function*
        (lambda (x y)
          (mapcar #'colfix (funcall fn x y)))))

(defun canvas-pen-width (n)
  (setf *current-pen-width* (round n)))

(defmacro with-pen-color ((r g b a) &body body)
  `(let ((ww::*current-pen-color* nil))
     (canvas-pen-color ,r ,g ,b ,a)
     ,@body))

(defun can-fill-canvas-at-p (x y)
  (with-slots (pixel-width pixel-height) *current-canvas*
    (and (< -1 x pixel-width)
         (< -1 y pixel-height))))

(defun rel-to-current-pos (path)
  (destructuring-bind (cx cy) *current-pen-position* 
    (loop for (x y) in path collect (list (+ cx x) (+ cy y)))))

(defun move-to (x y)
  (setf *current-pen-position* (list x y)))

(defun move-rel (dx dy)
  (setf *current-pen-position*
        (mapcar #'+ *current-pen-position* (list dx dy))))

(defun apply-pen-at (x y)
  (let ((w
          (max 0 (floor (* 0.5 *current-pen-width*)))))
    (destructuring-bind
        (cr cg cb ca)
        (if *current-pen-color-function*
            (funcall *current-pen-color-function* x y)
            *current-pen-color*) 
      (with-grid-rect (rx ry) ((- x w) (- y w) (+ x w) (+ y w))
        (when (can-fill-canvas-at-p rx ry) 
          (with-pixel (r g b a) (pixel *current-canvas* rx ry)
            (setf r cr g cg b cb a ca)))))))

(defun stroke-to (ex ey)
  (destructuring-bind (sx sy) *current-pen-position* 
    (with-grid-line (x y) (sx sy) (ex ey)
      (apply-pen-at x y)))
  (setf *current-pen-position* (list ex ey)))

(defun stroke-rel (dx dy)
  (apply #'stroke-to (mapcar #'+ *current-pen-position* (list dx dy))))

(defun stroke-path (path)
  (with-grid-path (x y) (path) (apply-pen-at x y))
  (setf *current-pen-position* (copy-list (first (last path)))))

(defun stroke-rel-path (path)
  (stroke-path
   (cons *current-pen-position*
         (rel-to-current-pos path))))

(defun fill-path (path)
  (let ((*current-pen-width* 1)) 
    (with-grid-path (x y) (path :interiorp t)
      (apply-pen-at x y))))

(defun fill-rel-path (path)
  (fill-path
   (cons *current-pen-position*
         (rel-to-current-pos path))))

(defun stroke-rect (left bottom right top)
  (stroke-path (list (list left bottom)
                     (list left top)
                     (list right top)
                     (list right bottom)
                     (list left bottom))))

(defun stroke-rel-rect (dx dy)
  (destructuring-bind (sx sy) *current-pen-position*
    (let ((left
            (if (plusp dx) sx (+ sx dx)))
          (right
            (if (plusp dx) (+ sx dx) sx))
          (bottom
            (if (plusp dy) sy (+ sx sy)))
          (top
            (if (plusp dy) (+ sy dy) sy)))
      (stroke-rect left right bottom top))))

(defun fill-rect (left bottom right top)
  (let ((*current-pen-width* 1))
    (with-grid-rect (x y) (left bottom right top)
      (apply-pen-at x y)))
  (setf *current-pen-position* (list left bottom)))

(defun fill-rel-rect (dx dy)
  (destructuring-bind (sx sy) *current-pen-position*
    (let ((left
            (if (plusp dx) sx (+ sx dx)))
          (right
            (if (plusp dx) (+ sx dx) sx))
          (bottom
            (if (plusp dy) sy (+ sx sy)))
          (top
            (if (plusp dy) (+ sy dy) sy)))
      (fill-rect left right bottom top))))

(defun stroke-bezier (control-pts &optional (curve-samples 10))
  (let (path)
    (with-grid-bezier (x y) (control-pts :count curve-samples)
      (push (list x y) path))
    (stroke-path path)))

(defun stroke-rel-bezier (rel-control-points &optional (curve-samples 10))
  (stroke-bezier (cons *current-pen-position*
                       (rel-to-current-pos rel-control-points))
                 curve-samples))