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|))
47 (setf (gethash 'uxul-world::anchor *leveleditor-images*) (stretched-image uxul-world::|anchor|))
50 (defun load-image-into-tk (png)
51 "return a tkobject with this image"
52 (let ((name (ltk::create-name)))
53 (ltk:format-wish "set ~A [ image create photo -data \"~A\" ]"
54 name (base64-encode-byteseq png))
55 (make-instance 'ltk:tkobject :name name)))
57 (defun config-button-image (button tkobject)
58 (ltk:format-wish "~A configure -image $~A" (ltk::widget-path button) (ltk::name tkobject)))
60 (defun item-table-to-list (item-table)
61 "Special Function for level-editor. Returns a list of lists of the
62 form (x y object arg1 arg2 ...)."
64 (maphash #'(lambda (key val)
66 (push (concatenate 'list (list (car key) (cdr key)) val) ret)))
70 (defun level-editor (&optional (level nil) (width 16) (height 16))
71 ;; hack. swap "width" and "height". (too lazy to change it properly by now)
72 (let ((nilpferd width))
74 (setf height nilpferd))
77 (let ((item-table (make-hash-table :test 'equal)))
78 ;;initialize given level
80 (setf (gethash (cons (car item) (cadr item)) item-table) (cddr item)))
83 ((uxul (load-image-into-tk (gethash 'uxul-world::uxul *leveleditor-images*)))
84 (leaf (load-image-into-tk (gethash 'uxul-world::leaf *leveleditor-images*)))
85 (nasobem (load-image-into-tk (gethash 'uxul-world::nasobem *leveleditor-images*)))
86 (blue-nasobem (load-image-into-tk (gethash 'uxul-world::blue-nasobem *leveleditor-images*)))
87 (burning-marshmallow (load-image-into-tk (gethash 'uxul-world::burning-marshmallow *leveleditor-images*)))
88 (gray-stone (load-image-into-tk (gethash 'uxul-world::gray-stone *leveleditor-images*)))
89 (brown-stone (load-image-into-tk (gethash 'uxul-world::brown-stone *leveleditor-images*)))
90 (empty (load-image-into-tk (gethash 'uxul-world::empty *leveleditor-images*)))
91 (tulip (load-image-into-tk (gethash 'uxul-world::tulip *leveleditor-images*)))
92 (key (load-image-into-tk (gethash 'uxul-world::key *leveleditor-images*)))
93 (door (load-image-into-tk (gethash 'uxul-world::door *leveleditor-images*)))
94 (anchor (load-image-into-tk (gethash 'uxul-world::anchor *leveleditor-images*)))
95 (anchors (make-hash-table :test 'equal))
96 (current-upper-left (cons 0 0))
97 (current-chosen-object 'uxul)
98 (objects-and-arrows (make-instance 'ltk:frame))
99 (grid-frame (make-instance 'ltk:frame))
100 (object-frame (make-instance 'ltk:frame :master objects-and-arrows))
101 (arrow-frame (make-instance 'ltk:frame :master objects-and-arrows))
102 (right-button (make-instance 'ltk:button :text ">"
103 :master arrow-frame))
104 (left-button (make-instance 'ltk:button :text "<"
105 :master arrow-frame))
106 (up-button (make-instance 'ltk:button :text "/\\"
107 :master arrow-frame))
108 (down-button (make-instance 'ltk:button :text "\\/"
109 :master arrow-frame))
110 (rright-button (make-instance 'ltk:button :text ">>"
111 :master arrow-frame))
112 (lleft-button (make-instance 'ltk:button :text "<<"
113 :master arrow-frame))
114 (uup-button (make-instance 'ltk:button :text "//\\\\"
115 :master arrow-frame))
116 (ddown-button (make-instance 'ltk:button :text "\\\\//"
117 :master arrow-frame))
118 (argument1-entry (make-instance 'ltk:entry :text ""
119 :master object-frame))
120 (argument2-entry (make-instance 'ltk:entry :text ""
121 :master object-frame))
122 (uxul-button (make-instance 'ltk:button :text ""
123 :master object-frame))
124 (nasobem-button (make-instance 'ltk:button :text ""
125 :master object-frame))
126 (blue-nasobem-button (make-instance 'ltk:button :text ""
127 :master object-frame))
128 (burning-marshmallow-button (make-instance 'ltk:button :text ""
129 :master object-frame))
130 (gray-stone-button (make-instance 'ltk:button :text ""
131 :master object-frame))
132 (brown-stone-button (make-instance 'ltk:button :text ""
133 :master object-frame))
134 (empty-button (make-instance 'ltk:button :text ""
135 :master object-frame))
136 (tulip-button (make-instance 'ltk:button :text ""
137 :master object-frame))
138 (leaf-button (make-instance 'ltk:button :text ""
139 :master object-frame))
140 (key-button (make-instance 'ltk:button :text ""
141 :master object-frame))
142 (door-button (make-instance 'ltk:button :text ""
143 :master object-frame))
144 (anchor-button (make-instance 'ltk:button :text ""
145 :master object-frame))
146 (btns (make-array (list width height) :adjustable nil :element-type 'ltk:button)))
147 (labels ((redraw-button (i j)
148 "Redraw Button (i, j)"
149 (let* ((current-upper-x (car current-upper-left))
150 (current-upper-y (cdr current-upper-left))
151 (cval (gethash (cons (+ i current-upper-x)
152 (+ j current-upper-y))
155 (cbtn (aref btns i j)))
158 (setf cval (car cval)))
160 ((eq cval 'uxul-world::leaf)
161 (config-button-image cbtn leaf))
162 ((eq cval 'uxul-world::nasobem)
163 (config-button-image cbtn nasobem))
164 ((eq cval 'uxul-world::blue-nasobem)
165 (config-button-image cbtn blue-nasobem))
166 ((eq cval 'uxul-world::burning-marshmallow)
167 (config-button-image cbtn burning-marshmallow))
168 ((eq cval 'uxul-world::gray-stone)
169 (config-button-image cbtn gray-stone))
170 ((eq cval 'uxul-world::brown-stone)
171 (config-button-image cbtn brown-stone))
173 (config-button-image cbtn empty))
174 ((eq cval 'uxul-world::tulip)
175 (config-button-image cbtn tulip))
176 ((eq cval 'uxul-world::door)
177 (config-button-image cbtn door))
178 ((eq cval 'uxul-world::key)
179 (config-button-image cbtn key))
180 ((eq cval 'uxul-world::anchor)
182 (format t (cadr cval2))
183 (write (gethash (cadr cval2) anchors))
184 (config-button-image cbtn (gethash (cadr cval2) anchors)))
185 ((eq cval 'uxul-world::uxul)
186 (config-button-image cbtn uxul)))))
191 (redraw-button i j))))
193 (let ((current-upper-x (car current-upper-left))
194 (current-upper-y (cdr current-upper-left)))
196 ((eql current-chosen-object 'uxul-world::anchor)
198 ((string= (ltk:text argument1-entry) "")
199 (ltk:do-msg "Please give an argument in the left textbox")
201 ((gethash (ltk:text argument1-entry) anchors nil)
202 (ltk:do-msg "Warning: You already set an
203 anchor with the same dungeon-name. Make
204 sure that you remove one of them. Behavior
205 is not specified in this case and may
208 (setf (gethash (ltk:text argument1-entry) anchors)
209 (load-image-into-tk (annotated-image (gethash 'uxul-world::anchor *leveleditor-images*) (ltk:text argument1-entry))))))))
210 (setf (gethash (cons (+ i current-upper-x)
211 (+ j current-upper-y))
213 (and current-chosen-object
214 (list current-chosen-object (ltk:text argument1-entry) (ltk:text argument2-entry))))
215 (redraw-button i j)))
216 (move-field-about (i j)
217 (let ((current-upper-y (car current-upper-left))
218 (current-upper-x (cdr current-upper-left)))
219 (setf current-upper-left (cons (+ i current-upper-y) (+ j current-upper-x))))
221 (ltk:pack grid-frame)
222 (ltk:grid arrow-frame 0 1)
223 (ltk:grid left-button 1 0)
224 (setf (ltk:command left-button) #'(lambda () (move-field-about 0 1)))
225 (ltk:grid lleft-button 2 0)
226 (setf (ltk:command lleft-button) #'(lambda () (move-field-about 0 (- width 1))))
227 (ltk:grid right-button 1 2)
228 (setf (ltk:command right-button) #'(lambda () (move-field-about 0 -1)))
229 (ltk:grid rright-button 0 2)
230 (setf (ltk:command rright-button) #'(lambda () (move-field-about 0 (- 1 width))))
231 (ltk:grid up-button 0 1)
232 (setf (ltk:command up-button) #'(lambda () (move-field-about 1 0)))
233 (ltk:grid uup-button 0 0)
234 (setf (ltk:command uup-button) #'(lambda () (move-field-about (- width 1) 0)))
235 (ltk:grid down-button 2 1)
236 (setf (ltk:command down-button) #'(lambda () (move-field-about -1 0)))
237 (ltk:grid ddown-button 2 2)
238 (setf (ltk:command ddown-button) #'(lambda () (move-field-about (- 1 width) 0)))
240 (ltk:grid argument1-entry 1 0 :columnspan 5)
241 (ltk:grid argument2-entry 1 6 :columnspan 5)
243 (ltk:grid empty-button 0 0)
244 (config-button-image empty-button empty)
245 (setf (ltk:command empty-button)
247 (setf current-chosen-object nil)))
248 (ltk:grid uxul-button 0 1)
249 (config-button-image uxul-button uxul)
250 (setf (ltk:command uxul-button)
252 (setf current-chosen-object 'uxul-world::uxul)))
253 (ltk:grid nasobem-button 0 2)
254 (config-button-image nasobem-button nasobem)
255 (setf (ltk:command nasobem-button)
257 (setf current-chosen-object 'uxul-world::nasobem)))
258 (ltk:grid blue-nasobem-button 0 3)
259 (config-button-image blue-nasobem-button blue-nasobem)
260 (setf (ltk:command blue-nasobem-button)
262 (setf current-chosen-object 'uxul-world::blue-nasobem)))
263 (ltk:grid burning-marshmallow-button 0 4)
264 (config-button-image burning-marshmallow-button burning-marshmallow)
265 (setf (ltk:command burning-marshmallow-button)
267 (setf current-chosen-object 'uxul-world::burning-marshmallow)))
268 (ltk:grid gray-stone-button 0 5)
269 (config-button-image gray-stone-button gray-stone)
270 (setf (ltk:command gray-stone-button)
272 (setf current-chosen-object 'uxul-world::gray-stone)))
273 (ltk:grid brown-stone-button 0 6)
274 (config-button-image brown-stone-button brown-stone)
275 (setf (ltk:command brown-stone-button)
277 (setf current-chosen-object 'uxul-world::brown-stone)))
278 (ltk:grid leaf-button 0 7)
279 (config-button-image leaf-button leaf)
280 (setf (ltk:command leaf-button)
282 (setf current-chosen-object 'uxul-world::leaf)))
284 (ltk:grid tulip-button 0 8)
285 (config-button-image tulip-button tulip)
286 (setf (ltk:command tulip-button)
288 (setf current-chosen-object 'uxul-world::tulip)))
290 (ltk:grid key-button 0 9)
291 (config-button-image key-button key)
292 (setf (ltk:command key-button)
294 (setf current-chosen-object 'uxul-world::key)))
296 (ltk:grid door-button 0 10)
297 (config-button-image door-button door)
298 (setf (ltk:command door-button)
300 (setf current-chosen-object 'uxul-world::door)))
302 (ltk:grid anchor-button 0 11)
303 (config-button-image anchor-button anchor)
304 (setf (ltk:command anchor-button)
306 (setf current-chosen-object 'uxul-world::anchor)))
308 (ltk:grid object-frame 0 0)
309 (ltk:pack objects-and-arrows)
314 (make-instance 'ltk:button
317 (setf (ltk:command cbtn) (let ((i i) (j j)) #'(lambda () (react i j))))
318 (config-button-image cbtn empty)
319 (setf (aref btns i j) cbtn)
320 (ltk:grid cbtn i j))))
322 (item-table-to-list item-table)))
325 (defun get-base64-char-for-number (i)
326 (declare (type (integer 0 63) i))
327 (elt "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" i))
329 (defun base64-encode-threebytes (byte1 byte2 byte3)
330 (declare (type (unsigned-byte 8) byte1 byte2 byte3))
333 (get-base64-char-for-number (logand #b111111 (ash byte1 -2)))
334 (get-base64-char-for-number (logand #b111111 (+ (ash (ash byte1 6) -2) (ash byte2 -4))))
335 (get-base64-char-for-number (logand #b111111 (+ (ash (ash byte2 4) -2) (ash byte3 -6))))
336 (get-base64-char-for-number (logand #b111111 (ash (ash byte3 2) -2)))) 'string))
339 (defun base64-encode-bytelist (bytelist &optional (ret ""))
343 (base64-encode-bytelist
347 (base64-encode-threebytes
351 ;;else (genau zwei elemente)
352 (concatenate 'string ret
353 (base64-encode-threebytes
358 ;;else (genau ein element)
359 (concatenate 'string ret
360 (base64-encode-threebytes
363 ;;else (kein element)
367 (defun base64-encode-byteseq (byteseq &optional (ret ""))
368 (case (length byteseq)
370 (1 (concatenate 'string ret
371 (base64-encode-threebytes
372 (elt byteseq 0) 0 0) "=="))
373 (2 (concatenate 'string ret
374 (base64-encode-threebytes
379 (t (base64-encode-byteseq
383 (base64-encode-threebytes
386 (elt byteseq 2)))))))