ReAntTweakBar.cpp 16 KB

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