This source file includes following definitions.
- gf_sg_route_new
- gf_sg_route_exists
- gf_sg_route_del
- gf_sg_route_del_by_id
- gf_sg_destroy_routes
- gf_sg_route_queue
- gf_sg_activate_routes
- gf_sg_route_unqueue
- gf_sg_route_find
- gf_sg_route_find_by_name
- gf_sg_route_set_id
- gf_sg_route_get_id
- gf_sg_route_set_name
- gf_sg_route_get_name
- gf_sg_route_setup
- gf_node_event_out_proto
- gf_sg_route_activate
- gf_node_event_out
- gf_node_event_out_str
- on_route_to_function
- gf_sg_route_new_to_callback
#include <gpac/internal/scenegraph_dev.h>
#include <gpac/nodes_mpeg4.h>
#include <gpac/nodes_x3d.h>
#ifndef GPAC_DISABLE_VRML
GF_Route* gf_sg_route_exists(GF_SceneGraph *sg, GF_Node *fromNode, u32 fromField, GF_Node *toNode, u32 toField);
GF_EXPORT
GF_Route *gf_sg_route_new(GF_SceneGraph *sg, GF_Node *fromNode, u32 fromField, GF_Node *toNode, u32 toField)
{
GF_Route *r;
if (!sg || !toNode || !fromNode) return NULL;
if ( (r = gf_sg_route_exists(sg, fromNode, fromField, toNode, toField)) )
return r;
GF_SAFEALLOC(r, GF_Route)
if (!r) return NULL;
r->FromNode = fromNode;
r->FromField.fieldIndex = fromField;
r->ToNode = toNode;
r->ToField.fieldIndex = toField;
r->graph = sg;
if (!fromNode->sgprivate->interact) {
GF_SAFEALLOC(fromNode->sgprivate->interact, struct _node_interactive_ext);
if (!fromNode->sgprivate->interact) {
GF_LOG(GF_LOG_ERROR, GF_LOG_SCENE, ("[VRML] Failed to create interact storage\n"));
gf_free(r);
return NULL;
}
}
if (!fromNode->sgprivate->interact->routes) fromNode->sgprivate->interact->routes = gf_list_new();
gf_list_add(fromNode->sgprivate->interact->routes, r);
gf_list_add(sg->Routes, r);
return r;
}
GF_Route* gf_sg_route_exists(GF_SceneGraph *sg, GF_Node *fromNode, u32 fromField, GF_Node *toNode, u32 toField)
{
u32 i = 0;
GF_Route* rt;
if ( !fromNode->sgprivate->interact || !fromNode->sgprivate->interact->routes )
return NULL;
while ( (rt = (GF_Route*)gf_list_enum(fromNode->sgprivate->interact->routes, &i) )) {
if ( rt->FromField.fieldIndex == fromField && rt->ToNode == toNode && rt->ToField.fieldIndex == toField )
return rt;
}
return NULL;
}
void gf_sg_route_del(GF_Route *r)
{
GF_SceneGraph *sg;
gf_list_del_item(r->graph->Routes, r);
if (r->FromNode && r->FromNode->sgprivate->interact && r->FromNode->sgprivate->interact->routes) {
gf_list_del_item(r->FromNode->sgprivate->interact->routes, r);
if (!gf_list_count(r->FromNode->sgprivate->interact->routes)) {
gf_list_del(r->FromNode->sgprivate->interact->routes);
r->FromNode->sgprivate->interact->routes = NULL;
}
}
if (r->ToNode && (r->ToField.fieldType==GF_SG_VRML_SCRIPT_FUNCTION) && r->ToField.on_event_in) {
r->is_setup = 0;
r->FromNode = NULL;
if (!r->graph->pOwningProto) r->ToField.on_event_in(r->ToNode, r);
}
r->is_setup = 0;
sg = r->graph;
while (sg->parent_scene) sg = sg->parent_scene;
gf_list_add(sg->routes_to_destroy, r);
gf_list_del_item(sg->routes_to_activate, r);
}
GF_Err gf_sg_route_del_by_id(GF_SceneGraph *sg,u32 routeID)
{
GF_Route *r;
if(!sg) return GF_BAD_PARAM;
r = gf_sg_route_find(sg, routeID);
if (!r) return GF_BAD_PARAM;
gf_sg_route_del(r);
return GF_OK;
}
void gf_sg_destroy_routes(GF_SceneGraph *sg)
{
while (gf_list_count(sg->routes_to_destroy) ) {
GF_Route *r = (GF_Route *)gf_list_get(sg->routes_to_destroy, 0);
gf_list_rem(sg->routes_to_destroy, 0);
gf_sg_route_unqueue(sg, r);
if (r->name) gf_free(r->name);
gf_free(r);
}
}
void gf_sg_route_queue(GF_SceneGraph *sg, GF_Route *r)
{
u32 now;
if (!sg) return;
while (sg->parent_scene) sg = sg->parent_scene;
now = 1 + sg->simulation_tick;
if (r->lastActivateTime >= now) return;
r->lastActivateTime = now;
gf_list_add(sg->routes_to_activate, r);
}
GF_EXPORT
void gf_sg_activate_routes(GF_SceneGraph *sg)
{
GF_Route *r;
GF_Node *targ;
if (!sg) return;
sg->simulation_tick++;
gf_sg_destroy_routes(sg);
while (gf_list_count(sg->routes_to_activate)) {
r = (GF_Route *)gf_list_get(sg->routes_to_activate, 0);
gf_list_rem(sg->routes_to_activate, 0);
if (r) {
targ = r->ToNode;
if (gf_sg_route_activate(r)) {
#ifdef GF_SELF_REPLACE_ENABLE
if (sg->graph_has_been_reset) {
sg->graph_has_been_reset = 0;
return;
}
#endif
if (r->is_setup) gf_node_changed(targ, &r->ToField);
}
}
}
}
void gf_sg_route_unqueue(GF_SceneGraph *sg, GF_Route *r)
{
while (sg->parent_scene) sg = sg->parent_scene;
gf_list_del_item(sg->routes_to_activate, r);
}
GF_Route *gf_sg_route_find(GF_SceneGraph *sg, u32 RouteID)
{
GF_Route *r;
u32 i=0;
while ((r = (GF_Route*)gf_list_enum(sg->Routes, &i))) {
if (r->ID == RouteID) return r;
}
return NULL;
}
GF_Route *gf_sg_route_find_by_name(GF_SceneGraph *sg, char *name)
{
GF_Route *r;
u32 i;
if (!sg || !name) return NULL;
i=0;
while ((r = (GF_Route*)gf_list_enum(sg->Routes, &i))) {
if (r->name && !strcmp(r->name, name)) return r;
}
return NULL;
}
GF_Err gf_sg_route_set_id(GF_Route *route, u32 ID)
{
GF_Route *ptr;
if (!route || !ID) return GF_BAD_PARAM;
ptr = gf_sg_route_find(route->graph, ID);
if (ptr) return GF_BAD_PARAM;
route->ID = ID;
return GF_OK;
}
u32 gf_sg_route_get_id(GF_Route *route)
{
return route->ID;
}
GF_Err gf_sg_route_set_name(GF_Route *route, char *name)
{
GF_Route *ptr;
if (!name || !route) return GF_BAD_PARAM;
ptr = gf_sg_route_find_by_name(route->graph, name);
if (ptr) return GF_BAD_PARAM;
if (route->name) gf_free(route->name);
route->name = gf_strdup(name);
return GF_OK;
}
char *gf_sg_route_get_name(GF_Route *route)
{
return route->name;
}
void gf_sg_route_setup(GF_Route *r)
{
gf_node_get_field(r->FromNode, r->FromField.fieldIndex, &r->FromField);
gf_node_get_field(r->ToNode, r->ToField.fieldIndex, &r->ToField);
switch (r->FromField.fieldType) {
case GF_SG_VRML_MFNODE:
if (r->ToField.fieldType != GF_SG_VRML_MFNODE) return;
break;
case GF_SG_VRML_SFNODE:
if (r->ToField.fieldType != GF_SG_VRML_SFNODE) return;
break;
}
r->is_setup = 1;
}
void gf_node_event_out_proto(GF_Node *node, u32 FieldIndex)
{
u32 i;
GF_Route *r;
if (!node) return;
if (!node->sgprivate->interact) return;
i=0;
while ((r = (GF_Route*)gf_list_enum(node->sgprivate->interact->routes, &i))) {
if (r->IS_route) continue;
if (r->FromNode != node) continue;
if (r->FromField.fieldIndex != FieldIndex) continue;
gf_sg_route_queue(node->sgprivate->scenegraph, r);
}
}
Bool gf_sg_route_activate(GF_Route *r)
{
Bool ret;
void VRML_FieldCopyCast(void *dest, u32 dst_field_type, void *orig, u32 ori_field_type);
assert(r->FromNode);
if (!r->is_setup) {
gf_sg_route_setup(r);
if (!r->is_setup) return 0;
if (r->IS_route) {
if (r->FromField.eventType == GF_SG_EVENT_OUT) return 0;
if (r->ToField.eventType == GF_SG_EVENT_OUT) return 0;
}
if (r->IS_route && ((r->ToNode->sgprivate->tag==TAG_MPEG4_Script)
#ifndef GPAC_DISABLE_X3D
|| (r->ToNode->sgprivate->tag==TAG_X3D_Script)
#endif
) && ((r->ToField.eventType==GF_SG_EVENT_IN) )
&& r->FromField.eventType==GF_SG_EVENT_IN) {
return 0;
}
}
#ifndef GPAC_DISABLE_LOG
if (gf_log_tool_level_on(GF_LOG_INTERACT, GF_LOG_DEBUG)) {
if (r->IS_route) {
GF_LOG(GF_LOG_DEBUG, GF_LOG_INTERACT, ("[VRML Event] executing %s.%s IS %s.%s", gf_node_get_name(r->FromNode), r->FromField.name, gf_node_get_name(r->ToNode), r->ToField.name));
} else {
GF_LOG(GF_LOG_DEBUG, GF_LOG_INTERACT, ("[VRML Event] executing ROUTE %s.%s TO %s.%s", gf_node_get_name(r->FromNode), r->FromField.name, gf_node_get_name(r->ToNode), r->ToField.name));
}
if (r->FromField.fieldType==GF_SG_VRML_SFBOOL) {
GF_LOG(GF_LOG_DEBUG, GF_LOG_INTERACT, ("\tBOOL VAL: %d\n", *((SFBool*)r->FromField.far_ptr)));
} else if (r->FromField.fieldType==GF_SG_VRML_SFINT32) {
GF_LOG(GF_LOG_DEBUG, GF_LOG_INTERACT, ("\tINT VAL: %d\n", *((SFInt32*)r->FromField.far_ptr)));
} else {
GF_LOG(GF_LOG_DEBUG, GF_LOG_INTERACT, ("\n"));
}
}
#endif
ret = 1;
switch (r->FromField.fieldType) {
case GF_SG_VRML_SFNODE:
if (* (GF_Node **) r->ToField.far_ptr != * (GF_Node **) r->FromField.far_ptr) {
GF_Node *n = * (GF_Node **) r->ToField.far_ptr;
if (n) gf_node_unregister(n, r->ToNode);
* (GF_Node **) r->ToField.far_ptr = * (GF_Node **) r->FromField.far_ptr;
n = * (GF_Node **) r->FromField.far_ptr;
gf_node_register(n, r->ToNode);
}
break;
case GF_SG_VRML_MFNODE:
{
GF_ChildNodeItem *last = NULL;
GF_ChildNodeItem *orig = *(GF_ChildNodeItem **)r->FromField.far_ptr;
gf_node_unregister_children(r->ToNode, *(GF_ChildNodeItem **)r->ToField.far_ptr );
*(GF_ChildNodeItem **)r->ToField.far_ptr = NULL;
while (orig) {
gf_node_list_add_child_last( (GF_ChildNodeItem **)r->ToField.far_ptr, orig->node, &last);
gf_node_register(orig->node, r->ToNode);
orig = orig->next;
}
}
break;
default:
if (r->ToField.fieldType==r->FromField.fieldType) {
if (gf_sg_vrml_field_equal(r->ToField.far_ptr, r->FromField.far_ptr, r->FromField.fieldType)) {
ret = 0;
} else {
gf_sg_vrml_field_copy(r->ToField.far_ptr, r->FromField.far_ptr, r->FromField.fieldType);
}
}
else {
VRML_FieldCopyCast(r->ToField.far_ptr, r->ToField.fieldType, r->FromField.far_ptr, r->FromField.fieldType);
}
break;
}
if (r->ToField.fieldType==GF_SG_VRML_GENERIC_FUNCTION) {
ret = 0;
}
#ifndef GPAC_DISABLE_LOG
if (gf_log_tool_level_on(GF_LOG_INTERACT, GF_LOG_DEBUG)) {
GF_LOG(GF_LOG_DEBUG, GF_LOG_INTERACT, ("[VRML Route] field copy/casted\n"));
}
#endif
if (r->ToField.on_event_in) {
r->ToField.on_event_in(r->ToNode, r);
}
else if (((r->ToNode->sgprivate->tag==TAG_MPEG4_Script)
#ifndef GPAC_DISABLE_X3D
|| (r->ToNode->sgprivate->tag==TAG_X3D_Script)
#endif
) && ((r->ToField.eventType==GF_SG_EVENT_IN) ) ) {
gf_sg_script_event_in(r->ToNode, &r->ToField);
}
else {
gf_sg_proto_propagate_event(r->ToNode, r->ToField.fieldIndex, r->FromNode);
if (r->ToField.eventType != GF_SG_EVENT_EXPOSED_FIELD)
gf_sg_proto_propagate_event(r->ToNode, r->ToField.fieldIndex, r->FromNode);
if (r->IS_route && r->ToField.eventType==GF_SG_EVENT_OUT)
gf_node_event_out(r->ToNode, r->ToField.fieldIndex);
}
if (r->ToField.eventType == GF_SG_EVENT_EXPOSED_FIELD) {
if (r->IS_route)
gf_node_event_out_proto(r->ToNode, r->ToField.fieldIndex);
else
gf_node_event_out(r->ToNode, r->ToField.fieldIndex);
}
#ifndef GPAC_DISABLE_LOG
if (gf_log_tool_level_on(GF_LOG_INTERACT, GF_LOG_DEBUG)) {
GF_LOG(GF_LOG_DEBUG, GF_LOG_INTERACT, ("[VRML Route] done executing (res %d)\n", ret));
}
#endif
return ret;
}
GF_EXPORT
void gf_node_event_out(GF_Node *node, u32 FieldIndex)
{
u32 i;
GF_Route *r;
if (!node) return;
if (!node->sgprivate->interact || !node->sgprivate->interact->routes) return;
i=0;
while ((r = (GF_Route*)gf_list_enum(node->sgprivate->interact->routes, &i))) {
if (r->FromNode != node) continue;
if (r->FromField.fieldIndex != FieldIndex) continue;
if (r->IS_route) {
if (gf_sg_route_activate(r))
gf_node_changed(r->ToNode, &r->ToField);
}
else {
gf_sg_route_queue(node->sgprivate->scenegraph, r);
}
}
}
GF_EXPORT
void gf_node_event_out_str(GF_Node *node, const char *eventName)
{
u32 i;
GF_Route *r;
if (!node->sgprivate->interact || !node->sgprivate->interact->routes) return;
i=0;
while ((r = (GF_Route*)gf_list_enum(node->sgprivate->interact->routes, &i))) {
if (!r->is_setup) gf_sg_route_setup(r);
if (stricmp(r->FromField.name, eventName)) continue;
if (r->IS_route) {
gf_sg_route_activate(r);
}
else {
gf_sg_route_queue(node->sgprivate->scenegraph, r);
}
}
}
typedef struct
{
GF_Route r;
void ( *route_callback) (void *param, GF_FieldInfo *from_field);
} GF_RouteToFunction;
static void on_route_to_function(GF_Node *node, GF_Route *r)
{
GF_RouteToFunction *rf = (GF_RouteToFunction *)r;
rf->route_callback(r->ToNode, &r->FromField);
}
GF_EXPORT
void gf_sg_route_new_to_callback(GF_SceneGraph *sg, GF_Node *fromNode, u32 fromField, void *cbk, void ( *route_callback) (void *param, GF_FieldInfo *from_field) )
{
GF_Route *r;
GF_RouteToFunction *rf;
GF_SAFEALLOC(rf, GF_RouteToFunction);
if (!rf) return;
rf->route_callback = route_callback;
r = (GF_Route *)rf;
r->FromNode = fromNode;
r->FromField.fieldIndex = fromField;
gf_node_get_field(r->FromNode, fromField, &r->FromField);
r->ToNode = (GF_Node *) cbk;
r->ToField.fieldType = GF_SG_VRML_GENERIC_FUNCTION;
r->ToField.on_event_in = on_route_to_function;
r->ToField.eventType = GF_SG_EVENT_IN;
r->ToField.far_ptr = NULL;
r->is_setup = 1;
r->graph = sg;
if (!fromNode->sgprivate->interact) {
GF_SAFEALLOC(fromNode->sgprivate->interact, struct _node_interactive_ext);
if (!fromNode->sgprivate->interact) {
GF_LOG(GF_LOG_ERROR, GF_LOG_SCENE, ("[VRML] Failed to create interact storage\n"));
gf_free(r);
return;
}
}
if (!fromNode->sgprivate->interact->routes) fromNode->sgprivate->interact->routes = gf_list_new();
gf_list_add(fromNode->sgprivate->interact->routes, r);
gf_list_add(fromNode->sgprivate->scenegraph->Routes, r);
}
#endif