ReAntTweakBar.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. #ifndef IGL_REANTTWEAKBAR_H
  2. #define IGL_REANTTWEAKBAR_H
  3. // ReAntTweakBar is a minimal wrapper for the AntTweakBar library that allows
  4. // "bars" to be saved and load from disk. Changing your existing app that users
  5. // AntTweakBar to use ReAntTweakBar is trivial.
  6. //
  7. // Many (but not all) variable types are supported. I'll try to keep track them
  8. // here:
  9. // TW_TYPE_BOOLCPP
  10. // TW_TYPE_QUAT4F
  11. // TW_TYPE_COLOR4F
  12. // TW_TYPE_COLOR3F
  13. // TW_TYPE_DIR3F
  14. // TW_TYPE_BOOL32
  15. // TW_TYPE_INT32
  16. // TW_TYPE_FLOAT
  17. // TW_TYPE_DOUBLE
  18. // and
  19. // custom TwTypes made with TwDefineEnum
  20. //
  21. // I'm working on adding the rest on an as-needed basis. Adding a new type only
  22. // requires changes in a few places...
  23. //
  24. //
  25. // Copyright Alec Jacobson, 2011
  26. //
  27. // This allows the user to have a non-global, static installation of
  28. // AntTweakBar
  29. #ifdef STATIC_ANTTWEAKBAR
  30. # include "AntTweakBar.h"
  31. #else
  32. # include <AntTweakBar.h>
  33. #endif
  34. #include <vector>
  35. #include <string>
  36. namespace igl
  37. {
  38. TwType ReTwDefineEnum(const char *name, const TwEnumVal *enumValues, unsigned int nbValues);
  39. struct ReTwRWItem
  40. {
  41. const char * name;
  42. TwType type;
  43. void * var;
  44. ReTwRWItem(
  45. const char * name,
  46. TwType type,
  47. void *var)
  48. {
  49. this->name = name;
  50. this->type = type;
  51. this->var = var;
  52. }
  53. };
  54. struct ReTwCBItem
  55. {
  56. const char * name;
  57. TwSetVarCallback setCallback;
  58. TwGetVarCallback getCallback;
  59. void * clientData;
  60. TwType type;
  61. ReTwCBItem(
  62. const char * name,
  63. TwType type,
  64. TwSetVarCallback setCallback,
  65. TwGetVarCallback getCallback,
  66. void * clientData)
  67. {
  68. this->name = name;
  69. this->type = type;
  70. this->setCallback = setCallback;
  71. this->getCallback = getCallback;
  72. this->clientData = clientData;
  73. }
  74. };
  75. class ReTwBar
  76. {
  77. // VARIABLES
  78. // Should be private, but seeing as I'm not going to implement all of the
  79. // AntTweakBar public functions right away, I'll expose this so that at
  80. // anytime AntTweakBar functions can be called directly on the bar
  81. public:
  82. TwBar * bar;
  83. private:
  84. std::vector<ReTwRWItem> rw_items;
  85. std::vector<ReTwCBItem> cb_items;
  86. // WRAPPERS FOR ANTTWEAKBAR FUNCTIONS
  87. public:
  88. void TwNewBar(const char *barName);
  89. int TwAddVarRW(
  90. const char *name,
  91. TwType type,
  92. void *var,
  93. const char *def,
  94. const bool record=true);
  95. int TwAddVarCB(
  96. const char *name,
  97. TwType type,
  98. TwSetVarCallback setCallback,
  99. TwGetVarCallback getCallback,
  100. void *clientData,
  101. const char *def,
  102. const bool record=true);
  103. // Wrappers for convenience (not recorded, just passed on)
  104. int TwAddVarRO(const char *name, TwType type, void *var, const char *def);
  105. int TwAddButton(
  106. const char *name,
  107. TwButtonCallback buttonCallback,
  108. void *clientData,
  109. const char *def);
  110. int TwSetParam(
  111. const char *varName,
  112. const char *paramName,
  113. TwParamValueType paramValueType,
  114. unsigned int inValueCount,
  115. const void *inValues);
  116. int TwGetParam(
  117. const char *varName,
  118. const char *paramName,
  119. TwParamValueType paramValueType,
  120. unsigned int outValueMaxCount,
  121. void *outValues);
  122. int TwRefreshBar();
  123. // IO FUNCTIONS
  124. public:
  125. // Save current items to file
  126. // Input:
  127. // file_name name of file to save data to, can be null which means print
  128. // to stdout
  129. // Return:
  130. // true only if there were no (fatal) errors
  131. bool save(const char *file_name);
  132. std::string get_value_as_string(
  133. void * var,
  134. TwType type);
  135. // Load into current items from file
  136. // Input:
  137. // file_name name of input file to load
  138. // Return:
  139. // true only if there were no (fatal) errors
  140. bool load(const char *file_name);
  141. // Get TwType from string
  142. // Input
  143. // type_str string of type
  144. // Output
  145. // type TwType converted from string
  146. // Returns
  147. // true only if string matched a valid type
  148. bool type_from_string(const char *type_str, TwType & type);
  149. // I realize that I mixing std::string and const char * all over the place.
  150. // What can you do...
  151. bool set_value_from_string(
  152. const char * name,
  153. TwType type,
  154. const char * value_str);
  155. };
  156. };
  157. // List of TwBar functions
  158. //TW_API TwBar * TW_CALL TwNewBar(const char *barName);
  159. //TW_API int TW_CALL TwDeleteBar(TwBar *bar);
  160. //TW_API int TW_CALL TwDeleteAllBars();
  161. //TW_API int TW_CALL TwSetTopBar(const TwBar *bar);
  162. //TW_API TwBar * TW_CALL TwGetTopBar();
  163. //TW_API int TW_CALL TwSetBottomBar(const TwBar *bar);
  164. //TW_API TwBar * TW_CALL TwGetBottomBar();
  165. //TW_API const char * TW_CALL TwGetBarName(TwBar *bar);
  166. //TW_API int TW_CALL TwGetBarCount();
  167. //TW_API TwBar * TW_CALL TwGetBarByIndex(int barIndex);
  168. //TW_API TwBar * TW_CALL TwGetBarByName(const char *barName);
  169. //TW_API int TW_CALL TwRefreshBar(TwBar *bar);
  170. //
  171. //TW_API int TW_CALL TwAddVarRW(TwBar *bar, const char *name, TwType type, void *var, const char *def);
  172. //TW_API int TW_CALL TwAddVarRO(TwBar *bar, const char *name, TwType type, const void *var, const char *def);
  173. //TW_API int TW_CALL TwAddVarCB(TwBar *bar, const char *name, TwType type, TwSetVarCallback setCallback, TwGetVarCallback getCallback, void *clientData, const char *def);
  174. //TW_API int TW_CALL TwAddButton(TwBar *bar, const char *name, TwButtonCallback callback, void *clientData, const char *def);
  175. //TW_API int TW_CALL TwAddSeparator(TwBar *bar, const char *name, const char *def);
  176. //TW_API int TW_CALL TwRemoveVar(TwBar *bar, const char *name);
  177. //TW_API int TW_CALL TwRemoveAllVars(TwBar *bar);
  178. // Implementation
  179. #include <cstdio>
  180. #include <sstream>
  181. #include <map>
  182. #define MAX_CB_VAR_SIZE 10
  183. // Max line size for reading files
  184. #define MAX_LINE 1000
  185. #define MAX_WORD 100
  186. // GLOBAL WRAPPERS
  187. namespace igl
  188. {
  189. std::map<TwType,std::pair<const char *,std::vector<TwEnumVal> > > ReTw_custom_types;
  190. }
  191. TwType igl::ReTwDefineEnum(
  192. const char *name,
  193. const TwEnumVal *enumValues,
  194. unsigned int nbValues)
  195. {
  196. // copy enum valus into vector
  197. std::vector<TwEnumVal> enum_vals;
  198. enum_vals.resize(nbValues);
  199. for(unsigned int j = 0; j<nbValues;j++)
  200. {
  201. enum_vals[j] = enumValues[j];
  202. }
  203. TwType type = TwDefineEnum(name,enumValues,nbValues);
  204. ReTw_custom_types[type] =
  205. std::pair<const char *,std::vector<TwEnumVal> >(name,enum_vals);
  206. return type;
  207. }
  208. namespace igl
  209. {
  210. struct ReTwTypeString
  211. {
  212. TwType type;
  213. const char * type_str;
  214. };
  215. #define RETW_NUM_DEFAULT_TYPE_STRINGS 23
  216. ReTwTypeString ReTwDefaultTypeStrings[RETW_NUM_DEFAULT_TYPE_STRINGS] =
  217. {
  218. {TW_TYPE_UNDEF,"TW_TYPE_UNDEF"},
  219. {TW_TYPE_BOOLCPP,"TW_TYPE_BOOLCPP"},
  220. {TW_TYPE_BOOL8,"TW_TYPE_BOOL8"},
  221. {TW_TYPE_BOOL16,"TW_TYPE_BOOL16"},
  222. {TW_TYPE_BOOL32,"TW_TYPE_BOOL32"},
  223. {TW_TYPE_CHAR,"TW_TYPE_CHAR"},
  224. {TW_TYPE_INT8,"TW_TYPE_INT8"},
  225. {TW_TYPE_UINT8,"TW_TYPE_UINT8"},
  226. {TW_TYPE_INT16,"TW_TYPE_INT16"},
  227. {TW_TYPE_UINT16,"TW_TYPE_UINT16"},
  228. {TW_TYPE_INT32,"TW_TYPE_INT32"},
  229. {TW_TYPE_UINT32,"TW_TYPE_UINT32"},
  230. {TW_TYPE_FLOAT,"TW_TYPE_FLOAT"},
  231. {TW_TYPE_DOUBLE,"TW_TYPE_DOUBLE"},
  232. {TW_TYPE_COLOR32,"TW_TYPE_COLOR32"},
  233. {TW_TYPE_COLOR3F,"TW_TYPE_COLOR3F"},
  234. {TW_TYPE_COLOR4F,"TW_TYPE_COLOR4F"},
  235. {TW_TYPE_CDSTRING,"TW_TYPE_CDSTRING"},
  236. {TW_TYPE_STDSTRING,"TW_TYPE_STDSTRING"},
  237. {TW_TYPE_QUAT4F,"TW_TYPE_QUAT4F"},
  238. {TW_TYPE_QUAT4D,"TW_TYPE_QUAT4D"},
  239. {TW_TYPE_DIR3F,"TW_TYPE_DIR3F"},
  240. {TW_TYPE_DIR3D,"TW_TYPE_DIR3D"}
  241. };
  242. }
  243. // BAR WRAPPERS
  244. void igl::ReTwBar::TwNewBar(const char *barName)
  245. {
  246. // double colon without anything in front of it means look for this in the
  247. // global namespace... I hope...
  248. this->bar = ::TwNewBar(barName);
  249. }
  250. int igl::ReTwBar::TwAddVarRW(
  251. const char *name,
  252. TwType type,
  253. void *var,
  254. const char *def,
  255. const bool record)
  256. {
  257. int ret = ::TwAddVarRW(this->bar,name,type,var,def);
  258. if(ret && record)
  259. {
  260. rw_items.push_back(ReTwRWItem(name,type,var));
  261. }
  262. return ret;
  263. }
  264. int igl::ReTwBar::TwAddVarCB(
  265. const char *name,
  266. TwType type,
  267. TwSetVarCallback setCallback,
  268. TwGetVarCallback getCallback,
  269. void *clientData,
  270. const char *def,
  271. const bool record)
  272. {
  273. int ret =
  274. ::TwAddVarCB(this->bar,name,type,setCallback,getCallback,clientData,def);
  275. if(ret && record)
  276. {
  277. cb_items.push_back(ReTwCBItem(name,type,setCallback,getCallback,clientData));
  278. }
  279. return ret;
  280. }
  281. int igl::ReTwBar::TwAddVarRO(
  282. const char *name,
  283. TwType type,
  284. void *var,
  285. const char *def)
  286. {
  287. int ret = ::TwAddVarRO(this->bar,name,type,var,def);
  288. // Read only variables are not recorded
  289. //if(ret)
  290. //{
  291. // rw_items.push_back(ReTwRWItem(name,type,var));
  292. //}
  293. return ret;
  294. }
  295. int igl::ReTwBar::TwAddButton(
  296. const char *name,
  297. TwButtonCallback buttonCallback,
  298. void *clientData,
  299. const char *def)
  300. {
  301. int ret =
  302. ::TwAddButton(this->bar,name,buttonCallback,clientData,def);
  303. // buttons are not recorded
  304. //if(ret)
  305. //{
  306. // cb_items.push_back(ReTwCBItem(name,type,setCallback,getCallback,clientData));
  307. //}
  308. return ret;
  309. }
  310. int igl::ReTwBar::TwSetParam(
  311. const char *varName,
  312. const char *paramName,
  313. TwParamValueType paramValueType,
  314. unsigned int inValueCount,
  315. const void *inValues)
  316. {
  317. // For now just pass these along
  318. return
  319. ::TwSetParam(
  320. this->bar,
  321. varName,
  322. paramName,
  323. paramValueType,
  324. inValueCount,
  325. inValues);
  326. }
  327. int igl::ReTwBar::TwGetParam(
  328. const char *varName,
  329. const char *paramName,
  330. TwParamValueType paramValueType,
  331. unsigned int outValueMaxCount,
  332. void *outValues)
  333. {
  334. return
  335. ::TwGetParam(
  336. this->bar,
  337. varName,
  338. paramName,
  339. paramValueType,
  340. outValueMaxCount,
  341. outValues);
  342. }
  343. int igl::ReTwBar::TwRefreshBar()
  344. {
  345. return ::TwRefreshBar(this->bar);
  346. }
  347. bool igl::ReTwBar::save(const char *file_name)
  348. {
  349. FILE * fp;
  350. if(file_name == NULL)
  351. {
  352. fp = stdout;
  353. }else
  354. {
  355. fp = fopen(file_name,"w");
  356. }
  357. if(fp == NULL)
  358. {
  359. printf("ERROR: not able to open %s for writing...\n",file_name);
  360. return false;
  361. }
  362. // Print all RW variables
  363. for(
  364. std::vector<ReTwRWItem>::iterator it = rw_items.begin();
  365. it != rw_items.end();
  366. it++)
  367. {
  368. const char * name = (*it).name;
  369. TwType type = (*it).type;
  370. void * var = (*it).var;
  371. fprintf(fp,"%s: %s\n",
  372. name,
  373. get_value_as_string(var,type).c_str());
  374. }
  375. char var[MAX_CB_VAR_SIZE];
  376. // Print all CB variables
  377. for(
  378. std::vector<ReTwCBItem>::iterator it = cb_items.begin();
  379. it != cb_items.end();
  380. it++)
  381. {
  382. const char * name = it->name;
  383. TwType type = it->type;
  384. //TwSetVarCallback setCallback = it->setCallback;
  385. TwGetVarCallback getCallback = it->getCallback;
  386. void * clientData = it->clientData;
  387. // I'm not sure how to do what I want to do. getCallback needs to be sure
  388. // that it can write to var. So var needs to point to a valid and big
  389. // enough chunk of memory
  390. getCallback(var,clientData);
  391. fprintf(fp,"%s: %s\n",
  392. name,
  393. get_value_as_string(var,type).c_str());
  394. }
  395. fprintf(fp,"\n");
  396. if(file_name != NULL)
  397. {
  398. fclose(fp);
  399. }
  400. // everything succeeded
  401. return true;
  402. }
  403. std::string igl::ReTwBar::get_value_as_string(
  404. void * var,
  405. TwType type)
  406. {
  407. std::stringstream sstr;
  408. switch(type)
  409. {
  410. case TW_TYPE_BOOLCPP:
  411. {
  412. sstr << "TW_TYPE_BOOLCPP" << " ";
  413. sstr << *(static_cast<bool*>(var));
  414. break;
  415. }
  416. case TW_TYPE_QUAT4F:
  417. {
  418. sstr << "TW_TYPE_QUAT4F" << " ";
  419. // Q: Why does casting to float* work? shouldn't I have to cast to
  420. // float**?
  421. float * q = static_cast<float*>(var);
  422. sstr << q[0] << " " << q[1] << " " << q[2] << " " << q[3];
  423. break;
  424. }
  425. case TW_TYPE_COLOR4F:
  426. {
  427. sstr << "TW_TYPE_COLOR4F" << " ";
  428. float * c = static_cast<float*>(var);
  429. sstr << c[0] << " " << c[1] << " " << c[2] << " " << c[3];
  430. break;
  431. }
  432. case TW_TYPE_COLOR3F:
  433. {
  434. sstr << "TW_TYPE_COLOR3F" << " ";
  435. float * c = static_cast<float*>(var);
  436. sstr << c[0] << " " << c[1] << " " << c[2];
  437. break;
  438. }
  439. case TW_TYPE_DIR3F:
  440. {
  441. sstr << "TW_TYPE_DIR3F" << " ";
  442. float * d = static_cast<float*>(var);
  443. sstr << d[0] << " " << d[1] << " " << d[2];
  444. break;
  445. }
  446. case TW_TYPE_BOOL32:
  447. {
  448. sstr << "TW_TYPE_BOOL32" << " ";
  449. sstr << *(static_cast<int*>(var));
  450. break;
  451. }
  452. case TW_TYPE_INT32:
  453. {
  454. sstr << "TW_TYPE_INT32" << " ";
  455. sstr << *(static_cast<int*>(var));
  456. break;
  457. }
  458. case TW_TYPE_FLOAT:
  459. {
  460. sstr << "TW_TYPE_FLOAT" << " ";
  461. sstr << *(static_cast<float*>(var));
  462. break;
  463. }
  464. case TW_TYPE_DOUBLE:
  465. {
  466. sstr << "TW_TYPE_DOUBLE" << " ";
  467. sstr << *(static_cast<double*>(var));
  468. break;
  469. }
  470. default:
  471. {
  472. std::map<TwType,std::pair<const char *,std::vector<TwEnumVal> > >::iterator iter =
  473. ReTw_custom_types.find(type);
  474. if(iter != ReTw_custom_types.end())
  475. {
  476. sstr << (*iter).second.first << " ";
  477. int enum_val = *(static_cast<int*>(var));
  478. // try find display name for enum value
  479. std::vector<TwEnumVal>::iterator eit = (*iter).second.second.begin();
  480. bool found = false;
  481. for(;eit<(*iter).second.second.end();eit++)
  482. {
  483. if(enum_val == eit->Value)
  484. {
  485. sstr << eit->Label;
  486. found = true;
  487. break;
  488. }
  489. }
  490. if(!found)
  491. {
  492. sstr << "ERROR_ENUM_VALUE_NOT_DEFINED";
  493. }
  494. }else
  495. {
  496. sstr << "ERROR_TYPE_NOT_SUPPORTED";
  497. }
  498. break;
  499. }
  500. }
  501. return sstr.str();
  502. }
  503. bool igl::ReTwBar::load(const char *file_name)
  504. {
  505. FILE * fp;
  506. fp = fopen(file_name,"r");
  507. if(fp == NULL)
  508. {
  509. printf("ERROR: not able to open %s for reading...\n",file_name);
  510. return false;
  511. }
  512. // go through file line by line
  513. char line[MAX_LINE];
  514. bool still_comments;
  515. char name[MAX_WORD];
  516. char type_str[MAX_WORD];
  517. char value_str[MAX_WORD];
  518. // line number
  519. int j = 0;
  520. bool finished = false;
  521. while(true)
  522. {
  523. // Eat comments
  524. still_comments = true;
  525. while(still_comments)
  526. {
  527. if(fgets(line,MAX_LINE,fp) == NULL)
  528. {
  529. finished = true;
  530. break;
  531. }
  532. // Blank lines and lines that begin with # are comments
  533. still_comments = (line[0] == '#' || line[0] == '\n');
  534. j++;
  535. }
  536. if(finished)
  537. {
  538. break;
  539. }
  540. sscanf(line,"%[^:]: %s %[^\n]",name,type_str,value_str);
  541. //printf("%s: %s %s\n",name, type_str,value_str);
  542. TwType type;
  543. if(!type_from_string(type_str,type))
  544. {
  545. printf("ERROR: %s type not found... Skipping...\n",type_str);
  546. continue;
  547. }
  548. set_value_from_string(name,type,value_str);
  549. }
  550. fclose(fp);
  551. // everything succeeded
  552. return true;
  553. }
  554. bool igl::ReTwBar::type_from_string(const char *type_str, TwType & type)
  555. {
  556. // first check default types
  557. for(int j = 0; j < RETW_NUM_DEFAULT_TYPE_STRINGS; j++)
  558. {
  559. if(strcmp(type_str,ReTwDefaultTypeStrings[j].type_str) == 0)
  560. {
  561. type = ReTwDefaultTypeStrings[j].type;
  562. return true;
  563. break;
  564. }
  565. }
  566. // then check custom types
  567. std::map<TwType,std::pair<const char *,std::vector<TwEnumVal> > >::iterator iter =
  568. ReTw_custom_types.begin();
  569. for(;iter != ReTw_custom_types.end(); iter++)
  570. {
  571. if(strcmp((*iter).second.first,type_str)==0)
  572. {
  573. type = (*iter).first;
  574. return true;
  575. }
  576. }
  577. return false;
  578. }
  579. bool igl::ReTwBar::set_value_from_string(
  580. const char * name,
  581. TwType type,
  582. const char * value_str)
  583. {
  584. void * value = NULL;
  585. // possible value slots
  586. int i;
  587. float v;
  588. double dv;
  589. float f[4];
  590. bool b;
  591. // First try to get value from default types
  592. switch(type)
  593. {
  594. case TW_TYPE_BOOLCPP:
  595. {
  596. int ib;
  597. if(sscanf(value_str," %d",&ib) == 1)
  598. {
  599. b = ib!=0;
  600. value = &b;
  601. }else
  602. {
  603. printf("ERROR: Bad value format...\n");
  604. return false;
  605. }
  606. break;
  607. }
  608. case TW_TYPE_QUAT4F:
  609. case TW_TYPE_COLOR4F:
  610. {
  611. if(sscanf(value_str," %f %f %f %f",&f[0],&f[1],&f[2],&f[3]) == 4)
  612. {
  613. value = &f;
  614. }else
  615. {
  616. printf("ERROR: Bad value format...\n");
  617. return false;
  618. }
  619. break;
  620. }
  621. case TW_TYPE_COLOR3F:
  622. case TW_TYPE_DIR3F:
  623. {
  624. if(sscanf(value_str," %f %f %f",&f[0],&f[1],&f[2]) == 3)
  625. {
  626. value = &f;
  627. }else
  628. {
  629. printf("ERROR: Bad value format...\n");
  630. return false;
  631. }
  632. break;
  633. }
  634. case TW_TYPE_BOOL32:
  635. case TW_TYPE_INT32:
  636. {
  637. if(sscanf(value_str," %d",&i) == 1)
  638. {
  639. value = &i;
  640. }else
  641. {
  642. printf("ERROR: Bad value format...\n");
  643. return false;
  644. }
  645. break;
  646. }
  647. case TW_TYPE_FLOAT:
  648. {
  649. if(sscanf(value_str," %f",&v) == 1)
  650. {
  651. value = &v;
  652. }else
  653. {
  654. printf("ERROR: Bad value format...\n");
  655. return false;
  656. }
  657. break;
  658. }
  659. case TW_TYPE_DOUBLE:
  660. {
  661. if(sscanf(value_str," %lf",&dv) == 1)
  662. {
  663. value = &dv;
  664. }else
  665. {
  666. printf("ERROR: Bad value format...\n");
  667. return false;
  668. }
  669. break;
  670. }
  671. default:
  672. // Try to find type in custom enum types
  673. std::map<TwType,std::pair<const char *,std::vector<TwEnumVal> > >::iterator iter =
  674. ReTw_custom_types.find(type);
  675. if(iter != ReTw_custom_types.end())
  676. {
  677. std::vector<TwEnumVal>::iterator eit = (*iter).second.second.begin();
  678. bool found = false;
  679. for(;eit<(*iter).second.second.end();eit++)
  680. {
  681. if(strcmp(value_str,eit->Label) == 0)
  682. {
  683. i = eit->Value;
  684. value = &i;
  685. found = true;
  686. break;
  687. }
  688. }
  689. if(!found)
  690. {
  691. printf("ERROR_ENUM_VALUE_NOT_DEFINED");
  692. }
  693. }else
  694. {
  695. printf("ERROR_TYPE_NOT_SUPPORTED\n");
  696. }
  697. break;
  698. }
  699. // Find variable based on name
  700. // First look in RW items
  701. bool item_found = false;
  702. for(
  703. std::vector<ReTwRWItem>::iterator it = rw_items.begin();
  704. it != rw_items.end();
  705. it++)
  706. {
  707. if(strcmp(it->name,name) == 0)
  708. {
  709. void * var = it->var;
  710. switch(type)
  711. {
  712. case TW_TYPE_BOOLCPP:
  713. {
  714. bool * bvar = static_cast<bool*>(var);
  715. bool * bvalue = static_cast<bool*>(value);
  716. *bvar = *bvalue;
  717. break;
  718. }
  719. case TW_TYPE_QUAT4F:
  720. case TW_TYPE_COLOR4F:
  721. {
  722. float * fvar = static_cast<float*>(var);
  723. float * fvalue = static_cast<float*>(value);
  724. fvar[0] = fvalue[0];
  725. fvar[1] = fvalue[1];
  726. fvar[2] = fvalue[2];
  727. fvar[3] = fvalue[3];
  728. break;
  729. }
  730. case TW_TYPE_COLOR3F:
  731. case TW_TYPE_DIR3F:
  732. {
  733. float * fvar = static_cast<float*>(var);
  734. float * fvalue = static_cast<float*>(value);
  735. fvar[0] = fvalue[0];
  736. fvar[1] = fvalue[1];
  737. fvar[2] = fvalue[2];
  738. break;
  739. }
  740. case TW_TYPE_BOOL32:
  741. case TW_TYPE_INT32:
  742. {
  743. int * ivar = static_cast<int*>(var);
  744. int * ivalue = static_cast<int*>(value);
  745. *ivar = *ivalue;
  746. break;
  747. }
  748. case TW_TYPE_FLOAT:
  749. {
  750. float * fvar = static_cast<float*>(var);
  751. float * fvalue = static_cast<float*>(value);
  752. *fvar = *fvalue;
  753. break;
  754. }
  755. case TW_TYPE_DOUBLE:
  756. {
  757. double * dvar = static_cast<double*>(var);
  758. double * fvalue = static_cast<double*>(value);
  759. *dvar = *fvalue;
  760. break;
  761. }
  762. default:
  763. // Try to find type in custom enum types
  764. std::map<TwType,std::pair<const char *,std::vector<TwEnumVal> > >::iterator iter =
  765. ReTw_custom_types.find(type);
  766. if(iter != ReTw_custom_types.end())
  767. {
  768. int * ivar = static_cast<int*>(var);
  769. std::vector<TwEnumVal>::iterator eit = (*iter).second.second.begin();
  770. bool found = false;
  771. for(;eit<(*iter).second.second.end();eit++)
  772. {
  773. if(strcmp(value_str,eit->Label) == 0)
  774. {
  775. *ivar = eit->Value;
  776. found = true;
  777. break;
  778. }
  779. }
  780. if(!found)
  781. {
  782. printf("ERROR_ENUM_VALUE_NOT_DEFINED");
  783. }
  784. }else
  785. {
  786. printf("ERROR_TYPE_NOT_SUPPORTED\n");
  787. }
  788. break;
  789. }
  790. item_found = true;
  791. break;
  792. }
  793. }
  794. // Try looking in CB items
  795. if(!item_found)
  796. {
  797. for(
  798. std::vector<ReTwCBItem>::iterator it = cb_items.begin();
  799. it != cb_items.end();
  800. it++)
  801. {
  802. if(strcmp(it->name,name) == 0)
  803. {
  804. it->setCallback(value,it->clientData);
  805. item_found = true;
  806. break;
  807. }
  808. }
  809. }
  810. if(!item_found)
  811. {
  812. printf("ERROR: item not found\n");
  813. }
  814. return true;
  815. }
  816. #endif