summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2022-07-31 12:41:55 -0400
committerDavid Thompson <dthompson2@worcester.edu>2022-07-31 12:41:55 -0400
commitdb3a517ff1dc820abc160be7cae39c18fc7564f1 (patch)
treee27221a9ca340648787a2e623167daa4a9aac8cf
parentd1b91f8037f99bbb26a51f6d7b720f23f6358aad (diff)
node-2d: Allow nodes to compute default size.
-rw-r--r--starling/node-2d.scm58
1 files changed, 36 insertions, 22 deletions
diff --git a/starling/node-2d.scm b/starling/node-2d.scm
index 00cbf4b..8c48966 100644
--- a/starling/node-2d.scm
+++ b/starling/node-2d.scm
@@ -79,6 +79,8 @@
world-matrix
width
height
+ default-width
+ default-height
bounding-box
on-child-resize
dirty!
@@ -359,12 +361,33 @@
(local-matrix #:getter local-matrix #:init-form (make-identity-matrix4))
(world-matrix #:getter world-matrix #:init-form (make-identity-matrix4))
(dirty-matrix? #:accessor dirty-matrix? #:init-form #t)
- ;; Bounding box for render culling, mouse selection, etc.
- (width #:accessor width #:init-keyword #:width #:init-form 0.0 #:watch? #t)
- (height #:accessor height #:init-keyword #:height #:init-form 0.0 #:watch? #t)
+ ;; Node dimensions. Stored as a rectangle for convenience
+ ;; elsewhere, so it can be used as a bounding box that doesn't take
+ ;; any transformation matrix into consideration.
+ (size #:getter size #:init-form (make-rect 0.0 0.0 0.0 0.0))
+ (width #:accessor width #:init-keyword #:width #:watch? #t #:allocation #:virtual
+ #:slot-ref (lambda (node) (rect-width (size node)))
+ #:slot-set! (lambda (node w) (set-rect-width! (size node) w)))
+ (height #:accessor height #:init-keyword #:height #:watch? #t #:allocation #:virtual
+ #:slot-ref (lambda (node) (rect-height (size node)))
+ #:slot-set! (lambda (node h) (set-rect-height! (size node) h)))
+ ;; Bounding box for render culling, mouse selection, clipping, etc.
+ ;; This bounding box incorporates the local transformation matrix.
(bounding-box #:getter bounding-box #:init-form (make-rect 0.0 0.0 0.0 0.0))
(dirty-bounding-box? #:accessor dirty-bounding-box? #:init-form #t))
+(define-method (initialize (node <node-2d>) args)
+ (next-method)
+ ;; If caller doesn't specify a custom width and height, let the node
+ ;; pick a reasonable default size.
+ (when (= (width node) 0.0)
+ (set! (width node) (default-width node)))
+ (when (= (height node) 0.0)
+ (set! (height node) (default-height node)))
+ ;; Build an initial bounding box.
+ (refresh-local-matrix node)
+ (refresh-bounding-box node))
+
(define-method (dirty! (node <node-2d>))
(set! (dirty-matrix? node) #t)
(set! (dirty-bounding-box? node) #t))
@@ -391,6 +414,11 @@
(refresh-local-matrix node)
(refresh-world-matrix node (parent node)))
+;; Size and bounding box
+
+(define-method (default-width (node <node-2d>)) 0.0)
+
+(define-method (default-height (node <node-2d>)) 0.0)
(define-method (on-child-resize (node <node-2d>) child)
#t)
@@ -656,29 +684,15 @@
#:init-keyword #:blend-mode
#:init-form blend:alpha))
-(define-method (refresh-sprite-size (sprite <sprite>))
- (let ((t (texture sprite)))
- (set! (width sprite) (texture-width t))
- (set! (height sprite) (texture-height t))))
-
-(define-method (on-asset-reload (sprite <sprite>) slot-name asset)
- (case slot-name
- ((texture)
- (refresh-sprite-size sprite))))
-
-(define-method (on-change (sprite <sprite>) slot-name old new)
- (case slot-name
- ((texture)
- (refresh-sprite-size sprite))
- (else
- (next-method))))
+(define-method (default-width (sprite <sprite>))
+ (texture-width (texture sprite)))
-(define-method (on-boot (sprite <sprite>))
- (refresh-sprite-size sprite))
+(define-method (default-height (sprite <sprite>))
+ (texture-height (texture sprite)))
(define-method (render (sprite <sprite>) alpha)
(let* ((tex (texture sprite))
- (rect (texture-gl-rect tex))
+ (rect (size sprite))
(batch (batch sprite))
(tint (tint sprite))
(matrix (world-matrix sprite)))