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))))
52 (defun stretched-image (imgs)
53 "Resize that image to 32x32 and convert it into a ppm."
55 (uxul-world::resize-bmp-blob (car imgs) 32 32)))
57 (defun annotated-image (img ann)
58 "Add a (lower-left) annotation."
59 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;FIXME
62 ;; (lisp-magick:with-magick-wand (mywand)
63 ;; (lisp-magick::magick-read-image-blob mywand img)
64 ;; (lisp-magick:with-drawing-wand (dw)
65 ;; (lisp-magick:with-pixel-wand (pw :comp (255 255 255))
66 ;; (lisp-magick::draw-set-text-under-color dw pw))
67 ;; (lisp-magick:with-pixel-wand (pw :comp (255 0 0))
68 ;; (lisp-magick::draw-set-fill-color dw pw))
69 ;; (lisp-magick:draw-annotation dw (coerce 0 'double-float) (coerce 32 'double-float) ann)
70 ;; (lisp-magick:magick-draw-image mywand dw))
71 ;; (lisp-magick::magick-set-format mywand "gif")
72 ;; (lisp-magick::magick-get-image-blob mywand)))
75 (defun numbered-image (img num)
76 "Annotate the image with a number."
77 (annotated-image img (format nil "~d" num)))
79 (defun prepare-images (&optional (care-about-initialization *leveleditor-images*))
80 (when (not care-about-initialization)
81 (setf *leveleditor-images* (make-hash-table))
82 (uxul-world::init-files)
83 (setf (gethash 'uxul-world::uxul *leveleditor-images*) (stretched-image uxul-world::|uxul_small1|))
84 (setf (gethash 'uxul-world::leaf *leveleditor-images*) (stretched-image uxul-world::|leaf|))
85 (setf (gethash 'uxul-world::nasobem *leveleditor-images*) (stretched-image uxul-world::|nasobem|))
86 (setf (gethash 'uxul-world::blue-nasobem *leveleditor-images*) (stretched-image uxul-world::|blue_nasobem|))
87 (setf (gethash 'uxul-world::burning-marshmallow *leveleditor-images*) (stretched-image uxul-world::|burning_marshmallow_ld1|))
88 (setf (gethash 'uxul-world::gray-stone *leveleditor-images*) (stretched-image uxul-world::|gray_stone|))
89 (setf (gethash 'uxul-world::brown-stone *leveleditor-images*) (stretched-image uxul-world::|brown_stone|))
90 (setf (gethash 'uxul-world::empty *leveleditor-images*) (stretched-image uxul-world::|empty|))
91 (setf (gethash 'uxul-world::tulip *leveleditor-images*) (stretched-image uxul-world::|tulip|))
92 (setf (gethash 'uxul-world::door *leveleditor-images*) (stretched-image uxul-world::|door|))
93 (setf (gethash 'uxul-world::key *leveleditor-images*) (stretched-image uxul-world::|key|))
94 (setf (gethash 'uxul-world::anchor *leveleditor-images*) (stretched-image uxul-world::|anchor|))
97 (defun load-image-into-tk (png)
98 "return a tkobject with this image"
99 (let ((name (ltk::create-name)))
100 (ltk:format-wish "set ~A [ image create photo -data \"~A\" ]"
101 name (base64-encode-byteseq png))
102 (make-instance 'ltk:tkobject :name name)))
104 (defun config-button-image (button tkobject)
105 (ltk:format-wish "~A configure -image $~A" (ltk::widget-path button) (ltk::name tkobject)))
107 (defun item-table-to-list (item-table)
108 "Special Function for level-editor. Returns a list of lists of the
109 form (x y object arg1 arg2 ...)."
111 (maphash #'(lambda (key val)
113 (push (concatenate 'list (list (car key) (cdr key)) val) ret)))
117 (defun level-editor (&optional (level nil) (width 16) (height 16))
118 ;; hack. swap "width" and "height". (too lazy to change it properly by now)
119 (let ((nilpferd width))
121 (setf height nilpferd))
124 (let ((item-table (make-hash-table :test 'equal)))
125 ;;initialize given level
127 (setf (gethash (cons (car item) (cadr item)) item-table) (cddr item)))
130 ((uxul (load-image-into-tk (gethash 'uxul-world::uxul *leveleditor-images*)))
131 (leaf (load-image-into-tk (gethash 'uxul-world::leaf *leveleditor-images*)))
132 (nasobem (load-image-into-tk (gethash 'uxul-world::nasobem *leveleditor-images*)))
133 (blue-nasobem (load-image-into-tk (gethash 'uxul-world::blue-nasobem *leveleditor-images*)))
134 (burning-marshmallow (load-image-into-tk (gethash 'uxul-world::burning-marshmallow *leveleditor-images*)))
135 (gray-stone (load-image-into-tk (gethash 'uxul-world::gray-stone *leveleditor-images*)))
136 (brown-stone (load-image-into-tk (gethash 'uxul-world::brown-stone *leveleditor-images*)))
137 (empty (load-image-into-tk (gethash 'uxul-world::empty *leveleditor-images*)))
138 (tulip (load-image-into-tk (gethash 'uxul-world::tulip *leveleditor-images*)))
139 (key (load-image-into-tk (gethash 'uxul-world::key *leveleditor-images*)))
140 (door (load-image-into-tk (gethash 'uxul-world::door *leveleditor-images*)))
141 (anchor (load-image-into-tk (gethash 'uxul-world::anchor *leveleditor-images*)))
142 (anchors (make-hash-table :test 'equal))
143 (current-upper-left (cons 0 0))
144 (current-chosen-object 'uxul)
145 (objects-and-arrows (make-instance 'ltk:frame))
146 (grid-frame (make-instance 'ltk:frame))
147 (object-frame (make-instance 'ltk:frame :master objects-and-arrows))
148 (arrow-frame (make-instance 'ltk:frame :master objects-and-arrows))
149 (right-button (make-instance 'ltk:button :text ">"
150 :master arrow-frame))
151 (left-button (make-instance 'ltk:button :text "<"
152 :master arrow-frame))
153 (up-button (make-instance 'ltk:button :text "/\\"
154 :master arrow-frame))
155 (down-button (make-instance 'ltk:button :text "\\/"
156 :master arrow-frame))
157 (rright-button (make-instance 'ltk:button :text ">>"
158 :master arrow-frame))
159 (lleft-button (make-instance 'ltk:button :text "<<"
160 :master arrow-frame))
161 (uup-button (make-instance 'ltk:button :text "//\\\\"
162 :master arrow-frame))
163 (ddown-button (make-instance 'ltk:button :text "\\\\//"
164 :master arrow-frame))
165 (argument1-entry (make-instance 'ltk:entry :text ""
166 :master object-frame))
167 (argument2-entry (make-instance 'ltk:entry :text ""
168 :master object-frame))
169 (uxul-button (make-instance 'ltk:button :text ""
170 :master object-frame))
171 (nasobem-button (make-instance 'ltk:button :text ""
172 :master object-frame))
173 (blue-nasobem-button (make-instance 'ltk:button :text ""
174 :master object-frame))
175 (burning-marshmallow-button (make-instance 'ltk:button :text ""
176 :master object-frame))
177 (gray-stone-button (make-instance 'ltk:button :text ""
178 :master object-frame))
179 (brown-stone-button (make-instance 'ltk:button :text ""
180 :master object-frame))
181 (empty-button (make-instance 'ltk:button :text ""
182 :master object-frame))
183 (tulip-button (make-instance 'ltk:button :text ""
184 :master object-frame))
185 (leaf-button (make-instance 'ltk:button :text ""
186 :master object-frame))
187 (key-button (make-instance 'ltk:button :text ""
188 :master object-frame))
189 (door-button (make-instance 'ltk:button :text ""
190 :master object-frame))
191 (anchor-button (make-instance 'ltk:button :text ""
192 :master object-frame))
193 (info-button (make-instance 'ltk:button :text "Info"
194 :master object-frame))
195 (btns (make-array (list width height) :adjustable nil :element-type 'ltk:button)))
196 (labels ((redraw-button (i j)
197 "Redraw Button (i, j)"
198 (let* ((current-upper-x (car current-upper-left))
199 (current-upper-y (cdr current-upper-left))
200 (cval (gethash (cons (+ i current-upper-x)
201 (+ j current-upper-y))
204 (cbtn (aref btns i j)))
207 (setf cval (car cval)))
209 ((eq cval 'uxul-world::leaf)
210 (config-button-image cbtn leaf))
211 ((eq cval 'uxul-world::nasobem)
212 (config-button-image cbtn nasobem))
213 ((eq cval 'uxul-world::blue-nasobem)
214 (config-button-image cbtn blue-nasobem))
215 ((eq cval 'uxul-world::burning-marshmallow)
216 (config-button-image cbtn burning-marshmallow))
217 ((eq cval 'uxul-world::gray-stone)
218 (config-button-image cbtn gray-stone))
219 ((eq cval 'uxul-world::brown-stone)
220 (config-button-image cbtn brown-stone))
222 (config-button-image cbtn empty))
223 ((eq cval 'uxul-world::tulip)
224 (config-button-image cbtn tulip))
225 ((eq cval 'uxul-world::door)
226 (config-button-image cbtn door))
227 ((eq cval 'uxul-world::key)
228 (config-button-image cbtn key))
229 ((eq cval 'uxul-world::anchor)
230 (config-button-image cbtn (gethash (cadr cval2) anchors)))
231 ((eq cval 'uxul-world::uxul)
232 (config-button-image cbtn uxul)))))
237 (redraw-button i j))))
239 (let ((current-upper-x (car current-upper-left))
240 (current-upper-y (cdr current-upper-left)))
242 ((eql current-chosen-object :info)
243 (let ((sym (gethash (cons (+ i current-upper-x)
244 (+ j current-upper-y))
248 (format nil (concatenate 'string
249 "Symbolname: \"" (symbol-name (car sym)) "\"~%"
250 "First argument: \"" (cadr sym) "\"~%"
251 "Second argument: \"" (caddr sym) "\"")))
252 (ltk:do-msg "There doesnt seem to be anything here."))
253 (return-from react)))
254 ((eql current-chosen-object 'uxul-world::anchor)
256 ((string= (ltk:text argument1-entry) "")
257 (ltk:do-msg "Please give an argument in the left textbox")
259 ((gethash (ltk:text argument1-entry) anchors nil)
260 (ltk:do-msg "Warning: You already set an
261 anchor with the same dungeon-name. Make
262 sure that you remove one of them. Behavior
263 is not specified in this case and may
266 (setf (gethash (ltk:text argument1-entry) anchors)
267 (load-image-into-tk (annotated-image (gethash 'uxul-world::anchor *leveleditor-images*) (ltk:text argument1-entry))))))))
268 (setf (gethash (cons (+ i current-upper-x)
269 (+ j current-upper-y))
271 (and current-chosen-object
272 (list current-chosen-object (ltk:text argument1-entry) (ltk:text argument2-entry))))
273 (redraw-button i j)))
274 (move-field-about (i j)
275 (let ((current-upper-y (car current-upper-left))
276 (current-upper-x (cdr current-upper-left)))
277 (setf current-upper-left (cons (+ i current-upper-y) (+ j current-upper-x))))
279 (ltk:pack grid-frame)
280 (ltk:grid arrow-frame 0 1)
281 (ltk:grid left-button 1 0)
282 (setf (ltk:command left-button) #'(lambda () (move-field-about 0 1)))
283 (ltk:grid lleft-button 2 0)
284 (setf (ltk:command lleft-button) #'(lambda () (move-field-about 0 (- width 1))))
285 (ltk:grid right-button 1 2)
286 (setf (ltk:command right-button) #'(lambda () (move-field-about 0 -1)))
287 (ltk:grid rright-button 0 2)
288 (setf (ltk:command rright-button) #'(lambda () (move-field-about 0 (- 1 width))))
289 (ltk:grid up-button 0 1)
290 (setf (ltk:command up-button) #'(lambda () (move-field-about 1 0)))
291 (ltk:grid uup-button 0 0)
292 (setf (ltk:command uup-button) #'(lambda () (move-field-about (- width 1) 0)))
293 (ltk:grid down-button 2 1)
294 (setf (ltk:command down-button) #'(lambda () (move-field-about -1 0)))
295 (ltk:grid ddown-button 2 2)
296 (setf (ltk:command ddown-button) #'(lambda () (move-field-about (- 1 width) 0)))
298 (ltk:grid argument1-entry 1 0 :columnspan 4)
299 (ltk:grid argument2-entry 1 5 :columnspan 4)
300 (ltk:grid info-button 1 9 :columnspan 2)
301 (setf (ltk:command info-button)
302 #'(lambda () (setf current-chosen-object :info)))
304 (ltk:grid empty-button 0 0)
305 (config-button-image empty-button empty)
306 (setf (ltk:command empty-button)
308 (setf current-chosen-object nil)))
309 (ltk:grid uxul-button 0 1)
310 (config-button-image uxul-button uxul)
311 (setf (ltk:command uxul-button)
313 (setf current-chosen-object 'uxul-world::uxul)))
314 (ltk:grid nasobem-button 0 2)
315 (config-button-image nasobem-button nasobem)
316 (setf (ltk:command nasobem-button)
318 (setf current-chosen-object 'uxul-world::nasobem)))
319 (ltk:grid blue-nasobem-button 0 3)
320 (config-button-image blue-nasobem-button blue-nasobem)
321 (setf (ltk:command blue-nasobem-button)
323 (setf current-chosen-object 'uxul-world::blue-nasobem)))
324 (ltk:grid burning-marshmallow-button 0 4)
325 (config-button-image burning-marshmallow-button burning-marshmallow)
326 (setf (ltk:command burning-marshmallow-button)
328 (setf current-chosen-object 'uxul-world::burning-marshmallow)))
329 (ltk:grid gray-stone-button 0 5)
330 (config-button-image gray-stone-button gray-stone)
331 (setf (ltk:command gray-stone-button)
333 (setf current-chosen-object 'uxul-world::gray-stone)))
334 (ltk:grid brown-stone-button 0 6)
335 (config-button-image brown-stone-button brown-stone)
336 (setf (ltk:command brown-stone-button)
338 (setf current-chosen-object 'uxul-world::brown-stone)))
339 (ltk:grid leaf-button 0 7)
340 (config-button-image leaf-button leaf)
341 (setf (ltk:command leaf-button)
343 (setf current-chosen-object 'uxul-world::leaf)))
345 (ltk:grid tulip-button 0 8)
346 (config-button-image tulip-button tulip)
347 (setf (ltk:command tulip-button)
349 (setf current-chosen-object 'uxul-world::tulip)))
351 (ltk:grid key-button 0 9)
352 (config-button-image key-button key)
353 (setf (ltk:command key-button)
355 (setf current-chosen-object 'uxul-world::key)))
357 (ltk:grid door-button 0 10)
358 (config-button-image door-button door)
359 (setf (ltk:command door-button)
361 (setf current-chosen-object 'uxul-world::door)))
363 (ltk:grid anchor-button 0 11)
364 (config-button-image anchor-button anchor)
365 (setf (ltk:command anchor-button)
367 (setf current-chosen-object 'uxul-world::anchor)))
369 (ltk:grid object-frame 0 0)
370 (ltk:pack objects-and-arrows)
375 (make-instance 'ltk:button
378 (setf (ltk:command cbtn) (let ((i i) (j j)) #'(lambda () (react i j))))
379 (config-button-image cbtn empty)
380 (setf (aref btns i j) cbtn)
381 (ltk:grid cbtn i j))))
383 (item-table-to-list item-table)))
386 (defun get-base64-char-for-number (i)
387 (declare (type (integer 0 63) i))
388 (elt "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" i))
390 (defun base64-encode-threebytes (byte1 byte2 byte3)
391 (declare (type (unsigned-byte 8) byte1 byte2 byte3))
394 (get-base64-char-for-number (logand #b111111 (ash byte1 -2)))
395 (get-base64-char-for-number (logand #b111111 (+ (ash (ash byte1 6) -2) (ash byte2 -4))))
396 (get-base64-char-for-number (logand #b111111 (+ (ash (ash byte2 4) -2) (ash byte3 -6))))
397 (get-base64-char-for-number (logand #b111111 (ash (ash byte3 2) -2)))) 'string))
400 (defun base64-encode-bytelist (bytelist &optional (ret ""))
404 (base64-encode-bytelist
408 (base64-encode-threebytes
412 ;;else (genau zwei elemente)
413 (concatenate 'string ret
414 (base64-encode-threebytes
419 ;;else (genau ein element)
420 (concatenate 'string ret
421 (base64-encode-threebytes
424 ;;else (kein element)
428 (defun base64-encode-byteseq (byteseq &optional (ret ""))
429 (case (length byteseq)
431 (1 (concatenate 'string ret
432 (base64-encode-threebytes
433 (elt byteseq 0) 0 0) "=="))
434 (2 (concatenate 'string ret
435 (base64-encode-threebytes
440 (t (base64-encode-byteseq
444 (base64-encode-threebytes
447 (elt byteseq 2)))))))