ReAntTweakBar.cpp 19 KB

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