ReAntTweakBar.cpp 16 KB

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