56 node->behaviors = asc_behavior_new_map(); |
56 node->behaviors = asc_behavior_new_map(); |
57 } |
57 } |
58 cxmutstr name = args.name == NULL |
58 cxmutstr name = args.name == NULL |
59 ? asc_util_gen_name("behavior") |
59 ? asc_util_gen_name("behavior") |
60 : cx_mutstr(strdup(args.name)); |
60 : cx_mutstr(strdup(args.name)); |
|
61 // FIXME: we need the ordered map here, because the execution order of behaviors is important |
61 AscBehavior *behavior = cxMapEmplace(node->behaviors, name); |
62 AscBehavior *behavior = cxMapEmplace(node->behaviors, name); |
62 assert(behavior != NULL); |
63 assert(behavior != NULL); |
63 behavior->enabled = true; |
64 behavior->enabled = !args.start_disabled; |
|
65 behavior->always_enabled = args.always_enabled; |
64 behavior->killed = false; |
66 behavior->killed = false; |
65 behavior->node = node; |
67 behavior->node = node; |
66 behavior->func = args.func; |
68 behavior->func = args.func; |
67 behavior->destroy_func = args.destroy_func; |
69 behavior->destroy_func = args.destroy_func; |
68 behavior->data = args.data; |
70 behavior->data = args.data; |
81 } |
83 } |
82 |
84 |
83 void asc_behavior_trigger(AscBehavior *behavior) { |
85 void asc_behavior_trigger(AscBehavior *behavior) { |
84 if (!behavior->enabled) return; |
86 if (!behavior->enabled) return; |
85 if (behavior->last_execution + behavior->interval > asc_context.total_nanos) return; |
87 if (behavior->last_execution + behavior->interval > asc_context.total_nanos) return; |
86 if (behavior->pause_while_hidden && asc_scene_node_is_hidden(behavior->node)) return; |
88 if (!behavior->always_enabled && behavior->pause_while_hidden && asc_scene_node_is_hidden(behavior->node)) return; |
87 |
89 |
88 behavior->func(behavior); |
90 behavior->func(behavior); |
89 behavior->last_execution = asc_context.total_nanos; |
91 behavior->last_execution = asc_context.total_nanos; |
90 } |
92 } |
91 |
93 |
97 // TODO: add asc_behavior_find() and asc_behavior_find_global() |
99 // TODO: add asc_behavior_find() and asc_behavior_find_global() |
98 |
100 |
99 void asc_behavior_enable_all(AscSceneNode *node) { |
101 void asc_behavior_enable_all(AscSceneNode *node) { |
100 CxMapIterator iter = cxMapIteratorValues(node->behaviors); |
102 CxMapIterator iter = cxMapIteratorValues(node->behaviors); |
101 cx_foreach(AscBehavior*, behavior, iter) { |
103 cx_foreach(AscBehavior*, behavior, iter) { |
102 behavior->enabled = true; |
104 asc_behavior_enable(behavior); |
103 } |
105 } |
104 } |
106 } |
105 |
107 |
106 void asc_behavior_disable_all(AscSceneNode *node) { |
108 void asc_behavior_disable_all(AscSceneNode *node) { |
107 CxMapIterator iter = cxMapIteratorValues(node->behaviors); |
109 CxMapIterator iter = cxMapIteratorValues(node->behaviors); |
108 cx_foreach(AscBehavior*, behavior, iter) { |
110 cx_foreach(AscBehavior*, behavior, iter) { |
109 behavior->enabled = false; |
111 asc_behavior_disable(behavior); |
110 } |
112 } |
111 } |
113 } |
112 |
114 |
113 void asc_behavior_pause_all_while_hidden(AscSceneNode *node) { |
115 void asc_behavior_pause_all_while_hidden(AscSceneNode *node) { |
114 CxMapIterator iter = cxMapIteratorValues(node->behaviors); |
116 CxMapIterator iter = cxMapIteratorValues(node->behaviors); |
115 cx_foreach(AscBehavior*, behavior, iter) { |
117 cx_foreach(AscBehavior*, behavior, iter) { |
116 behavior->pause_while_hidden = true; |
118 asc_behavior_pause_while_hidden(behavior); |
117 } |
119 } |
118 } |
120 } |
119 |
121 |
120 void asc_behavior_continue_all_while_hidden(AscSceneNode *node) { |
122 void asc_behavior_continue_all_while_hidden(AscSceneNode *node) { |
121 CxMapIterator iter = cxMapIteratorValues(node->behaviors); |
123 CxMapIterator iter = cxMapIteratorValues(node->behaviors); |
122 cx_foreach(AscBehavior*, behavior, iter) { |
124 cx_foreach(AscBehavior*, behavior, iter) { |
123 behavior->pause_while_hidden= false; |
125 asc_behavior_continue_while_hidden(behavior); |
124 } |
126 } |
125 } |
127 } |