1 ;;; Copyright 2009 Christoph Senjak
3 (in-package :uxul-world-leveleditor)
5 (defparameter *leveleditor-images* nil)
7 (defun stretched-base64-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 (base64-encode-byteseq (lisp-magick::magick-get-image-blob mywand))))
15 (defun prepare-base64-images (&optional (care-about-initialization *leveleditor-images*))
16 (when (not care-about-initialization)
17 (setf *leveleditor-images* (make-hash-table))
18 (setf (gethash 'uxul-world::uxul *leveleditor-images*) (stretched-base64-image uxul-world::|uxul_small1|))
19 (setf (gethash 'uxul-world::leaf *leveleditor-images*) (stretched-base64-image uxul-world::|leaf|))
20 (setf (gethash 'uxul-world::nasobem *leveleditor-images*) (stretched-base64-image uxul-world::|nasobem|))
21 (setf (gethash 'uxul-world::blue-nasobem *leveleditor-images*) (stretched-base64-image uxul-world::|blue_nasobem|))
22 (setf (gethash 'uxul-world::burning-marshmallow *leveleditor-images*) (stretched-base64-image uxul-world::|burning_marshmallow_ld1|))
23 (setf (gethash 'uxul-world::gray-stone *leveleditor-images*) (stretched-base64-image uxul-world::|gray_stone|))
24 (setf (gethash 'uxul-world::brown-stone *leveleditor-images*) (stretched-base64-image uxul-world::|brown_stone|))
25 (setf (gethash 'uxul-world::empty *leveleditor-images*) (stretched-base64-image uxul-world::|empty|))
26 (setf (gethash 'uxul-world::tulip *leveleditor-images*) (stretched-base64-image uxul-world::|tulip|))))
28 (defun load-image-into-tk (png-base64)
29 "return a tkobject with this image"
30 (let ((name (ltk::create-name)))
31 (ltk:format-wish "set ~A [ image create photo -data \"~A\" ]"
33 (make-instance 'ltk:tkobject :name name)))
35 (defun config-button-image (button tkobject)
36 (ltk:format-wish "~A configure -image $~A" (ltk::widget-path button) (ltk::name tkobject)))
38 (defun item-table-to-list (item-table)
39 "Special Function vor level-editor. Returns a list of lists of the
42 (maphash #'(lambda (key val)
44 (push (list (car key) (cdr key) val) ret)))
48 (defun level-editor (&optional (level nil) (width 16) (height 16))
49 (prepare-base64-images)
50 (let ((item-table (make-hash-table :test 'equal)))
51 ;;initialize given level
53 (setf (gethash (cons (car item) (cadr item)) item-table) (caddr item)))
57 ((uxul (load-image-into-tk (gethash 'uxul-world::uxul *leveleditor-images*)))
58 (leaf (load-image-into-tk (gethash 'uxul-world::leaf *leveleditor-images*)))
59 (nasobem (load-image-into-tk (gethash 'uxul-world::nasobem *leveleditor-images*)))
60 (blue-nasobem (load-image-into-tk (gethash 'uxul-world::blue-nasobem *leveleditor-images*)))
61 (burning-marshmallow (load-image-into-tk (gethash 'uxul-world::burning-marshmallow *leveleditor-images*)))
62 (gray-stone (load-image-into-tk (gethash 'uxul-world::gray-stone *leveleditor-images*)))
63 (brown-stone (load-image-into-tk (gethash 'uxul-world::brown-stone *leveleditor-images*)))
64 (empty (load-image-into-tk (gethash 'uxul-world::empty *leveleditor-images*)))
65 (tulip (load-image-into-tk (gethash 'uxul-world::tulip *leveleditor-images*)))
66 (current-upper-left (cons 0 0))
67 (current-chosen-object 'uxul)
68 (objects-and-arrows (make-instance 'ltk:frame))
69 (object-frame (make-instance 'ltk:frame :master objects-and-arrows))
70 (arrow-frame (make-instance 'ltk:frame :master objects-and-arrows))
71 (grid-frame (make-instance 'ltk:frame))
72 (right-button (make-instance 'ltk:button :text ">"
74 (left-button (make-instance 'ltk:button :text "<"
76 (up-button (make-instance 'ltk:button :text "/\\"
78 (down-button (make-instance 'ltk:button :text "\\/"
80 (rright-button (make-instance 'ltk:button :text ">>"
82 (lleft-button (make-instance 'ltk:button :text "<<"
84 (uup-button (make-instance 'ltk:button :text "//\\\\"
86 (ddown-button (make-instance 'ltk:button :text "\\\\//"
88 (uxul-button (make-instance 'ltk:button :text ""
89 :master object-frame))
90 (nasobem-button (make-instance 'ltk:button :text ""
91 :master object-frame))
92 (blue-nasobem-button (make-instance 'ltk:button :text ""
93 :master object-frame))
94 (burning-marshmallow-button (make-instance 'ltk:button :text ""
95 :master object-frame))
96 (gray-stone-button (make-instance 'ltk:button :text ""
97 :master object-frame))
98 (brown-stone-button (make-instance 'ltk:button :text ""
99 :master object-frame))
100 (empty-button (make-instance 'ltk:button :text ""
101 :master object-frame))
102 (tulip-button (make-instance 'ltk:button :text ""
103 :master object-frame))
104 (leaf-button (make-instance 'ltk:button :text ""
105 :master object-frame))
106 (btns (make-array (list width height) :adjustable nil :element-type 'ltk:button)))
107 (labels ((redraw-button (i j)
108 "Redraw Button (i, j)"
109 (let* ((current-upper-x (car current-upper-left))
110 (current-upper-y (cdr current-upper-left))
111 (cval (gethash (cons (+ i current-upper-x)
112 (+ j current-upper-y))
114 (cbtn (aref btns i j)))
116 (setf cval (car cval)))
118 ((eq cval 'uxul-world::leaf)
119 (config-button-image cbtn leaf))
120 ((eq cval 'uxul-world::nasobem)
121 (config-button-image cbtn nasobem))
122 ((eq cval 'uxul-world::blue-nasobem)
123 (config-button-image cbtn blue-nasobem))
124 ((eq cval 'uxul-world::burning-marshmallow)
125 (config-button-image cbtn burning-marshmallow))
126 ((eq cval 'uxul-world::gray-stone)
127 (config-button-image cbtn gray-stone))
128 ((eq cval 'uxul-world::brown-stone)
129 (config-button-image cbtn brown-stone))
131 (config-button-image cbtn empty))
132 ((eq cval 'uxul-world::tulip)
133 (config-button-image cbtn tulip))
134 ((eq cval 'uxul-world::uxul)
135 (config-button-image cbtn uxul)))))
140 (redraw-button i j))))
142 (let ((current-upper-x (car current-upper-left))
143 (current-upper-y (cdr current-upper-left)))
145 ((eq current-chosen-object 'burning-marshmallow)
146 (setf (gethash (cons (+ i current-upper-x)
147 (+ j current-upper-y))
148 item-table) 'burning-marshmallow))
150 (setf (gethash (cons (+ i current-upper-x)
151 (+ j current-upper-y))
152 item-table) current-chosen-object)))
153 (redraw-button i j)))
154 (move-field-about (i j)
155 (let ((current-upper-y (car current-upper-left))
156 (current-upper-x (cdr current-upper-left)))
157 (setf current-upper-left (cons (+ i current-upper-y) (+ j current-upper-x))))
159 (ltk:pack grid-frame)
160 (ltk:grid arrow-frame 0 1)
161 (ltk:grid left-button 1 0)
162 (setf (ltk:command left-button) #'(lambda () (move-field-about 0 1)))
163 (ltk:grid lleft-button 2 0)
164 (setf (ltk:command lleft-button) #'(lambda () (move-field-about 0 15)))
165 (ltk:grid right-button 1 2)
166 (setf (ltk:command right-button) #'(lambda () (move-field-about 0 -1)))
167 (ltk:grid rright-button 0 2)
168 (setf (ltk:command rright-button) #'(lambda () (move-field-about 0 -15)))
169 (ltk:grid up-button 0 1)
170 (setf (ltk:command up-button) #'(lambda () (move-field-about 1 0)))
171 (ltk:grid uup-button 0 0)
172 (setf (ltk:command uup-button) #'(lambda () (move-field-about 15 0)))
173 (ltk:grid down-button 2 1)
174 (setf (ltk:command down-button) #'(lambda () (move-field-about -1 0)))
175 (ltk:grid ddown-button 2 2)
176 (setf (ltk:command ddown-button) #'(lambda () (move-field-about -15 0)))
178 (ltk:grid empty-button 0 0)
179 (config-button-image empty-button empty)
180 (setf (ltk:command empty-button)
182 (setf current-chosen-object nil)))
183 (ltk:grid uxul-button 0 1)
184 (config-button-image uxul-button uxul)
185 (setf (ltk:command uxul-button)
187 (setf current-chosen-object 'uxul-world::uxul)))
188 (ltk:grid nasobem-button 0 2)
189 (config-button-image nasobem-button nasobem)
190 (setf (ltk:command nasobem-button)
192 (setf current-chosen-object 'uxul-world::nasobem)))
193 (ltk:grid blue-nasobem-button 0 3)
194 (config-button-image blue-nasobem-button blue-nasobem)
195 (setf (ltk:command blue-nasobem-button)
197 (setf current-chosen-object 'uxul-world::blue-nasobem)))
198 (ltk:grid burning-marshmallow-button 0 4)
199 (config-button-image burning-marshmallow-button burning-marshmallow)
200 (setf (ltk:command burning-marshmallow-button)
202 (setf current-chosen-object 'uxul-world::burning-marshmallow)))
203 (ltk:grid gray-stone-button 0 5)
204 (config-button-image gray-stone-button gray-stone)
205 (setf (ltk:command gray-stone-button)
207 (setf current-chosen-object 'uxul-world::gray-stone)))
208 (ltk:grid brown-stone-button 0 6)
209 (config-button-image brown-stone-button brown-stone)
210 (setf (ltk:command brown-stone-button)
212 (setf current-chosen-object 'uxul-world::brown-stone)))
213 (ltk:grid leaf-button 0 7)
214 (config-button-image leaf-button leaf)
215 (setf (ltk:command leaf-button)
217 (setf current-chosen-object 'uxul-world::leaf)))
219 (ltk:grid tulip-button 0 8)
220 (config-button-image tulip-button tulip)
221 (setf (ltk:command tulip-button)
223 (setf current-chosen-object 'uxul-world::tulip)))
225 (ltk:grid object-frame 0 0)
226 (ltk:pack objects-and-arrows)
231 (make-instance 'ltk:button
234 (setf (ltk:command cbtn) (let ((i i) (j j)) #'(lambda () (react i j))))
235 (config-button-image cbtn empty)
236 (setf (aref btns i j) cbtn)
237 (ltk:grid cbtn i j))))
239 (item-table-to-list item-table)))
242 (defun get-base64-char-for-number (i)
243 (declare (type (integer 0 63) i))
244 (elt "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" i))
246 (defun base64-encode-threebytes (byte1 byte2 byte3)
247 (declare (type (unsigned-byte 8) byte1 byte2 byte3))
250 (get-base64-char-for-number (logand #b111111 (ash byte1 -2)))
251 (get-base64-char-for-number (logand #b111111 (+ (ash (ash byte1 6) -2) (ash byte2 -4))))
252 (get-base64-char-for-number (logand #b111111 (+ (ash (ash byte2 4) -2) (ash byte3 -6))))
253 (get-base64-char-for-number (logand #b111111 (ash (ash byte3 2) -2)))) 'string))
256 (defun base64-encode-bytelist (bytelist &optional (ret ""))
260 (base64-encode-bytelist
264 (base64-encode-threebytes
268 ;;else (genau zwei elemente)
269 (concatenate 'string ret
270 (base64-encode-threebytes
275 ;;else (genau ein element)
276 (concatenate 'string ret
277 (base64-encode-threebytes
280 ;;else (kein element)
284 (defun base64-encode-byteseq (byteseq &optional (ret ""))
285 (case (length byteseq)
287 (1 (concatenate 'string ret
288 (base64-encode-threebytes
289 (elt byteseq 0) 0 0) "=="))
290 (2 (concatenate 'string ret
291 (base64-encode-threebytes
296 (t (base64-encode-byteseq
300 (base64-encode-threebytes
303 (elt byteseq 2)))))))