ReAntTweakBar.h 8.8 KB

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