1 ;;; Copyright 2009-2011 Christoph Senjak
3 (in-package :uxul-world-leveleditor)
5 (defparameter *leveleditor-images* nil)
7 (defun bmp-to-gif (inblob)
8 "Convert BMP to Grayscale-Gif using skippy."
10 ((w (uxul-world::bmp-width inblob))
11 (h (uxul-world::bmp-height inblob))
12 (img (uxul-world::bmp-pixel-data inblob))
13 (color-table (skippy:make-color-table))
16 ((v (make-array '(256) :adjustable nil)))
19 (skippy:ensure-color (skippy:rgb-color i i i) color-table)))
21 (grayscale (lambda (x)
22 ;; 0 becomes transparent
27 (/ (+ (elt x 1) (elt x 2) (elt x 0)) 3)))))))
29 (let ((id (skippy:make-image-data w h)))
32 (setf (elt id (+ i (* w (- h j 1))))
36 :element-type '(unsigned-byte 8)
38 :displaced-index-offset
39 (* 4 (+ i (* w j)))))))))
41 (image (skippy:make-image :width w :height h
42 :image-data image-data
43 :transparency-index 0))
44 (data-stream (skippy:make-data-stream
46 :color-table color-table)))
47 (skippy:add-image image data-stream)
48 (flexi-streams:with-output-to-sequence (out)
49 (skippy:write-data-stream data-stream out))))
51 (defun stretched-image (imgs)
52 "Resize that image to 32x32 and convert it into a gif."
58 (*spritesheet* uxul-world::*spritesheet*)
59 (image (uxul-world::sub-image x y w h (car *spritesheet*)
61 (cadr *spritesheet*))))
63 (uxul-world::resize-bmp-blob image 32 32))))
65 (defun annotated-image (img ann)
66 "Add a (lower-left) annotation."
67 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;FIXME
70 ;; (lisp-magick:with-magick-wand (mywand)
71 ;; (lisp-magick::magick-read-image-blob mywand img)
72 ;; (lisp-magick:with-drawing-wand (dw)
73 ;; (lisp-magick:with-pixel-wand (pw :comp (255 255 255))
74 ;; (lisp-magick::draw-set-text-under-color dw pw))
75 ;; (lisp-magick:with-pixel-wand (pw :comp (255 0 0))
76 ;; (lisp-magick::draw-set-fill-color dw pw))
77 ;; (lisp-magick:draw-annotation dw (coerce 0 'double-float) (coerce 32 'double-float) ann)
78 ;; (lisp-magick:magick-draw-image mywand dw))
79 ;; (lisp-magick::magick-set-format mywand "gif")
80 ;; (lisp-magick::magick-get-image-blob mywand)))
83 (defun numbered-image (img num)
84 "Annotate the image with a number."
85 (annotated-image img (format nil "~d" num)))
87 (defun prepare-images (&optional (care-about-initialization *leveleditor-images*))
88 (when (not care-about-initialization)
89 (setf *leveleditor-images* (make-hash-table))
90 (uxul-world::init-files)
91 (setf (gethash 'uxul-world::uxul *leveleditor-images*) (stretched-image uxul-world::|uxul_small1|))
92 (setf (gethash 'uxul-world::leaf *leveleditor-images*) (stretched-image uxul-world::|leaf|))
93 (setf (gethash 'uxul-world::nasobem *leveleditor-images*) (stretched-image uxul-world::|nasobem|))
94 (setf (gethash 'uxul-world::blue-nasobem *leveleditor-images*) (stretched-image uxul-world::|blue_nasobem|))
95 (setf (gethash 'uxul-world::burning-marshmallow *leveleditor-images*) (stretched-image uxul-world::|burning_marshmallow_ld1|))
96 (setf (gethash 'uxul-world::gray-stone *leveleditor-images*) (stretched-image uxul-world::|gray_stone|))
97 (setf (gethash 'uxul-world::brown-stone *leveleditor-images*) (stretched-image uxul-world::|brown_stone|))
98 (setf (gethash 'uxul-world::empty *leveleditor-images*) (stretched-image uxul-world::|empty|))
99 (setf (gethash 'uxul-world::tulip *leveleditor-images*) (stretched-image uxul-world::|tulip|))
100 (setf (gethash 'uxul-world::door *leveleditor-images*) (stretched-image uxul-world::|door|))
101 (setf (gethash 'uxul-world::key *leveleditor-images*) (stretched-image uxul-world::|key|))
102 (setf (gethash 'uxul-world::anchor *leveleditor-images*) (stretched-image uxul-world::|anchor|))
105 (defun load-image-into-tk (png)
106 "return a tkobject with this image"
107 (let ((name (ltk::create-name)))
108 (ltk:format-wish "set ~A [ image create photo -data \"~A\" ]"
109 name (base64-encode-byteseq png))
110 (make-instance 'ltk:tkobject :name name)))
112 (defun config-button-image (button tkobject)
113 (ltk:format-wish "~A configure -image $~A" (ltk::widget-path button) (ltk::name tkobject)))
115 (defun item-table-to-list (item-table)
116 "Special Function for level-editor. Returns a list of lists of the
117 form (x y object arg1 arg2 ...)."
119 (maphash #'(lambda (key val)
121 (push (concatenate 'list (list (car key) (cdr key)) val) ret)))
125 (defun level-editor (&optional (level nil) (width 16) (height 16))
126 ;; hack. swap "width" and "height". (too lazy to change it properly by now)
127 (let ((nilpferd width))
129 (setf height nilpferd))
132 (let ((item-table (make-hash-table :test 'equal)))
133 ;;initialize given level
135 (setf (gethash (cons (car item) (cadr item)) item-table) (cddr item)))
138 ((uxul (load-image-into-tk (gethash 'uxul-world::uxul *leveleditor-images*)))
139 (leaf (load-image-into-tk (gethash 'uxul-world::leaf *leveleditor-images*)))
140 (nasobem (load-image-into-tk (gethash 'uxul-world::nasobem *leveleditor-images*)))
141 (blue-nasobem (load-image-into-tk (gethash 'uxul-world::blue-nasobem *leveleditor-images*)))
142 (burning-marshmallow (load-image-into-tk (gethash 'uxul-world::burning-marshmallow *leveleditor-images*)))
143 (gray-stone (load-image-into-tk (gethash 'uxul-world::gray-stone *leveleditor-images*)))
144 (brown-stone (load-image-into-tk (gethash 'uxul-world::brown-stone *leveleditor-images*)))
145 (empty (load-image-into-tk (gethash 'uxul-world::empty *leveleditor-images*)))
146 (tulip (load-image-into-tk (gethash 'uxul-world::tulip *leveleditor-images*)))
147 (key (load-image-into-tk (gethash 'uxul-world::key *leveleditor-images*)))
148 (door (load-image-into-tk (gethash 'uxul-world::door *leveleditor-images*)))
149 (anchor (load-image-into-tk (gethash 'uxul-world::anchor *leveleditor-images*)))
150 (anchors (make-hash-table :test 'equal))
151 (current-upper-left (cons 0 0))
152 (current-chosen-object 'uxul)
153 (objects-and-arrows (make-instance 'ltk:frame))
154 (grid-frame (make-instance 'ltk:frame))
155 (object-frame (make-instance 'ltk:frame :master objects-and-arrows))
156 (arrow-frame (make-instance 'ltk:frame :master objects-and-arrows))
157 (right-button (make-instance 'ltk:button :text ">"
158 :master arrow-frame))
159 (left-button (make-instance 'ltk:button :text "<"
160 :master arrow-frame))
161 (up-button (make-instance 'ltk:button :text "/\\"
162 :master arrow-frame))
163 (down-button (make-instance 'ltk:button :text "\\/"
164 :master arrow-frame))
165 (rright-button (make-instance 'ltk:button :text ">>"
166 :master arrow-frame))
167 (lleft-button (make-instance 'ltk:button :text "<<"
168 :master arrow-frame))
169 (uup-button (make-instance 'ltk:button :text "//\\\\"
170 :master arrow-frame))
171 (ddown-button (make-instance 'ltk:button :text "\\\\//"
172 :master arrow-frame))
173 (argument1-entry (make-instance 'ltk:entry :text ""
174 :master object-frame))
175 (argument2-entry (make-instance 'ltk:entry :text ""
176 :master object-frame))
177 (uxul-button (make-instance 'ltk:button :text ""
178 :master object-frame))
179 (nasobem-button (make-instance 'ltk:button :text ""
180 :master object-frame))
181 (blue-nasobem-button (make-instance 'ltk:button :text ""
182 :master object-frame))
183 (burning-marshmallow-button (make-instance 'ltk:button :text ""
184 :master object-frame))
185 (gray-stone-button (make-instance 'ltk:button :text ""
186 :master object-frame))
187 (brown-stone-button (make-instance 'ltk:button :text ""
188 :master object-frame))
189 (empty-button (make-instance 'ltk:button :text ""
190 :master object-frame))
191 (tulip-button (make-instance 'ltk:button :text ""
192 :master object-frame))
193 (leaf-button (make-instance 'ltk:button :text ""
194 :master object-frame))
195 (key-button (make-instance 'ltk:button :text ""
196 :master object-frame))
197 (door-button (make-instance 'ltk:button :text ""
198 :master object-frame))
199 (anchor-button (make-instance 'ltk:button :text ""
200 :master object-frame))
201 (info-button (make-instance 'ltk:button :text "Info"
202 :master object-frame))
203 (btns (make-array (list width height) :adjustable nil :element-type 'ltk:button)))
204 (labels ((redraw-button (i j)
205 "Redraw Button (i, j)"
206 (let* ((current-upper-x (car current-upper-left))
207 (current-upper-y (cdr current-upper-left))
208 (cval (gethash (cons (+ i current-upper-x)
209 (+ j current-upper-y))
212 (cbtn (aref btns i j)))
215 (setf cval (car cval)))
217 ((eq cval 'uxul-world::leaf)
218 (config-button-image cbtn leaf))
219 ((eq cval 'uxul-world::nasobem)
220 (config-button-image cbtn nasobem))
221 ((eq cval 'uxul-world::blue-nasobem)
222 (config-button-image cbtn blue-nasobem))
223 ((eq cval 'uxul-world::burning-marshmallow)
224 (config-button-image cbtn burning-marshmallow))
225 ((eq cval 'uxul-world::gray-stone)
226 (config-button-image cbtn gray-stone))
227 ((eq cval 'uxul-world::brown-stone)
228 (config-button-image cbtn brown-stone))
230 (config-button-image cbtn empty))
231 ((eq cval 'uxul-world::tulip)
232 (config-button-image cbtn tulip))
233 ((eq cval 'uxul-world::door)
234 (config-button-image cbtn door))
235 ((eq cval 'uxul-world::key)
236 (config-button-image cbtn key))
237 ((eq cval 'uxul-world::anchor)
238 (config-button-image cbtn (gethash (cadr cval2) anchors)))
239 ((eq cval 'uxul-world::uxul)
240 (config-button-image cbtn uxul)))))
245 (redraw-button i j))))
247 (let ((current-upper-x (car current-upper-left))
248 (current-upper-y (cdr current-upper-left)))
250 ((eql current-chosen-object :info)
251 (let ((sym (gethash (cons (+ i current-upper-x)
252 (+ j current-upper-y))
256 (format nil (concatenate 'string
257 "Symbolname: \"" (symbol-name (car sym)) "\"~%"
258 "First argument: \"" (cadr sym) "\"~%"
259 "Second argument: \"" (caddr sym) "\"")))
260 (ltk:do-msg "There doesnt seem to be anything here."))
261 (return-from react)))
262 ((eql current-chosen-object 'uxul-world::anchor)
264 ((string= (ltk:text argument1-entry) "")
265 (ltk:do-msg "Please give an argument in the left textbox")
267 ((gethash (ltk:text argument1-entry) anchors nil)
268 (ltk:do-msg "Warning: You already set an
269 anchor with the same dungeon-name. Make
270 sure that you remove one of them. Behavior
271 is not specified in this case and may
274 (setf (gethash (ltk:text argument1-entry) anchors)
275 (load-image-into-tk (annotated-image (gethash 'uxul-world::anchor *leveleditor-images*) (ltk:text argument1-entry))))))))
276 (setf (gethash (cons (+ i current-upper-x)
277 (+ j current-upper-y))
279 (and current-chosen-object
280 (list current-chosen-object (ltk:text argument1-entry) (ltk:text argument2-entry))))
281 (redraw-button i j)))
282 (move-field-about (i j)
283 (let ((current-upper-y (car current-upper-left))
284 (current-upper-x (cdr current-upper-left)))
285 (setf current-upper-left (cons (+ i current-upper-y) (+ j current-upper-x))))
287 (ltk:pack grid-frame)
288 (ltk:grid arrow-frame 0 1)
289 (ltk:grid left-button 1 0)
290 (setf (ltk:command left-button) #'(lambda () (move-field-about 0 1)))
291 (ltk:grid lleft-button 2 0)
292 (setf (ltk:command lleft-button) #'(lambda () (move-field-about 0 (- width 1))))
293 (ltk:grid right-button 1 2)
294 (setf (ltk:command right-button) #'(lambda () (move-field-about 0 -1)))
295 (ltk:grid rright-button 0 2)
296 (setf (ltk:command rright-button) #'(lambda () (move-field-about 0 (- 1 width))))
297 (ltk:grid up-button 0 1)
298 (setf (ltk:command up-button) #'(lambda () (move-field-about 1 0)))
299 (ltk:grid uup-button 0 0)
300 (setf (ltk:command uup-button) #'(lambda () (move-field-about (- width 1) 0)))
301 (ltk:grid down-button 2 1)
302 (setf (ltk:command down-button) #'(lambda () (move-field-about -1 0)))
303 (ltk:grid ddown-button 2 2)
304 (setf (ltk:command ddown-button) #'(lambda () (move-field-about (- 1 width) 0)))
306 (ltk:grid argument1-entry 1 0 :columnspan 4)
307 (ltk:grid argument2-entry 1 5 :columnspan 4)
308 (ltk:grid info-button 1 9 :columnspan 2)
309 (setf (ltk:command info-button)
310 #'(lambda () (setf current-chosen-object :info)))
312 (ltk:grid empty-button 0 0)
313 (config-button-image empty-button empty)
314 (setf (ltk:command empty-button)
316 (setf current-chosen-object nil)))
317 (ltk:grid uxul-button 0 1)
318 (config-button-image uxul-button uxul)
319 (setf (ltk:command uxul-button)
321 (setf current-chosen-object 'uxul-world::uxul)))
322 (ltk:grid nasobem-button 0 2)
323 (config-button-image nasobem-button nasobem)
324 (setf (ltk:command nasobem-button)
326 (setf current-chosen-object 'uxul-world::nasobem)))
327 (ltk:grid blue-nasobem-button 0 3)
328 (config-button-image blue-nasobem-button blue-nasobem)
329 (setf (ltk:command blue-nasobem-button)
331 (setf current-chosen-object 'uxul-world::blue-nasobem)))
332 (ltk:grid burning-marshmallow-button 0 4)
333 (config-button-image burning-marshmallow-button burning-marshmallow)
334 (setf (ltk:command burning-marshmallow-button)
336 (setf current-chosen-object 'uxul-world::burning-marshmallow)))
337 (ltk:grid gray-stone-button 0 5)
338 (config-button-image gray-stone-button gray-stone)
339 (setf (ltk:command gray-stone-button)
341 (setf current-chosen-object 'uxul-world::gray-stone)))
342 (ltk:grid brown-stone-button 0 6)
343 (config-button-image brown-stone-button brown-stone)
344 (setf (ltk:command brown-stone-button)
346 (setf current-chosen-object 'uxul-world::brown-stone)))
347 (ltk:grid leaf-button 0 7)
348 (config-button-image leaf-button leaf)
349 (setf (ltk:command leaf-button)
351 (setf current-chosen-object 'uxul-world::leaf)))
353 (ltk:grid tulip-button 0 8)
354 (config-button-image tulip-button tulip)
355 (setf (ltk:command tulip-button)
357 (setf current-chosen-object 'uxul-world::tulip)))
359 (ltk:grid key-button 0 9)
360 (config-button-image key-button key)
361 (setf (ltk:command key-button)
363 (setf current-chosen-object 'uxul-world::key)))
365 (ltk:grid door-button 0 10)
366 (config-button-image door-button door)
367 (setf (ltk:command door-button)
369 (setf current-chosen-object 'uxul-world::door)))
371 (ltk:grid anchor-button 0 11)
372 (config-button-image anchor-button anchor)
373 (setf (ltk:command anchor-button)
375 (setf current-chosen-object 'uxul-world::anchor)))
377 (ltk:grid object-frame 0 0)
378 (ltk:pack objects-and-arrows)
383 (make-instance 'ltk:button
386 (setf (ltk:command cbtn) (let ((i i) (j j)) #'(lambda () (react i j))))
387 (config-button-image cbtn empty)
388 (setf (aref btns i j) cbtn)
389 (ltk:grid cbtn i j))))
391 (item-table-to-list item-table)))
394 (defun get-base64-char-for-number (i)
395 (declare (type (integer 0 63) i))
396 (elt "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" i))
398 (defun base64-encode-threebytes (byte1 byte2 byte3)
399 (declare (type (unsigned-byte 8) byte1 byte2 byte3))
402 (get-base64-char-for-number (logand #b111111 (ash byte1 -2)))
403 (get-base64-char-for-number (logand #b111111 (+ (ash (ash byte1 6) -2) (ash byte2 -4))))
404 (get-base64-char-for-number (logand #b111111 (+ (ash (ash byte2 4) -2) (ash byte3 -6))))
405 (get-base64-char-for-number (logand #b111111 (ash (ash byte3 2) -2)))) 'string))
408 (defun base64-encode-bytelist (bytelist &optional (ret ""))
412 (base64-encode-bytelist
416 (base64-encode-threebytes
420 ;;else (genau zwei elemente)
421 (concatenate 'string ret
422 (base64-encode-threebytes
427 ;;else (genau ein element)
428 (concatenate 'string ret
429 (base64-encode-threebytes
432 ;;else (kein element)
436 (defun base64-encode-byteseq (byteseq &optional (ret ""))
437 (case (length byteseq)
439 (1 (concatenate 'string ret
440 (base64-encode-threebytes
441 (elt byteseq 0) 0 0) "=="))
442 (2 (concatenate 'string ret
443 (base64-encode-threebytes
448 (t (base64-encode-byteseq
452 (base64-encode-threebytes
455 (elt byteseq 2)))))))