ReAntTweakBar.cpp 19 KB

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