ReAntTweakBar.cpp 16 KB

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