ReAntTweakBar.h 7.8 KB

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