From d6d3bede2aed711d906d82c86ea5ddb410e41549 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Tue, 16 Mar 2021 17:11:43 -0400 Subject: Add watchable slots to metaclass. --- starling/node-2d.scm | 30 +++++++++++++----------------- starling/node.scm | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/starling/node-2d.scm b/starling/node-2d.scm index 8ff7623..43e073e 100644 --- a/starling/node-2d.scm +++ b/starling/node-2d.scm @@ -453,8 +453,8 @@ ;;; (define-class () - (atlas #:accessor atlas #:init-keyword #:atlas #:asset? #t) - (index #:accessor index #:init-keyword #:index)) + (atlas #:accessor atlas #:init-keyword #:atlas #:asset? #t #:watch? #t) + (index #:accessor index #:init-keyword #:index #:init-value 0 #:watch? #t)) (define-method (sync-texture (sprite )) (set! (texture sprite) (texture-atlas-ref (atlas sprite) (index sprite)))) @@ -463,12 +463,7 @@ (next-method) (sync-texture sprite)) -(define-method ((setter atlas) (sprite ) atlas) - (slot-set! sprite 'atlas atlas) - (sync-texture sprite)) - -(define-method ((setter index) (sprite ) i) - (slot-set! sprite 'index i) +(define-method (on-change (sprite ) slot-name old new) (sync-texture sprite)) @@ -481,7 +476,7 @@ (frame-duration #:getter frame-duration #:init-keyword #:frame-duration #:init-form 250)) -(define-class () +(define-class () (atlas #:accessor atlas #:init-keyword #:atlas #:asset? #t) (animations #:accessor animations #:init-keyword #:animations) (current-animation #:accessor current-animation @@ -500,7 +495,8 @@ (time (mod (- (elapsed-time) (start-time sprite)) anim-duration)) (frame (vector-ref frames (inexact->exact (floor (/ time frame-duration)))))) - (set! (texture sprite) (texture-atlas-ref (atlas sprite) frame)))) + (when (not (= frame (index sprite))) + (set! (index sprite) frame)))) (define-method (change-animation (sprite ) name) (set! (current-animation sprite) name) @@ -534,16 +530,17 @@ ;;; (define-class () - (painter #:accessor painter #:init-keyword #:painter) + (painter #:accessor painter #:init-keyword #:painter #:watch? #t) (canvas #:accessor canvas #:init-thunk make-empty-canvas)) (define-method (initialize (path ) args) (next-method) (set-canvas-painter! (canvas path) (painter path))) -(define-method ((setter painter) (path ) p) - (slot-set! path 'painter p) - (set-canvas-painter! (canvas path) p)) +(define-method (on-change (path ) slot-name old new) + (pk 'change slot-name old new) + (when (eq? slot-name 'painter) + (set-canvas-painter! (canvas path) new))) (define-method (render (path ) alpha) (draw-canvas* (canvas path) (world-matrix path))) @@ -555,7 +552,7 @@ (define-class