#include "ReAntTweakBar.h" #include #include #include #define MAX_CB_VAR_SIZE 10 // Max line size for reading files #define MAX_LINE 1000 #define MAX_WORD 100 // GLOBAL WRAPPERS namespace igl { std::map > > ReTw_custom_types; } TwType igl::ReTwDefineEnum( const char *name, const TwEnumVal *enumValues, unsigned int nbValues) { // copy enum valus into vector std::vector enum_vals; enum_vals.resize(nbValues); for(unsigned int j = 0; j >(name,enum_vals); return type; } namespace igl { struct ReTwTypeString { TwType type; const char * type_str; }; #define RETW_NUM_DEFAULT_TYPE_STRINGS 23 ReTwTypeString ReTwDefaultTypeStrings[RETW_NUM_DEFAULT_TYPE_STRINGS] = { {TW_TYPE_UNDEF,"TW_TYPE_UNDEF"}, {TW_TYPE_BOOLCPP,"TW_TYPE_BOOLCPP"}, {TW_TYPE_BOOL8,"TW_TYPE_BOOL8"}, {TW_TYPE_BOOL16,"TW_TYPE_BOOL16"}, {TW_TYPE_BOOL32,"TW_TYPE_BOOL32"}, {TW_TYPE_CHAR,"TW_TYPE_CHAR"}, {TW_TYPE_INT8,"TW_TYPE_INT8"}, {TW_TYPE_UINT8,"TW_TYPE_UINT8"}, {TW_TYPE_INT16,"TW_TYPE_INT16"}, {TW_TYPE_UINT16,"TW_TYPE_UINT16"}, {TW_TYPE_INT32,"TW_TYPE_INT32"}, {TW_TYPE_UINT32,"TW_TYPE_UINT32"}, {TW_TYPE_FLOAT,"TW_TYPE_FLOAT"}, {TW_TYPE_DOUBLE,"TW_TYPE_DOUBLE"}, {TW_TYPE_COLOR32,"TW_TYPE_COLOR32"}, {TW_TYPE_COLOR3F,"TW_TYPE_COLOR3F"}, {TW_TYPE_COLOR4F,"TW_TYPE_COLOR4F"}, {TW_TYPE_CDSTRING,"TW_TYPE_CDSTRING"}, {TW_TYPE_STDSTRING,"TW_TYPE_STDSTRING"}, {TW_TYPE_QUAT4F,"TW_TYPE_QUAT4F"}, {TW_TYPE_QUAT4D,"TW_TYPE_QUAT4D"}, {TW_TYPE_DIR3F,"TW_TYPE_DIR3F"}, {TW_TYPE_DIR3D,"TW_TYPE_DIR3D"} }; } // BAR WRAPPERS void igl::ReTwBar::TwNewBar(const char *barName) { // double colon without anything in front of it means look for this in the // global namespace... I hope... this->bar = ::TwNewBar(barName); } int igl::ReTwBar::TwAddVarRW( const char *name, TwType type, void *var, const char *def, const bool record) { int ret = ::TwAddVarRW(this->bar,name,type,var,def); if(ret && record) { rw_items.push_back(ReTwRWItem(name,type,var)); } return ret; } int igl::ReTwBar::TwAddVarCB( const char *name, TwType type, TwSetVarCallback setCallback, TwGetVarCallback getCallback, void *clientData, const char *def, const bool record) { int ret = ::TwAddVarCB(this->bar,name,type,setCallback,getCallback,clientData,def); if(ret && record) { cb_items.push_back(ReTwCBItem(name,type,setCallback,getCallback,clientData)); } return ret; } int igl::ReTwBar::TwAddVarRO( const char *name, TwType type, void *var, const char *def) { int ret = ::TwAddVarRO(this->bar,name,type,var,def); // Read only variables are not recorded //if(ret) //{ // rw_items.push_back(ReTwRWItem(name,type,var)); //} return ret; } int igl::ReTwBar::TwAddButton( const char *name, TwButtonCallback buttonCallback, void *clientData, const char *def) { int ret = ::TwAddButton(this->bar,name,buttonCallback,clientData,def); // buttons are not recorded //if(ret) //{ // cb_items.push_back(ReTwCBItem(name,type,setCallback,getCallback,clientData)); //} return ret; } int igl::ReTwBar::TwSetParam( const char *varName, const char *paramName, TwParamValueType paramValueType, unsigned int inValueCount, const void *inValues) { // For now just pass these along return ::TwSetParam( this->bar, varName, paramName, paramValueType, inValueCount, inValues); } int igl::ReTwBar::TwGetParam( const char *varName, const char *paramName, TwParamValueType paramValueType, unsigned int outValueMaxCount, void *outValues) { return ::TwGetParam( this->bar, varName, paramName, paramValueType, outValueMaxCount, outValues); } int igl::ReTwBar::TwRefreshBar() { return ::TwRefreshBar(this->bar); } int igl::ReTwBar::TwTerminate() { return ::TwTerminate(); } bool igl::ReTwBar::save(const char *file_name) { FILE * fp; if(file_name == NULL) { fp = stdout; }else { fp = fopen(file_name,"w"); } if(fp == NULL) { printf("ERROR: not able to open %s for writing...\n",file_name); return false; } // Print all RW variables for( std::vector::iterator it = rw_items.begin(); it != rw_items.end(); it++) { const char * name = (*it).name; TwType type = (*it).type; void * var = (*it).var; fprintf(fp,"%s: %s\n", name, get_value_as_string(var,type).c_str()); } char var[MAX_CB_VAR_SIZE]; // Print all CB variables for( std::vector::iterator it = cb_items.begin(); it != cb_items.end(); it++) { const char * name = it->name; TwType type = it->type; //TwSetVarCallback setCallback = it->setCallback; TwGetVarCallback getCallback = it->getCallback; void * clientData = it->clientData; // I'm not sure how to do what I want to do. getCallback needs to be sure // that it can write to var. So var needs to point to a valid and big // enough chunk of memory getCallback(var,clientData); fprintf(fp,"%s: %s\n", name, get_value_as_string(var,type).c_str()); } fprintf(fp,"\n"); if(file_name != NULL) { fclose(fp); } // everything succeeded return true; } std::string igl::ReTwBar::get_value_as_string( void * var, TwType type) { std::stringstream sstr; switch(type) { case TW_TYPE_BOOLCPP: { sstr << "TW_TYPE_BOOLCPP" << " "; sstr << *(static_cast(var)); break; } case TW_TYPE_QUAT4F: { sstr << "TW_TYPE_QUAT4F" << " "; // Q: Why does casting to float* work? shouldn't I have to cast to // float**? float * q = static_cast(var); sstr << q[0] << " " << q[1] << " " << q[2] << " " << q[3]; break; } case TW_TYPE_COLOR4F: { sstr << "TW_TYPE_COLOR4F" << " "; float * c = static_cast(var); sstr << c[0] << " " << c[1] << " " << c[2] << " " << c[3]; break; } case TW_TYPE_COLOR3F: { sstr << "TW_TYPE_COLOR3F" << " "; float * c = static_cast(var); sstr << c[0] << " " << c[1] << " " << c[2]; break; } case TW_TYPE_DIR3F: { sstr << "TW_TYPE_DIR3F" << " "; float * d = static_cast(var); sstr << d[0] << " " << d[1] << " " << d[2]; break; } case TW_TYPE_BOOL32: { sstr << "TW_TYPE_BOOL32" << " "; sstr << *(static_cast(var)); break; } case TW_TYPE_INT32: { sstr << "TW_TYPE_INT32" << " "; sstr << *(static_cast(var)); break; } case TW_TYPE_FLOAT: { sstr << "TW_TYPE_FLOAT" << " "; sstr << *(static_cast(var)); break; } case TW_TYPE_DOUBLE: { sstr << "TW_TYPE_DOUBLE" << " "; sstr << *(static_cast(var)); break; } default: { std::map > >::iterator iter = ReTw_custom_types.find(type); if(iter != ReTw_custom_types.end()) { sstr << (*iter).second.first << " "; int enum_val = *(static_cast(var)); // try find display name for enum value std::vector::iterator eit = (*iter).second.second.begin(); bool found = false; for(;eit<(*iter).second.second.end();eit++) { if(enum_val == eit->Value) { sstr << eit->Label; found = true; break; } } if(!found) { sstr << "ERROR_ENUM_VALUE_NOT_DEFINED"; } }else { sstr << "ERROR_TYPE_NOT_SUPPORTED"; } break; } } return sstr.str(); } bool igl::ReTwBar::load(const char *file_name) { FILE * fp; fp = fopen(file_name,"r"); if(fp == NULL) { printf("ERROR: not able to open %s for reading...\n",file_name); return false; } // go through file line by line char line[MAX_LINE]; bool still_comments; char name[MAX_WORD]; char type_str[MAX_WORD]; char value_str[MAX_WORD]; // line number int j = 0; bool finished = false; while(true) { // Eat comments still_comments = true; while(still_comments) { if(fgets(line,MAX_LINE,fp) == NULL) { finished = true; break; } // Blank lines and lines that begin with # are comments still_comments = (line[0] == '#' || line[0] == '\n'); j++; } if(finished) { break; } sscanf(line,"%[^:]: %s %[^\n]",name,type_str,value_str); //printf("%s: %s %s\n",name, type_str,value_str); TwType type; if(!type_from_string(type_str,type)) { printf("ERROR: %s type not found... Skipping...\n",type_str); continue; } set_value_from_string(name,type,value_str); } fclose(fp); // everything succeeded return true; } bool igl::ReTwBar::type_from_string(const char *type_str, TwType & type) { // first check default types for(int j = 0; j < RETW_NUM_DEFAULT_TYPE_STRINGS; j++) { if(strcmp(type_str,ReTwDefaultTypeStrings[j].type_str) == 0) { type = ReTwDefaultTypeStrings[j].type; return true; break; } } // then check custom types std::map > >::iterator iter = ReTw_custom_types.begin(); for(;iter != ReTw_custom_types.end(); iter++) { if(strcmp((*iter).second.first,type_str)==0) { type = (*iter).first; return true; } } return false; } bool igl::ReTwBar::set_value_from_string( const char * name, TwType type, const char * value_str) { void * value = NULL; // possible value slots int i; float v; double dv; float f[4]; bool b; // First try to get value from default types switch(type) { case TW_TYPE_BOOLCPP: { int ib; if(sscanf(value_str," %d",&ib) == 1) { b = ib!=0; value = &b; }else { printf("ERROR: Bad value format...\n"); return false; } break; } case TW_TYPE_QUAT4F: case TW_TYPE_COLOR4F: { if(sscanf(value_str," %f %f %f %f",&f[0],&f[1],&f[2],&f[3]) == 4) { value = &f; }else { printf("ERROR: Bad value format...\n"); return false; } break; } case TW_TYPE_COLOR3F: case TW_TYPE_DIR3F: { if(sscanf(value_str," %f %f %f",&f[0],&f[1],&f[2]) == 3) { value = &f; }else { printf("ERROR: Bad value format...\n"); return false; } break; } case TW_TYPE_BOOL32: case TW_TYPE_INT32: { if(sscanf(value_str," %d",&i) == 1) { value = &i; }else { printf("ERROR: Bad value format...\n"); return false; } break; } case TW_TYPE_FLOAT: { if(sscanf(value_str," %f",&v) == 1) { value = &v; }else { printf("ERROR: Bad value format...\n"); return false; } break; } case TW_TYPE_DOUBLE: { if(sscanf(value_str," %lf",&dv) == 1) { value = &dv; }else { printf("ERROR: Bad value format...\n"); return false; } break; } default: // Try to find type in custom enum types std::map > >::iterator iter = ReTw_custom_types.find(type); if(iter != ReTw_custom_types.end()) { std::vector::iterator eit = (*iter).second.second.begin(); bool found = false; for(;eit<(*iter).second.second.end();eit++) { if(strcmp(value_str,eit->Label) == 0) { i = eit->Value; value = &i; found = true; break; } } if(!found) { printf("ERROR_ENUM_VALUE_NOT_DEFINED"); } }else { printf("ERROR_TYPE_NOT_SUPPORTED\n"); } break; } // Find variable based on name // First look in RW items bool item_found = false; for( std::vector::iterator it = rw_items.begin(); it != rw_items.end(); it++) { if(strcmp(it->name,name) == 0) { void * var = it->var; switch(type) { case TW_TYPE_BOOLCPP: { bool * bvar = static_cast(var); bool * bvalue = static_cast(value); *bvar = *bvalue; break; } case TW_TYPE_QUAT4F: case TW_TYPE_COLOR4F: { float * fvar = static_cast(var); float * fvalue = static_cast(value); fvar[0] = fvalue[0]; fvar[1] = fvalue[1]; fvar[2] = fvalue[2]; fvar[3] = fvalue[3]; break; } case TW_TYPE_COLOR3F: case TW_TYPE_DIR3F: { float * fvar = static_cast(var); float * fvalue = static_cast(value); fvar[0] = fvalue[0]; fvar[1] = fvalue[1]; fvar[2] = fvalue[2]; break; } case TW_TYPE_BOOL32: case TW_TYPE_INT32: { int * ivar = static_cast(var); int * ivalue = static_cast(value); *ivar = *ivalue; break; } case TW_TYPE_FLOAT: { float * fvar = static_cast(var); float * fvalue = static_cast(value); *fvar = *fvalue; break; } case TW_TYPE_DOUBLE: { double * dvar = static_cast(var); double * fvalue = static_cast(value); *dvar = *fvalue; break; } default: // Try to find type in custom enum types std::map > >::iterator iter = ReTw_custom_types.find(type); if(iter != ReTw_custom_types.end()) { int * ivar = static_cast(var); std::vector::iterator eit = (*iter).second.second.begin(); bool found = false; for(;eit<(*iter).second.second.end();eit++) { if(strcmp(value_str,eit->Label) == 0) { *ivar = eit->Value; found = true; break; } } if(!found) { printf("ERROR_ENUM_VALUE_NOT_DEFINED"); } }else { printf("ERROR_TYPE_NOT_SUPPORTED\n"); } break; } item_found = true; break; } } // Try looking in CB items if(!item_found) { for( std::vector::iterator it = cb_items.begin(); it != cb_items.end(); it++) { if(strcmp(it->name,name) == 0) { it->setCallback(value,it->clientData); item_found = true; break; } } } if(!item_found) { printf("ERROR: item not found\n"); } return true; }