ReAntTweakBar.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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_ANTTWEAKBAR_REANTTWEAKBAR_H
  9. #define IGL_ANTTWEAKBAR_REANTTWEAKBAR_H
  10. #include "../igl_inline.h"
  11. // ReAntTweakBar is a minimal wrapper for the AntTweakBar library that allows
  12. // "bars" to be saved and load from disk. Changing your existing app that uses
  13. // AntTweakBar to use ReAntTweakBar is trivial.
  14. //
  15. // Many (but not all) variable types are supported. I'll try to keep track them
  16. // here:
  17. // TW_TYPE_BOOLCPP
  18. // TW_TYPE_QUAT4F
  19. // TW_TYPE_QUAT4D
  20. // TW_TYPE_COLOR4F
  21. // TW_TYPE_COLOR4D
  22. // TW_TYPE_COLOR3F
  23. // TW_TYPE_DIR3F
  24. // TW_TYPE_DIR3D
  25. // TW_TYPE_BOOL32
  26. // TW_TYPE_INT32
  27. // TW_TYPE_UINT32
  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. namespace anttweakbar
  59. {
  60. TwType ReTwDefineEnum(
  61. const char *name,
  62. const TwEnumVal *enumValues,
  63. unsigned int nbValues);
  64. TwType ReTwDefineEnumFromString(const char * name,const char * enumString);
  65. struct ReTwRWItem
  66. {
  67. //const char * name;
  68. std::string name;
  69. TwType type;
  70. void * var;
  71. // Default constructor
  72. IGL_INLINE ReTwRWItem(
  73. const std::string _name,
  74. TwType _type,
  75. void *_var):
  76. name(_name),
  77. type(_type),
  78. var(_var)
  79. {
  80. }
  81. // Shallow copy constructor
  82. // I solemnly swear it's OK to copy var this way
  83. // Q: Is it really?
  84. IGL_INLINE ReTwRWItem(const ReTwRWItem & that):
  85. name(that.name),
  86. type(that.type),
  87. var(that.var)
  88. {
  89. }
  90. // Shallow assignment
  91. // I solemnly swear it's OK to copy var this way
  92. IGL_INLINE ReTwRWItem & operator=(const ReTwRWItem & that)
  93. {
  94. if(this != &that)
  95. {
  96. this->name = that.name;
  97. this->type = that.type;
  98. this->var = that.var;
  99. }
  100. return *this;
  101. }
  102. };
  103. struct ReTwCBItem
  104. {
  105. //const char * name;
  106. std::string name;
  107. TwType type;
  108. TwSetVarCallback setCallback;
  109. TwGetVarCallback getCallback;
  110. void * clientData;
  111. // Default constructor
  112. IGL_INLINE ReTwCBItem(
  113. const std::string _name,
  114. TwType _type,
  115. TwSetVarCallback _setCallback,
  116. TwGetVarCallback _getCallback,
  117. void * _clientData):
  118. name(_name),
  119. type(_type),
  120. setCallback(_setCallback),
  121. getCallback(_getCallback),
  122. clientData(_clientData)
  123. {
  124. }
  125. // Shallow copy
  126. // I solemnly swear it's OK to copy clientData this way
  127. IGL_INLINE ReTwCBItem(const ReTwCBItem & that):
  128. name(that.name),
  129. type(that.type),
  130. setCallback(that.setCallback),
  131. getCallback(that.getCallback),
  132. clientData(that.clientData)
  133. {
  134. }
  135. // Shallow assignment
  136. // I solemnly swear it's OK to copy clientData this way
  137. IGL_INLINE ReTwCBItem & operator=(const ReTwCBItem & that)
  138. {
  139. if(this != &that)
  140. {
  141. name = that.name;
  142. type = that.type;
  143. setCallback = that.setCallback;
  144. getCallback = that.getCallback;
  145. clientData = that.clientData;
  146. }
  147. return *this;
  148. }
  149. };
  150. class ReTwBar
  151. {
  152. // VARIABLES
  153. // Should be private, but seeing as I'm not going to implement all of the
  154. // AntTweakBar public functions right away, I'll expose this so that at
  155. // anytime AntTweakBar functions can be called directly on the bar
  156. public:
  157. TwBar * bar;
  158. std::string name;
  159. protected:
  160. std::vector<ReTwRWItem> rw_items;
  161. std::vector<ReTwCBItem> cb_items;
  162. public:
  163. // Default constructor with explicit initialization
  164. IGL_INLINE ReTwBar();
  165. private:
  166. // Copy constructor does shallow copy
  167. IGL_INLINE ReTwBar(const ReTwBar & that);
  168. // Assignment operator does shallow assignment
  169. IGL_INLINE ReTwBar &operator=(const ReTwBar & that);
  170. // WRAPPERS FOR ANTTWEAKBAR FUNCTIONS
  171. public:
  172. IGL_INLINE void TwNewBar(const char *_name);
  173. IGL_INLINE int TwAddVarRW(
  174. const char *name,
  175. TwType type,
  176. void *var,
  177. const char *def,
  178. const bool record=true);
  179. IGL_INLINE int TwAddVarCB(
  180. const char *name,
  181. TwType type,
  182. TwSetVarCallback setCallback,
  183. TwGetVarCallback getCallback,
  184. void *clientData,
  185. const char *def,
  186. const bool record=true);
  187. // Wrappers for convenience (not recorded, just passed on)
  188. IGL_INLINE int TwAddVarRO(const char *name, TwType type, void *var, const char *def);
  189. IGL_INLINE int TwAddButton(
  190. const char *name,
  191. TwButtonCallback buttonCallback,
  192. void *clientData,
  193. const char *def);
  194. IGL_INLINE int TwSetParam(
  195. const char *varName,
  196. const char *paramName,
  197. TwParamValueType paramValueType,
  198. unsigned int inValueCount,
  199. const void *inValues);
  200. IGL_INLINE int TwGetParam(
  201. const char *varName,
  202. const char *paramName,
  203. TwParamValueType paramValueType,
  204. unsigned int outValueMaxCount,
  205. void *outValues);
  206. IGL_INLINE int TwRefreshBar();
  207. IGL_INLINE int TwTerminate();
  208. // IO FUNCTIONS
  209. public:
  210. // Save current items to file
  211. // Input:
  212. // file_name name of file to save data to, can be null which means print
  213. // to stdout
  214. // Return:
  215. // true only if there were no (fatal) errors
  216. IGL_INLINE bool save(const char *file_name);
  217. std::string get_value_as_string(
  218. void * var,
  219. TwType type);
  220. // Load into current items from file
  221. // Input:
  222. // file_name name of input file to load
  223. // Return:
  224. // true only if there were no (fatal) errors
  225. IGL_INLINE bool load(const char *file_name);
  226. // Get TwType from string
  227. // Input
  228. // type_str string of type
  229. // Output
  230. // type TwType converted from string
  231. // Returns
  232. // true only if string matched a valid type
  233. IGL_INLINE bool type_from_string(const char *type_str, TwType & type);
  234. // I realize that I mix std::string and const char * all over the place.
  235. // What can you do...
  236. IGL_INLINE bool set_value_from_string(
  237. const char * name,
  238. TwType type,
  239. const char * value_str);
  240. IGL_INLINE const std::vector<ReTwRWItem> & get_rw_items();
  241. IGL_INLINE const std::vector<ReTwCBItem> & get_cb_items();
  242. };
  243. }
  244. }
  245. // List of TwBar functions
  246. //TW_API TwBar * TW_CALL TwNewBar(const char *barName);
  247. //TW_API int TW_CALL TwDeleteBar(TwBar *bar);
  248. //TW_API int TW_CALL TwDeleteAllBars();
  249. //TW_API int TW_CALL TwSetTopBar(const TwBar *bar);
  250. //TW_API TwBar * TW_CALL TwGetTopBar();
  251. //TW_API int TW_CALL TwSetBottomBar(const TwBar *bar);
  252. //TW_API TwBar * TW_CALL TwGetBottomBar();
  253. //TW_API const char * TW_CALL TwGetBarName(TwBar *bar);
  254. //TW_API int TW_CALL TwGetBarCount();
  255. //TW_API TwBar * TW_CALL TwGetBarByIndex(int barIndex);
  256. //TW_API TwBar * TW_CALL TwGetBarByName(const char *barName);
  257. //TW_API int TW_CALL TwRefreshBar(TwBar *bar);
  258. //TW_API int TW_CALL TwTerminate();
  259. //
  260. //TW_API int TW_CALL TwAddVarRW(TwBar *bar, const char *name, TwType type, void *var, const char *def);
  261. //TW_API int TW_CALL TwAddVarRO(TwBar *bar, const char *name, TwType type, const void *var, const char *def);
  262. //TW_API int TW_CALL TwAddVarCB(TwBar *bar, const char *name, TwType type, TwSetVarCallback setCallback, TwGetVarCallback getCallback, void *clientData, const char *def);
  263. //TW_API int TW_CALL TwAddButton(TwBar *bar, const char *name, TwButtonCallback callback, void *clientData, const char *def);
  264. //TW_API int TW_CALL TwAddSeparator(TwBar *bar, const char *name, const char *def);
  265. //TW_API int TW_CALL TwRemoveVar(TwBar *bar, const char *name);
  266. //TW_API int TW_CALL TwRemoveAllVars(TwBar *bar);
  267. // Until AntTweakBar dependency folder exists, this is header-only
  268. #ifndef IGL_STATIC_LIBRARY
  269. # include "ReAntTweakBar.cpp"
  270. #endif
  271. #endif