ReAntTweakBar.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #ifndef IGL_REANTTWEAKBAR_H
  2. #define IGL_REANTTWEAKBAR_H
  3. #include "igl_inline.h"
  4. // ReAntTweakBar is a minimal wrapper for the AntTweakBar library that allows
  5. // "bars" to be saved and load from disk. Changing your existing app that uses
  6. // AntTweakBar to use ReAntTweakBar is trivial.
  7. //
  8. // Many (but not all) variable types are supported. I'll try to keep track them
  9. // here:
  10. // TW_TYPE_BOOLCPP
  11. // TW_TYPE_QUAT4F
  12. // TW_TYPE_QUAT4D
  13. // TW_TYPE_COLOR4F
  14. // TW_TYPE_COLOR4D
  15. // TW_TYPE_COLOR3F
  16. // TW_TYPE_DIR3F
  17. // TW_TYPE_DIR3D
  18. // TW_TYPE_BOOL32
  19. // TW_TYPE_INT32
  20. // TW_TYPE_FLOAT
  21. // TW_TYPE_DOUBLE
  22. // TW_TYPE_UINT8
  23. // and
  24. // custom TwTypes made with TwDefineEnum
  25. //
  26. // I'm working on adding the rest on an as-needed basis. Adding a new type only
  27. // requires changes in a few places...
  28. //
  29. //
  30. // Copyright Alec Jacobson, 2011
  31. //
  32. // Known bugs:
  33. // TwDefineEnumFromString is not supported (use ReTwDefineEnum instead)
  34. // Custom enums should not have spaces in their names
  35. // This allows the user to have a non-global, static installation of
  36. // AntTweakBar
  37. #ifdef STATIC_ANTTWEAKBAR
  38. # include "AntTweakBar.h"
  39. #else
  40. # include <AntTweakBar.h>
  41. #endif
  42. #include <vector>
  43. #include <string>
  44. #include <cassert>
  45. namespace igl
  46. {
  47. TwType ReTwDefineEnum(const char *name, const TwEnumVal *enumValues, unsigned int nbValues);
  48. struct ReTwRWItem
  49. {
  50. //const char * name;
  51. std::string name;
  52. TwType type;
  53. void * var;
  54. // Default constructor
  55. ReTwRWItem(
  56. const std::string _name,
  57. TwType _type,
  58. void *_var):
  59. name(_name),
  60. type(_type),
  61. var(_var)
  62. {
  63. }
  64. // Shallow copy constructor
  65. ReTwRWItem(const ReTwRWItem & that):
  66. name(that.name),
  67. type(that.type),
  68. var(that.var)
  69. {
  70. // I don't think you should be using this!
  71. assert(false);
  72. }
  73. // Shallow assignment
  74. ReTwRWItem & operator=(const ReTwRWItem & that)
  75. {
  76. // I don't think you should be using this!
  77. assert(false);
  78. if(this != &that)
  79. {
  80. this->name = that.name;
  81. this->type = that.type;
  82. this->var = that.var;
  83. }
  84. return *this;
  85. }
  86. };
  87. struct ReTwCBItem
  88. {
  89. //const char * name;
  90. std::string name;
  91. TwType type;
  92. TwSetVarCallback setCallback;
  93. TwGetVarCallback getCallback;
  94. void * clientData;
  95. // Default constructor
  96. ReTwCBItem(
  97. const std::string _name,
  98. TwType _type,
  99. TwSetVarCallback _setCallback,
  100. TwGetVarCallback _getCallback,
  101. void * _clientData):
  102. name(_name),
  103. type(_type),
  104. setCallback(_setCallback),
  105. getCallback(_getCallback),
  106. clientData(_clientData)
  107. {
  108. }
  109. // Shallow copy
  110. ReTwCBItem(const ReTwCBItem & that):
  111. name(that.name),
  112. type(that.type),
  113. setCallback(that.setCallback),
  114. getCallback(that.getCallback),
  115. clientData(that.clientData)
  116. {
  117. // I don't think you should be using this!
  118. assert(false);
  119. }
  120. // Shallow assignment
  121. ReTwCBItem & operator=(const ReTwCBItem & that)
  122. {
  123. // I don't think you should be using this!
  124. assert(false);
  125. if(this != &that)
  126. {
  127. name = that.name;
  128. type = that.type;
  129. setCallback = that.setCallback;
  130. getCallback = that.getCallback;
  131. clientData = that.clientData;
  132. }
  133. return *this;
  134. }
  135. };
  136. class ReTwBar
  137. {
  138. // VARIABLES
  139. // Should be private, but seeing as I'm not going to implement all of the
  140. // AntTweakBar public functions right away, I'll expose this so that at
  141. // anytime AntTweakBar functions can be called directly on the bar
  142. public:
  143. TwBar * bar;
  144. protected:
  145. std::vector<ReTwRWItem> rw_items;
  146. std::vector<ReTwCBItem> cb_items;
  147. public:
  148. // Default constructor with explicit initialization
  149. ReTwBar();
  150. private:
  151. // Copy constructor does shallow copy
  152. ReTwBar(const ReTwBar & that);
  153. // Assignment operator does shallow assignment
  154. ReTwBar &operator=(const ReTwBar & that);
  155. // WRAPPERS FOR ANTTWEAKBAR FUNCTIONS
  156. public:
  157. void TwNewBar(const char *barName);
  158. int TwAddVarRW(
  159. const char *name,
  160. TwType type,
  161. void *var,
  162. const char *def,
  163. const bool record=true);
  164. int TwAddVarCB(
  165. const char *name,
  166. TwType type,
  167. TwSetVarCallback setCallback,
  168. TwGetVarCallback getCallback,
  169. void *clientData,
  170. const char *def,
  171. const bool record=true);
  172. // Wrappers for convenience (not recorded, just passed on)
  173. int TwAddVarRO(const char *name, TwType type, void *var, const char *def);
  174. int TwAddButton(
  175. const char *name,
  176. TwButtonCallback buttonCallback,
  177. void *clientData,
  178. const char *def);
  179. int TwSetParam(
  180. const char *varName,
  181. const char *paramName,
  182. TwParamValueType paramValueType,
  183. unsigned int inValueCount,
  184. const void *inValues);
  185. int TwGetParam(
  186. const char *varName,
  187. const char *paramName,
  188. TwParamValueType paramValueType,
  189. unsigned int outValueMaxCount,
  190. void *outValues);
  191. int TwRefreshBar();
  192. int TwTerminate();
  193. // IO FUNCTIONS
  194. public:
  195. // Save current items to file
  196. // Input:
  197. // file_name name of file to save data to, can be null which means print
  198. // to stdout
  199. // Return:
  200. // true only if there were no (fatal) errors
  201. bool save(const char *file_name);
  202. std::string get_value_as_string(
  203. void * var,
  204. TwType type);
  205. // Load into current items from file
  206. // Input:
  207. // file_name name of input file to load
  208. // Return:
  209. // true only if there were no (fatal) errors
  210. bool load(const char *file_name);
  211. // Get TwType from string
  212. // Input
  213. // type_str string of type
  214. // Output
  215. // type TwType converted from string
  216. // Returns
  217. // true only if string matched a valid type
  218. bool type_from_string(const char *type_str, TwType & type);
  219. // I realize that I mix std::string and const char * all over the place.
  220. // What can you do...
  221. bool set_value_from_string(
  222. const char * name,
  223. TwType type,
  224. const char * value_str);
  225. };
  226. };
  227. // List of TwBar functions
  228. //TW_API TwBar * TW_CALL TwNewBar(const char *barName);
  229. //TW_API int TW_CALL TwDeleteBar(TwBar *bar);
  230. //TW_API int TW_CALL TwDeleteAllBars();
  231. //TW_API int TW_CALL TwSetTopBar(const TwBar *bar);
  232. //TW_API TwBar * TW_CALL TwGetTopBar();
  233. //TW_API int TW_CALL TwSetBottomBar(const TwBar *bar);
  234. //TW_API TwBar * TW_CALL TwGetBottomBar();
  235. //TW_API const char * TW_CALL TwGetBarName(TwBar *bar);
  236. //TW_API int TW_CALL TwGetBarCount();
  237. //TW_API TwBar * TW_CALL TwGetBarByIndex(int barIndex);
  238. //TW_API TwBar * TW_CALL TwGetBarByName(const char *barName);
  239. //TW_API int TW_CALL TwRefreshBar(TwBar *bar);
  240. //TW_API int TW_CALL TwTerminate();
  241. //
  242. //TW_API int TW_CALL TwAddVarRW(TwBar *bar, const char *name, TwType type, void *var, const char *def);
  243. //TW_API int TW_CALL TwAddVarRO(TwBar *bar, const char *name, TwType type, const void *var, const char *def);
  244. //TW_API int TW_CALL TwAddVarCB(TwBar *bar, const char *name, TwType type, TwSetVarCallback setCallback, TwGetVarCallback getCallback, void *clientData, const char *def);
  245. //TW_API int TW_CALL TwAddButton(TwBar *bar, const char *name, TwButtonCallback callback, void *clientData, const char *def);
  246. //TW_API int TW_CALL TwAddSeparator(TwBar *bar, const char *name, const char *def);
  247. //TW_API int TW_CALL TwRemoveVar(TwBar *bar, const char *name);
  248. //TW_API int TW_CALL TwRemoveAllVars(TwBar *bar);
  249. #ifdef IGL_HEADER_ONLY
  250. # include "ReAntTweakBar.cpp"
  251. #endif
  252. #endif