0f01e4009c4843eff3d75c80e2dd99c1e7e8228c
[uxul-world.git] / leveleditor.lisp
1 ;;; Copyright 2009 Christoph Senjak
2
3 (in-package :uxul-world-leveleditor)
4
5 (defparameter *leveleditor-images* nil)
6
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)))
14
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)))
28
29 (defun numbered-image (img num)
30   "Annotate the image with a number."
31   (annotated-image img (format nil "~d" num)))
32
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|))
49 ))
50
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)))
57
58 (defun config-button-image (button tkobject)
59   (ltk:format-wish "~A configure -image $~A" (ltk::widget-path button) (ltk::name tkobject)))
60
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 ...)."
64   (let ((ret nil))
65     (maphash #'(lambda (key val)
66                  (when val
67                    (push (concatenate 'list (list (car key) (cdr key)) val)  ret)))
68              item-table)
69     ret))
70
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))
74     (setf width height)
75     (setf height nilpferd))
76
77   (prepare-images)
78   (let ((item-table (make-hash-table :test 'equal)))
79     ;;initialize given level
80     (dolist (item level)
81       (setf (gethash (cons (car item) (cadr item)) item-table) (cddr item)))   
82     (ltk:with-ltk ()
83       (let*
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))
156                                         item-table nil))
157                           (cval2 nil)
158                          (cbtn (aref btns i j)))
159                      (when (listp cval)
160                        (setf cval2 cval)
161                        (setf cval (car cval)))
162                      (cond
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))
175                        ((eq cval nil)
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)))))
187                  (redraw-buttons ()
188                    "Redraw all Buttons"
189                      (dotimes (i width)
190                        (dotimes (j height)
191                          (redraw-button i j))))
192                  (react (i j)
193                    (let ((current-upper-x (car current-upper-left))
194                          (current-upper-y (cdr current-upper-left)))
195                      (cond
196                        ((eql current-chosen-object :info)
197                         (let ((sym (gethash (cons (+ i current-upper-x)
198                                           (+ j current-upper-y))
199                                     item-table nil)))
200                           (if sym
201                               (ltk:do-msg
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)
209                         (cond
210                           ((string= (ltk:text argument1-entry) "")
211                            (ltk:do-msg "Please give an argument in the left textbox")
212                            (return-from react))
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
218                            change."))
219                           (t
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))
224                                     item-table)
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))))
232                    (redraw-buttons)))
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)))
251
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)))
257
258           (ltk:grid empty-button 0 0)
259           (config-button-image empty-button empty)
260           (setf (ltk:command empty-button)
261                              #'(lambda ()
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)
266                              #'(lambda ()
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)
271                              #'(lambda ()
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)
276                              #'(lambda ()
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)
281                              #'(lambda ()
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)
286                              #'(lambda ()
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)
291                              #'(lambda ()
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)
296                              #'(lambda ()
297                                  (setf current-chosen-object 'uxul-world::leaf)))
298
299           (ltk:grid tulip-button 0 8)
300           (config-button-image tulip-button tulip)
301           (setf (ltk:command tulip-button)
302                              #'(lambda ()
303                                  (setf current-chosen-object 'uxul-world::tulip)))
304
305           (ltk:grid key-button 0 9)
306           (config-button-image key-button key)
307           (setf (ltk:command key-button)
308                              #'(lambda ()
309                                  (setf current-chosen-object 'uxul-world::key)))
310
311           (ltk:grid door-button 0 10)
312           (config-button-image door-button door)
313           (setf (ltk:command door-button)
314                              #'(lambda ()
315                                  (setf current-chosen-object 'uxul-world::door)))
316
317           (ltk:grid anchor-button 0 11)
318           (config-button-image anchor-button anchor)
319           (setf (ltk:command anchor-button)
320                              #'(lambda ()
321                                  (setf current-chosen-object 'uxul-world::anchor)))
322
323           (ltk:grid object-frame 0 0)
324           (ltk:pack objects-and-arrows)
325
326           (dotimes (i width)
327             (dotimes (j height)
328               (let ((cbtn
329                      (make-instance 'ltk:button
330                                     :master grid-frame
331                                     :text "")))
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))))
336           (redraw-buttons))))
337     (item-table-to-list item-table)))
338
339
340 (defun get-base64-char-for-number (i)
341   (declare (type (integer 0 63) i))
342   (elt "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" i))
343
344 (defun base64-encode-threebytes (byte1 byte2 byte3)
345   (declare (type (unsigned-byte 8) byte1 byte2 byte3))
346   (coerce
347    (list
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))  
352
353
354 (defun base64-encode-bytelist (bytelist &optional (ret ""))
355   (if bytelist
356       (if (cdr bytelist)
357           (if (cddr bytelist)
358               (base64-encode-bytelist
359                (cdddr bytelist)
360                (concatenate 'string
361                             ret
362                             (base64-encode-threebytes
363                              (car bytelist)
364                              (cadr bytelist)
365                              (caddr bytelist))))
366               ;;else (genau zwei elemente)
367               (concatenate 'string ret                     
368                            (base64-encode-threebytes
369                             (car bytelist)
370                             (cadr bytelist)
371                             0)
372                            "="))
373           ;;else (genau ein element)
374           (concatenate 'string ret                         
375                        (base64-encode-threebytes
376                         (car bytelist) 0 0)
377                        "=="))
378       ;;else (kein element)
379       ret))
380
381
382 (defun base64-encode-byteseq (byteseq &optional (ret ""))
383   (case (length byteseq)
384     (0 ret)
385     (1 (concatenate 'string ret                    
386                     (base64-encode-threebytes
387                      (elt byteseq 0) 0 0) "=="))
388     (2 (concatenate 'string ret                    
389                     (base64-encode-threebytes
390                      (elt byteseq 0)
391                      (elt byteseq 1)
392                      0)
393                     "="))
394     (t (base64-encode-byteseq
395         (subseq byteseq 3)
396         (concatenate 'string
397                      ret
398                      (base64-encode-threebytes
399                       (elt byteseq 0)
400                       (elt byteseq 1)
401                       (elt byteseq 2)))))))