ReAntTweakBar.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. #include "ReAntTweakBar.h"
  2. #ifndef IGL_NO_ANTTWEAKBAR
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <sstream>
  6. #include <iostream>
  7. #include <iomanip>
  8. #include <map>
  9. // GLOBAL WRAPPERS
  10. namespace
  11. {
  12. std::map<TwType,std::pair<const char *,std::vector<TwEnumVal> > > ReTw_custom_types;
  13. }
  14. IGL_INLINE TwType igl::ReTwDefineEnum(
  15. const char *name,
  16. const TwEnumVal *enumValues,
  17. unsigned int nbValues)
  18. {
  19. // copy enum valus into vector
  20. std::vector<TwEnumVal> enum_vals;
  21. enum_vals.resize(nbValues);
  22. for(unsigned int j = 0; j<nbValues;j++)
  23. {
  24. enum_vals[j] = enumValues[j];
  25. }
  26. TwType type = TwDefineEnum(name,enumValues,nbValues);
  27. ReTw_custom_types[type] =
  28. std::pair<const char *,std::vector<TwEnumVal> >(name,enum_vals);
  29. return type;
  30. }
  31. IGL_INLINE TwType igl::ReTwDefineEnum(
  32. const char * _Name,
  33. const char * _EnumString)
  34. {
  35. // Taken directly from TwMgr.cpp, just replace TwDefineEnum with
  36. // ReTwDefineEnum
  37. using namespace std;
  38. {
  39. if (_EnumString == NULL)
  40. return ReTwDefineEnum(_Name, NULL, 0);
  41. // split enumString
  42. stringstream EnumStream(_EnumString);
  43. string Label;
  44. vector<string> Labels;
  45. while( getline(EnumStream, Label, ',') ) {
  46. // trim Label
  47. size_t Start = Label.find_first_not_of(" \n\r\t");
  48. size_t End = Label.find_last_not_of(" \n\r\t");
  49. if( Start==string::npos || End==string::npos )
  50. Label = "";
  51. else
  52. Label = Label.substr(Start, (End-Start)+1);
  53. // store Label
  54. Labels.push_back(Label);
  55. }
  56. // create TwEnumVal array
  57. vector<TwEnumVal> Vals(Labels.size());
  58. for( int i=0; i<(int)Labels.size(); i++ )
  59. {
  60. Vals[i].Value = i;
  61. Vals[i].Label = Labels[i].c_str();
  62. }
  63. return ReTwDefineEnum(_Name, Vals.empty() ? NULL : &(Vals[0]), (unsigned int)Vals.size());
  64. }
  65. }
  66. namespace
  67. {
  68. struct ReTwTypeString
  69. {
  70. TwType type;
  71. const char * type_str;
  72. };
  73. #define RETW_NUM_DEFAULT_TYPE_STRINGS 23
  74. ReTwTypeString ReTwDefaultTypeStrings[RETW_NUM_DEFAULT_TYPE_STRINGS] =
  75. {
  76. {TW_TYPE_UNDEF,"TW_TYPE_UNDEF"},
  77. {TW_TYPE_BOOLCPP,"TW_TYPE_BOOLCPP"},
  78. {TW_TYPE_BOOL8,"TW_TYPE_BOOL8"},
  79. {TW_TYPE_BOOL16,"TW_TYPE_BOOL16"},
  80. {TW_TYPE_BOOL32,"TW_TYPE_BOOL32"},
  81. {TW_TYPE_CHAR,"TW_TYPE_CHAR"},
  82. {TW_TYPE_INT8,"TW_TYPE_INT8"},
  83. {TW_TYPE_UINT8,"TW_TYPE_UINT8"},
  84. {TW_TYPE_INT16,"TW_TYPE_INT16"},
  85. {TW_TYPE_UINT16,"TW_TYPE_UINT16"},
  86. {TW_TYPE_INT32,"TW_TYPE_INT32"},
  87. {TW_TYPE_UINT32,"TW_TYPE_UINT32"},
  88. {TW_TYPE_FLOAT,"TW_TYPE_FLOAT"},
  89. {TW_TYPE_DOUBLE,"TW_TYPE_DOUBLE"},
  90. {TW_TYPE_COLOR32,"TW_TYPE_COLOR32"},
  91. {TW_TYPE_COLOR3F,"TW_TYPE_COLOR3F"},
  92. {TW_TYPE_COLOR4F,"TW_TYPE_COLOR4F"},
  93. {TW_TYPE_CDSTRING,"TW_TYPE_CDSTRING"},
  94. {TW_TYPE_STDSTRING,"TW_TYPE_STDSTRING"},
  95. {TW_TYPE_QUAT4F,"TW_TYPE_QUAT4F"},
  96. {TW_TYPE_QUAT4D,"TW_TYPE_QUAT4D"},
  97. {TW_TYPE_DIR3F,"TW_TYPE_DIR3F"},
  98. {TW_TYPE_DIR3D,"TW_TYPE_DIR3D"}
  99. };
  100. }
  101. IGL_INLINE igl::ReTwBar::ReTwBar():
  102. bar(NULL),rw_items(),cb_items()
  103. {
  104. }
  105. IGL_INLINE igl::ReTwBar::ReTwBar(const igl::ReTwBar & that):
  106. bar(that.bar),
  107. rw_items(that.rw_items),
  108. cb_items(that.cb_items)
  109. {
  110. }
  111. IGL_INLINE igl::ReTwBar & igl::ReTwBar::operator=(const igl::ReTwBar & that)
  112. {
  113. // check for self assignment
  114. if(this != &that)
  115. {
  116. bar = that.bar;
  117. rw_items = that.rw_items;
  118. cb_items = that.cb_items;
  119. }
  120. return *this;
  121. }
  122. // BAR WRAPPERS
  123. IGL_INLINE void igl::ReTwBar::TwNewBar(const char *name)
  124. {
  125. // double colon without anything in front of it means look for this in the
  126. // global namespace... I hope...
  127. this->name = name;
  128. this->bar = ::TwNewBar(name);
  129. }
  130. IGL_INLINE int igl::ReTwBar::TwAddVarRW(
  131. const char *name,
  132. TwType type,
  133. void *var,
  134. const char *def,
  135. const bool record)
  136. {
  137. int ret = ::TwAddVarRW(this->bar,name,type,var,def);
  138. if(ret && record)
  139. {
  140. rw_items.push_back(ReTwRWItem(name,type,var));
  141. }
  142. return ret;
  143. }
  144. IGL_INLINE int igl::ReTwBar::TwAddVarCB(
  145. const char *name,
  146. TwType type,
  147. TwSetVarCallback setCallback,
  148. TwGetVarCallback getCallback,
  149. void *clientData,
  150. const char *def,
  151. const bool record)
  152. {
  153. int ret =
  154. ::TwAddVarCB(this->bar,name,type,setCallback,getCallback,clientData,def);
  155. if(ret && record)
  156. {
  157. cb_items.push_back(ReTwCBItem(name,type,setCallback,getCallback,clientData));
  158. }
  159. return ret;
  160. }
  161. IGL_INLINE int igl::ReTwBar::TwAddVarRO(
  162. const char *name,
  163. TwType type,
  164. void *var,
  165. const char *def)
  166. {
  167. int ret = ::TwAddVarRO(this->bar,name,type,var,def);
  168. // Read only variables are not recorded
  169. //if(ret)
  170. //{
  171. // rw_items.push_back(ReTwRWItem(name,type,var));
  172. //}
  173. return ret;
  174. }
  175. IGL_INLINE int igl::ReTwBar::TwAddButton(
  176. const char *name,
  177. TwButtonCallback buttonCallback,
  178. void *clientData,
  179. const char *def)
  180. {
  181. int ret =
  182. ::TwAddButton(this->bar,name,buttonCallback,clientData,def);
  183. // buttons are not recorded
  184. //if(ret)
  185. //{
  186. // cb_items.push_back(ReTwCBItem(name,type,setCallback,getCallback,clientData));
  187. //}
  188. return ret;
  189. }
  190. IGL_INLINE int igl::ReTwBar::TwSetParam(
  191. const char *varName,
  192. const char *paramName,
  193. TwParamValueType paramValueType,
  194. unsigned int inValueCount,
  195. const void *inValues)
  196. {
  197. // For now just pass these along
  198. return
  199. ::TwSetParam(
  200. this->bar,
  201. varName,
  202. paramName,
  203. paramValueType,
  204. inValueCount,
  205. inValues);
  206. }
  207. IGL_INLINE int igl::ReTwBar::TwGetParam(
  208. const char *varName,
  209. const char *paramName,
  210. TwParamValueType paramValueType,
  211. unsigned int outValueMaxCount,
  212. void *outValues)
  213. {
  214. return
  215. ::TwGetParam(
  216. this->bar,
  217. varName,
  218. paramName,
  219. paramValueType,
  220. outValueMaxCount,
  221. outValues);
  222. }
  223. IGL_INLINE int igl::ReTwBar::TwRefreshBar()
  224. {
  225. return ::TwRefreshBar(this->bar);
  226. }
  227. IGL_INLINE int igl::ReTwBar::TwTerminate()
  228. {
  229. //std::cout<<"TwTerminate"<<std::endl;
  230. int r = ::TwTerminate();
  231. //std::cout<<" "<<r<<std::endl;
  232. return r;
  233. }
  234. IGL_INLINE bool igl::ReTwBar::save(const char *file_name)
  235. {
  236. FILE * fp;
  237. if(file_name == NULL)
  238. {
  239. fp = stdout;
  240. }else
  241. {
  242. fp = fopen(file_name,"w");
  243. }
  244. if(fp == NULL)
  245. {
  246. printf("ERROR: not able to open %s for writing...\n",file_name);
  247. return false;
  248. }
  249. // Print all RW variables
  250. for(
  251. std::vector<ReTwRWItem>::iterator it = rw_items.begin();
  252. it != rw_items.end();
  253. it++)
  254. {
  255. std::string s = (*it).name;
  256. const char * name = s.c_str();
  257. TwType type = (*it).type;
  258. void * var = (*it).var;
  259. fprintf(fp,"%s: %s\n",
  260. name,
  261. get_value_as_string(var,type).c_str());
  262. }
  263. char var[REANTTWEAKBAR_MAX_CB_VAR_SIZE];
  264. // Print all CB variables
  265. for(
  266. std::vector<ReTwCBItem>::iterator it = cb_items.begin();
  267. it != cb_items.end();
  268. it++)
  269. {
  270. const char * name = it->name.c_str();
  271. TwType type = it->type;
  272. //TwSetVarCallback setCallback = it->setCallback;
  273. TwGetVarCallback getCallback = it->getCallback;
  274. void * clientData = it->clientData;
  275. // I'm not sure how to do what I want to do. getCallback needs to be sure
  276. // that it can write to var. So var needs to point to a valid and big
  277. // enough chunk of memory
  278. getCallback(var,clientData);
  279. fprintf(fp,"%s: %s\n",
  280. name,
  281. get_value_as_string(var,type).c_str());
  282. }
  283. fprintf(fp,"\n");
  284. if(file_name != NULL)
  285. {
  286. fclose(fp);
  287. }
  288. // everything succeeded
  289. return true;
  290. }
  291. IGL_INLINE std::string igl::ReTwBar::get_value_as_string(
  292. void * var,
  293. TwType type)
  294. {
  295. std::stringstream sstr;
  296. switch(type)
  297. {
  298. case TW_TYPE_BOOLCPP:
  299. {
  300. sstr << "TW_TYPE_BOOLCPP" << " ";
  301. sstr << *(static_cast<bool*>(var));
  302. break;
  303. }
  304. case TW_TYPE_QUAT4D:
  305. {
  306. sstr << "TW_TYPE_QUAT4D" << " ";
  307. // Q: Why does casting to double* work? shouldn't I have to cast to
  308. // double**?
  309. double * q = static_cast<double*>(var);
  310. sstr << q[0] << " " << q[1] << " " << q[2] << " " << q[3];
  311. break;
  312. }
  313. case TW_TYPE_QUAT4F:
  314. {
  315. sstr << "TW_TYPE_QUAT4F" << " ";
  316. // Q: Why does casting to float* work? shouldn't I have to cast to
  317. // float**?
  318. float * q = static_cast<float*>(var);
  319. sstr << q[0] << " " << q[1] << " " << q[2] << " " << q[3];
  320. break;
  321. }
  322. case TW_TYPE_COLOR4F:
  323. {
  324. sstr << "TW_TYPE_COLOR4F" << " ";
  325. float * c = static_cast<float*>(var);
  326. sstr << c[0] << " " << c[1] << " " << c[2] << " " << c[3];
  327. break;
  328. }
  329. case TW_TYPE_COLOR3F:
  330. {
  331. sstr << "TW_TYPE_COLOR3F" << " ";
  332. float * c = static_cast<float*>(var);
  333. sstr << c[0] << " " << c[1] << " " << c[2];
  334. break;
  335. }
  336. case TW_TYPE_DIR3D:
  337. {
  338. sstr << "TW_TYPE_DIR3D" << " ";
  339. double * d = static_cast<double*>(var);
  340. sstr << d[0] << " " << d[1] << " " << d[2];
  341. break;
  342. }
  343. case TW_TYPE_DIR3F:
  344. {
  345. sstr << "TW_TYPE_DIR3F" << " ";
  346. float * d = static_cast<float*>(var);
  347. sstr << d[0] << " " << d[1] << " " << d[2];
  348. break;
  349. }
  350. case TW_TYPE_BOOL32:
  351. {
  352. sstr << "TW_TYPE_BOOL32" << " ";
  353. sstr << *(static_cast<int*>(var));
  354. break;
  355. }
  356. case TW_TYPE_UINT8:
  357. {
  358. sstr << "TW_TYPE_UINT8" << " ";
  359. // Cast to int so that it's human readable
  360. sstr << (int)*(static_cast<unsigned char*>(var));
  361. break;
  362. }
  363. case TW_TYPE_INT32:
  364. {
  365. sstr << "TW_TYPE_INT32" << " ";
  366. sstr << *(static_cast<int*>(var));
  367. break;
  368. }
  369. case TW_TYPE_FLOAT:
  370. {
  371. sstr << "TW_TYPE_FLOAT" << " ";
  372. sstr << *(static_cast<float*>(var));
  373. break;
  374. }
  375. case TW_TYPE_DOUBLE:
  376. {
  377. sstr << "TW_TYPE_DOUBLE" << " ";
  378. sstr << std::setprecision(15) << *(static_cast<double*>(var));
  379. break;
  380. }
  381. case TW_TYPE_STDSTRING:
  382. {
  383. sstr << "TW_TYPE_STDSTRING" << " ";
  384. std::string *destPtr = static_cast<std::string *>(var);
  385. sstr << destPtr->c_str();
  386. break;
  387. }
  388. default:
  389. {
  390. std::map<TwType,std::pair<const char *,std::vector<TwEnumVal> > >::iterator iter =
  391. ReTw_custom_types.find(type);
  392. if(iter != ReTw_custom_types.end())
  393. {
  394. sstr << (*iter).second.first << " ";
  395. int enum_val = *(static_cast<int*>(var));
  396. // try find display name for enum value
  397. std::vector<TwEnumVal>::iterator eit = (*iter).second.second.begin();
  398. bool found = false;
  399. for(;eit<(*iter).second.second.end();eit++)
  400. {
  401. if(enum_val == eit->Value)
  402. {
  403. sstr << eit->Label;
  404. found = true;
  405. break;
  406. }
  407. }
  408. if(!found)
  409. {
  410. sstr << "ERROR_ENUM_VALUE_NOT_DEFINED";
  411. }
  412. }else
  413. {
  414. sstr << "ERROR_TYPE_NOT_SUPPORTED";
  415. }
  416. break;
  417. }
  418. }
  419. return sstr.str();
  420. }
  421. IGL_INLINE bool igl::ReTwBar::load(const char *file_name)
  422. {
  423. FILE * fp;
  424. fp = fopen(file_name,"r");
  425. if(fp == NULL)
  426. {
  427. printf("ERROR: not able to open %s for reading...\n",file_name);
  428. return false;
  429. }
  430. // go through file line by line
  431. char line[REANTTWEAKBAR_MAX_LINE];
  432. bool still_comments;
  433. char name[REANTTWEAKBAR_MAX_WORD];
  434. char type_str[REANTTWEAKBAR_MAX_WORD];
  435. char value_str[REANTTWEAKBAR_MAX_WORD];
  436. // line number
  437. int j = 0;
  438. bool finished = false;
  439. while(true)
  440. {
  441. // Eat comments
  442. still_comments = true;
  443. while(still_comments)
  444. {
  445. if(fgets(line,REANTTWEAKBAR_MAX_LINE,fp) == NULL)
  446. {
  447. finished = true;
  448. break;
  449. }
  450. // Blank lines and lines that begin with # are comments
  451. still_comments = (line[0] == '#' || line[0] == '\n');
  452. j++;
  453. }
  454. if(finished)
  455. {
  456. break;
  457. }
  458. sscanf(line,"%[^:]: %s %[^\n]",name,type_str,value_str);
  459. //printf("%s: %s %s\n",name, type_str,value_str);
  460. TwType type;
  461. if(!type_from_string(type_str,type))
  462. {
  463. printf("ERROR: %s type not found... Skipping...\n",type_str);
  464. continue;
  465. }
  466. set_value_from_string(name,type,value_str);
  467. }
  468. fclose(fp);
  469. // everything succeeded
  470. return true;
  471. }
  472. IGL_INLINE bool igl::ReTwBar::type_from_string(const char *type_str, TwType & type)
  473. {
  474. // first check default types
  475. for(int j = 0; j < RETW_NUM_DEFAULT_TYPE_STRINGS; j++)
  476. {
  477. if(strcmp(type_str,ReTwDefaultTypeStrings[j].type_str) == 0)
  478. {
  479. type = ReTwDefaultTypeStrings[j].type;
  480. return true;
  481. break;
  482. }
  483. }
  484. // then check custom types
  485. std::map<TwType,std::pair<const char *,std::vector<TwEnumVal> > >::iterator iter =
  486. ReTw_custom_types.begin();
  487. for(;iter != ReTw_custom_types.end(); iter++)
  488. {
  489. if(strcmp((*iter).second.first,type_str)==0)
  490. {
  491. type = (*iter).first;
  492. return true;
  493. }
  494. }
  495. return false;
  496. }
  497. bool igl::ReTwBar::set_value_from_string(
  498. const char * name,
  499. TwType type,
  500. const char * value_str)
  501. {
  502. void * value = NULL;
  503. // possible value slots
  504. int i;
  505. float v;
  506. double dv;
  507. float f[4];
  508. double d[4];
  509. bool b;
  510. unsigned char uc;
  511. std::string s;
  512. // First try to get value from default types
  513. switch(type)
  514. {
  515. case TW_TYPE_BOOLCPP:
  516. {
  517. int ib;
  518. if(sscanf(value_str," %d",&ib) == 1)
  519. {
  520. b = ib!=0;
  521. value = &b;
  522. }else
  523. {
  524. printf("ERROR: Bad value format...\n");
  525. return false;
  526. }
  527. break;
  528. }
  529. case TW_TYPE_QUAT4D:
  530. //case TW_TYPE_COLOR4D:
  531. {
  532. if(sscanf(value_str," %lf %lf %lf %lf",&d[0],&d[1],&d[2],&d[3]) == 4)
  533. {
  534. value = &d;
  535. }else
  536. {
  537. printf("ERROR: Bad value format...\n");
  538. return false;
  539. }
  540. break;
  541. }
  542. case TW_TYPE_QUAT4F:
  543. case TW_TYPE_COLOR4F:
  544. {
  545. if(sscanf(value_str," %f %f %f %f",&f[0],&f[1],&f[2],&f[3]) == 4)
  546. {
  547. value = &f;
  548. }else
  549. {
  550. printf("ERROR: Bad value format...\n");
  551. return false;
  552. }
  553. break;
  554. }
  555. //case TW_TYPE_COLOR3D:
  556. case TW_TYPE_DIR3D:
  557. {
  558. if(sscanf(value_str," %lf %lf %lf",&d[0],&d[1],&d[2]) == 3)
  559. {
  560. value = &d;
  561. }else
  562. {
  563. printf("ERROR: Bad value format...\n");
  564. return false;
  565. }
  566. break;
  567. }
  568. case TW_TYPE_COLOR3F:
  569. case TW_TYPE_DIR3F:
  570. {
  571. if(sscanf(value_str," %f %f %f",&f[0],&f[1],&f[2]) == 3)
  572. {
  573. value = &f;
  574. }else
  575. {
  576. printf("ERROR: Bad value format...\n");
  577. return false;
  578. }
  579. break;
  580. }
  581. case TW_TYPE_UINT8:
  582. {
  583. if(sscanf(value_str," %d",&i) == 1)
  584. {
  585. // Cast to unsigned char
  586. uc = (unsigned char) i;
  587. value = &uc;
  588. }else
  589. {
  590. printf("ERROR: Bad value format...\n");
  591. return false;
  592. }
  593. break;
  594. }
  595. case TW_TYPE_BOOL32:
  596. case TW_TYPE_INT32:
  597. {
  598. if(sscanf(value_str," %d",&i) == 1)
  599. {
  600. value = &i;
  601. }else
  602. {
  603. printf("ERROR: Bad value format...\n");
  604. return false;
  605. }
  606. break;
  607. }
  608. case TW_TYPE_FLOAT:
  609. {
  610. if(sscanf(value_str," %f",&v) == 1)
  611. {
  612. value = &v;
  613. }else
  614. {
  615. printf("ERROR: Bad value format...\n");
  616. return false;
  617. }
  618. break;
  619. }
  620. case TW_TYPE_DOUBLE:
  621. {
  622. if(sscanf(value_str," %lf",&dv) == 1)
  623. {
  624. value = &dv;
  625. }else
  626. {
  627. printf("ERROR: Bad value format...\n");
  628. return false;
  629. }
  630. break;
  631. }
  632. case TW_TYPE_STDSTRING:
  633. {
  634. s = value_str;
  635. value = &s;
  636. break;
  637. }
  638. default:
  639. // Try to find type in custom enum types
  640. std::map<TwType,std::pair<const char *,std::vector<TwEnumVal> > >::iterator iter =
  641. ReTw_custom_types.find(type);
  642. if(iter != ReTw_custom_types.end())
  643. {
  644. std::vector<TwEnumVal>::iterator eit = (*iter).second.second.begin();
  645. bool found = false;
  646. for(;eit<(*iter).second.second.end();eit++)
  647. {
  648. if(strcmp(value_str,eit->Label) == 0)
  649. {
  650. i = eit->Value;
  651. value = &i;
  652. found = true;
  653. break;
  654. }
  655. }
  656. if(!found)
  657. {
  658. printf("ERROR_ENUM_VALUE_NOT_DEFINED");
  659. }
  660. }else
  661. {
  662. printf("ERROR_TYPE_NOT_SUPPORTED\n");
  663. }
  664. break;
  665. }
  666. // Find variable based on name
  667. // First look in RW items
  668. bool item_found = false;
  669. for(
  670. std::vector<ReTwRWItem>::iterator it = rw_items.begin();
  671. it != rw_items.end();
  672. it++)
  673. {
  674. if(it->name == name)
  675. {
  676. void * var = it->var;
  677. switch(type)
  678. {
  679. case TW_TYPE_BOOLCPP:
  680. {
  681. bool * bvar = static_cast<bool*>(var);
  682. bool * bvalue = static_cast<bool*>(value);
  683. *bvar = *bvalue;
  684. break;
  685. }
  686. case TW_TYPE_QUAT4D:
  687. //case TW_TYPE_COLOR4D:
  688. {
  689. double * dvar = static_cast<double*>(var);
  690. double * dvalue = static_cast<double*>(value);
  691. dvar[0] = dvalue[0];
  692. dvar[1] = dvalue[1];
  693. dvar[2] = dvalue[2];
  694. dvar[3] = dvalue[3];
  695. break;
  696. }
  697. case TW_TYPE_QUAT4F:
  698. case TW_TYPE_COLOR4F:
  699. {
  700. float * fvar = static_cast<float*>(var);
  701. float * fvalue = static_cast<float*>(value);
  702. fvar[0] = fvalue[0];
  703. fvar[1] = fvalue[1];
  704. fvar[2] = fvalue[2];
  705. fvar[3] = fvalue[3];
  706. break;
  707. }
  708. //case TW_TYPE_COLOR3D:
  709. case TW_TYPE_DIR3D:
  710. {
  711. double * dvar = static_cast<double*>(var);
  712. double * dvalue = static_cast<double*>(value);
  713. dvar[0] = dvalue[0];
  714. dvar[1] = dvalue[1];
  715. dvar[2] = dvalue[2];
  716. break;
  717. }
  718. case TW_TYPE_COLOR3F:
  719. case TW_TYPE_DIR3F:
  720. {
  721. float * fvar = static_cast<float*>(var);
  722. float * fvalue = static_cast<float*>(value);
  723. fvar[0] = fvalue[0];
  724. fvar[1] = fvalue[1];
  725. fvar[2] = fvalue[2];
  726. break;
  727. }
  728. case TW_TYPE_UINT8:
  729. {
  730. unsigned char * ucvar = static_cast<unsigned char*>(var);
  731. unsigned char * ucvalue = static_cast<unsigned char*>(value);
  732. *ucvar = *ucvalue;
  733. break;
  734. }
  735. case TW_TYPE_BOOL32:
  736. case TW_TYPE_INT32:
  737. {
  738. int * ivar = static_cast<int*>(var);
  739. int * ivalue = static_cast<int*>(value);
  740. *ivar = *ivalue;
  741. break;
  742. }
  743. case TW_TYPE_FLOAT:
  744. {
  745. float * fvar = static_cast<float*>(var);
  746. float * fvalue = static_cast<float*>(value);
  747. *fvar = *fvalue;
  748. break;
  749. }
  750. case TW_TYPE_DOUBLE:
  751. {
  752. double * dvar = static_cast<double*>(var);
  753. double * fvalue = static_cast<double*>(value);
  754. *dvar = *fvalue;
  755. break;
  756. }
  757. case TW_TYPE_STDSTRING:
  758. {
  759. std::string * svar = static_cast<std::string*>(var);
  760. std::string * svalue = static_cast<std::string*>(value);
  761. *svar = *svalue;
  762. break;
  763. }
  764. default:
  765. // Try to find type in custom enum types
  766. std::map<TwType,std::pair<const char *,std::vector<TwEnumVal> > >::iterator iter =
  767. ReTw_custom_types.find(type);
  768. if(iter != ReTw_custom_types.end())
  769. {
  770. int * ivar = static_cast<int*>(var);
  771. std::vector<TwEnumVal>::iterator eit = (*iter).second.second.begin();
  772. bool found = false;
  773. for(;eit<(*iter).second.second.end();eit++)
  774. {
  775. if(strcmp(value_str,eit->Label) == 0)
  776. {
  777. *ivar = eit->Value;
  778. found = true;
  779. break;
  780. }
  781. }
  782. if(!found)
  783. {
  784. printf("ERROR_ENUM_VALUE_NOT_DEFINED");
  785. }
  786. }else
  787. {
  788. printf("ERROR_TYPE_NOT_SUPPORTED\n");
  789. }
  790. break;
  791. }
  792. item_found = true;
  793. break;
  794. }
  795. }
  796. // Try looking in CB items
  797. if(!item_found)
  798. {
  799. for(
  800. std::vector<ReTwCBItem>::iterator it = cb_items.begin();
  801. it != cb_items.end();
  802. it++)
  803. {
  804. if(it->name==name)
  805. {
  806. it->setCallback(value,it->clientData);
  807. item_found = true;
  808. break;
  809. }
  810. }
  811. }
  812. if(!item_found)
  813. {
  814. printf("ERROR: item '%s' not found\n",name);
  815. }
  816. return true;
  817. }
  818. IGL_INLINE const std::vector<igl::ReTwRWItem> & igl::ReTwBar::get_rw_items()
  819. {
  820. return rw_items;
  821. }
  822. IGL_INLINE const std::vector<igl::ReTwCBItem> & igl::ReTwBar::get_cb_items()
  823. {
  824. return cb_items;
  825. }
  826. #endif