ReAntTweakBar.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. TwType ReTwDefineEnum(const char * name,const char * enumString);
  56. struct ReTwRWItem
  57. {
  58. //const char * name;
  59. std::string name;
  60. TwType type;
  61. void * var;
  62. // Default constructor
  63. IGL_INLINE ReTwRWItem(
  64. const std::string _name,
  65. TwType _type,
  66. void *_var):
  67. name(_name),
  68. type(_type),
  69. var(_var)
  70. {
  71. }
  72. // Shallow copy constructor
  73. // I solemnly swear it's OK to copy var this way
  74. IGL_INLINE ReTwRWItem(const ReTwRWItem & that):
  75. name(that.name),
  76. type(that.type),
  77. var(that.var)
  78. {
  79. }
  80. // Shallow assignment
  81. // I solemnly swear it's OK to copy var this way
  82. IGL_INLINE ReTwRWItem & operator=(const ReTwRWItem & that)
  83. {
  84. if(this != &that)
  85. {
  86. this->name = that.name;
  87. this->type = that.type;
  88. this->var = that.var;
  89. }
  90. return *this;
  91. }
  92. };
  93. struct ReTwCBItem
  94. {
  95. //const char * name;
  96. std::string name;
  97. TwType type;
  98. TwSetVarCallback setCallback;
  99. TwGetVarCallback getCallback;
  100. void * clientData;
  101. // Default constructor
  102. IGL_INLINE ReTwCBItem(
  103. const std::string _name,
  104. TwType _type,
  105. TwSetVarCallback _setCallback,
  106. TwGetVarCallback _getCallback,
  107. void * _clientData):
  108. name(_name),
  109. type(_type),
  110. setCallback(_setCallback),
  111. getCallback(_getCallback),
  112. clientData(_clientData)
  113. {
  114. }
  115. // Shallow copy
  116. // I solemnly swear it's OK to copy clientData this way
  117. IGL_INLINE ReTwCBItem(const ReTwCBItem & that):
  118. name(that.name),
  119. type(that.type),
  120. setCallback(that.setCallback),
  121. getCallback(that.getCallback),
  122. clientData(that.clientData)
  123. {
  124. }
  125. // Shallow assignment
  126. // I solemnly swear it's OK to copy clientData this way
  127. IGL_INLINE ReTwCBItem & operator=(const ReTwCBItem & that)
  128. {
  129. if(this != &that)
  130. {
  131. name = that.name;
  132. type = that.type;
  133. setCallback = that.setCallback;
  134. getCallback = that.getCallback;
  135. clientData = that.clientData;
  136. }
  137. return *this;
  138. }
  139. };
  140. class ReTwBar
  141. {
  142. // VARIABLES
  143. // Should be private, but seeing as I'm not going to implement all of the
  144. // AntTweakBar public functions right away, I'll expose this so that at
  145. // anytime AntTweakBar functions can be called directly on the bar
  146. public:
  147. TwBar * bar;
  148. // Alec: This causes trouble (not sure why)
  149. std::string name;
  150. protected:
  151. std::vector<ReTwRWItem> rw_items;
  152. std::vector<ReTwCBItem> cb_items;
  153. public:
  154. // Default constructor with explicit initialization
  155. IGL_INLINE ReTwBar();
  156. private:
  157. // Copy constructor does shallow copy
  158. IGL_INLINE ReTwBar(const ReTwBar & that);
  159. // Assignment operator does shallow assignment
  160. IGL_INLINE ReTwBar &operator=(const ReTwBar & that);
  161. // WRAPPERS FOR ANTTWEAKBAR FUNCTIONS
  162. public:
  163. void TwNewBar(const char *barName);
  164. IGL_INLINE int TwAddVarRW(
  165. const char *name,
  166. TwType type,
  167. void *var,
  168. const char *def,
  169. const bool record=true);
  170. IGL_INLINE int TwAddVarCB(
  171. const char *name,
  172. TwType type,
  173. TwSetVarCallback setCallback,
  174. TwGetVarCallback getCallback,
  175. void *clientData,
  176. const char *def,
  177. const bool record=true);
  178. // Wrappers for convenience (not recorded, just passed on)
  179. IGL_INLINE int TwAddVarRO(const char *name, TwType type, void *var, const char *def);
  180. IGL_INLINE int TwAddButton(
  181. const char *name,
  182. TwButtonCallback buttonCallback,
  183. void *clientData,
  184. const char *def);
  185. IGL_INLINE int TwSetParam(
  186. const char *varName,
  187. const char *paramName,
  188. TwParamValueType paramValueType,
  189. unsigned int inValueCount,
  190. const void *inValues);
  191. IGL_INLINE int TwGetParam(
  192. const char *varName,
  193. const char *paramName,
  194. TwParamValueType paramValueType,
  195. unsigned int outValueMaxCount,
  196. void *outValues);
  197. IGL_INLINE int TwRefreshBar();
  198. IGL_INLINE int TwTerminate();
  199. // IO FUNCTIONS
  200. public:
  201. // Save current items to file
  202. // Input:
  203. // file_name name of file to save data to, can be null which means print
  204. // to stdout
  205. // Return:
  206. // true only if there were no (fatal) errors
  207. IGL_INLINE bool save(const char *file_name);
  208. std::string get_value_as_string(
  209. void * var,
  210. TwType type);
  211. // Load into current items from file
  212. // Input:
  213. // file_name name of input file to load
  214. // Return:
  215. // true only if there were no (fatal) errors
  216. IGL_INLINE bool load(const char *file_name);
  217. // Get TwType from string
  218. // Input
  219. // type_str string of type
  220. // Output
  221. // type TwType converted from string
  222. // Returns
  223. // true only if string matched a valid type
  224. IGL_INLINE bool type_from_string(const char *type_str, TwType & type);
  225. // I realize that I mix std::string and const char * all over the place.
  226. // What can you do...
  227. IGL_INLINE bool set_value_from_string(
  228. const char * name,
  229. TwType type,
  230. const char * value_str);
  231. const std::vector<ReTwRWItem> & get_rw_items();
  232. const std::vector<ReTwCBItem> & get_cb_items();
  233. };
  234. }
  235. // List of TwBar functions
  236. //TW_API TwBar * TW_CALL TwNewBar(const char *barName);
  237. //TW_API int TW_CALL TwDeleteBar(TwBar *bar);
  238. //TW_API int TW_CALL TwDeleteAllBars();
  239. //TW_API int TW_CALL TwSetTopBar(const TwBar *bar);
  240. //TW_API TwBar * TW_CALL TwGetTopBar();
  241. //TW_API int TW_CALL TwSetBottomBar(const TwBar *bar);
  242. //TW_API TwBar * TW_CALL TwGetBottomBar();
  243. //TW_API const char * TW_CALL TwGetBarName(TwBar *bar);
  244. //TW_API int TW_CALL TwGetBarCount();
  245. //TW_API TwBar * TW_CALL TwGetBarByIndex(int barIndex);
  246. //TW_API TwBar * TW_CALL TwGetBarByName(const char *barName);
  247. //TW_API int TW_CALL TwRefreshBar(TwBar *bar);
  248. //TW_API int TW_CALL TwTerminate();
  249. //
  250. //TW_API int TW_CALL TwAddVarRW(TwBar *bar, const char *name, TwType type, void *var, const char *def);
  251. //TW_API int TW_CALL TwAddVarRO(TwBar *bar, const char *name, TwType type, const void *var, const char *def);
  252. //TW_API int TW_CALL TwAddVarCB(TwBar *bar, const char *name, TwType type, TwSetVarCallback setCallback, TwGetVarCallback getCallback, void *clientData, const char *def);
  253. //TW_API int TW_CALL TwAddButton(TwBar *bar, const char *name, TwButtonCallback callback, void *clientData, const char *def);
  254. //TW_API int TW_CALL TwAddSeparator(TwBar *bar, const char *name, const char *def);
  255. //TW_API int TW_CALL TwRemoveVar(TwBar *bar, const char *name);
  256. //TW_API int TW_CALL TwRemoveAllVars(TwBar *bar);
  257. #ifdef IGL_HEADER_ONLY
  258. # include "ReAntTweakBar.cpp"
  259. #endif
  260. #endif
  261. #endif