Tue, 19 Aug 2025 18:23:47 +0200
behaviors can now be paused while the node is hidden
demo/snake/snake.c | file | annotate | diff | comparison | revisions | |
src/ascension/behavior.h | file | annotate | diff | comparison | revisions | |
src/behavior.c | file | annotate | diff | comparison | revisions | |
src/scene.c | file | annotate | diff | comparison | revisions |
--- a/demo/snake/snake.c Tue Aug 19 18:05:35 2025 +0200 +++ b/demo/snake/snake.c Tue Aug 19 18:23:47 2025 +0200 @@ -184,6 +184,7 @@ ); asc_behavior_add(node, .func = fps_counter_update, .interval = asc_seconds(1)); asc_behavior_add(node, fps_counter_tie_to_corner); + asc_behavior_pause_all_while_hidden(node); asc_ui_add_node(node); return node; }
--- a/src/ascension/behavior.h Tue Aug 19 18:05:35 2025 +0200 +++ b/src/ascension/behavior.h Tue Aug 19 18:23:47 2025 +0200 @@ -44,6 +44,7 @@ uint64_t last_execution; uint64_t interval; cxmutstr name; + bool pause_while_hidden; bool enabled; bool killed; }; @@ -69,6 +70,11 @@ * Optional name of the behavior. */ const char *name; + /** + * Pauses execution of the behavior when the node is hidden. + * @see asc_scene_node_hide() + */ + bool pause_while_hidden; }; AscBehavior *asc_behavior_add_(AscSceneNode *node, struct asc_behavior_create_args args); @@ -119,8 +125,54 @@ behavior->enabled = false; } +/** + * Pauses execution of the behavior when the node is hidden. + * + * @param behavior the behavior + * @see asc_scene_node_hide() + */ +static inline void asc_behavior_pause_while_hidden(AscBehavior *behavior) { + behavior->pause_while_hidden = true; +} + +/** + * Do not pause execution of the behavior when the node is hidden. + * + * @param behavior the behavior + * @see asc_scene_node_hide() + */ +static inline void asc_behavior_continue_while_hidden(AscBehavior *behavior) { + behavior->pause_while_hidden = false; +} + +/** + * Enables all behaviors of a node. + * + * @param node the scene node + */ void asc_behavior_enable_all(AscSceneNode *node); +/** + * Disables all behaviors of a node. + * + * @param node the scene node + */ void asc_behavior_disable_all(AscSceneNode *node); +/** + * Pauses all behviors of a node when it gets hidden. + * + * @param node the scene node + * @see asc_scene_node_hide() + */ +void asc_behavior_pause_all_while_hidden(AscSceneNode *node); + +/** + * Makes all behaviors of a node continue even when the node is hidden. + * + * @param node the scene node + * @see asc_scene_node_hide() + */ +void asc_behavior_continue_all_while_hidden(AscSceneNode *node); + #endif
--- a/src/behavior.c Tue Aug 19 18:05:35 2025 +0200 +++ b/src/behavior.c Tue Aug 19 18:23:47 2025 +0200 @@ -68,6 +68,7 @@ behavior->data = args.data; behavior->interval = args.interval; behavior->last_execution = 0; + behavior->pause_while_hidden = args.pause_while_hidden; behavior->name = name; asc_dprintf("Create behavior: %"CX_PRIstr, CX_SFMT(behavior->name)); return behavior; @@ -80,8 +81,9 @@ } void asc_behavior_trigger(AscBehavior *behavior) { - assert(behavior->enabled); + if (!behavior->enabled) return; if (behavior->last_execution + behavior->interval > asc_context.total_nanos) return; + if (behavior->pause_while_hidden && asc_scene_node_is_hidden(behavior->node)) return; behavior->func(behavior); behavior->last_execution = asc_context.total_nanos; @@ -106,4 +108,18 @@ cx_foreach(AscBehavior*, behavior, iter) { behavior->enabled = false; } -} \ No newline at end of file +} + +void asc_behavior_pause_all_while_hidden(AscSceneNode *node) { + CxMapIterator iter = cxMapIteratorValues(node->behaviors); + cx_foreach(AscBehavior*, behavior, iter) { + behavior->pause_while_hidden = true; + } +} + +void asc_behavior_continue_all_while_hidden(AscSceneNode *node) { + CxMapIterator iter = cxMapIteratorValues(node->behaviors); + cx_foreach(AscBehavior*, behavior, iter) { + behavior->pause_while_hidden= false; + } +}
--- a/src/scene.c Tue Aug 19 18:05:35 2025 +0200 +++ b/src/scene.c Tue Aug 19 18:23:47 2025 +0200 @@ -75,9 +75,7 @@ cx_foreach(AscSceneNode*, node, iter) { CxMapIterator behavior_iter = cxMapMutIteratorValues(node->behaviors); cx_foreach(AscBehavior*, behavior, behavior_iter) { - if (behavior->enabled) { - asc_behavior_trigger(behavior); - } + asc_behavior_trigger(behavior); if (behavior->killed) { cxIteratorFlagRemoval(behavior_iter); }