ReAntTweakBar.h 8.7 KB

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