From 4a9357d489352102570a6a2d7afb3bf096105195 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Thu, 11 Jul 2019 18:31:35 -0400 Subject: node: Add on-attach and on-detach methods. --- starling/node.scm | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/starling/node.scm b/starling/node.scm index 07fef48..920a9bd 100644 --- a/starling/node.scm +++ b/starling/node.scm @@ -1,5 +1,5 @@ ;;; Starling Game Engine -;;; Copyright © 2018 David Thompson +;;; Copyright © 2018, 2019 David Thompson ;;; ;;; This program is free software: you can redistribute it and/or ;;; modify it under the terms of the GNU General Public License as @@ -50,6 +50,8 @@ render-tree child-ref & + on-attach + on-detach attach-to detach run-script @@ -205,6 +207,12 @@ represented as a ratio in the range [0, 1]." ((_ parent child-name . rest) (& (child-ref parent 'child-name) . rest)))) +(define-method (on-attach (parent ) (child )) + #t) + +(define-method (on-detach (parent ) (child )) + #t) + (define-method (attach-to (new-parent ) . new-children) "Attach NEW-CHILDREN to NEW-PARENT." ;; Validate all children first. The whole operation will fail if @@ -230,6 +238,10 @@ represented as a ratio in the range [0, 1]." ;; must also be active. (when (active? new-parent) (activate child))) + new-children) + ;; Notify parent of attach event. + (for-each (lambda (child) + (on-attach new-parent child)) new-children)) (define-method (detach (node )) @@ -242,7 +254,8 @@ represented as a ratio in the range [0, 1]." ;; Detaching deactives the node and all of its children. (when (active? node) (deactivate node)) - (set! (parent node) #f))) + (set! (parent node) #f) + (on-detach p node))) (define-method (detach . nodes) "Detach all NODES from their respective parents." -- cgit v1.2.3