diff options
author | Colin Okay <colin@cicadas.surf> | 2022-07-06 09:33:14 -0500 |
---|---|---|
committer | Colin Okay <colin@cicadas.surf> | 2022-07-06 09:33:14 -0500 |
commit | fda1d9d08349dfe103b7af3ef8f305c1701933f6 (patch) | |
tree | 5cb896cf87d140df5bd05cb1f56566cfb0f17407 /src/core | |
parent | 8c94460d8c8f8b44ca9bcdebbf2906e84c969b19 (diff) |
[refactor] containers have render bounds
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/affine.lisp | 26 | ||||
-rw-r--r-- | src/core/container.lisp | 52 | ||||
-rw-r--r-- | src/core/unit.lisp | 9 |
3 files changed, 77 insertions, 10 deletions
diff --git a/src/core/affine.lisp b/src/core/affine.lisp index 7c87d16..7cfb805 100644 --- a/src/core/affine.lisp +++ b/src/core/affine.lisp @@ -3,7 +3,7 @@ (in-package #:wheelwork) (defclass/std affine (unit) - ((cached-model cached-projected-matrix cached-application :a) + ((cached-model cached-projected-matrix cached-application cached-rectangle :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))) @@ -13,9 +13,8 @@ (case (closer-mop:slot-definition-name slot) ((x y scale-x scale-y rotation) (setf (cached-model affine) nil - (cached-projected-matrix affine) nil)))) - - + (cached-projected-matrix affine) nil + (cached-rectangle affine) nil)))) (defun scale-by (affine amount) (with-accessors ((sx scale-x) (sy scale-y)) affine @@ -69,3 +68,22 @@ (setf (cached-projected-matrix affine) (mat:marr (mat:m* (application-projection (app-of-unit affine)) (model-matrix affine)))))) + +(defmethod get-rect ((affine-unit affine)) + (or (cached-rectangle affine-unit) + (setf (cached-rectangle affine-unit) + (with-accessors ((x x) (y y) (w width) (h height) (r rotation)) affine-unit + (let ((m + (mat:meye 4)) + (tr + (vec:vec (+ x (* 0.5 w)) (+ y (* 0.5 h)) 0.0))) + (mat:nmtranslate m tr) + (mat:nmrotate m vec:+vz+ r) + (mat:nmtranslate m (vec:v* -1.0 tr)) + + (list (mat:m* m (vec:vec x y 0.0 1.0)) + (mat:m* m (vec:vec x (+ y h) 0.0 1.0)) + (mat:m* m (vec:vec (+ x w) (+ y h) 0.0 1.0)) + (mat:m* m (vec:vec (+ x w) y 0.0 1.0)) + (mat:m* m (vec:vec x y 0.0 1.0)))))))) + diff --git a/src/core/container.lisp b/src/core/container.lisp index af01ff1..1410310 100644 --- a/src/core/container.lisp +++ b/src/core/container.lisp @@ -4,11 +4,46 @@ (defclass/std container (unit) - ((units :with :a)) + ((units :with :a) + (cached-rect :with) + (left right top bottom :with :std 0 :doc "container's box coordinates.")) (:documentation "Just a list of units. Made into a class so that transformation affine transformations methods can be specialzied on whole groups of units")) +(defmethod (setf closer-mop:slot-value-using-class) + (newval class (container container) slot) + (case (closer-mop:slot-definition-name slot) + ((left right top bottom) + (setf (container-cached-rect container) nil)))) + +(defmethod get-rect ((container container)) + (with-slots (cached-rect left right top bottom) container + (or cached-rect + (setf cached-rect + (list* (vec:vec left bottom 0 1.0) + (vec:vec left top 0 1.0) + (vec:vec right top 0 1.0) + (vec:vec right bottom 0 1.0) + (vec:vec left bottom 0 1.0)))))) + + +(defun point-in-contanier-box-p (c x y) + "Returns T if the ponit X Y is inside the bounds of the container." + (and (<= (container-left c) x (container-right c)) + (<= (container-bottom c) y (container-top c)))) + +(defun visible-in-container-p (unit) + "Returns T if UNIT is VISIBLEP, if it is inside a CONTAINER, if any + of the corners of its bounding rectangle are inside that CONTAINER. + + UNIT must implement the affine protocol." + (when (unit-visiblep unit) + (when-let ((container + (unit-container unit))) + (loop for pt in (get-rect unit) + thereis (point-in-contanier-box-p container (vec:vx pt) (vec:vy pt)))))) + (defmethod drop-unit ((unit unit)) "Removes a unit from its container. Returns T if the unit actually was removed." (when-let (container (unit-container unit)) @@ -30,3 +65,18 @@ necessary." (defmethod cleanup ((container container)) (dolist (u (container-units container)) (cleanup u))) + +(defmethod render ((container container)) + (let ((current + (gl:get* :scissor-box)) + (scale + (application-scale + (app-of-unit container)) + )) + (with-slots (left right top bottom) container + (gl:scissor (* left scale) (* scale bottom) (* scale (- right left)) (* scale (- top bottom))) + (unwind-protect + (dolist (u (container-units container)) + (when (visible-in-container-p u) + (render u))) + (gl:scissor (aref current 0) (aref current 1) (aref current 2) (aref current 3)))))) diff --git a/src/core/unit.lisp b/src/core/unit.lisp index fb02f08..e93ba56 100644 --- a/src/core/unit.lisp +++ b/src/core/unit.lisp @@ -21,11 +21,10 @@ in this application." (or (cached-application unit) (setf (cached-application unit) (labels ((rec (u) - (when-let (c (unit-container u)) - (etypecase c - (application c) - (unit (rec c)) - (null nil))))) + (etypecase u + (application u) + (unit (rec (unit-container u))) + (nil nil)))) (rec unit))))) |