ReAntTweakBar.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. namespace igl
  41. {
  42. TwType ReTwDefineEnum(const char *name, const TwEnumVal *enumValues, unsigned int nbValues);
  43. struct ReTwRWItem
  44. {
  45. //const char * name;
  46. std::string name;
  47. TwType type;
  48. void * var;
  49. ReTwRWItem(
  50. const std::string name,
  51. TwType type,
  52. void *var)
  53. {
  54. this->name = name;
  55. this->type = type;
  56. this->var = var;
  57. }
  58. };
  59. struct ReTwCBItem
  60. {
  61. //const char * name;
  62. std::string name;
  63. TwSetVarCallback setCallback;
  64. TwGetVarCallback getCallback;
  65. void * clientData;
  66. TwType type;
  67. ReTwCBItem(
  68. const std::string name,
  69. TwType type,
  70. TwSetVarCallback setCallback,
  71. TwGetVarCallback getCallback,
  72. void * clientData)
  73. {
  74. this->name = name;
  75. this->type = type;
  76. this->setCallback = setCallback;
  77. this->getCallback = getCallback;
  78. this->clientData = clientData;
  79. }
  80. };
  81. class ReTwBar
  82. {
  83. // VARIABLES
  84. // Should be private, but seeing as I'm not going to implement all of the
  85. // AntTweakBar public functions right away, I'll expose this so that at
  86. // anytime AntTweakBar functions can be called directly on the bar
  87. public:
  88. TwBar * bar;
  89. private:
  90. std::vector<ReTwRWItem> rw_items;
  91. std::vector<ReTwCBItem> cb_items;
  92. // WRAPPERS FOR ANTTWEAKBAR FUNCTIONS
  93. public:
  94. void TwNewBar(const char *barName);
  95. int TwAddVarRW(
  96. const char *name,
  97. TwType type,
  98. void *var,
  99. const char *def,
  100. const bool record=true);
  101. int TwAddVarCB(
  102. const char *name,
  103. TwType type,
  104. TwSetVarCallback setCallback,
  105. TwGetVarCallback getCallback,
  106. void *clientData,
  107. const char *def,
  108. const bool record=true);
  109. // Wrappers for convenience (not recorded, just passed on)
  110. int TwAddVarRO(const char *name, TwType type, void *var, const char *def);
  111. int TwAddButton(
  112. const char *name,
  113. TwButtonCallback buttonCallback,
  114. void *clientData,
  115. const char *def);
  116. int TwSetParam(
  117. const char *varName,
  118. const char *paramName,
  119. TwParamValueType paramValueType,
  120. unsigned int inValueCount,
  121. const void *inValues);
  122. int TwGetParam(
  123. const char *varName,
  124. const char *paramName,
  125. TwParamValueType paramValueType,
  126. unsigned int outValueMaxCount,
  127. void *outValues);
  128. int TwRefreshBar();
  129. int TwTerminate();
  130. // IO FUNCTIONS
  131. public:
  132. // Save current items to file
  133. // Input:
  134. // file_name name of file to save data to, can be null which means print
  135. // to stdout
  136. // Return:
  137. // true only if there were no (fatal) errors
  138. bool save(const char *file_name);
  139. std::string get_value_as_string(
  140. void * var,
  141. TwType type);
  142. // Load into current items from file
  143. // Input:
  144. // file_name name of input file to load
  145. // Return:
  146. // true only if there were no (fatal) errors
  147. bool load(const char *file_name);
  148. // Get TwType from string
  149. // Input
  150. // type_str string of type
  151. // Output
  152. // type TwType converted from string
  153. // Returns
  154. // true only if string matched a valid type
  155. bool type_from_string(const char *type_str, TwType & type);
  156. // I realize that I mix std::string and const char * all over the place.
  157. // What can you do...
  158. bool set_value_from_string(
  159. const char * name,
  160. TwType type,
  161. const char * value_str);
  162. };
  163. };
  164. // List of TwBar functions
  165. //TW_API TwBar * TW_CALL TwNewBar(const char *barName);
  166. //TW_API int TW_CALL TwDeleteBar(TwBar *bar);
  167. //TW_API int TW_CALL TwDeleteAllBars();
  168. //TW_API int TW_CALL TwSetTopBar(const TwBar *bar);
  169. //TW_API TwBar * TW_CALL TwGetTopBar();
  170. //TW_API int TW_CALL TwSetBottomBar(const TwBar *bar);
  171. //TW_API TwBar * TW_CALL TwGetBottomBar();
  172. //TW_API const char * TW_CALL TwGetBarName(TwBar *bar);
  173. //TW_API int TW_CALL TwGetBarCount();
  174. //TW_API TwBar * TW_CALL TwGetBarByIndex(int barIndex);
  175. //TW_API TwBar * TW_CALL TwGetBarByName(const char *barName);
  176. //TW_API int TW_CALL TwRefreshBar(TwBar *bar);
  177. //TW_API int TW_CALL TwTerminate();
  178. //
  179. //TW_API int TW_CALL TwAddVarRW(TwBar *bar, const char *name, TwType type, void *var, const char *def);
  180. //TW_API int TW_CALL TwAddVarRO(TwBar *bar, const char *name, TwType type, const void *var, const char *def);
  181. //TW_API int TW_CALL TwAddVarCB(TwBar *bar, const char *name, TwType type, TwSetVarCallback setCallback, TwGetVarCallback getCallback, void *clientData, const char *def);
  182. //TW_API int TW_CALL TwAddButton(TwBar *bar, const char *name, TwButtonCallback callback, void *clientData, const char *def);
  183. //TW_API int TW_CALL TwAddSeparator(TwBar *bar, const char *name, const char *def);
  184. //TW_API int TW_CALL TwRemoveVar(TwBar *bar, const char *name);
  185. //TW_API int TW_CALL TwRemoveAllVars(TwBar *bar);
  186. #ifdef IGL_HEADER_ONLY
  187. # include "ReAntTweakBar.cpp"
  188. #endif
  189. #endif