diff options
author | Colin Okay <colin@cicadas.surf> | 2022-06-30 07:35:16 -0500 |
---|---|---|
committer | Colin Okay <colin@cicadas.surf> | 2022-06-30 07:35:16 -0500 |
commit | e0fc8f0f7a8b4756226cfb5e1c7581e411420228 (patch) | |
tree | 57213d8d2997e34d1ba674360b870d4223cd4908 | |
parent | 56b743bbfe56823bfe482a4f4e579512041918d2 (diff) |
[refactor] factored out affine from unit; made container a unit;
-rw-r--r-- | examples/02-moving-bitmp.lisp | 28 | ||||
-rw-r--r-- | examples/03-font-render.lisp | 18 | ||||
-rw-r--r-- | src/core-units/affine.lisp | 71 | ||||
-rw-r--r-- | src/core-units/container.lisp | 2 | ||||
-rw-r--r-- | src/core-units/unit.lisp | 65 | ||||
-rw-r--r-- | src/interactive-units/bitmap.lisp | 3 | ||||
-rw-r--r-- | src/interactive-units/text.lisp | 2 | ||||
-rw-r--r-- | src/wheelwork.lisp | 2 | ||||
-rw-r--r-- | wheelwork.asd | 3 |
9 files changed, 104 insertions, 90 deletions
diff --git a/examples/02-moving-bitmp.lisp b/examples/02-moving-bitmp.lisp index 3c9ee89..bb6ae8f 100644 --- a/examples/02-moving-bitmp.lisp +++ b/examples/02-moving-bitmp.lisp @@ -15,20 +15,20 @@ "Move the target around, grow and shrink it. Print out its position no matter what happens." (case scancode - (:scancode-left (decf (ww::unit-x target) )) - (:scancode-right (incf (ww::unit-x target) )) - (:scancode-down (decf (ww::unit-y target) )) - (:scancode-up (incf (ww::unit-y target) )) + (:scancode-left (decf (ww::x target) )) + (:scancode-right (incf (ww::x target) )) + (:scancode-down (decf (ww::y target) )) + (:scancode-up (incf (ww::y target) )) (:scancode-w (incf (ww::unit-width target) 20)) - (:scancode-r (incf (ww::unit-rotation target) (/ pi 3))) - (:scancode-l (decf (ww::unit-rotation target) (/ pi 3))) + (:scancode-r (incf (ww::rotation target) (/ pi 3))) + (:scancode-l (decf (ww::rotation target) (/ pi 3))) (:scancode-equals (when (or (member :lshift modifiers) (member :rshift modifiers)) (ww::scale-by target 1.10))) (:scancode-minus (ww::scale-by target 0.9))) (format t "ghoul pos: ~a,~a~%" - (ww::unit-x target) (ww::unit-y target)))) + (ww::x target) (ww::y target)))) (ww::defhandler animate-move-thing (ww::on-keydown () @@ -37,8 +37,8 @@ (when (member scancode '(:scancode-left :scancode-right :scancode-down :scancode-up)) (unless (gethash target *shared-anim-table*) (setf (gethash target *shared-anim-table*) t) - (let* ((tx (ww::unit-x target)) - (ty (ww::unit-y target)) + (let* ((tx (ww::x target)) + (ty (ww::y target)) (destx tx) (desty ty) (dx 0) @@ -66,7 +66,7 @@ (ww::defhandler flip-on-click (ww::on-mousedown () - (incf (ww::unit-rotation target) (ww::radians 180) ))) + (incf (ww::rotation target) (ww::radians 180) ))) (ww::defhandler twirl-on-click (ww::on-mousedown () @@ -78,9 +78,9 @@ (ww::on-perframe () (if (< rot (* 8 pi)) (setf rot (+ 0.3 rot) - (ww::unit-rotation target) rot) + (ww::rotation target) rot) (progn - (setf (ww::unit-rotation target) 0.0) + (setf (ww::rotation target) 0.0) (ww::remove-handler target 'ww::perframe) (remhash target *shared-anim-table*))))))))) @@ -117,8 +117,8 @@ (ww::add-handler bm #'mouse-over) ;;second - (setf (ww::unit-x bm2) 90 - (ww::unit-y bm2) 90) + (setf (ww::x bm2) 90 + (ww::y bm2) 90) (ww::add-handler bm2 #'move-thing) (ww::add-handler bm2 #'twirl-on-click ) (ww::add-handler bm2 #'look-at-me) diff --git a/examples/03-font-render.lisp b/examples/03-font-render.lisp index 6f7d1a7..e36dc44 100644 --- a/examples/03-font-render.lisp +++ b/examples/03-font-render.lisp @@ -16,15 +16,15 @@ (format t "Pressed a key, changing the color~%") (setf (ww::text-color target) (random-text-color)) - (with-accessors ((x ww::unit-x) (y ww::unit-y) (w ww::unit-width) (h ww::unit-height)) target + (with-accessors ((x ww::x) (y ww::y) (w ww::unit-width) (h ww::unit-height)) target (format t "x:~a,y:~a,width:~a,height:~a~%" x y w h)))) (ww::defhandler marquee (ww::on-perframe () - (when (< 900 (ww::unit-x target)) - (setf (ww::unit-x target) + (when (< 900 (ww::x target)) + (setf (ww::x target) -800)) - (incf (ww::unit-x target) 5 ))) + (incf (ww::x target) 5 ))) (defvar *spin-table* (make-hash-table :synchronized t)) @@ -36,9 +36,9 @@ (setf (gethash target *spin-table*) (+ rot 0.88) (ww::text-color target) (random-text-color) - (ww::unit-rotation target) rot) + (ww::rotation target) rot) (progn - (setf (ww::unit-rotation target) 0.0) + (setf (ww::rotation target) 0.0) (ww::remove-handler target #'spin) (remhash target *spin-table*)))))) @@ -61,8 +61,8 @@ (ww::scale-by hello 3.0) (setf - (ww::unit-x hello) (* 0.5 (- 800 (ww::unit-width hello))) - (ww::unit-y hello) (* 0.5 (- 600 (ww::unit-height hello)))) + (ww::x hello) (* 0.5 (- 800 (ww::unit-width hello))) + (ww::y hello) (* 0.5 (- 600 (ww::unit-height hello)))) (ww::add-handler hello #'marquee) (ww::add-handler hello #'change-text-color) (ww::add-handler hello #'twirl-on-click) @@ -71,7 +71,7 @@ (ww::scale-by instructions 2.0) (setf - (ww::unit-x instructions) (* 0.5 (- 800 (ww::unit-width instructions)))) + (ww::x instructions) (* 0.5 (- 800 (ww::unit-width instructions)))) (ww::add-unit app instructions))) diff --git a/src/core-units/affine.lisp b/src/core-units/affine.lisp new file mode 100644 index 0000000..e4db874 --- /dev/null +++ b/src/core-units/affine.lisp @@ -0,0 +1,71 @@ +;;;; affine.lisp + +(in-package #:wheelwork) + +(defclass/std affine (unit) + ((cached-model cached-projected-matrix cached-application :a) + (base-width base-height :r :std 1.0 :doc "Determined by content.") + (scale-x scale-y :std 1.0) + (rotation x y :std 0.0))) + +(defmethod (setf closer-mop:slot-value-using-class) :after + (newval class (affine affine) slot) + (case (closer-mop:slot-definition-name slot) + ((x y scale-x scale-y rotation) + (setf (cached-model affine) nil + (cached-projected-matrix affine) nil)))) + +(defun scale-by (affine amount) + (with-slots (scale-x scale-y) affine + (setf scale-x (* amount scale-x) + scale-y (* amount scale-y)))) + +(defun set-width-preserve-aspect (affine new-width) + (scale-by affine (/ new-width (unit-width affine)))) + +(defun set-height-preserve-aspect (affine new-height) + (scale-by affine (/ new-height (unit-height affine) ))) + +(defmethod unit-width ((affine affine)) + (with-slots (scale-x base-width) affine + (* scale-x base-width))) + +(defmethod unit-height ((affine affine)) + (with-slots (scale-y base-height) affine + (* scale-y base-height))) + +(defmethod (setf unit-width) (newval (affine affine)) + (with-slots (scale-x base-width) affine + (setf scale-x (coerce (/ newval base-width) 'single-float)))) + +(defmethod (setf unit-height) (newval (affine affine)) + (with-slots (scale-y base-height) affine + (setf scale-y (coerce (/ newval base-height) 'single-float)))) + + + +(defmethod model-matrix :around ((u affine)) + (or (cached-model u) + (setf (cached-model u) + (call-next-method)))) + +(defmethod model-matrix ((u affine)) + (let ((m (mat:meye 4))) + (with-slots (x y base-width scale-x base-height scale-y rotation) u + (let ((uw (* base-width scale-x)) + (uh (* base-height scale-y))) + (mat:nmtranslate m (vec:vec x y 0.0)) + + (mat:nmtranslate m (vec:v* 0.5 (vec:vec uw uh 0.0))) + (mat:nmrotate m vec:+vz+ rotation) + (mat:nmtranslate m (vec:v* -0.5 (vec:vec uw uh 0.0))) + + (mat:nmscale m (vec:vec uw uh 1.0)))) + m)) + + +(defun projected-matrix (affine) + (or (cached-projected-matrix affine) + (setf (cached-projected-matrix affine) + (mat:marr (mat:m* (application-projection (app-of-unit affine)) + (model-matrix affine)))))) diff --git a/src/core-units/container.lisp b/src/core-units/container.lisp index afa68b3..af01ff1 100644 --- a/src/core-units/container.lisp +++ b/src/core-units/container.lisp @@ -3,7 +3,7 @@ (in-package #:wheelwork) -(defclass/std container () +(defclass/std container (unit) ((units :with :a)) (:documentation "Just a list of units. Made into a class so that transformation affine transformations methods can be specialzied on diff --git a/src/core-units/unit.lisp b/src/core-units/unit.lisp index 31f268c..20e05e2 100644 --- a/src/core-units/unit.lisp +++ b/src/core-units/unit.lisp @@ -3,70 +3,15 @@ (in-package #:wheelwork) (defclass/std unit () - ((cached-model cached-projected-matrix cached-application :a) - (container :with :a) - (base-width base-height :r :std 1.0 :doc "Determined by content.") - (scale-x scale-y :with :std 1.0) - (rotation x y :with :std 0.0) - (opacity :std 1.0 :doc "0.0 indicates it will not be rendred."))) + ((cached-application :a) + (container :with :a))) (defmethod (setf closer-mop:slot-value-using-class) :after (newval class (unit unit) slot) (case (closer-mop:slot-definition-name slot) - ((x y scale-x scale-y rotation) - (setf (cached-model unit) nil - (cached-projected-matrix unit) nil)) (container (setf (cached-application unit) nil)))) -(defun scale-by (unit amount) - (with-slots (scale-x scale-y) unit - (setf scale-x (* amount scale-x) - scale-y (* amount scale-y)))) - -(defun set-width-preserve-aspect (unit new-width) - (scale-by unit (/ new-width (unit-width unit)))) - -(defun set-height-preserve-aspect (unit new-height) - (scale-by unit (/ new-height (unit-height unit) ))) - -(defmethod unit-width ((unit unit)) - (with-slots (scale-x base-width) unit - (* scale-x base-width))) - -(defmethod unit-height ((unit unit)) - (with-slots (scale-y base-height) unit - (* scale-y base-height))) - -(defmethod (setf unit-width) (newval (unit unit)) - (with-slots (scale-x base-width) unit - (setf scale-x (coerce (/ newval base-width) 'single-float)))) - -(defmethod (setf unit-height) (newval (unit unit)) - (with-slots (scale-y base-height) unit - (setf scale-y (coerce (/ newval base-height) 'single-float)))) - - - -(defmethod model-matrix :around ((u unit)) - (or (cached-model u) - (setf (cached-model u) - (call-next-method)))) - -(defmethod model-matrix ((u unit)) - (let ((m (mat:meye 4))) - (with-slots (x y base-width scale-x base-height scale-y rotation) u - (let ((uw (* base-width scale-x)) - (uh (* base-height scale-y))) - (mat:nmtranslate m (vec:vec x y 0.0)) - - (mat:nmtranslate m (vec:v* 0.5 (vec:vec uw uh 0.0))) - (mat:nmrotate m vec:+vz+ rotation) - (mat:nmtranslate m (vec:v* -0.5 (vec:vec uw uh 0.0))) - - (mat:nmscale m (vec:vec uw uh 1.0)))) - m)) - (defun app-of-unit (unit) "Returns the APPLICATION instance, if any, of which this UNIT is a @@ -82,8 +27,4 @@ in this application." (null nil))))) (rec unit))))) -(defun projected-matrix (unit) - (or (cached-projected-matrix unit) - (setf (cached-projected-matrix unit) - (mat:marr (mat:m* (application-projection (app-of-unit unit)) - (model-matrix unit)))))) + diff --git a/src/interactive-units/bitmap.lisp b/src/interactive-units/bitmap.lisp index 95dfff5..cc4b4f7 100644 --- a/src/interactive-units/bitmap.lisp +++ b/src/interactive-units/bitmap.lisp @@ -2,7 +2,7 @@ (in-package #:wheelwork) -(defclass/std bitmap (unit interactive) +(defclass/std bitmap (affine interactive) ((texture :ri :std (error "A bitmap requires a texture.")) (vao shader :with :r :static))) @@ -57,6 +57,7 @@ (gl:delete-program shader)) (setf vao nil shader nil))) + (defmethod render ((bitmap bitmap)) (with-slots (texture vao shader) bitmap (gl:active-texture 0) diff --git a/src/interactive-units/text.lisp b/src/interactive-units/text.lisp index 1cecdbf..caba48e 100644 --- a/src/interactive-units/text.lisp +++ b/src/interactive-units/text.lisp @@ -2,7 +2,7 @@ (in-package #:wheelwork) -(defclass/std text (unit interactive) +(defclass/std text (affine interactive) ((font :with :ri :std (error "A font is required") :type font) (content :with :ri :std "") (color :with :std #(1.0 1.0 1.0 1.0)) diff --git a/src/wheelwork.lisp b/src/wheelwork.lisp index b6da6b9..885ae1e 100644 --- a/src/wheelwork.lisp +++ b/src/wheelwork.lisp @@ -66,7 +66,7 @@ TARGET is FOCUSABLEP" (defun get-rect (unit) "Returns a list of vectors representing the path of the smallest rectangle that encloses the unit. The rectangle is scaled and rotated." - (with-accessors ((x unit-x) (y unit-y) (w unit-width) (h unit-height) (r unit-rotation)) unit + (with-accessors ((x x) (y y) (w unit-width) (h unit-height) (r rotation)) unit (let ((m (mat:meye 4)) (tr diff --git a/wheelwork.asd b/wheelwork.asd index ef3a1a3..4acb79e 100644 --- a/wheelwork.asd +++ b/wheelwork.asd @@ -31,7 +31,8 @@ (:file "font"))) (:module "core-units" :components ((:file "unit") - (:file "container"))) + (:file "container") + (:file "affine"))) (:module "events" :components ((:file "event-handler") (:file "listener-and-interactive"))) |