ReAntTweakBar.h 8.2 KB

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