Now using OpenGL-VBOs.
[uxul-world.git] / leveleditor.lisp
1 ;;; Copyright 2009-2011 Christoph Senjak
2
3 (in-package :uxul-world-leveleditor)
4
5 (defparameter *leveleditor-images* nil)
6
7 (defun bmp-to-gif (inblob)
8   "Convert BMP to Grayscale-Gif using skippy."
9   (let*
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))
14        (colors
15         (let
16             ((v (make-array '(256) :adjustable nil)))
17           (dotimes (i 256)
18             (setf (elt v i)
19                (skippy:ensure-color (skippy:rgb-color i i i)  color-table)))
20           v))
21        (grayscale (lambda (x)
22                     ;; 0 becomes transparent
23                     (cond
24                       ((< (elt x 3) 128) 0)
25                       (t (max 1
26                               (round
27                                (/ (+ (elt x 1) (elt x 2) (elt x 0)) 3)))))))
28        (image-data
29         (let ((id (skippy:make-image-data w h)))
30           (dotimes (i w)
31             (dotimes (j h)
32               (setf (elt id (+ i (* w (- h j 1))))
33                     (elt colors
34                          (funcall grayscale
35                                   (make-array '(4)
36                                               :element-type '(unsigned-byte 8)
37                                               :displaced-to img
38                                               :displaced-index-offset
39                                               (* 4 (+ i (* w j)))))))))
40           id))
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
45                      :height h :width w
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))))
50       
51
52 (defun stretched-image (imgs)
53   "Resize that image to 32x32 and convert it into a ppm."
54   (bmp-to-gif
55    (uxul-world::resize-bmp-blob imgs 32 32)))
56
57 (defun annotated-image (img ann)
58   "Add a (lower-left) annotation."
59 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;FIXME
60 img
61
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)))
73 )
74
75 (defun numbered-image (img num)
76   "Annotate the image with a number."
77   (annotated-image img (format nil "~d" num)))
78
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|))
95 ))
96
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)))
103
104 (defun config-button-image (button tkobject)
105   (ltk:format-wish "~A configure -image $~A" (ltk::widget-path button) (ltk::name tkobject)))
106
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 ...)."
110   (let ((ret nil))
111     (maphash #'(lambda (key val)
112                  (when val
113                    (push (concatenate 'list (list (car key) (cdr key)) val)  ret)))
114              item-table)
115     ret))
116
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))
120     (setf width height)
121     (setf height nilpferd))
122
123   (prepare-images)
124   (let ((item-table (make-hash-table :test 'equal)))
125     ;;initialize given level
126     (dolist (item level)
127       (setf (gethash (cons (car item) (cadr item)) item-table) (cddr item)))   
128     (ltk:with-ltk ()
129       (let*
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))
202                                         item-table nil))
203                           (cval2 nil)
204                          (cbtn (aref btns i j)))
205                      (when (listp cval)
206                        (setf cval2 cval)
207                        (setf cval (car cval)))
208                      (cond
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))
221                        ((eq cval nil)
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)))))
233                  (redraw-buttons ()
234                    "Redraw all Buttons"
235                      (dotimes (i width)
236                        (dotimes (j height)
237                          (redraw-button i j))))
238                  (react (i j)
239                    (let ((current-upper-x (car current-upper-left))
240                          (current-upper-y (cdr current-upper-left)))
241                      (cond
242                        ((eql current-chosen-object :info)
243                         (let ((sym (gethash (cons (+ i current-upper-x)
244                                           (+ j current-upper-y))
245                                     item-table nil)))
246                           (if sym
247                               (ltk:do-msg
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)
255                         (cond
256                           ((string= (ltk:text argument1-entry) "")
257                            (ltk:do-msg "Please give an argument in the left textbox")
258                            (return-from react))
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
264                            change."))
265                           (t
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))
270                                     item-table)
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))))
278                    (redraw-buttons)))
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)))
297
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)))
303
304           (ltk:grid empty-button 0 0)
305           (config-button-image empty-button empty)
306           (setf (ltk:command empty-button)
307                              #'(lambda ()
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)
312                              #'(lambda ()
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)
317                              #'(lambda ()
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)
322                              #'(lambda ()
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)
327                              #'(lambda ()
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)
332                              #'(lambda ()
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)
337                              #'(lambda ()
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)
342                              #'(lambda ()
343                                  (setf current-chosen-object 'uxul-world::leaf)))
344
345           (ltk:grid tulip-button 0 8)
346           (config-button-image tulip-button tulip)
347           (setf (ltk:command tulip-button)
348                              #'(lambda ()
349                                  (setf current-chosen-object 'uxul-world::tulip)))
350
351           (ltk:grid key-button 0 9)
352           (config-button-image key-button key)
353           (setf (ltk:command key-button)
354                              #'(lambda ()
355                                  (setf current-chosen-object 'uxul-world::key)))
356
357           (ltk:grid door-button 0 10)
358           (config-button-image door-button door)
359           (setf (ltk:command door-button)
360                              #'(lambda ()
361                                  (setf current-chosen-object 'uxul-world::door)))
362
363           (ltk:grid anchor-button 0 11)
364           (config-button-image anchor-button anchor)
365           (setf (ltk:command anchor-button)
366                              #'(lambda ()
367                                  (setf current-chosen-object 'uxul-world::anchor)))
368
369           (ltk:grid object-frame 0 0)
370           (ltk:pack objects-and-arrows)
371
372           (dotimes (i width)
373             (dotimes (j height)
374               (let ((cbtn
375                      (make-instance 'ltk:button
376                                     :master grid-frame
377                                     :text "")))
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))))
382           (redraw-buttons))))
383     (item-table-to-list item-table)))
384
385
386 (defun get-base64-char-for-number (i)
387   (declare (type (integer 0 63) i))
388   (elt "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" i))
389
390 (defun base64-encode-threebytes (byte1 byte2 byte3)
391   (declare (type (unsigned-byte 8) byte1 byte2 byte3))
392   (coerce
393    (list
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))  
398
399
400 (defun base64-encode-bytelist (bytelist &optional (ret ""))
401   (if bytelist
402       (if (cdr bytelist)
403           (if (cddr bytelist)
404               (base64-encode-bytelist
405                (cdddr bytelist)
406                (concatenate 'string
407                             ret
408                             (base64-encode-threebytes
409                              (car bytelist)
410                              (cadr bytelist)
411                              (caddr bytelist))))
412               ;;else (genau zwei elemente)
413               (concatenate 'string ret                     
414                            (base64-encode-threebytes
415                             (car bytelist)
416                             (cadr bytelist)
417                             0)
418                            "="))
419           ;;else (genau ein element)
420           (concatenate 'string ret                         
421                        (base64-encode-threebytes
422                         (car bytelist) 0 0)
423                        "=="))
424       ;;else (kein element)
425       ret))
426
427
428 (defun base64-encode-byteseq (byteseq &optional (ret ""))
429   (case (length byteseq)
430     (0 ret)
431     (1 (concatenate 'string ret                    
432                     (base64-encode-threebytes
433                      (elt byteseq 0) 0 0) "=="))
434     (2 (concatenate 'string ret                    
435                     (base64-encode-threebytes
436                      (elt byteseq 0)
437                      (elt byteseq 1)
438                      0)
439                     "="))
440     (t (base64-encode-byteseq
441         (subseq byteseq 3)
442         (concatenate 'string
443                      ret
444                      (base64-encode-threebytes
445                       (elt byteseq 0)
446                       (elt byteseq 1)
447                       (elt byteseq 2)))))))