ReAntTweakBar.cpp 16 KB

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