aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core-units
diff options
context:
space:
mode:
Diffstat (limited to 'src/core-units')
-rw-r--r--src/core-units/container.lisp32
-rw-r--r--src/core-units/unit.lisp71
2 files changed, 103 insertions, 0 deletions
diff --git a/src/core-units/container.lisp b/src/core-units/container.lisp
new file mode 100644
index 0000000..afa68b3
--- /dev/null
+++ b/src/core-units/container.lisp
@@ -0,0 +1,32 @@
+;;;; units/container.lisp
+
+(in-package #:wheelwork)
+
+
+(defclass/std container ()
+ ((units :with :a))
+ (: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 drop-unit ((unit unit))
+ "Removes a unit from its container. Returns T if the unit actually was removed."
+ (when-let (container (unit-container unit))
+ (setf
+ (container-units container) (delete unit (container-units container))
+ (unit-container unit) nil)
+ t))
+
+(defmethod add-unit ((container container) (unit unit))
+ "Adds a unit to the end of a container (thus affecting render
+order). Makes sure to remove the unit from its current container if
+necessary."
+ (when (unit-container unit)
+ (drop-unit unit))
+ (push unit (container-units container))
+ (setf (unit-container unit) container)
+ unit)
+
+(defmethod cleanup ((container container))
+ (dolist (u (container-units container))
+ (cleanup u)))
diff --git a/src/core-units/unit.lisp b/src/core-units/unit.lisp
new file mode 100644
index 0000000..939293b
--- /dev/null
+++ b/src/core-units/unit.lisp
@@ -0,0 +1,71 @@
+;;;; units/unit.lisp
+
+(in-package #:wheelwork)
+
+(defclass/std unit ()
+ ((cached-model cached-projected-matrix :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.")))
+
+
+(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 (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))))
+
+(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))
+
+(defmethod projected-matrix ((thing unit))
+ (or (cached-projected-matrix thing)
+ (setf (cached-projected-matrix thing)
+ (mat:marr (mat:m* (application-projection *application*)
+ (model-matrix thing))))))