-;;; Copyright 2009 Christoph Senjak
+;;; Copyright 2009-2011 Christoph Senjak
(in-package :uxul-world-leveleditor)
(defparameter *leveleditor-images* nil)
-(defun stretched-image (img)
- "Call ImageMagick to resize that file to 32x32."
- (lisp-magick:with-magick-wand (mywand)
- (lisp-magick::magick-read-image-blob mywand img)
- (lisp-magick::magick-resize-image mywand 32 32 #x00000000 1d0)
- (lisp-magick::magick-set-format mywand "gif")
- (lisp-magick::magick-get-image-blob mywand)))
+(defun bmp-to-gif (inblob)
+ "Convert BMP to Grayscale-Gif using skippy."
+ (let*
+ ((w (uxul-world::bmp-width inblob))
+ (h (uxul-world::bmp-height inblob))
+ (img (uxul-world::bmp-pixel-data inblob))
+ (color-table (skippy:make-color-table))
+ (colors
+ (let
+ ((v (make-array '(256) :adjustable nil)))
+ (dotimes (i 256)
+ (setf (elt v i)
+ (skippy:ensure-color (skippy:rgb-color i i i) color-table)))
+ v))
+ (grayscale (lambda (x)
+ ;; 0 becomes transparent
+ (cond
+ ((< (elt x 3) 128) 0)
+ (t (max 1
+ (round
+ (/ (+ (elt x 1) (elt x 2) (elt x 0)) 3)))))))
+ (image-data
+ (let ((id (skippy:make-image-data w h)))
+ (dotimes (i w)
+ (dotimes (j h)
+ (setf (elt id (+ i (* w (- h j 1))))
+ (elt colors
+ (funcall grayscale
+ (make-array '(4)
+ :element-type '(unsigned-byte 8)
+ :displaced-to img
+ :displaced-index-offset
+ (* 4 (+ i (* w j)))))))))
+ id))
+ (image (skippy:make-image :width w :height h
+ :image-data image-data
+ :transparency-index 0))
+ (data-stream (skippy:make-data-stream
+ :height h :width w
+ :color-table color-table)))
+ (skippy:add-image image data-stream)
+ (flexi-streams:with-output-to-sequence (out)
+ (skippy:write-data-stream data-stream out))))
+
+
+(defun stretched-image (imgs)
+ "Resize that image to 32x32 and convert it into a ppm."
+ (bmp-to-gif
+ (uxul-world::resize-bmp-blob imgs 32 32)))
(defun annotated-image (img ann)
"Add a (lower-left) annotation."
- (lisp-magick:with-magick-wand (mywand)
- (lisp-magick::magick-read-image-blob mywand img)
- (lisp-magick:with-drawing-wand (dw)
- (lisp-magick:with-pixel-wand (pw :comp (255 255 255))
- (lisp-magick::draw-set-text-under-color dw pw))
- (lisp-magick:with-pixel-wand (pw :comp (255 0 0))
- (lisp-magick::draw-set-fill-color dw pw))
- (lisp-magick:draw-annotation dw (coerce 0 'double-float) (coerce 32 'double-float) ann)
- (lisp-magick:magick-draw-image mywand dw))
- (lisp-magick::magick-set-format mywand "gif")
- (lisp-magick::magick-get-image-blob mywand)))
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;FIXME
+img
+
+;; (lisp-magick:with-magick-wand (mywand)
+;; (lisp-magick::magick-read-image-blob mywand img)
+;; (lisp-magick:with-drawing-wand (dw)
+;; (lisp-magick:with-pixel-wand (pw :comp (255 255 255))
+;; (lisp-magick::draw-set-text-under-color dw pw))
+;; (lisp-magick:with-pixel-wand (pw :comp (255 0 0))
+;; (lisp-magick::draw-set-fill-color dw pw))
+;; (lisp-magick:draw-annotation dw (coerce 0 'double-float) (coerce 32 'double-float) ann)
+;; (lisp-magick:magick-draw-image mywand dw))
+;; (lisp-magick::magick-set-format mywand "gif")
+;; (lisp-magick::magick-get-image-blob mywand)))
+)
(defun numbered-image (img num)
"Annotate the image with a number."
(defun prepare-images (&optional (care-about-initialization *leveleditor-images*))
(when (not care-about-initialization)
(setf *leveleditor-images* (make-hash-table))
+ (uxul-world::init-files)
(setf (gethash 'uxul-world::uxul *leveleditor-images*) (stretched-image uxul-world::|uxul_small1|))
(setf (gethash 'uxul-world::leaf *leveleditor-images*) (stretched-image uxul-world::|leaf|))
(setf (gethash 'uxul-world::nasobem *leveleditor-images*) (stretched-image uxul-world::|nasobem|))
(setf (gethash 'uxul-world::tulip *leveleditor-images*) (stretched-image uxul-world::|tulip|))
(setf (gethash 'uxul-world::door *leveleditor-images*) (stretched-image uxul-world::|door|))
(setf (gethash 'uxul-world::key *leveleditor-images*) (stretched-image uxul-world::|key|))
+ (setf (gethash 'uxul-world::anchor *leveleditor-images*) (stretched-image uxul-world::|anchor|))
))
(defun load-image-into-tk (png)
ret))
(defun level-editor (&optional (level nil) (width 16) (height 16))
+ ;; hack. swap "width" and "height". (too lazy to change it properly by now)
+ (let ((nilpferd width))
+ (setf width height)
+ (setf height nilpferd))
+
(prepare-images)
(let ((item-table (make-hash-table :test 'equal)))
;;initialize given level
(dolist (item level)
- (setf (gethash (cons (car item) (cadr item)) item-table) (cddr item)))
-
+ (setf (gethash (cons (car item) (cadr item)) item-table) (cddr item)))
(ltk:with-ltk ()
(let*
((uxul (load-image-into-tk (gethash 'uxul-world::uxul *leveleditor-images*)))
(tulip (load-image-into-tk (gethash 'uxul-world::tulip *leveleditor-images*)))
(key (load-image-into-tk (gethash 'uxul-world::key *leveleditor-images*)))
(door (load-image-into-tk (gethash 'uxul-world::door *leveleditor-images*)))
+ (anchor (load-image-into-tk (gethash 'uxul-world::anchor *leveleditor-images*)))
+ (anchors (make-hash-table :test 'equal))
(current-upper-left (cons 0 0))
(current-chosen-object 'uxul)
(objects-and-arrows (make-instance 'ltk:frame))
:master object-frame))
(door-button (make-instance 'ltk:button :text ""
:master object-frame))
+ (anchor-button (make-instance 'ltk:button :text ""
+ :master object-frame))
+ (info-button (make-instance 'ltk:button :text "Info"
+ :master object-frame))
(btns (make-array (list width height) :adjustable nil :element-type 'ltk:button)))
(labels ((redraw-button (i j)
"Redraw Button (i, j)"
(cval (gethash (cons (+ i current-upper-x)
(+ j current-upper-y))
item-table nil))
+ (cval2 nil)
(cbtn (aref btns i j)))
- (if (listp cval)
- (setf cval (car cval)))
+ (when (listp cval)
+ (setf cval2 cval)
+ (setf cval (car cval)))
(cond
((eq cval 'uxul-world::leaf)
(config-button-image cbtn leaf))
(config-button-image cbtn door))
((eq cval 'uxul-world::key)
(config-button-image cbtn key))
+ ((eq cval 'uxul-world::anchor)
+ (config-button-image cbtn (gethash (cadr cval2) anchors)))
((eq cval 'uxul-world::uxul)
(config-button-image cbtn uxul)))))
(redraw-buttons ()
(react (i j)
(let ((current-upper-x (car current-upper-left))
(current-upper-y (cdr current-upper-left)))
+ (cond
+ ((eql current-chosen-object :info)
+ (let ((sym (gethash (cons (+ i current-upper-x)
+ (+ j current-upper-y))
+ item-table nil)))
+ (if sym
+ (ltk:do-msg
+ (format nil (concatenate 'string
+ "Symbolname: \"" (symbol-name (car sym)) "\"~%"
+ "First argument: \"" (cadr sym) "\"~%"
+ "Second argument: \"" (caddr sym) "\"")))
+ (ltk:do-msg "There doesnt seem to be anything here."))
+ (return-from react)))
+ ((eql current-chosen-object 'uxul-world::anchor)
+ (cond
+ ((string= (ltk:text argument1-entry) "")
+ (ltk:do-msg "Please give an argument in the left textbox")
+ (return-from react))
+ ((gethash (ltk:text argument1-entry) anchors nil)
+ (ltk:do-msg "Warning: You already set an
+ anchor with the same dungeon-name. Make
+ sure that you remove one of them. Behavior
+ is not specified in this case and may
+ change."))
+ (t
+ (setf (gethash (ltk:text argument1-entry) anchors)
+ (load-image-into-tk (annotated-image (gethash 'uxul-world::anchor *leveleditor-images*) (ltk:text argument1-entry))))))))
(setf (gethash (cons (+ i current-upper-x)
(+ j current-upper-y))
item-table)
(ltk:grid ddown-button 2 2)
(setf (ltk:command ddown-button) #'(lambda () (move-field-about (- 1 width) 0)))
- (ltk:grid argument1-entry 1 0 :columnspan 5)
- (ltk:grid argument2-entry 1 6 :columnspan 5)
+ (ltk:grid argument1-entry 1 0 :columnspan 4)
+ (ltk:grid argument2-entry 1 5 :columnspan 4)
+ (ltk:grid info-button 1 9 :columnspan 2)
+ (setf (ltk:command info-button)
+ #'(lambda () (setf current-chosen-object :info)))
(ltk:grid empty-button 0 0)
(config-button-image empty-button empty)
#'(lambda ()
(setf current-chosen-object 'uxul-world::door)))
+ (ltk:grid anchor-button 0 11)
+ (config-button-image anchor-button anchor)
+ (setf (ltk:command anchor-button)
+ #'(lambda ()
+ (setf current-chosen-object 'uxul-world::anchor)))
+
(ltk:grid object-frame 0 0)
(ltk:pack objects-and-arrows)