ReAntTweakBar.cpp 19 KB

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