ReAntTweakBar.h 5.9 KB

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