123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685 |
- #include "ReAntTweakBar.h"
- #include <cstdio>
- #include <sstream>
- #include <map>
- #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<TwType,std::pair<const char *,std::vector<TwEnumVal> > > ReTw_custom_types;
- }
- TwType igl::ReTwDefineEnum(
- const char *name,
- const TwEnumVal *enumValues,
- unsigned int nbValues)
- {
- // copy enum valus into vector
- std::vector<TwEnumVal> enum_vals;
- enum_vals.resize(nbValues);
- for(unsigned int j = 0; j<nbValues;j++)
- {
- enum_vals[j] = enumValues[j];
- }
- TwType type = TwDefineEnum(name,enumValues,nbValues);
- ReTw_custom_types[type] =
- std::pair<const char *,std::vector<TwEnumVal> >(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<ReTwRWItem>::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<ReTwCBItem>::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<bool*>(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<float*>(var);
- sstr << q[0] << " " << q[1] << " " << q[2] << " " << q[3];
- break;
- }
- case TW_TYPE_COLOR4F:
- {
- sstr << "TW_TYPE_COLOR4F" << " ";
- float * c = static_cast<float*>(var);
- sstr << c[0] << " " << c[1] << " " << c[2] << " " << c[3];
- break;
- }
- case TW_TYPE_COLOR3F:
- {
- sstr << "TW_TYPE_COLOR3F" << " ";
- float * c = static_cast<float*>(var);
- sstr << c[0] << " " << c[1] << " " << c[2];
- break;
- }
- case TW_TYPE_DIR3F:
- {
- sstr << "TW_TYPE_DIR3F" << " ";
- float * d = static_cast<float*>(var);
- sstr << d[0] << " " << d[1] << " " << d[2];
- break;
- }
- case TW_TYPE_BOOL32:
- {
- sstr << "TW_TYPE_BOOL32" << " ";
- sstr << *(static_cast<int*>(var));
- break;
- }
- case TW_TYPE_INT32:
- {
- sstr << "TW_TYPE_INT32" << " ";
- sstr << *(static_cast<int*>(var));
- break;
- }
- case TW_TYPE_FLOAT:
- {
- sstr << "TW_TYPE_FLOAT" << " ";
- sstr << *(static_cast<float*>(var));
- break;
- }
- case TW_TYPE_DOUBLE:
- {
- sstr << "TW_TYPE_DOUBLE" << " ";
- sstr << *(static_cast<double*>(var));
- break;
- }
- default:
- {
- std::map<TwType,std::pair<const char *,std::vector<TwEnumVal> > >::iterator iter =
- ReTw_custom_types.find(type);
- if(iter != ReTw_custom_types.end())
- {
- sstr << (*iter).second.first << " ";
- int enum_val = *(static_cast<int*>(var));
- // try find display name for enum value
- std::vector<TwEnumVal>::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<TwType,std::pair<const char *,std::vector<TwEnumVal> > >::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<TwType,std::pair<const char *,std::vector<TwEnumVal> > >::iterator iter =
- ReTw_custom_types.find(type);
- if(iter != ReTw_custom_types.end())
- {
- std::vector<TwEnumVal>::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<ReTwRWItem>::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<bool*>(var);
- bool * bvalue = static_cast<bool*>(value);
- *bvar = *bvalue;
- break;
- }
- case TW_TYPE_QUAT4F:
- case TW_TYPE_COLOR4F:
- {
- float * fvar = static_cast<float*>(var);
- float * fvalue = static_cast<float*>(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<float*>(var);
- float * fvalue = static_cast<float*>(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<int*>(var);
- int * ivalue = static_cast<int*>(value);
- *ivar = *ivalue;
- break;
- }
- case TW_TYPE_FLOAT:
- {
- float * fvar = static_cast<float*>(var);
- float * fvalue = static_cast<float*>(value);
- *fvar = *fvalue;
- break;
- }
- case TW_TYPE_DOUBLE:
- {
- double * dvar = static_cast<double*>(var);
- double * fvalue = static_cast<double*>(value);
- *dvar = *fvalue;
- break;
- }
- default:
- // Try to find type in custom enum types
- std::map<TwType,std::pair<const char *,std::vector<TwEnumVal> > >::iterator iter =
- ReTw_custom_types.find(type);
- if(iter != ReTw_custom_types.end())
- {
- int * ivar = static_cast<int*>(var);
- std::vector<TwEnumVal>::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<ReTwCBItem>::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;
- }
|