ReAntTweakBar.h 8.7 KB

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