ReAntTweakBar.h 8.5 KB

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