1 // ---------------------------------------------------------------------------
4 // @brief Tweak bar manager.
5 // @author Philippe Decaudin - http://www.antisphere.com
6 // @license This file is part of the AntTweakBar library.
7 // For conditions of distribution and use, see License.txt
9 // notes: Private header
12 // ---------------------------------------------------------------------------
15 #if !defined ANT_TW_MGR_INCLUDED
16 #define ANT_TW_MGR_INCLUDED
18 #include <AntTweakBar.h>
19 #define ANT_CALL TW_CALL
24 #include "AntPerfTimer.h"
27 //#define BENCH // uncomment to activate benchmarks
30 # define PERF(cmd) cmd
35 const int NB_ROTO_CURSORS = 12;
38 // ---------------------------------------------------------------------------
39 // API unexposed by AntTweakBar.h
40 // ---------------------------------------------------------------------------
42 // bar states -> use TwDefine instead
46 TW_STATE_ICONIFIED = 2,
48 TW_STATE_UNICONIFIED = 4,
51 /*ANT_TWEAK_BAR_API*/ int ANT_CALL TwSetBarState(TwBar *bar, TwState state);
52 /*ANT_TWEAK_BAR_API*/ //TwState ANT_CALL TwGetBarState(const TwBar *bar);
53 // var states -> use TwDefine instead: visible/iconified implemented only as string commands
54 //ANT_TWEAK_BAR_API int ANT_CALL TwSetVarState(TwBar *bar, const char *name, TwState state);
55 //ANT_TWEAK_BAR_API TwState ANT_CALL TwGetVarState(const TwBar *bar, const char *name);
58 typedef void (ANT_CALL *TwStructExtInitCallback)(void *structExtValue, void *clientData);
59 typedef void (ANT_CALL *TwCopyVarFromExtCallback)(void *structValue, const void *structExtValue, unsigned int structExtMemberIndex, void *clientData);
60 typedef void (ANT_CALL *TwCopyVarToExtCallback)(const void *structValue, void *structExtValue, unsigned int structExtMemberIndex, void *clientData);
61 /*ANT_TWEAK_BAR_API*/ TwType ANT_CALL TwDefineStructExt(const char *name, const TwStructMember *structExtMembers, unsigned int nbExtMembers, size_t structSize, size_t structExtSize, TwStructExtInitCallback structExtInitCallback, TwCopyVarFromExtCallback copyVarFromExtCallback, TwCopyVarToExtCallback copyVarToExtCallback, TwSummaryCallback summaryCallback, void *clientData, const char *help);
62 typedef void (ANT_CALL *TwCustomDrawCallback)(int w, int h, void *structExtValue, void *clientData, TwBar *bar, CTwVarGroup *varGrp);
63 typedef bool (ANT_CALL *TwCustomMouseMotionCallback)(int mouseX, int mouseY, int w, int h, void *structExtValue, void *clientData, TwBar *bar, CTwVarGroup *varGrp);
64 typedef bool (ANT_CALL *TwCustomMouseButtonCallback)(TwMouseButtonID button, bool pressed, int mouseX, int mouseY, int w, int h, void *structExtValue, void *clientData, TwBar *bar, CTwVarGroup *varGrp);
65 typedef void (ANT_CALL *TwCustomMouseLeaveCallback)(void *structExtValue, void *clientData, TwBar *bar);
81 // ---------------------------------------------------------------------------
82 // AntTweakBar Manager
83 // ---------------------------------------------------------------------------
87 ETwGraphAPI m_GraphAPI;
90 class ITwGraph * m_Graph;
93 const CTexFont * m_CurrentFont;
95 std::vector<TwBar*> m_Bars;
96 std::vector<int> m_Order;
98 std::vector<bool> m_MinOccupied;
99 void Minimize(TwBar *_Bar);
100 void Maximize(TwBar *_Bar);
101 void Hide(TwBar *_Bar);
102 void Unhide(TwBar *_Bar);
103 void SetFont(const CTexFont *_Font, bool _ResizeBars);
106 int m_LastMouseWheelPos;
107 int m_IconPos; // 0: bottom-left, 1:bottom-right, 2:top-left, 3:top-right
108 int m_IconAlign; // 0: vertical, 1: horizontal
109 int m_IconMarginX, m_IconMarginY;
110 bool m_FontResizable;
111 std::string m_BarAlwaysOnTop;
112 std::string m_BarAlwaysOnBottom;
113 bool m_UseOldColorScheme;
115 EButtonAlign m_ButtonAlign;
116 bool m_OverlapContent;
121 float m_LastHelpUpdateTime;
122 void UpdateHelpBar();
123 bool m_HelpBarNotUpToDate;
124 bool m_HelpBarUpdateNow;
125 void * m_KeyPressedTextObj;
126 bool m_KeyPressedBuildText;
127 std::string m_KeyPressedStr;
128 float m_KeyPressedTime;
129 void * m_InfoTextObj;
130 bool m_InfoBuildText;
131 int m_BarInitColorHue;
132 int FindBar(const char *_Name) const;
133 int HasAttrib(const char *_Attrib, bool *_HasValue) const;
134 int SetAttrib(int _AttribID, const char *_Value);
135 ERetType GetAttrib(int _AttribID, std::vector<double>& outDouble, std::ostringstream& outString) const;
136 void SetLastError(const char *_StaticErrorMesssage); // _StaticErrorMesssage must be a static string
137 const char * GetLastError(); // returns a static string describing the error, and set LastError to NULL
138 const char * CheckLastError() const; // returns the LastError, but does not set it to NULL
139 void SetCurrentDbgParams(const char *file, int line);
141 //bool IsProcessing() const { return m_Processing);
142 //void SetProcessing(bool processing) { m_Processing = processing; }
144 CTwMgr(ETwGraphAPI _GraphAPI, void *_Device, int _WndID);
153 std::string m_DefString;
160 std::vector<CStructMember> m_Members;
162 TwSummaryCallback m_SummaryCallback;
163 void * m_SummaryClientData;
166 size_t m_ClientStructSize;
167 TwStructExtInitCallback m_StructExtInitCallback;
168 TwCopyVarFromExtCallback m_CopyVarFromExtCallback;
169 TwCopyVarToExtCallback m_CopyVarToExtCallback;
170 void * m_ExtClientData;
171 CStruct() : m_IsExt(false), m_StructExtInitCallback(NULL), m_CopyVarFromExtCallback(NULL), m_CopyVarToExtCallback(NULL), m_ExtClientData(NULL) {}
172 static void ANT_CALL DefaultSummary(char *_SummaryString, size_t _SummaryMaxLength, const void *_Value, void *_ClientData);
173 static void * s_PassProxyAsClientData;
175 std::vector<CStruct> m_Structs;
177 // followings are used for TwAddVarCB( ... StructType ... )
182 bool m_DeleteStructData;
183 void * m_StructExtData;
184 TwSetVarCallback m_StructSetCallback;
185 TwGetVarCallback m_StructGetCallback;
186 void * m_StructClientData;
187 TwCustomDrawCallback m_CustomDrawCallback;
188 TwCustomMouseMotionCallback m_CustomMouseMotionCallback;
189 TwCustomMouseButtonCallback m_CustomMouseButtonCallback;
190 TwCustomMouseLeaveCallback m_CustomMouseLeaveCallback;
191 bool m_CustomCaptureFocus;
192 int m_CustomIndexFirst;
193 int m_CustomIndexLast;
199 CStructProxy * m_StructProxy;
201 struct CTwVar * m_Var;
202 struct CTwVarGroup * m_VarParent;
206 static void ANT_CALL SetCB(const void *_Value, void *_ClientData);
207 static void ANT_CALL GetCB(void *_Value, void *_ClientData);
209 std::list<CStructProxy> m_StructProxies; // elements should not move
210 std::list<CMemberProxy> m_MemberProxies; // elements should not move
211 //void InitVarData(TwType _Type, void *_Data, size_t _Size);
212 //void UninitVarData(TwType _Type, void *_Data, size_t _Size);
217 typedef std::map<unsigned int, std::string> CEntries;
220 std::vector<CEnum> m_Enums;
222 TwType m_TypeColor32;
223 TwType m_TypeColor3F;
224 TwType m_TypeColor4F;
230 std::vector<char> m_CSStringBuffer;
233 std::string * m_ClientStdStringPtr;
234 char m_LocalString[sizeof(std::string)+2*sizeof(void*)]; //+2*sizeof(void*) because of VC++ std::string extra info in Debug
235 TwSetVarCallback m_ClientSetCallback;
236 TwGetVarCallback m_ClientGetCallback;
238 static void ANT_CALL SetCB(const void *_Value, void *_ClientData);
239 static void ANT_CALL GetCB(void *_Value, void *_ClientData);
241 std::list<CCDStdString> m_CDStdStrings;
242 struct CClientStdString // Convertion between VC++ Debug/Release std::string
245 void FromLib(const char *libStr);
246 std::string& ToClient();
248 char m_Data[sizeof(std::string)+2*sizeof(void *)];
249 std::string m_LibStr;
251 struct CLibStdString // Convertion between VC++ Debug/Release std::string
254 void FromClient(const std::string& clientStr);
255 std::string& ToLib();
257 char m_Data[sizeof(std::string)+2*sizeof(void *)];
259 struct CCDStdStringRecord
262 char m_PrevValue[sizeof(std::string)+2*sizeof(void*)];
263 CClientStdString m_ClientStdString;
265 std::vector<CCDStdStringRecord> m_CDStdStringRecords;
266 void UnrollCDStdString(std::vector<CCDStdStringRecord>& _Records, TwType _Type, void *_Data);
267 void RestoreCDStdString(const std::vector<CCDStdStringRecord>& _Records);
268 std::map<void *, std::vector<char> > m_CDStdStringCopyBuffers;
270 struct CCustom // custom var type
272 virtual ~CCustom() = 0;
274 std::vector<CCustom *> m_Customs;
277 double m_LastMousePressedTime;
278 TwMouseButtonID m_LastMousePressedButtonID;
279 int m_LastMousePressedPosition[2];
280 double m_RepeatMousePressedDelay;
281 double m_RepeatMousePressedPeriod;
282 bool m_CanRepeatMousePressed;
283 bool m_IsRepeatingMousePressed;
284 double m_LastDrawTime;
286 #if defined(ANT_WINDOWS)
287 typedef HCURSOR CCursor;
288 CCursor PixmapCursor(int _CurIdx);
289 #elif defined(ANT_UNIX)
290 typedef Cursor CCursor;
291 CCursor PixmapCursor(int _CurIdx);
292 Display * m_CurrentXDisplay;
293 Window m_CurrentXWindow;
294 #elif defined(ANT_OSX)
295 typedef NSCursor * CCursor;
296 CCursor PixmapCursor(int _CurIdx);
297 #endif // defined(ANT_UNIX)
298 bool m_CursorsCreated;
299 void CreateCursors();
301 void SetCursor(CCursor _Cursor);
302 CCursor m_CursorArrow;
303 CCursor m_CursorMove;
306 CCursor m_CursorTopLeft;
307 CCursor m_CursorTopRight;
308 CCursor m_CursorBottomLeft;
309 CCursor m_CursorBottomRight;
310 CCursor m_CursorHelp;
311 CCursor m_CursorHand;
312 CCursor m_CursorCross;
313 CCursor m_CursorUpArrow;
315 CCursor m_CursorIBeam;
316 CCursor m_RotoCursors[NB_ROTO_CURSORS];
317 CCursor m_CursorCenter;
318 CCursor m_CursorPoint;
320 TwCopyCDStringToClient m_CopyCDStringToClient;
321 TwCopyStdStringToClient m_CopyStdStringToClient;
322 size_t m_ClientStdStringStructSize;
323 TwType m_ClientStdStringBaseType;
326 int m_NbMinimizedBars;
327 const char * m_LastError;
328 const char * m_CurrentDbgFile;
329 int m_CurrentDbgLine;
333 extern CTwMgr *g_TwMgr;
336 // ---------------------------------------------------------------------------
337 // Extra functions and TwTypes
338 // ---------------------------------------------------------------------------
341 bool TwGetKeyCode(int *_Code, int *_Modif, const char *_String);
342 bool TwGetKeyString(std::string *_String, int _Code, int _Modif);
344 const TwType TW_TYPE_SHORTCUT = TwType(0xfff1);
345 const TwType TW_TYPE_HELP_GRP = TwType(0xfff2);
346 const TwType TW_TYPE_HELP_ATOM = TwType(0xfff3);
347 const TwType TW_TYPE_HELP_HEADER = TwType(0xfff4);
348 const TwType TW_TYPE_HELP_STRUCT = TwType(0xfff5);
349 const TwType TW_TYPE_BUTTON = TwType(0xfff6);
350 const TwType TW_TYPE_CDSTDSTRING = TwType(0xfff7);
351 const TwType TW_TYPE_STRUCT_BASE = TwType(0x10000000);
352 const TwType TW_TYPE_ENUM_BASE = TwType(0x20000000);
353 const TwType TW_TYPE_CSSTRING_BASE = TW_TYPE_CSSTRING(0); // defined as 0x30000000 (see AntTweakBar.h)
354 const TwType TW_TYPE_CSSTRING_MAX = TW_TYPE_CSSTRING(0xfffffff);
355 #define TW_CSSTRING_SIZE(type) ((int)((type)&0xfffffff))
356 const TwType TW_TYPE_CUSTOM_BASE = TwType(0x40000000);
357 const TwType TW_TYPE_STDSTRING_VS2008 = TwType(0x2fff0000);
358 const TwType TW_TYPE_STDSTRING_VS2010 = TwType(0x2ffe0000);
360 extern "C" int ANT_CALL TwSetLastError(const char *_StaticErrorMessage);
362 //const TwGraphAPI TW_OPENGL_CORE = (TwGraphAPI)5; // WIP (note: OpenGL Core Profil requires OpenGL 3.2 or later)
368 CRect() : X(0), Y(0), W(0), H(0) {}
369 CRect(int _X, int _Y, int _W, int _H) : X(_X), Y(_Y), W(_W), H(_H) {}
370 bool operator==(const CRect& _Rect) { return (Empty() && _Rect.Empty()) || (X==_Rect.X && Y==_Rect.Y && W==_Rect.W && H==_Rect.H); }
371 bool Empty(int _Margin=0) const { return (W<=_Margin || H<=_Margin); }
372 bool Subtract(const CRect& _Rect, std::vector<CRect>& _OutRects) const;
373 bool Subtract(const std::vector<CRect>& _Rects, std::vector<CRect>& _OutRects) const;
377 // ---------------------------------------------------------------------------
378 // Global bar attribs
379 // ---------------------------------------------------------------------------
398 // ---------------------------------------------------------------------------
400 // ---------------------------------------------------------------------------
408 bool m_HLS, m_HasAlpha, m_OGL;
411 unsigned int m_PrevConvertedColor;
412 CTwMgr::CStructProxy*m_StructProxy;
415 static void ANT_CALL InitColor32CB(void *_ExtValue, void *_ClientData);
416 static void ANT_CALL InitColor3FCB(void *_ExtValue, void *_ClientData);
417 static void ANT_CALL InitColor4FCB(void *_ExtValue, void *_ClientData);
418 static void ANT_CALL CopyVarFromExtCB(void *_VarValue, const void *_ExtValue, unsigned int _ExtMemberIndex, void *_ClientData);
419 static void ANT_CALL CopyVarToExtCB(const void *_VarValue, void *_ExtValue, unsigned int _ExtMemberIndex, void *_ClientData);
420 static void ANT_CALL SummaryCB(char *_SummaryString, size_t _SummaryMaxLength, const void *_ExtValue, void *_ClientData);
421 static void CreateTypes();
425 // ---------------------------------------------------------------------------
426 // Quaternion struct ext
427 // ---------------------------------------------------------------------------
430 struct CQuaternionExt
432 double Qx, Qy, Qz, Qs; // Quat value
433 double Vx, Vy, Vz, Angle; // Not used
434 double Dx, Dy, Dz; // Dir value set when used as a direction
435 bool m_AAMode; // Axis & angle mode -> disabled
436 bool m_ShowVal; // Display values
437 bool m_IsFloat; // Quat/Dir uses floats
438 bool m_IsDir; // Mapped to a dir vector instead of a quat
439 double m_Dir[3]; // If not zero, display one direction vector
440 color32 m_DirColor; // Direction vector color
441 float m_Permute[3][3]; // Permute frame axis
442 CTwMgr::CStructProxy*m_StructProxy;
443 static void ANT_CALL InitQuat4FCB(void *_ExtValue, void *_ClientData);
444 static void ANT_CALL InitQuat4DCB(void *_ExtValue, void *_ClientData);
445 static void ANT_CALL InitDir3FCB(void *_ExtValue, void *_ClientData);
446 static void ANT_CALL InitDir3DCB(void *_ExtValue, void *_ClientData);
447 static void ANT_CALL CopyVarFromExtCB(void *_VarValue, const void *_ExtValue, unsigned int _ExtMemberIndex, void *_ClientData);
448 static void ANT_CALL CopyVarToExtCB(const void *_VarValue, void *_ExtValue, unsigned int _ExtMemberIndex, void *_ClientData);
449 static void ANT_CALL SummaryCB(char *_SummaryString, size_t _SummaryMaxLength, const void *_ExtValue, void *_ClientData);
450 static void ANT_CALL DrawCB(int _W, int _H, void *_ExtValue, void *_ClientData, TwBar *_Bar, CTwVarGroup *varGrp);
451 static bool ANT_CALL MouseMotionCB(int _MouseX, int _MouseY, int _W, int _H, void *_StructExtValue, void *_ClientData, TwBar *_Bar, CTwVarGroup *varGrp);
452 static bool ANT_CALL MouseButtonCB(TwMouseButtonID _Button, bool _Pressed, int _MouseX, int _MouseY, int _W, int _H, void *_StructExtValue, void *_ClientData, TwBar *_Bar, CTwVarGroup *varGrp);
453 static void ANT_CALL MouseLeaveCB(void *_StructExtValue, void *_ClientData, TwBar *_Bar);
454 static void CreateTypes();
455 static TwType s_CustomType;
456 void ConvertToAxisAngle();
457 void ConvertFromAxisAngle();
459 static std::vector<float> s_SphTri;
460 static std::vector<color32> s_SphCol;
461 static std::vector<int> s_SphTriProj;
462 static std::vector<color32> s_SphColLight;
463 static std::vector<float> s_ArrowTri[4];
464 static std::vector<int> s_ArrowTriProj[4];
465 static std::vector<float> s_ArrowNorm[4];
466 static std::vector<color32> s_ArrowColLight[4];
467 enum EArrowParts { ARROW_CONE, ARROW_CONE_CAP, ARROW_CYL, ARROW_CYL_CAP };
468 static void CreateSphere();
469 static void CreateArrow();
470 static void ApplyQuat(float *outX, float *outY, float *outZ, float x, float y, float z, float qx, float qy, float qz, float qs);
471 static void QuatFromDir(double *outQx, double *outQy, double *outQz, double *outQs, double dx, double dy, double dz);
472 inline void Permute(float *outX, float *outY, float *outZ, float x, float y, float z);
473 inline void PermuteInv(float *outX, float *outY, float *outZ, float x, float y, float z);
474 inline void Permute(double *outX, double *outY, double *outZ, double x, double y, double z);
475 inline void PermuteInv(double *outX, double *outY, double *outZ, double x, double y, double z);
478 double m_OrigQuat[4];
479 float m_OrigX, m_OrigY;
480 double m_PrevX, m_PrevY;
484 // ---------------------------------------------------------------------------
485 // CTwFPU objects set and restore the fpu precision if needed.
486 // (could be useful because DirectX changes it and AntTweakBar requires default double precision)
487 // ---------------------------------------------------------------------------
495 state0 = _controlfp(0, 0);
496 if( (state0&MCW_PC)==_PC_24 ) // we need at least _PC_53
497 _controlfp(_PC_53, MCW_PC);
505 if( (state0&MCW_PC)==_PC_24 )
506 _controlfp(_PC_24, MCW_PC);
515 // ---------------------------------------------------------------------------
518 #endif // !defined ANT_TW_MGR_INCLUDED