ReAntTweakBar.h 8.8 KB

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