~lthms/fairy.lisp

ee08d011e59d5424dbd9dcd7d27a3083426c989f — Thomas Letan 6 years ago 735c45d
feature: It is possible to associate a key to an element in a layer
2 files changed, 19 insertions(+), 6 deletions(-)

M example.lisp
M fairy.lisp
M example.lisp => example.lisp +10 -1
@@ 9,7 9,7 @@
                             :height 150
                             :color (gamekit:vec4 1 0.3 0 1)))

(fairy:add-child *root* *rec*)
(fairy:add-child *root* *rec* :with-key :rectangle)

;; Then, we create a trivial-gamekit application.
(gamekit:defgame app () ()


@@ 27,6 27,15 @@

;; It is possible to update the scene description, and it will update the
;; screen.
(sleep 1)
(setf (fairy:width *rec*) 200)

(sleep 1)
(setf (fairy:origin *rec*) (gamekit:vec2 230 20))

(sleep 1)
(setf (fairy:boundary *root*) (gamekit:vec2 240 50))

;; And even remove an element from the scene
(sleep 1)
(fairy:delete-child *root* *rec*)

M fairy.lisp => fairy.lisp +9 -5
@@ 10,7 10,8 @@
           layer
           boundary
           add-child
           delete-child))
           delete-child
           delete-child-with-key))

(defgeneric draw (element)
  (:documentation "Draw the element on the screen"))


@@ 66,10 67,13 @@
        (ge.vg:scissors (gamekit:vec2 0 0)
                        (gamekit:x boundary) (gamekit:y boundary)))
      (dolist (e children)
        (draw e))))
        (draw (cdr e)))))

(defmethod add-child ((el layer) child)
  (setf (children el) (cons child (children el))))
(defmethod add-child ((el layer) child &key with-key)
  (setf (children el) (cons (cons with-key child) (children el))))

(defmethod delete-child ((el layer) child)
  (setf (children el) (delete child (children el))))
  (setf (children el) (remove child (children el) :key #'cdr)))

(defmethod delete-child-with-key ((el layer) key)
  (setf (children el) (remove key (children el) :key #'car)))