1 //  ---------------------------------------------------------------------------
2 //
3 //  @file       TwBar.h
4 //  @brief      Tweak bar and var classes.
5 //  @author     Philippe Decaudin
6 //  @license    This file is part of the AntTweakBar library.
7 //              For conditions of distribution and use, see License.txt
8 //
9 //  note:       Private header
10 //
11 //  ---------------------------------------------------------------------------
12 
13 
14 #if !defined ANT_TW_BAR_INCLUDED
15 #define ANT_TW_BAR_INCLUDED
16 
17 #include <AntTweakBar.h>
18 #include "TwColors.h"
19 
20 #define ANT_TWEAK_BAR_DLL "AntTweakBar"
21 
22 
23 //  ---------------------------------------------------------------------------
24 
25 bool IsCustomType(int _Type);
26 
27 struct CTwVar
28 {
29     std::string             m_Name;
30     std::string             m_Label;
31     std::string             m_Help;
32     bool                    m_IsRoot;
33     bool                    m_DontClip;
34     bool                    m_Visible;
35     signed short            m_LeftMargin;
36     signed short            m_TopMargin;
37     const color32 *         m_ColorPtr;
38     const color32 *         m_BgColorPtr;
39 
40     virtual bool            IsGroup() const = 0;
IsCustomCTwVar41     virtual bool            IsCustom() const { return false; }
42     virtual const CTwVar *  Find(const char *_Name, struct CTwVarGroup **_Parent, int *_Index) const = 0;
43     virtual int             HasAttrib(const char *_Attrib, bool *_HasValue) const;
44     virtual int             SetAttrib(int _AttribID, const char *_Value, TwBar *_Bar, struct CTwVarGroup *_VarParent, int _VarIndex);
45     virtual ERetType        GetAttrib(int _AttribID, TwBar *_Bar, struct CTwVarGroup *_VarParent, int _VarIndex, std::vector<double>& outDouble, std::ostringstream& outString) const;
46     virtual void            SetReadOnly(bool _ReadOnly) = 0;
47     virtual bool            IsReadOnly() const = 0;
48                             CTwVar();
~CTwVarCTwVar49     virtual                 ~CTwVar() {}
50 
51     static size_t           GetDataSize(TwType _Type);
52 };
53 
54 
55 struct CTwVarAtom : CTwVar
56 {
57     ETwType                 m_Type;
58     void *                  m_Ptr;
59     TwSetVarCallback        m_SetCallback;
60     TwGetVarCallback        m_GetCallback;
61     void *                  m_ClientData;
62     bool                    m_ReadOnly;
63     bool                    m_NoSlider;
64     int                     m_KeyIncr[2];   // [0]=key_code [1]=modifiers
65     int                     m_KeyDecr[2];   // [0]=key_code [1]=modifiers
66 
67     template <typename _T>  struct TVal
68     {
69         _T                  m_Min;
70         _T                  m_Max;
71         _T                  m_Step;
72         signed char         m_Precision;
73         bool                m_Hexa;
74     };
75     union UVal
76     {
77         TVal<unsigned char> m_Char;
78         TVal<signed char>   m_Int8;
79         TVal<unsigned char> m_UInt8;
80         TVal<signed short>  m_Int16;
81         TVal<unsigned short>m_UInt16;
82         TVal<signed int>    m_Int32;
83         TVal<unsigned int>  m_UInt32;
84         TVal<float>         m_Float32;
85         TVal<double>        m_Float64;
86         struct CBoolVal
87         {
88             char *          m_TrueString;
89             char *          m_FalseString;
90             bool            m_FreeTrueString;
91             bool            m_FreeFalseString;
92         }                   m_Bool;
93         struct CEnumVal     // empty -> enum entries are deduced from m_Type
94         {
95             //typedef std::map<unsigned int, std::string> CEntries;
96             //CEntries *    m_Entries;
97         }                   m_Enum;
98         struct CShortcutVal
99         {
100             int             m_Incr[2];
101             int             m_Decr[2];
102         }                   m_Shortcut;
103         struct CHelpStruct
104         {
105             int             m_StructType;
106         }                   m_HelpStruct;
107         struct CButtonVal
108         {
109             TwButtonCallback m_Callback;
110             int             m_Separator;
111         }                   m_Button;
112         struct CCustomVal
113         {
114             CTwMgr::CMemberProxy *m_MemberProxy;
115         }                   m_Custom;
116     };
117     UVal                    m_Val;
118 
IsGroupCTwVarAtom119     virtual bool            IsGroup() const { return false; }
IsCustomCTwVarAtom120     virtual bool            IsCustom() const { return IsCustomType(m_Type); }
121     virtual void            ValueToString(std::string *_Str) const;
122     virtual double          ValueToDouble() const;
123     virtual void            ValueFromDouble(double _Val);
124     virtual void            MinMaxStepToDouble(double *_Min, double *_Max, double *_Step) const;
125     virtual const CTwVar *  Find(const char *_Name, struct CTwVarGroup **_Parent, int *_Index) const;
126     virtual int             HasAttrib(const char *_Attrib, bool *_HasValue) const;
127     virtual int             SetAttrib(int _AttribID, const char *_Value, TwBar *_Bar, struct CTwVarGroup *_VarParent, int _VarIndex);
128     virtual ERetType        GetAttrib(int _AttribID, TwBar *_Bar, struct CTwVarGroup *_VarParent, int _VarIndex, std::vector<double>& outDouble, std::ostringstream& outString) const;
129     virtual void            Increment(int _Step);
130     virtual void            SetDefaults();
SetReadOnlyCTwVarAtom131     virtual void            SetReadOnly(bool _ReadOnly) { m_ReadOnly=_ReadOnly; if( m_Type!=TW_TYPE_BUTTON && m_SetCallback==NULL && m_Ptr==NULL ) m_ReadOnly=true; }
IsReadOnlyCTwVarAtom132     virtual bool            IsReadOnly() const { if( m_Type!=TW_TYPE_BUTTON && m_SetCallback==NULL && m_Ptr==NULL ) return true; else return m_ReadOnly; }
133     //virtual int           DefineEnum(const TwEnumVal *_EnumValues, unsigned int _NbValues);
134                             CTwVarAtom();
135     virtual                 ~CTwVarAtom();
136 };
137 
138 
139 struct CTwVarGroup : CTwVar
140 {
141     std::vector<CTwVar *>   m_Vars;
142     bool                    m_Open;
143     TwSummaryCallback       m_SummaryCallback;
144     void *                  m_SummaryClientData;
145     void *                  m_StructValuePtr;
146     TwType                  m_StructType;
147 
IsGroupCTwVarGroup148     virtual bool            IsGroup() const { return true; }
149     virtual const CTwVar *  Find(const char *_Name, CTwVarGroup **_Parent, int *_Index) const;
150     virtual int             HasAttrib(const char *_Attrib, bool *_HasValue) const;
151     virtual int             SetAttrib(int _AttribID, const char *_Value, TwBar *_Bar, struct CTwVarGroup *_VarParent, int _VarIndex);
152     virtual ERetType        GetAttrib(int _AttribID, TwBar *_Bar, struct CTwVarGroup *_VarParent, int _VarIndex, std::vector<double>& outDouble, std::ostringstream& outString) const;
153     virtual CTwVarAtom *    FindShortcut(int _Key, int _Modifiers, bool *_DoIncr);
SetReadOnlyCTwVarGroup154     virtual void            SetReadOnly(bool _ReadOnly) { for(size_t i=0; i<m_Vars.size(); ++i) if(m_Vars[i]) m_Vars[i]->SetReadOnly(_ReadOnly); }
IsReadOnlyCTwVarGroup155     virtual bool            IsReadOnly() const { for(size_t i=0; i<m_Vars.size(); ++i) if(m_Vars[i] && !m_Vars[i]->IsReadOnly()) return false; return true; }
CTwVarGroupCTwVarGroup156                             CTwVarGroup()   { m_Open=false; m_StructType=TW_TYPE_UNDEF; m_SummaryCallback=NULL; m_SummaryClientData=NULL; m_StructValuePtr=NULL; }
157     virtual                 ~CTwVarGroup();
158 };
159 
160 //  ---------------------------------------------------------------------------
161 
162 struct CTwBar
163 {
164     std::string             m_Name;
165     std::string             m_Label;
166     std::string             m_Help;
167     bool                    m_Visible;
168     int                     m_PosX;
169     int                     m_PosY;
170     int                     m_Width;
171     int                     m_Height;
172     color32                 m_Color;
173     bool                    m_DarkText;
174     const CTexFont *        m_Font;
175     int                     m_ValuesWidth;
176     int                     m_Sep;
177     int                     m_LineSep;
178     int                     m_FirstLine;
179     float                   m_UpdatePeriod;
180     bool                    m_IsHelpBar;
181     int                     m_MinNumber;    // accessed by TwDeleteBar
182     bool                    m_IsPopupList;
183     CTwVarAtom *            m_VarEnumLinkedToPopupList;
184     CTwBar *                m_BarLinkedToPopupList;
185     bool                    m_Resizable;
186     bool                    m_Movable;
187     bool                    m_Iconifiable;
188     bool                    m_Contained;
189 
190     CTwVarGroup             m_VarRoot;
191 
192     enum EDrawPart          { DRAW_BG=(1<<0), DRAW_CONTENT=(1<<1), DRAW_ALL=DRAW_BG|DRAW_CONTENT };
193     void                    Draw(int _DrawPart=DRAW_ALL);
194     void                    NotUpToDate();
195     const CTwVar *          Find(const char *_Name, CTwVarGroup **_Parent=NULL, int *_Index=NULL) const;
196     CTwVar *                Find(const char *_Name, CTwVarGroup **_Parent=NULL, int *_Index=NULL);
197     int                     HasAttrib(const char *_Attrib, bool *_HasValue) const;
198     int                     SetAttrib(int _AttribID, const char *_Value);
199     ERetType                GetAttrib(int _AttribID, std::vector<double>& outDouble, std::ostringstream& outString) const;
200     bool                    MouseMotion(int _X, int _Y);
201     bool                    MouseButton(ETwMouseButtonID _Button, bool _Pressed, int _X, int _Y);
202     bool                    MouseWheel(int _Pos, int _PrevPos, int _MouseX, int _MouseY);
203     bool                    KeyPressed(int _Key, int _Modifiers);
204     bool                    KeyTest(int _Key, int _Modifiers);
IsMinimizedCTwBar205     bool                    IsMinimized() const { return m_IsMinimized; }
IsDraggingCTwBar206     bool                    IsDragging() const  { return m_MouseDrag; }
207     bool                    Show(CTwVar *_Var); // display the line associated to _Var
208     bool                    OpenHier(CTwVarGroup *_Root, CTwVar *_Var); // open a hierarchy if it contains _Var
209     int                     LineInHier(CTwVarGroup *_Root, CTwVar *_Var); // returns the number of the line associated to _Var
UnHighlightLineCTwBar210     void                    UnHighlightLine() { m_HighlightedLine = -1; NotUpToDate(); } // used by PopupCallback
HaveFocusCTwBar211     void                    HaveFocus(bool _Focus) { m_DrawHandles = _Focus; }           // used by PopupCallback
StopEditInPlaceCTwBar212     void                    StopEditInPlace() { if( m_EditInPlace.m_Active ) EditInPlaceEnd(false); }
213                             CTwBar(const char *_Name);
214                             ~CTwBar();
215 
216     color32                 m_ColBg, m_ColBg1, m_ColBg2;
217     color32                 m_ColHighBg0;
218     color32                 m_ColHighBg1;
219     color32                 m_ColLabelText;
220     color32                 m_ColStructText;
221     color32                 m_ColValBg;
222     color32                 m_ColValText;
223     color32                 m_ColValTextRO;
224     color32                 m_ColValTextNE;
225     color32                 m_ColValMin;
226     color32                 m_ColValMax;
227     color32                 m_ColStructBg;
228     color32                 m_ColTitleBg;
229     color32                 m_ColTitleHighBg;
230     color32                 m_ColTitleUnactiveBg;
231     color32                 m_ColTitleText;
232     color32                 m_ColTitleShadow;
233     color32                 m_ColLine;
234     color32                 m_ColLineShadow;
235     color32                 m_ColUnderline;
236     color32                 m_ColBtn;
237     color32                 m_ColHighBtn;
238     color32                 m_ColFold;
239     color32                 m_ColHighFold;
240     color32                 m_ColGrpBg;
241     color32                 m_ColGrpText;
242     color32                 m_ColHierBg;
243     color32                 m_ColShortcutText;
244     color32                 m_ColShortcutBg;
245     color32                 m_ColInfoText;
246     color32                 m_ColHelpBg;
247     color32                 m_ColHelpText;
248     color32                 m_ColRoto;
249     color32                 m_ColRotoVal;
250     color32                 m_ColRotoBound;
251     color32                 m_ColEditBg;
252     color32                 m_ColEditText;
253     color32                 m_ColEditSelBg;
254     color32                 m_ColEditSelText;
255     color32                 m_ColSeparator;
256     color32                 m_ColStaticText;
257     void                    UpdateColors();
258 
259 protected:
260     int                     m_TitleWidth;
261     int                     m_VarX0;
262     int                     m_VarX1;
263     int                     m_VarX2;
264     int                     m_VarY0;
265     int                     m_VarY1;
266     int                     m_VarY2;
267     int                     m_ScrollYW;
268     int                     m_ScrollYH;
269     int                     m_ScrollY0;
270     int                     m_ScrollY1;
271     int                     m_NbHierLines;
272     int                     m_NbDisplayedLines;
273     bool                    m_UpToDate;
274     float                   m_LastUpdateTime;
275     void                    Update();
276 
277     bool                    m_MouseDrag;
278     bool                    m_MouseDragVar;
279     bool                    m_MouseDragTitle;
280     bool                    m_MouseDragScroll;
281     bool                    m_MouseDragResizeUR;
282     bool                    m_MouseDragResizeUL;
283     bool                    m_MouseDragResizeLR;
284     bool                    m_MouseDragResizeLL;
285     bool                    m_MouseDragValWidth;
286     int                     m_MouseOriginX;
287     int                     m_MouseOriginY;
288     double                  m_ValuesWidthRatio;
289     bool                    m_VarHasBeenIncr;
290     int                     m_FirstLine0;
291     int                     m_HighlightedLine;
292     int                     m_HighlightedLinePrev;
293     int                     m_HighlightedLineLastValid;
294     bool                    m_HighlightIncrBtn;
295     bool                    m_HighlightDecrBtn;
296     bool                    m_HighlightRotoBtn;
297     bool                    m_HighlightListBtn;
298     bool                    m_HighlightBoolBtn;
299     bool                    m_HighlightClickBtn;
300     double                  m_HighlightClickBtnAuto;
301     bool                    m_HighlightTitle;
302     bool                    m_HighlightScroll;
303     bool                    m_HighlightUpScroll;
304     bool                    m_HighlightDnScroll;
305     bool                    m_HighlightMinimize;
306     bool                    m_HighlightFont;
307     bool                    m_HighlightValWidth;
308     bool                    m_HighlightLabelsHeader;
309     bool                    m_HighlightValuesHeader;
310     bool                    m_DrawHandles;
311 
312     bool                    m_IsMinimized;
313     int                     m_MinPosX;
314     int                     m_MinPosY;
315     bool                    m_HighlightMaximize;
316     bool                    m_DrawIncrDecrBtn;
317     bool                    m_DrawRotoBtn;
318     bool                    m_DrawClickBtn;
319     bool                    m_DrawListBtn;
320     bool                    m_DrawBoolBtn;
321     EButtonAlign            m_ButtonAlign;
322 
323     struct CHierTag
324     {
325         CTwVar *            m_Var;
326         int                 m_Level;
327         bool                m_Closing;
328     };
329     std::vector<CHierTag>   m_HierTags;
330     void                    BrowseHierarchy(int *_LineNum, int _CurrLevel, const CTwVar *_Var, int _First, int _Last);
331     void *                  m_TitleTextObj;
332     void *                  m_LabelsTextObj;
333     void *                  m_ValuesTextObj;
334     void *                  m_ShortcutTextObj;
335     int                     m_ShortcutLine;
336     void *                  m_HeadersTextObj;
337     void                    ListLabels(std::vector<std::string>& _Labels, std::vector<color32>& _Colors, std::vector<color32>& _BgColors, bool *_HasBgColors, const CTexFont *_Font, int _AtomWidthMax, int _GroupWidthMax);
338     void                    ListValues(std::vector<std::string>& _Values, std::vector<color32>& _Colors, std::vector<color32>& _BgColors, const CTexFont *_Font, int _WidthMax);
339     int                     ComputeLabelsWidth(const CTexFont *_Font);
340     int                     ComputeValuesWidth(const CTexFont *_Font);
341     void                    DrawHierHandle();
342 
343     enum EValuesWidthFit    { VALUES_WIDTH_FIT = -5555 };
344 
345     // RotoSlider
346     struct  CPoint
347     {
348         int                 x, y;
CPointCTwBar::CPoint349                             CPoint() {}
CPointCTwBar::CPoint350                             CPoint(int _X, int _Y):x(_X), y(_Y) {}
351         const CPoint        operator+ (const CPoint& p) const { return CPoint(x+p.x, y+p.y); }
352         const CPoint        operator- (const CPoint& p) const { return CPoint(x-p.x, y-p.y); }
353     };
354     struct CRotoSlider
355     {
356                             CRotoSlider();
357         CTwVarAtom *        m_Var;
358         double              m_PreciseValue;
359         double              m_CurrentValue;
360         double              m_Value0;
361         double              m_ValueAngle0;
362         bool                m_Active;
363         bool                m_ActiveMiddle;
364         CPoint              m_Origin;
365         CPoint              m_Current;
366         bool                m_HasPrevious;
367         CPoint              m_Previous;
368         double              m_Angle0;
369         double              m_AngleDT;
370         int                 m_Subdiv;
371     };
372     CRotoSlider             m_Roto;
373     int                     m_RotoMinRadius;
374     int                     m_RotoNbSubdiv; // number of steps for one turn
375     void                    RotoDraw();
376     void                    RotoOnMouseMove(int _X, int _Y);
377     void                    RotoOnLButtonDown(int _X, int _Y);
378     void                    RotoOnLButtonUp(int _X, int _Y);
379     void                    RotoOnMButtonDown(int _X, int _Y);
380     void                    RotoOnMButtonUp(int _X, int _Y);
381     double                  RotoGetValue() const;
382     void                    RotoSetValue(double _Val);
383     double                  RotoGetMin() const;
384     double                  RotoGetMax() const;
385     double                  RotoGetStep() const;
386     double                  RotoGetSteppedValue() const;
387 
388     // Edit-in-place
389     struct CEditInPlace
390     {
391                             CEditInPlace();
392                             ~CEditInPlace();
393         CTwVarAtom *        m_Var;
394         bool                m_Active;
395         std::string         m_String;
396         void *              m_EditTextObj;
397         void *              m_EditSelTextObj;
398         int                 m_CaretPos;
399         int                 m_SelectionStart;
400         int                 m_X, m_Y;
401         int                 m_Width;
402         int                 m_FirstChar;
403         std::string         m_Clipboard;
404     };
405     CEditInPlace            m_EditInPlace;
406     void                    EditInPlaceDraw();
407     bool                    EditInPlaceAcceptVar(const CTwVarAtom* _Var);
408     bool                    EditInPlaceIsReadOnly();
409     void                    EditInPlaceStart(CTwVarAtom* _Var, int _X, int _Y, int _Width);
410     void                    EditInPlaceEnd(bool _Commit);
411     bool                    EditInPlaceKeyPressed(int _Key, int _Modifiers);
412     bool                    EditInPlaceEraseSelect();
413     bool                    EditInPlaceMouseMove(int _X, int _Y, bool _Select);
414     bool                    EditInPlaceSetClipboard(const std::string& _String);
415     bool                    EditInPlaceGetClipboard(std::string *_OutString);
416 
417     struct CCustomRecord
418     {
419         int                 m_IndexMin;
420         int                 m_IndexMax;
421         int                 m_XMin, m_XMax;
422         int                 m_YMin, m_YMax; // Y visible range
423         int                 m_Y0, m_Y1;     // Y widget range
424         CTwVarGroup *       m_Var;
425     };
426     typedef std::map<CTwMgr::CStructProxy*, CCustomRecord> CustomMap;
427     CustomMap               m_CustomRecords;
428     CTwMgr::CStructProxy *  m_CustomActiveStructProxy;
429 
430     friend struct CTwMgr;
431 };
432 
433 void DrawArc(int _X, int _Y, int _Radius, float _StartAngleDeg, float _EndAngleDeg, color32 _Color);
434 
435 //  ---------------------------------------------------------------------------
436 
437 
438 #endif // !defined ANT_TW_BAR_INCLUDED
439