ReAntTweakBar.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #ifndef IGL_REANTTWEAKBAR_H
  2. #define IGL_REANTTWEAKBAR_H
  3. #ifndef IGL_NO_ANTTWEAKBAR
  4. #include "igl_inline.h"
  5. // ReAntTweakBar is a minimal wrapper for the AntTweakBar library that allows
  6. // "bars" to be saved and load from disk. Changing your existing app that uses
  7. // AntTweakBar to use ReAntTweakBar is trivial.
  8. //
  9. // Many (but not all) variable types are supported. I'll try to keep track them
  10. // here:
  11. // TW_TYPE_BOOLCPP
  12. // TW_TYPE_QUAT4F
  13. // TW_TYPE_QUAT4D
  14. // TW_TYPE_COLOR4F
  15. // TW_TYPE_COLOR4D
  16. // TW_TYPE_COLOR3F
  17. // TW_TYPE_DIR3F
  18. // TW_TYPE_DIR3D
  19. // TW_TYPE_BOOL32
  20. // TW_TYPE_INT32
  21. // TW_TYPE_FLOAT
  22. // TW_TYPE_DOUBLE
  23. // TW_TYPE_UINT8
  24. // and
  25. // custom TwTypes made with TwDefineEnum
  26. //
  27. // I'm working on adding the rest on an as-needed basis. Adding a new type only
  28. // requires changes in a few places...
  29. //
  30. //
  31. // Copyright Alec Jacobson, 2011
  32. //
  33. // Known bugs:
  34. // TwDefineEnumFromString is not supported (use ReTwDefineEnum instead)
  35. // Custom enums should not have spaces in their names
  36. // This allows the user to have a non-global, static installation of
  37. // AntTweakBar
  38. #ifdef STATIC_ANTTWEAKBAR
  39. # include "AntTweakBar.h"
  40. #else
  41. # include <AntTweakBar.h>
  42. #endif
  43. #include <vector>
  44. #include <string>
  45. #define REANTTWEAKBAR_MAX_CB_VAR_SIZE 1000
  46. // Max line size for reading files
  47. #define REANTTWEAKBAR_MAX_LINE 1000
  48. #define REANTTWEAKBAR_MAX_WORD 100
  49. namespace igl
  50. {
  51. TwType ReTwDefineEnum(
  52. const char *name,
  53. const TwEnumVal *enumValues,
  54. unsigned int nbValues);
  55. struct ReTwRWItem
  56. {
  57. //const char * name;
  58. std::string name;
  59. TwType type;
  60. void * var;
  61. // Default constructor
  62. ReTwRWItem(
  63. const std::string _name,
  64. TwType _type,
  65. void *_var):
  66. name(_name),
  67. type(_type),
  68. var(_var)
  69. {
  70. }
  71. // Shallow copy constructor
  72. // I solemnly swear it's OK to copy var this way
  73. ReTwRWItem(const ReTwRWItem & that):
  74. name(that.name),
  75. type(that.type),
  76. var(that.var)
  77. {
  78. }
  79. // Shallow assignment
  80. // I solemnly swear it's OK to copy var this way
  81. ReTwRWItem & operator=(const ReTwRWItem & that)
  82. {
  83. if(this != &that)
  84. {
  85. this->name = that.name;
  86. this->type = that.type;
  87. this->var = that.var;
  88. }
  89. return *this;
  90. }
  91. };
  92. struct ReTwCBItem
  93. {
  94. //const char * name;
  95. std::string name;
  96. TwType type;
  97. TwSetVarCallback setCallback;
  98. TwGetVarCallback getCallback;
  99. void * clientData;
  100. // Default constructor
  101. ReTwCBItem(
  102. const std::string _name,
  103. TwType _type,
  104. TwSetVarCallback _setCallback,
  105. TwGetVarCallback _getCallback,
  106. void * _clientData):
  107. name(_name),
  108. type(_type),
  109. setCallback(_setCallback),
  110. getCallback(_getCallback),
  111. clientData(_clientData)
  112. {
  113. }
  114. // Shallow copy
  115. // I solemnly swear it's OK to copy clientData this way
  116. ReTwCBItem(const ReTwCBItem & that):
  117. name(that.name),
  118. type(that.type),
  119. setCallback(that.setCallback),
  120. getCallback(that.getCallback),
  121. clientData(that.clientData)
  122. {
  123. }
  124. // Shallow assignment
  125. // I solemnly swear it's OK to copy clientData this way
  126. ReTwCBItem & operator=(const ReTwCBItem & that)
  127. {
  128. if(this != &that)
  129. {
  130. name = that.name;
  131. type = that.type;
  132. setCallback = that.setCallback;
  133. getCallback = that.getCallback;
  134. clientData = that.clientData;
  135. }
  136. return *this;
  137. }
  138. };
  139. class ReTwBar
  140. {
  141. // VARIABLES
  142. // Should be private, but seeing as I'm not going to implement all of the
  143. // AntTweakBar public functions right away, I'll expose this so that at
  144. // anytime AntTweakBar functions can be called directly on the bar
  145. public:
  146. TwBar * bar;
  147. // Alec: This causes trouble (not sure why)
  148. //std::string name;
  149. protected:
  150. std::vector<ReTwRWItem> rw_items;
  151. std::vector<ReTwCBItem> cb_items;
  152. public:
  153. // Default constructor with explicit initialization
  154. ReTwBar();
  155. private:
  156. // Copy constructor does shallow copy
  157. ReTwBar(const ReTwBar & that);
  158. // Assignment operator does shallow assignment
  159. ReTwBar &operator=(const ReTwBar & that);
  160. // WRAPPERS FOR ANTTWEAKBAR FUNCTIONS
  161. public:
  162. void TwNewBar(const char *barName);
  163. int TwAddVarRW(
  164. const char *name,
  165. TwType type,
  166. void *var,
  167. const char *def,
  168. const bool record=true);
  169. int TwAddVarCB(
  170. const char *name,
  171. TwType type,
  172. TwSetVarCallback setCallback,
  173. TwGetVarCallback getCallback,
  174. void *clientData,
  175. const char *def,
  176. const bool record=true);
  177. // Wrappers for convenience (not recorded, just passed on)
  178. int TwAddVarRO(const char *name, TwType type, void *var, const char *def);
  179. int TwAddButton(
  180. const char *name,
  181. TwButtonCallback buttonCallback,
  182. void *clientData,
  183. const char *def);
  184. int TwSetParam(
  185. const char *varName,
  186. const char *paramName,
  187. TwParamValueType paramValueType,
  188. unsigned int inValueCount,
  189. const void *inValues);
  190. int TwGetParam(
  191. const char *varName,
  192. const char *paramName,
  193. TwParamValueType paramValueType,
  194. unsigned int outValueMaxCount,
  195. void *outValues);
  196. int TwRefreshBar();
  197. int TwTerminate();
  198. // IO FUNCTIONS
  199. public:
  200. // Save current items to file
  201. // Input:
  202. // file_name name of file to save data to, can be null which means print
  203. // to stdout
  204. // Return:
  205. // true only if there were no (fatal) errors
  206. bool save(const char *file_name);
  207. std::string get_value_as_string(
  208. void * var,
  209. TwType type);
  210. // Load into current items from file
  211. // Input:
  212. // file_name name of input file to load
  213. // Return:
  214. // true only if there were no (fatal) errors
  215. bool load(const char *file_name);
  216. // Get TwType from string
  217. // Input
  218. // type_str string of type
  219. // Output
  220. // type TwType converted from string
  221. // Returns
  222. // true only if string matched a valid type
  223. bool type_from_string(const char *type_str, TwType & type);
  224. // I realize that I mix std::string and const char * all over the place.
  225. // What can you do...
  226. bool set_value_from_string(
  227. const char * name,
  228. TwType type,
  229. const char * value_str);
  230. const std::vector<ReTwRWItem> & get_rw_items();
  231. const std::vector<ReTwCBItem> & get_cb_items();
  232. };
  233. };
  234. // List of TwBar functions
  235. //TW_API TwBar * TW_CALL TwNewBar(const char *barName);
  236. //TW_API int TW_CALL TwDeleteBar(TwBar *bar);
  237. //TW_API int TW_CALL TwDeleteAllBars();
  238. //TW_API int TW_CALL TwSetTopBar(const TwBar *bar);
  239. //TW_API TwBar * TW_CALL TwGetTopBar();
  240. //TW_API int TW_CALL TwSetBottomBar(const TwBar *bar);
  241. //TW_API TwBar * TW_CALL TwGetBottomBar();
  242. //TW_API const char * TW_CALL TwGetBarName(TwBar *bar);
  243. //TW_API int TW_CALL TwGetBarCount();
  244. //TW_API TwBar * TW_CALL TwGetBarByIndex(int barIndex);
  245. //TW_API TwBar * TW_CALL TwGetBarByName(const char *barName);
  246. //TW_API int TW_CALL TwRefreshBar(TwBar *bar);
  247. //TW_API int TW_CALL TwTerminate();
  248. //
  249. //TW_API int TW_CALL TwAddVarRW(TwBar *bar, const char *name, TwType type, void *var, const char *def);
  250. //TW_API int TW_CALL TwAddVarRO(TwBar *bar, const char *name, TwType type, const void *var, const char *def);
  251. //TW_API int TW_CALL TwAddVarCB(TwBar *bar, const char *name, TwType type, TwSetVarCallback setCallback, TwGetVarCallback getCallback, void *clientData, const char *def);
  252. //TW_API int TW_CALL TwAddButton(TwBar *bar, const char *name, TwButtonCallback callback, void *clientData, const char *def);
  253. //TW_API int TW_CALL TwAddSeparator(TwBar *bar, const char *name, const char *def);
  254. //TW_API int TW_CALL TwRemoveVar(TwBar *bar, const char *name);
  255. //TW_API int TW_CALL TwRemoveAllVars(TwBar *bar);
  256. #ifdef IGL_HEADER_ONLY
  257. # include "ReAntTweakBar.cpp"
  258. #endif
  259. #endif
  260. #endif