ReAntTweakBar.h 8.0 KB

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