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 (uxul-world::init-files)
37 (setf (gethash 'uxul-world::uxul *leveleditor-images*) (stretched-image uxul-world::|uxul_small1|))
38 (setf (gethash 'uxul-world::leaf *leveleditor-images*) (stretched-image uxul-world::|leaf|))
39 (setf (gethash 'uxul-world::nasobem *leveleditor-images*) (stretched-image uxul-world::|nasobem|))
40 (setf (gethash 'uxul-world::blue-nasobem *leveleditor-images*) (stretched-image uxul-world::|blue_nasobem|))
41 (setf (gethash 'uxul-world::burning-marshmallow *leveleditor-images*) (stretched-image uxul-world::|burning_marshmallow_ld1|))
42 (setf (gethash 'uxul-world::gray-stone *leveleditor-images*) (stretched-image uxul-world::|gray_stone|))
43 (setf (gethash 'uxul-world::brown-stone *leveleditor-images*) (stretched-image uxul-world::|brown_stone|))
44 (setf (gethash 'uxul-world::empty *leveleditor-images*) (stretched-image uxul-world::|empty|))
45 (setf (gethash 'uxul-world::tulip *leveleditor-images*) (stretched-image uxul-world::|tulip|))
46 (setf (gethash 'uxul-world::door *leveleditor-images*) (stretched-image uxul-world::|door|))
47 (setf (gethash 'uxul-world::key *leveleditor-images*) (stretched-image uxul-world::|key|))
48 (setf (gethash 'uxul-world::anchor *leveleditor-images*) (stretched-image uxul-world::|anchor|))
51 (defun load-image-into-tk (png)
52 "return a tkobject with this image"
53 (let ((name (ltk::create-name)))
54 (ltk:format-wish "set ~A [ image create photo -data \"~A\" ]"
55 name (base64-encode-byteseq png))
56 (make-instance 'ltk:tkobject :name name)))
58 (defun config-button-image (button tkobject)
59 (ltk:format-wish "~A configure -image $~A" (ltk::widget-path button) (ltk::name tkobject)))
61 (defun item-table-to-list (item-table)
62 "Special Function for level-editor. Returns a list of lists of the
63 form (x y object arg1 arg2 ...)."
65 (maphash #'(lambda (key val)
67 (push (concatenate 'list (list (car key) (cdr key)) val) ret)))
71 (defun level-editor (&optional (level nil) (width 16) (height 16))
72 ;; hack. swap "width" and "height". (too lazy to change it properly by now)
73 (let ((nilpferd width))
75 (setf height nilpferd))
78 (let ((item-table (make-hash-table :test 'equal)))
79 ;;initialize given level
81 (setf (gethash (cons (car item) (cadr item)) item-table) (cddr item)))
84 ((uxul (load-image-into-tk (gethash 'uxul-world::uxul *leveleditor-images*)))
85 (leaf (load-image-into-tk (gethash 'uxul-world::leaf *leveleditor-images*)))
86 (nasobem (load-image-into-tk (gethash 'uxul-world::nasobem *leveleditor-images*)))
87 (blue-nasobem (load-image-into-tk (gethash 'uxul-world::blue-nasobem *leveleditor-images*)))
88 (burning-marshmallow (load-image-into-tk (gethash 'uxul-world::burning-marshmallow *leveleditor-images*)))
89 (gray-stone (load-image-into-tk (gethash 'uxul-world::gray-stone *leveleditor-images*)))
90 (brown-stone (load-image-into-tk (gethash 'uxul-world::brown-stone *leveleditor-images*)))
91 (empty (load-image-into-tk (gethash 'uxul-world::empty *leveleditor-images*)))
92 (tulip (load-image-into-tk (gethash 'uxul-world::tulip *leveleditor-images*)))
93 (key (load-image-into-tk (gethash 'uxul-world::key *leveleditor-images*)))
94 (door (load-image-into-tk (gethash 'uxul-world::door *leveleditor-images*)))
95 (anchor (load-image-into-tk (gethash 'uxul-world::anchor *leveleditor-images*)))
96 (anchors (make-hash-table :test 'equal))
97 (current-upper-left (cons 0 0))
98 (current-chosen-object 'uxul)
99 (objects-and-arrows (make-instance 'ltk:frame))
100 (grid-frame (make-instance 'ltk:frame))
101 (object-frame (make-instance 'ltk:frame :master objects-and-arrows))
102 (arrow-frame (make-instance 'ltk:frame :master objects-and-arrows))
103 (right-button (make-instance 'ltk:button :text ">"
104 :master arrow-frame))
105 (left-button (make-instance 'ltk:button :text "<"
106 :master arrow-frame))
107 (up-button (make-instance 'ltk:button :text "/\\"
108 :master arrow-frame))
109 (down-button (make-instance 'ltk:button :text "\\/"
110 :master arrow-frame))
111 (rright-button (make-instance 'ltk:button :text ">>"
112 :master arrow-frame))
113 (lleft-button (make-instance 'ltk:button :text "<<"
114 :master arrow-frame))
115 (uup-button (make-instance 'ltk:button :text "//\\\\"
116 :master arrow-frame))
117 (ddown-button (make-instance 'ltk:button :text "\\\\//"
118 :master arrow-frame))
119 (argument1-entry (make-instance 'ltk:entry :text ""
120 :master object-frame))
121 (argument2-entry (make-instance 'ltk:entry :text ""
122 :master object-frame))
123 (uxul-button (make-instance 'ltk:button :text ""
124 :master object-frame))
125 (nasobem-button (make-instance 'ltk:button :text ""
126 :master object-frame))
127 (blue-nasobem-button (make-instance 'ltk:button :text ""
128 :master object-frame))
129 (burning-marshmallow-button (make-instance 'ltk:button :text ""
130 :master object-frame))
131 (gray-stone-button (make-instance 'ltk:button :text ""
132 :master object-frame))
133 (brown-stone-button (make-instance 'ltk:button :text ""
134 :master object-frame))
135 (empty-button (make-instance 'ltk:button :text ""
136 :master object-frame))
137 (tulip-button (make-instance 'ltk:button :text ""
138 :master object-frame))
139 (leaf-button (make-instance 'ltk:button :text ""
140 :master object-frame))
141 (key-button (make-instance 'ltk:button :text ""
142 :master object-frame))
143 (door-button (make-instance 'ltk:button :text ""
144 :master object-frame))
145 (anchor-button (make-instance 'ltk:button :text ""
146 :master object-frame))
147 (info-button (make-instance 'ltk:button :text "Info"
148 :master object-frame))
149 (btns (make-array (list width height) :adjustable nil :element-type 'ltk:button)))
150 (labels ((redraw-button (i j)
151 "Redraw Button (i, j)"
152 (let* ((current-upper-x (car current-upper-left))
153 (current-upper-y (cdr current-upper-left))
154 (cval (gethash (cons (+ i current-upper-x)
155 (+ j current-upper-y))
158 (cbtn (aref btns i j)))
161 (setf cval (car cval)))
163 ((eq cval 'uxul-world::leaf)
164 (config-button-image cbtn leaf))
165 ((eq cval 'uxul-world::nasobem)
166 (config-button-image cbtn nasobem))
167 ((eq cval 'uxul-world::blue-nasobem)
168 (config-button-image cbtn blue-nasobem))
169 ((eq cval 'uxul-world::burning-marshmallow)
170 (config-button-image cbtn burning-marshmallow))
171 ((eq cval 'uxul-world::gray-stone)
172 (config-button-image cbtn gray-stone))
173 ((eq cval 'uxul-world::brown-stone)
174 (config-button-image cbtn brown-stone))
176 (config-button-image cbtn empty))
177 ((eq cval 'uxul-world::tulip)
178 (config-button-image cbtn tulip))
179 ((eq cval 'uxul-world::door)
180 (config-button-image cbtn door))
181 ((eq cval 'uxul-world::key)
182 (config-button-image cbtn key))
183 ((eq cval 'uxul-world::anchor)
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 :info)
197 (let ((sym (gethash (cons (+ i current-upper-x)
198 (+ j current-upper-y))
202 (format nil (concatenate 'string
203 "Symbolname: \"" (symbol-name (car sym)) "\"~%"
204 "First argument: \"" (cadr sym) "\"~%"
205 "Second argument: \"" (caddr sym) "\"")))
206 (ltk:do-msg "There doesnt seem to be anything here."))
207 (return-from react)))
208 ((eql current-chosen-object 'uxul-world::anchor)
210 ((string= (ltk:text argument1-entry) "")
211 (ltk:do-msg "Please give an argument in the left textbox")
213 ((gethash (ltk:text argument1-entry) anchors nil)
214 (ltk:do-msg "Warning: You already set an
215 anchor with the same dungeon-name. Make
216 sure that you remove one of them. Behavior
217 is not specified in this case and may
220 (setf (gethash (ltk:text argument1-entry) anchors)
221 (load-image-into-tk (annotated-image (gethash 'uxul-world::anchor *leveleditor-images*) (ltk:text argument1-entry))))))))
222 (setf (gethash (cons (+ i current-upper-x)
223 (+ j current-upper-y))
225 (and current-chosen-object
226 (list current-chosen-object (ltk:text argument1-entry) (ltk:text argument2-entry))))
227 (redraw-button i j)))
228 (move-field-about (i j)
229 (let ((current-upper-y (car current-upper-left))
230 (current-upper-x (cdr current-upper-left)))
231 (setf current-upper-left (cons (+ i current-upper-y) (+ j current-upper-x))))
233 (ltk:pack grid-frame)
234 (ltk:grid arrow-frame 0 1)
235 (ltk:grid left-button 1 0)
236 (setf (ltk:command left-button) #'(lambda () (move-field-about 0 1)))
237 (ltk:grid lleft-button 2 0)
238 (setf (ltk:command lleft-button) #'(lambda () (move-field-about 0 (- width 1))))
239 (ltk:grid right-button 1 2)
240 (setf (ltk:command right-button) #'(lambda () (move-field-about 0 -1)))
241 (ltk:grid rright-button 0 2)
242 (setf (ltk:command rright-button) #'(lambda () (move-field-about 0 (- 1 width))))
243 (ltk:grid up-button 0 1)
244 (setf (ltk:command up-button) #'(lambda () (move-field-about 1 0)))
245 (ltk:grid uup-button 0 0)
246 (setf (ltk:command uup-button) #'(lambda () (move-field-about (- width 1) 0)))
247 (ltk:grid down-button 2 1)
248 (setf (ltk:command down-button) #'(lambda () (move-field-about -1 0)))
249 (ltk:grid ddown-button 2 2)
250 (setf (ltk:command ddown-button) #'(lambda () (move-field-about (- 1 width) 0)))
252 (ltk:grid argument1-entry 1 0 :columnspan 4)
253 (ltk:grid argument2-entry 1 5 :columnspan 4)
254 (ltk:grid info-button 1 9 :columnspan 2)
255 (setf (ltk:command info-button)
256 #'(lambda () (setf current-chosen-object :info)))
258 (ltk:grid empty-button 0 0)
259 (config-button-image empty-button empty)
260 (setf (ltk:command empty-button)
262 (setf current-chosen-object nil)))
263 (ltk:grid uxul-button 0 1)
264 (config-button-image uxul-button uxul)
265 (setf (ltk:command uxul-button)
267 (setf current-chosen-object 'uxul-world::uxul)))
268 (ltk:grid nasobem-button 0 2)
269 (config-button-image nasobem-button nasobem)
270 (setf (ltk:command nasobem-button)
272 (setf current-chosen-object 'uxul-world::nasobem)))
273 (ltk:grid blue-nasobem-button 0 3)
274 (config-button-image blue-nasobem-button blue-nasobem)
275 (setf (ltk:command blue-nasobem-button)
277 (setf current-chosen-object 'uxul-world::blue-nasobem)))
278 (ltk:grid burning-marshmallow-button 0 4)
279 (config-button-image burning-marshmallow-button burning-marshmallow)
280 (setf (ltk:command burning-marshmallow-button)
282 (setf current-chosen-object 'uxul-world::burning-marshmallow)))
283 (ltk:grid gray-stone-button 0 5)
284 (config-button-image gray-stone-button gray-stone)
285 (setf (ltk:command gray-stone-button)
287 (setf current-chosen-object 'uxul-world::gray-stone)))
288 (ltk:grid brown-stone-button 0 6)
289 (config-button-image brown-stone-button brown-stone)
290 (setf (ltk:command brown-stone-button)
292 (setf current-chosen-object 'uxul-world::brown-stone)))
293 (ltk:grid leaf-button 0 7)
294 (config-button-image leaf-button leaf)
295 (setf (ltk:command leaf-button)
297 (setf current-chosen-object 'uxul-world::leaf)))
299 (ltk:grid tulip-button 0 8)
300 (config-button-image tulip-button tulip)
301 (setf (ltk:command tulip-button)
303 (setf current-chosen-object 'uxul-world::tulip)))
305 (ltk:grid key-button 0 9)
306 (config-button-image key-button key)
307 (setf (ltk:command key-button)
309 (setf current-chosen-object 'uxul-world::key)))
311 (ltk:grid door-button 0 10)
312 (config-button-image door-button door)
313 (setf (ltk:command door-button)
315 (setf current-chosen-object 'uxul-world::door)))
317 (ltk:grid anchor-button 0 11)
318 (config-button-image anchor-button anchor)
319 (setf (ltk:command anchor-button)
321 (setf current-chosen-object 'uxul-world::anchor)))
323 (ltk:grid object-frame 0 0)
324 (ltk:pack objects-and-arrows)
329 (make-instance 'ltk:button
332 (setf (ltk:command cbtn) (let ((i i) (j j)) #'(lambda () (react i j))))
333 (config-button-image cbtn empty)
334 (setf (aref btns i j) cbtn)
335 (ltk:grid cbtn i j))))
337 (item-table-to-list item-table)))
340 (defun get-base64-char-for-number (i)
341 (declare (type (integer 0 63) i))
342 (elt "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" i))
344 (defun base64-encode-threebytes (byte1 byte2 byte3)
345 (declare (type (unsigned-byte 8) byte1 byte2 byte3))
348 (get-base64-char-for-number (logand #b111111 (ash byte1 -2)))
349 (get-base64-char-for-number (logand #b111111 (+ (ash (ash byte1 6) -2) (ash byte2 -4))))
350 (get-base64-char-for-number (logand #b111111 (+ (ash (ash byte2 4) -2) (ash byte3 -6))))
351 (get-base64-char-for-number (logand #b111111 (ash (ash byte3 2) -2)))) 'string))
354 (defun base64-encode-bytelist (bytelist &optional (ret ""))
358 (base64-encode-bytelist
362 (base64-encode-threebytes
366 ;;else (genau zwei elemente)
367 (concatenate 'string ret
368 (base64-encode-threebytes
373 ;;else (genau ein element)
374 (concatenate 'string ret
375 (base64-encode-threebytes
378 ;;else (kein element)
382 (defun base64-encode-byteseq (byteseq &optional (ret ""))
383 (case (length byteseq)
385 (1 (concatenate 'string ret
386 (base64-encode-threebytes
387 (elt byteseq 0) 0 0) "=="))
388 (2 (concatenate 'string ret
389 (base64-encode-threebytes
394 (t (base64-encode-byteseq
398 (base64-encode-threebytes
401 (elt byteseq 2)))))))