From db3a517ff1dc820abc160be7cae39c18fc7564f1 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Sun, 31 Jul 2022 12:41:55 -0400 Subject: node-2d: Allow nodes to compute default size. --- starling/node-2d.scm | 58 ++++++++++++++++++++++++++++++++-------------------- 1 file 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 ) 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 )) (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 )) 0.0) + +(define-method (default-height (node )) 0.0) (define-method (on-child-resize (node ) child) #t) @@ -656,29 +684,15 @@ #:init-keyword #:blend-mode #:init-form blend:alpha)) -(define-method (refresh-sprite-size (sprite )) - (let ((t (texture sprite))) - (set! (width sprite) (texture-width t)) - (set! (height sprite) (texture-height t)))) - -(define-method (on-asset-reload (sprite ) slot-name asset) - (case slot-name - ((texture) - (refresh-sprite-size sprite)))) - -(define-method (on-change (sprite ) slot-name old new) - (case slot-name - ((texture) - (refresh-sprite-size sprite)) - (else - (next-method)))) +(define-method (default-width (sprite )) + (texture-width (texture sprite))) -(define-method (on-boot (sprite )) - (refresh-sprite-size sprite)) +(define-method (default-height (sprite )) + (texture-height (texture sprite))) (define-method (render (sprite ) alpha) (let* ((tex (texture sprite)) - (rect (texture-gl-rect tex)) + (rect (size sprite)) (batch (batch sprite)) (tint (tint sprite)) (matrix (world-matrix sprite))) -- cgit v1.2.3