ReAntTweakBar.h 7.7 KB

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