ReAntTweakBar.cpp 19 KB

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