;;;; unit.lisp (in-package #:wheelwork) (def:class unit () (cached-model cached-projected-matrix cached-rectangle :initform nil :documentation "internal caches") ((visiblep "Whether or not to render unit") :prefix :type boolean :initform t) ((in-scene-p "Indicates if unit is considered for display & events") :prefix :type boolean :initform nil) ((region "The screen region where this unit will be visible.") :prefix :type region :initform *application*) ((base-height "Content's base height") (base-width "Content's base width") :ro :type float :initform 1.0) ((scale-x "Factor by which to resize base-width") (scale-y "Factor by which to resize base-heght") :type float :initform 1.0) ((rotation "Rotation in radians about objects' bounding box center") (x "X position, → is positive direction") (y "Y position, ↑ is positive direction") :type float :initform 0.0) :documentation "Fundamental display unit") (defmethod render :around ((unit unit)) (when (unit-visiblep unit) (if (not (eq *application* (unit-region unit))) (let ((sb (gl:get* :scissor-box)) (scale (application-scale *application*))) (with-slots (left right top bottom) (unit-region unit) (gl:scissor (* left scale) (* bottom scale) (* scale (- right left)) (* scale (- top bottom))) (call-next-method)) (gl:scissor (aref sb 0) (aref sb 1) (aref sb 2) (aref sb 3))) (call-next-method)))) (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 (cached-rectangle unit) nil)))) (defun scale-by (unit amount) (with-accessors ((sx scale-x) (sy scale-y)) unit (setf sx (* amount sx) sy (* amount sy)))) (defun set-width-preserve-aspect (unit new-width) (scale-by unit (/ new-width (width unit)))) (defun set-height-preserve-aspect (unit new-height) (scale-by unit (/ new-height (height unit) ))) (defmethod width ((unit unit)) (with-slots (scale-x base-width) unit (* scale-x base-width))) (defmethod height ((unit unit)) (with-slots (scale-y base-height) unit (* scale-y base-height))) (defmethod (setf width) (newval (unit unit)) (with-slots (scale-x base-width) unit (setf scale-x (coerce (/ newval base-width) 'single-float)))) (defmethod (setf 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 projected-matrix (unit) (or (cached-projected-matrix unit) (setf (cached-projected-matrix unit) (mat:marr (mat:m* (application-projection *application*) (model-matrix unit)))))) (defmethod get-rect ((unit unit)) (or (cached-rectangle unit) (setf (cached-rectangle unit) (with-accessors ((x x) (y y) (w width) (h height) (r rotation)) 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)) (loop for vec in (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))) collect (list (vec:vx vec) (vec:vy vec)))))))) (defun units-intersect-p (au1 au2) "Returns T if the two units AU1 an AU2 intersect. Both must implement GET-RECT." (paths-intersect-p (get-rect au1) (get-rect au2)))