ReAntTweakBar.h 6.0 KB

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