ReAntTweakBar.h 8.2 KB

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