ReAntTweakBar.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. std::string name;
  147. protected:
  148. std::vector<ReTwRWItem> rw_items;
  149. std::vector<ReTwCBItem> cb_items;
  150. public:
  151. // Default constructor with explicit initialization
  152. ReTwBar();
  153. private:
  154. // Copy constructor does shallow copy
  155. ReTwBar(const ReTwBar & that);
  156. // Assignment operator does shallow assignment
  157. ReTwBar &operator=(const ReTwBar & that);
  158. // WRAPPERS FOR ANTTWEAKBAR FUNCTIONS
  159. public:
  160. void TwNewBar(const char *barName);
  161. int TwAddVarRW(
  162. const char *name,
  163. TwType type,
  164. void *var,
  165. const char *def,
  166. const bool record=true);
  167. int TwAddVarCB(
  168. const char *name,
  169. TwType type,
  170. TwSetVarCallback setCallback,
  171. TwGetVarCallback getCallback,
  172. void *clientData,
  173. const char *def,
  174. const bool record=true);
  175. // Wrappers for convenience (not recorded, just passed on)
  176. int TwAddVarRO(const char *name, TwType type, void *var, const char *def);
  177. int TwAddButton(
  178. const char *name,
  179. TwButtonCallback buttonCallback,
  180. void *clientData,
  181. const char *def);
  182. int TwSetParam(
  183. const char *varName,
  184. const char *paramName,
  185. TwParamValueType paramValueType,
  186. unsigned int inValueCount,
  187. const void *inValues);
  188. int TwGetParam(
  189. const char *varName,
  190. const char *paramName,
  191. TwParamValueType paramValueType,
  192. unsigned int outValueMaxCount,
  193. void *outValues);
  194. int TwRefreshBar();
  195. int TwTerminate();
  196. // IO FUNCTIONS
  197. public:
  198. // Save current items to file
  199. // Input:
  200. // file_name name of file to save data to, can be null which means print
  201. // to stdout
  202. // Return:
  203. // true only if there were no (fatal) errors
  204. bool save(const char *file_name);
  205. std::string get_value_as_string(
  206. void * var,
  207. TwType type);
  208. // Load into current items from file
  209. // Input:
  210. // file_name name of input file to load
  211. // Return:
  212. // true only if there were no (fatal) errors
  213. bool load(const char *file_name);
  214. // Get TwType from string
  215. // Input
  216. // type_str string of type
  217. // Output
  218. // type TwType converted from string
  219. // Returns
  220. // true only if string matched a valid type
  221. bool type_from_string(const char *type_str, TwType & type);
  222. // I realize that I mix std::string and const char * all over the place.
  223. // What can you do...
  224. bool set_value_from_string(
  225. const char * name,
  226. TwType type,
  227. const char * value_str);
  228. const std::vector<ReTwRWItem> & get_rw_items();
  229. const std::vector<ReTwCBItem> & get_cb_items();
  230. };
  231. };
  232. // List of TwBar functions
  233. //TW_API TwBar * TW_CALL TwNewBar(const char *barName);
  234. //TW_API int TW_CALL TwDeleteBar(TwBar *bar);
  235. //TW_API int TW_CALL TwDeleteAllBars();
  236. //TW_API int TW_CALL TwSetTopBar(const TwBar *bar);
  237. //TW_API TwBar * TW_CALL TwGetTopBar();
  238. //TW_API int TW_CALL TwSetBottomBar(const TwBar *bar);
  239. //TW_API TwBar * TW_CALL TwGetBottomBar();
  240. //TW_API const char * TW_CALL TwGetBarName(TwBar *bar);
  241. //TW_API int TW_CALL TwGetBarCount();
  242. //TW_API TwBar * TW_CALL TwGetBarByIndex(int barIndex);
  243. //TW_API TwBar * TW_CALL TwGetBarByName(const char *barName);
  244. //TW_API int TW_CALL TwRefreshBar(TwBar *bar);
  245. //TW_API int TW_CALL TwTerminate();
  246. //
  247. //TW_API int TW_CALL TwAddVarRW(TwBar *bar, const char *name, TwType type, void *var, const char *def);
  248. //TW_API int TW_CALL TwAddVarRO(TwBar *bar, const char *name, TwType type, const void *var, const char *def);
  249. //TW_API int TW_CALL TwAddVarCB(TwBar *bar, const char *name, TwType type, TwSetVarCallback setCallback, TwGetVarCallback getCallback, void *clientData, const char *def);
  250. //TW_API int TW_CALL TwAddButton(TwBar *bar, const char *name, TwButtonCallback callback, void *clientData, const char *def);
  251. //TW_API int TW_CALL TwAddSeparator(TwBar *bar, const char *name, const char *def);
  252. //TW_API int TW_CALL TwRemoveVar(TwBar *bar, const char *name);
  253. //TW_API int TW_CALL TwRemoveAllVars(TwBar *bar);
  254. #ifdef IGL_HEADER_ONLY
  255. # include "ReAntTweakBar.cpp"
  256. #endif
  257. #endif