1 ;;; Copyright 2009 Christoph Senjak
3 (in-package :uxul-world-leveleditor)
5 (defparameter *leveleditor-images* nil)
7 (defun stretched-image (img)
8 "Call ImageMagick to resize that file to 32x32."
9 (lisp-magick:with-magick-wand (mywand)
10 (lisp-magick::magick-read-image-blob mywand img)
11 (lisp-magick::magick-resize-image mywand 32 32 #x00000000 1d0)
12 (lisp-magick::magick-set-format mywand "gif")
13 (lisp-magick::magick-get-image-blob mywand)))
15 (defun annotated-image (img ann)
16 "Add a (lower-left) annotation."
17 (lisp-magick:with-magick-wand (mywand)
18 (lisp-magick::magick-read-image-blob mywand img)
19 (lisp-magick:with-drawing-wand (dw)
20 (lisp-magick:with-pixel-wand (pw :comp (255 255 255))
21 (lisp-magick::draw-set-text-under-color dw pw))
22 (lisp-magick:with-pixel-wand (pw :comp (255 0 0))
23 (lisp-magick::draw-set-fill-color dw pw))
24 (lisp-magick:draw-annotation dw (coerce 0 'double-float) (coerce 32 'double-float) ann)
25 (lisp-magick:magick-draw-image mywand dw))
26 (lisp-magick::magick-set-format mywand "gif")
27 (lisp-magick::magick-get-image-blob mywand)))
29 (defun numbered-image (img num)
30 "Annotate the image with a number."
31 (annotated-image img (format nil "~d" num)))
33 (defun prepare-images (&optional (care-about-initialization *leveleditor-images*))
34 (when (not care-about-initialization)
35 (setf *leveleditor-images* (make-hash-table))
36 (setf (gethash 'uxul-world::uxul *leveleditor-images*) (stretched-image uxul-world::|uxul_small1|))
37 (setf (gethash 'uxul-world::leaf *leveleditor-images*) (stretched-image uxul-world::|leaf|))
38 (setf (gethash 'uxul-world::nasobem *leveleditor-images*) (stretched-image uxul-world::|nasobem|))
39 (setf (gethash 'uxul-world::blue-nasobem *leveleditor-images*) (stretched-image uxul-world::|blue_nasobem|))
40 (setf (gethash 'uxul-world::burning-marshmallow *leveleditor-images*) (stretched-image uxul-world::|burning_marshmallow_ld1|))
41 (setf (gethash 'uxul-world::gray-stone *leveleditor-images*) (stretched-image uxul-world::|gray_stone|))
42 (setf (gethash 'uxul-world::brown-stone *leveleditor-images*) (stretched-image uxul-world::|brown_stone|))
43 (setf (gethash 'uxul-world::empty *leveleditor-images*) (stretched-image uxul-world::|empty|))
44 (setf (gethash 'uxul-world::tulip *leveleditor-images*) (stretched-image uxul-world::|tulip|))
45 (setf (gethash 'uxul-world::door *leveleditor-images*) (stretched-image uxul-world::|door|))
46 (setf (gethash 'uxul-world::key *leveleditor-images*) (stretched-image uxul-world::|key|))
49 (defun load-image-into-tk (png)
50 "return a tkobject with this image"
51 (let ((name (ltk::create-name)))
52 (ltk:format-wish "set ~A [ image create photo -data \"~A\" ]"
53 name (base64-encode-byteseq png))
54 (make-instance 'ltk:tkobject :name name)))
56 (defun config-button-image (button tkobject)
57 (ltk:format-wish "~A configure -image $~A" (ltk::widget-path button) (ltk::name tkobject)))
59 (defun item-table-to-list (item-table)
60 "Special Function for level-editor. Returns a list of lists of the
61 form (x y object arg1 arg2 ...)."
63 (maphash #'(lambda (key val)
65 (push (concatenate 'list (list (car key) (cdr key)) val) ret)))
69 (defun level-editor (&optional (level nil) (width 16) (height 16))
71 (let ((item-table (make-hash-table :test 'equal)))
72 ;;initialize given level
74 (setf (gethash (cons (car item) (cadr item)) item-table) (cddr item)))
78 ((uxul (load-image-into-tk (gethash 'uxul-world::uxul *leveleditor-images*)))
79 (leaf (load-image-into-tk (gethash 'uxul-world::leaf *leveleditor-images*)))
80 (nasobem (load-image-into-tk (gethash 'uxul-world::nasobem *leveleditor-images*)))
81 (blue-nasobem (load-image-into-tk (gethash 'uxul-world::blue-nasobem *leveleditor-images*)))
82 (burning-marshmallow (load-image-into-tk (gethash 'uxul-world::burning-marshmallow *leveleditor-images*)))
83 (gray-stone (load-image-into-tk (gethash 'uxul-world::gray-stone *leveleditor-images*)))
84 (brown-stone (load-image-into-tk (gethash 'uxul-world::brown-stone *leveleditor-images*)))
85 (empty (load-image-into-tk (gethash 'uxul-world::empty *leveleditor-images*)))
86 (tulip (load-image-into-tk (gethash 'uxul-world::tulip *leveleditor-images*)))
87 (key (load-image-into-tk (gethash 'uxul-world::key *leveleditor-images*)))
88 (door (load-image-into-tk (gethash 'uxul-world::door *leveleditor-images*)))
89 (current-upper-left (cons 0 0))
90 (current-chosen-object 'uxul)
91 (objects-and-arrows (make-instance 'ltk:frame))
92 (grid-frame (make-instance 'ltk:frame))
93 (object-frame (make-instance 'ltk:frame :master objects-and-arrows))
94 (arrow-frame (make-instance 'ltk:frame :master objects-and-arrows))
95 (right-button (make-instance 'ltk:button :text ">"
97 (left-button (make-instance 'ltk:button :text "<"
99 (up-button (make-instance 'ltk:button :text "/\\"
100 :master arrow-frame))
101 (down-button (make-instance 'ltk:button :text "\\/"
102 :master arrow-frame))
103 (rright-button (make-instance 'ltk:button :text ">>"
104 :master arrow-frame))
105 (lleft-button (make-instance 'ltk:button :text "<<"
106 :master arrow-frame))
107 (uup-button (make-instance 'ltk:button :text "//\\\\"
108 :master arrow-frame))
109 (ddown-button (make-instance 'ltk:button :text "\\\\//"
110 :master arrow-frame))
111 (argument1-entry (make-instance 'ltk:entry :text ""
112 :master object-frame))
113 (argument2-entry (make-instance 'ltk:entry :text ""
114 :master object-frame))
115 (uxul-button (make-instance 'ltk:button :text ""
116 :master object-frame))
117 (nasobem-button (make-instance 'ltk:button :text ""
118 :master object-frame))
119 (blue-nasobem-button (make-instance 'ltk:button :text ""
120 :master object-frame))
121 (burning-marshmallow-button (make-instance 'ltk:button :text ""
122 :master object-frame))
123 (gray-stone-button (make-instance 'ltk:button :text ""
124 :master object-frame))
125 (brown-stone-button (make-instance 'ltk:button :text ""
126 :master object-frame))
127 (empty-button (make-instance 'ltk:button :text ""
128 :master object-frame))
129 (tulip-button (make-instance 'ltk:button :text ""
130 :master object-frame))
131 (leaf-button (make-instance 'ltk:button :text ""
132 :master object-frame))
133 (key-button (make-instance 'ltk:button :text ""
134 :master object-frame))
135 (door-button (make-instance 'ltk:button :text ""
136 :master object-frame))
137 (btns (make-array (list width height) :adjustable nil :element-type 'ltk:button)))
138 (labels ((redraw-button (i j)
139 "Redraw Button (i, j)"
140 (let* ((current-upper-x (car current-upper-left))
141 (current-upper-y (cdr current-upper-left))
142 (cval (gethash (cons (+ i current-upper-x)
143 (+ j current-upper-y))
145 (cbtn (aref btns i j)))
147 (setf cval (car cval)))
149 ((eq cval 'uxul-world::leaf)
150 (config-button-image cbtn leaf))
151 ((eq cval 'uxul-world::nasobem)
152 (config-button-image cbtn nasobem))
153 ((eq cval 'uxul-world::blue-nasobem)
154 (config-button-image cbtn blue-nasobem))
155 ((eq cval 'uxul-world::burning-marshmallow)
156 (config-button-image cbtn burning-marshmallow))
157 ((eq cval 'uxul-world::gray-stone)
158 (config-button-image cbtn gray-stone))
159 ((eq cval 'uxul-world::brown-stone)
160 (config-button-image cbtn brown-stone))
162 (config-button-image cbtn empty))
163 ((eq cval 'uxul-world::tulip)
164 (config-button-image cbtn tulip))
165 ((eq cval 'uxul-world::door)
166 (config-button-image cbtn door))
167 ((eq cval 'uxul-world::key)
168 (config-button-image cbtn key))
169 ((eq cval 'uxul-world::uxul)
170 (config-button-image cbtn uxul)))))
175 (redraw-button i j))))
177 (let ((current-upper-x (car current-upper-left))
178 (current-upper-y (cdr current-upper-left)))
179 (setf (gethash (cons (+ i current-upper-x)
180 (+ j current-upper-y))
182 (and current-chosen-object
183 (list current-chosen-object (ltk:text argument1-entry) (ltk:text argument2-entry))))
184 (redraw-button i j)))
185 (move-field-about (i j)
186 (let ((current-upper-y (car current-upper-left))
187 (current-upper-x (cdr current-upper-left)))
188 (setf current-upper-left (cons (+ i current-upper-y) (+ j current-upper-x))))
190 (ltk:pack grid-frame)
191 (ltk:grid arrow-frame 0 1)
192 (ltk:grid left-button 1 0)
193 (setf (ltk:command left-button) #'(lambda () (move-field-about 0 1)))
194 (ltk:grid lleft-button 2 0)
195 (setf (ltk:command lleft-button) #'(lambda () (move-field-about 0 (- width 1))))
196 (ltk:grid right-button 1 2)
197 (setf (ltk:command right-button) #'(lambda () (move-field-about 0 -1)))
198 (ltk:grid rright-button 0 2)
199 (setf (ltk:command rright-button) #'(lambda () (move-field-about 0 (- 1 width))))
200 (ltk:grid up-button 0 1)
201 (setf (ltk:command up-button) #'(lambda () (move-field-about 1 0)))
202 (ltk:grid uup-button 0 0)
203 (setf (ltk:command uup-button) #'(lambda () (move-field-about (- width 1) 0)))
204 (ltk:grid down-button 2 1)
205 (setf (ltk:command down-button) #'(lambda () (move-field-about -1 0)))
206 (ltk:grid ddown-button 2 2)
207 (setf (ltk:command ddown-button) #'(lambda () (move-field-about (- 1 width) 0)))
209 (ltk:grid argument1-entry 1 0 :columnspan 5)
210 (ltk:grid argument2-entry 1 6 :columnspan 5)
212 (ltk:grid empty-button 0 0)
213 (config-button-image empty-button empty)
214 (setf (ltk:command empty-button)
216 (setf current-chosen-object nil)))
217 (ltk:grid uxul-button 0 1)
218 (config-button-image uxul-button uxul)
219 (setf (ltk:command uxul-button)
221 (setf current-chosen-object 'uxul-world::uxul)))
222 (ltk:grid nasobem-button 0 2)
223 (config-button-image nasobem-button nasobem)
224 (setf (ltk:command nasobem-button)
226 (setf current-chosen-object 'uxul-world::nasobem)))
227 (ltk:grid blue-nasobem-button 0 3)
228 (config-button-image blue-nasobem-button blue-nasobem)
229 (setf (ltk:command blue-nasobem-button)
231 (setf current-chosen-object 'uxul-world::blue-nasobem)))
232 (ltk:grid burning-marshmallow-button 0 4)
233 (config-button-image burning-marshmallow-button burning-marshmallow)
234 (setf (ltk:command burning-marshmallow-button)
236 (setf current-chosen-object 'uxul-world::burning-marshmallow)))
237 (ltk:grid gray-stone-button 0 5)
238 (config-button-image gray-stone-button gray-stone)
239 (setf (ltk:command gray-stone-button)
241 (setf current-chosen-object 'uxul-world::gray-stone)))
242 (ltk:grid brown-stone-button 0 6)
243 (config-button-image brown-stone-button brown-stone)
244 (setf (ltk:command brown-stone-button)
246 (setf current-chosen-object 'uxul-world::brown-stone)))
247 (ltk:grid leaf-button 0 7)
248 (config-button-image leaf-button leaf)
249 (setf (ltk:command leaf-button)
251 (setf current-chosen-object 'uxul-world::leaf)))
253 (ltk:grid tulip-button 0 8)
254 (config-button-image tulip-button tulip)
255 (setf (ltk:command tulip-button)
257 (setf current-chosen-object 'uxul-world::tulip)))
259 (ltk:grid key-button 0 9)
260 (config-button-image key-button key)
261 (setf (ltk:command key-button)
263 (setf current-chosen-object 'uxul-world::key)))
265 (ltk:grid door-button 0 10)
266 (config-button-image door-button door)
267 (setf (ltk:command door-button)
269 (setf current-chosen-object 'uxul-world::door)))
271 (ltk:grid object-frame 0 0)
272 (ltk:pack objects-and-arrows)
277 (make-instance 'ltk:button
280 (setf (ltk:command cbtn) (let ((i i) (j j)) #'(lambda () (react i j))))
281 (config-button-image cbtn empty)
282 (setf (aref btns i j) cbtn)
283 (ltk:grid cbtn i j))))
285 (item-table-to-list item-table)))
288 (defun get-base64-char-for-number (i)
289 (declare (type (integer 0 63) i))
290 (elt "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" i))
292 (defun base64-encode-threebytes (byte1 byte2 byte3)
293 (declare (type (unsigned-byte 8) byte1 byte2 byte3))
296 (get-base64-char-for-number (logand #b111111 (ash byte1 -2)))
297 (get-base64-char-for-number (logand #b111111 (+ (ash (ash byte1 6) -2) (ash byte2 -4))))
298 (get-base64-char-for-number (logand #b111111 (+ (ash (ash byte2 4) -2) (ash byte3 -6))))
299 (get-base64-char-for-number (logand #b111111 (ash (ash byte3 2) -2)))) 'string))
302 (defun base64-encode-bytelist (bytelist &optional (ret ""))
306 (base64-encode-bytelist
310 (base64-encode-threebytes
314 ;;else (genau zwei elemente)
315 (concatenate 'string ret
316 (base64-encode-threebytes
321 ;;else (genau ein element)
322 (concatenate 'string ret
323 (base64-encode-threebytes
326 ;;else (kein element)
330 (defun base64-encode-byteseq (byteseq &optional (ret ""))
331 (case (length byteseq)
333 (1 (concatenate 'string ret
334 (base64-encode-threebytes
335 (elt byteseq 0) 0 0) "=="))
336 (2 (concatenate 'string ret
337 (base64-encode-threebytes
342 (t (base64-encode-byteseq
346 (base64-encode-threebytes
349 (elt byteseq 2)))))))