ReAntTweakBar.h 7.7 KB

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