TwBar.h 19.8 KB
Newer Older
Philip Trettner's avatar
Philip Trettner committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
//  ---------------------------------------------------------------------------
//
//  @file       TwBar.h
//  @brief      Tweak bar and var classes.
//  @author     Philippe Decaudin
//  @license    This file is part of the AntTweakBar library.
//              For conditions of distribution and use, see License.txt
//
//  note:       Private header
//
//  ---------------------------------------------------------------------------


#if !defined ANT_TW_BAR_INCLUDED
#define ANT_TW_BAR_INCLUDED

#include <AntTweakBar.h>
#include "TwColors.h"
  
#define ANT_TWEAK_BAR_DLL "AntTweakBar"


//  ---------------------------------------------------------------------------

bool IsCustomType(int _Type);

struct CTwVar
{
    std::string             m_Name;
    std::string             m_Label;
    std::string             m_Help;
    bool                    m_IsRoot;
    bool                    m_DontClip;
    bool                    m_Visible;
    signed short            m_LeftMargin;
    signed short            m_TopMargin;
    const color32 *         m_ColorPtr;
    const color32 *         m_BgColorPtr;

    virtual bool            IsGroup() const = 0;
    virtual bool            IsCustom() const { return false; }
    virtual const CTwVar *  Find(const char *_Name, struct CTwVarGroup **_Parent, int *_Index) const = 0;
    virtual int             HasAttrib(const char *_Attrib, bool *_HasValue) const;
    virtual int             SetAttrib(int _AttribID, const char *_Value, TwBar *_Bar, struct CTwVarGroup *_VarParent, int _VarIndex);
    virtual ERetType        GetAttrib(int _AttribID, TwBar *_Bar, struct CTwVarGroup *_VarParent, int _VarIndex, std::vector<double>& outDouble, std::ostringstream& outString) const;
    virtual void            SetReadOnly(bool _ReadOnly) = 0;
    virtual bool            IsReadOnly() const = 0;
                            CTwVar();
    virtual                 ~CTwVar() {}

    static size_t           GetDataSize(TwType _Type);
};


struct CTwVarAtom : CTwVar
{
    ETwType                 m_Type;
    void *                  m_Ptr;
    TwSetVarCallback        m_SetCallback;
    TwGetVarCallback        m_GetCallback;
    void *                  m_ClientData;
    bool                    m_ReadOnly;
    bool                    m_NoSlider;
    int                     m_KeyIncr[2];   // [0]=key_code [1]=modifiers
    int                     m_KeyDecr[2];   // [0]=key_code [1]=modifiers

    template <typename _T>  struct TVal
    {
        _T                  m_Min;
        _T                  m_Max;
        _T                  m_Step;
        signed char         m_Precision;
        bool                m_Hexa;
    };
    union UVal
    {
        TVal<unsigned char> m_Char;
        TVal<signed char>   m_Int8;
        TVal<unsigned char> m_UInt8;
        TVal<signed short>  m_Int16;
        TVal<unsigned short>m_UInt16;
        TVal<signed int>    m_Int32;
        TVal<unsigned int>  m_UInt32;
        TVal<float>         m_Float32;
        TVal<double>        m_Float64;
        struct CBoolVal
        {
            char *          m_TrueString;
            char *          m_FalseString;
            bool            m_FreeTrueString;
            bool            m_FreeFalseString;
        }                   m_Bool;
        struct CEnumVal     // empty -> enum entries are deduced from m_Type
        {
            //typedef std::map<unsigned int, std::string> CEntries;
            //CEntries *    m_Entries;
        }                   m_Enum;
        struct CShortcutVal
        {
            int             m_Incr[2];
            int             m_Decr[2];
        }                   m_Shortcut;
        struct CHelpStruct
        {
            int             m_StructType;
        }                   m_HelpStruct;
        struct CButtonVal
        {
            TwButtonCallback m_Callback;
            int             m_Separator;
        }                   m_Button;
        struct CCustomVal
        {
            CTwMgr::CMemberProxy *m_MemberProxy;
        }                   m_Custom;
    };
    UVal                    m_Val;

    virtual bool            IsGroup() const { return false; }
    virtual bool            IsCustom() const { return IsCustomType(m_Type); }
    virtual void            ValueToString(std::string *_Str) const;
    virtual double          ValueToDouble() const;
    virtual void            ValueFromDouble(double _Val);
    virtual void            MinMaxStepToDouble(double *_Min, double *_Max, double *_Step) const;
    virtual const CTwVar *  Find(const char *_Name, struct CTwVarGroup **_Parent, int *_Index) const;
    virtual int             HasAttrib(const char *_Attrib, bool *_HasValue) const;
    virtual int             SetAttrib(int _AttribID, const char *_Value, TwBar *_Bar, struct CTwVarGroup *_VarParent, int _VarIndex);
    virtual ERetType        GetAttrib(int _AttribID, TwBar *_Bar, struct CTwVarGroup *_VarParent, int _VarIndex, std::vector<double>& outDouble, std::ostringstream& outString) const;
    virtual void            Increment(int _Step);
    virtual void            SetDefaults();
    virtual void            SetReadOnly(bool _ReadOnly) { m_ReadOnly=_ReadOnly; if( m_Type!=TW_TYPE_BUTTON && m_SetCallback==NULL && m_Ptr==NULL ) m_ReadOnly=true; }
    virtual bool            IsReadOnly() const { if( m_Type!=TW_TYPE_BUTTON && m_SetCallback==NULL && m_Ptr==NULL ) return true; else return m_ReadOnly; }
    //virtual int           DefineEnum(const TwEnumVal *_EnumValues, unsigned int _NbValues);
                            CTwVarAtom();
    virtual                 ~CTwVarAtom();
};


struct CTwVarGroup : CTwVar
{
    std::vector<CTwVar *>   m_Vars;
    bool                    m_Open;
    TwSummaryCallback       m_SummaryCallback;
    void *                  m_SummaryClientData;
    void *                  m_StructValuePtr;
    TwType                  m_StructType;

    virtual bool            IsGroup() const { return true; }
    virtual const CTwVar *  Find(const char *_Name, CTwVarGroup **_Parent, int *_Index) const;
    virtual int             HasAttrib(const char *_Attrib, bool *_HasValue) const;
    virtual int             SetAttrib(int _AttribID, const char *_Value, TwBar *_Bar, struct CTwVarGroup *_VarParent, int _VarIndex);
    virtual ERetType        GetAttrib(int _AttribID, TwBar *_Bar, struct CTwVarGroup *_VarParent, int _VarIndex, std::vector<double>& outDouble, std::ostringstream& outString) const;
    virtual CTwVarAtom *    FindShortcut(int _Key, int _Modifiers, bool *_DoIncr);
    virtual void            SetReadOnly(bool _ReadOnly) { for(size_t i=0; i<m_Vars.size(); ++i) if(m_Vars[i]) m_Vars[i]->SetReadOnly(_ReadOnly); }
    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; }
                            CTwVarGroup()   { m_Open=false; m_StructType=TW_TYPE_UNDEF; m_SummaryCallback=NULL; m_SummaryClientData=NULL; m_StructValuePtr=NULL; }
    virtual                 ~CTwVarGroup();
};

//  ---------------------------------------------------------------------------

struct CTwBar
{
    std::string             m_Name;
    std::string             m_Label;
    std::string             m_Help;
    bool                    m_Visible;
    int                     m_PosX;
    int                     m_PosY;
    int                     m_Width;
    int                     m_Height;
    color32                 m_Color;
    bool                    m_DarkText;
    const CTexFont *        m_Font;
    int                     m_ValuesWidth;
    int                     m_Sep;
    int                     m_LineSep;
    int                     m_FirstLine;
    float                   m_UpdatePeriod;
    bool                    m_IsHelpBar;
    int                     m_MinNumber;    // accessed by TwDeleteBar
    bool                    m_IsPopupList;
    CTwVarAtom *            m_VarEnumLinkedToPopupList;
    CTwBar *                m_BarLinkedToPopupList;
    bool                    m_Resizable;
    bool                    m_Movable;
    bool                    m_Iconifiable;
    bool                    m_Contained;

    CTwVarGroup             m_VarRoot;

    enum EDrawPart          { DRAW_BG=(1<<0), DRAW_CONTENT=(1<<1), DRAW_ALL=DRAW_BG|DRAW_CONTENT };
    void                    Draw(int _DrawPart=DRAW_ALL);
    void                    NotUpToDate();
    const CTwVar *          Find(const char *_Name, CTwVarGroup **_Parent=NULL, int *_Index=NULL) const;
    CTwVar *                Find(const char *_Name, CTwVarGroup **_Parent=NULL, int *_Index=NULL);
    int                     HasAttrib(const char *_Attrib, bool *_HasValue) const;
    int                     SetAttrib(int _AttribID, const char *_Value);
    ERetType                GetAttrib(int _AttribID, std::vector<double>& outDouble, std::ostringstream& outString) const;
    bool                    MouseMotion(int _X, int _Y);
    bool                    MouseButton(ETwMouseButtonID _Button, bool _Pressed, int _X, int _Y);
    bool                    MouseWheel(int _Pos, int _PrevPos, int _MouseX, int _MouseY);
    bool                    KeyPressed(int _Key, int _Modifiers);
    bool                    KeyTest(int _Key, int _Modifiers);
    bool                    IsMinimized() const { return m_IsMinimized; }
    bool                    IsDragging() const  { return m_MouseDrag; }
    bool                    Show(CTwVar *_Var); // display the line associated to _Var
    bool                    OpenHier(CTwVarGroup *_Root, CTwVar *_Var); // open a hierarchy if it contains _Var
    int                     LineInHier(CTwVarGroup *_Root, CTwVar *_Var); // returns the number of the line associated to _Var
    void                    UnHighlightLine() { m_HighlightedLine = -1; NotUpToDate(); } // used by PopupCallback
    void                    HaveFocus(bool _Focus) { m_DrawHandles = _Focus; }           // used by PopupCallback
    void                    StopEditInPlace() { if( m_EditInPlace.m_Active ) EditInPlaceEnd(false); }
                            CTwBar(const char *_Name);
                            ~CTwBar();

    color32                 m_ColBg, m_ColBg1, m_ColBg2;
    color32                 m_ColHighBg0;
    color32                 m_ColHighBg1;
    color32                 m_ColLabelText;
    color32                 m_ColStructText;
    color32                 m_ColValBg;
    color32                 m_ColValText;
    color32                 m_ColValTextRO;
    color32                 m_ColValTextNE;
    color32                 m_ColValMin;
    color32                 m_ColValMax;
    color32                 m_ColStructBg;
    color32                 m_ColTitleBg;
    color32                 m_ColTitleHighBg;
    color32                 m_ColTitleUnactiveBg;
    color32                 m_ColTitleText;
    color32                 m_ColTitleShadow;
    color32                 m_ColLine;
    color32                 m_ColLineShadow;
    color32                 m_ColUnderline;
    color32                 m_ColBtn;
    color32                 m_ColHighBtn;
    color32                 m_ColFold;
    color32                 m_ColHighFold;
    color32                 m_ColGrpBg;
    color32                 m_ColGrpText;
    color32                 m_ColHierBg;
    color32                 m_ColShortcutText;
    color32                 m_ColShortcutBg;
    color32                 m_ColInfoText;
    color32                 m_ColHelpBg;
    color32                 m_ColHelpText;
    color32                 m_ColRoto;
    color32                 m_ColRotoVal;
    color32                 m_ColRotoBound;
    color32                 m_ColEditBg;
    color32                 m_ColEditText;
    color32                 m_ColEditSelBg;
    color32                 m_ColEditSelText;
    color32                 m_ColSeparator;
    color32                 m_ColStaticText;
    void                    UpdateColors();

protected:
    int                     m_TitleWidth;
    int                     m_VarX0;
    int                     m_VarX1;
    int                     m_VarX2;
    int                     m_VarY0;
    int                     m_VarY1;
    int                     m_VarY2;
    int                     m_ScrollYW;
    int                     m_ScrollYH;
    int                     m_ScrollY0;
    int                     m_ScrollY1;
    int                     m_NbHierLines;
    int                     m_NbDisplayedLines;
    bool                    m_UpToDate;
    float                   m_LastUpdateTime;
    void                    Update();

    bool                    m_MouseDrag;
    bool                    m_MouseDragVar;
    bool                    m_MouseDragTitle;
    bool                    m_MouseDragScroll;
    bool                    m_MouseDragResizeUR;
    bool                    m_MouseDragResizeUL;
    bool                    m_MouseDragResizeLR;
    bool                    m_MouseDragResizeLL;
    bool                    m_MouseDragValWidth;
    int                     m_MouseOriginX;
    int                     m_MouseOriginY;
    double                  m_ValuesWidthRatio;
    bool                    m_VarHasBeenIncr;
    int                     m_FirstLine0;
    int                     m_HighlightedLine;
    int                     m_HighlightedLinePrev;
    int                     m_HighlightedLineLastValid;
    bool                    m_HighlightIncrBtn;
    bool                    m_HighlightDecrBtn;
    bool                    m_HighlightRotoBtn;
    bool                    m_HighlightListBtn;
    bool                    m_HighlightBoolBtn;
    bool                    m_HighlightClickBtn;
    double                  m_HighlightClickBtnAuto;
    bool                    m_HighlightTitle;
    bool                    m_HighlightScroll;
    bool                    m_HighlightUpScroll;
    bool                    m_HighlightDnScroll;
    bool                    m_HighlightMinimize;
    bool                    m_HighlightFont;
    bool                    m_HighlightValWidth;
    bool                    m_HighlightLabelsHeader;
    bool                    m_HighlightValuesHeader;
    bool                    m_DrawHandles;

    bool                    m_IsMinimized;
    int                     m_MinPosX;
    int                     m_MinPosY;
    bool                    m_HighlightMaximize;
    bool                    m_DrawIncrDecrBtn;
    bool                    m_DrawRotoBtn;
    bool                    m_DrawClickBtn;
    bool                    m_DrawListBtn;
    bool                    m_DrawBoolBtn;
    EButtonAlign            m_ButtonAlign;

    struct CHierTag
    {
        CTwVar *            m_Var;
        int                 m_Level;
        bool                m_Closing;
    };
    std::vector<CHierTag>   m_HierTags;
    void                    BrowseHierarchy(int *_LineNum, int _CurrLevel, const CTwVar *_Var, int _First, int _Last);
    void *                  m_TitleTextObj;
    void *                  m_LabelsTextObj;
    void *                  m_ValuesTextObj;
    void *                  m_ShortcutTextObj;
    int                     m_ShortcutLine;
    void *                  m_HeadersTextObj;
    void                    ListLabels(std::vector<std::string>& _Labels, std::vector<color32>& _Colors, std::vector<color32>& _BgColors, bool *_HasBgColors, const CTexFont *_Font, int _AtomWidthMax, int _GroupWidthMax);
    void                    ListValues(std::vector<std::string>& _Values, std::vector<color32>& _Colors, std::vector<color32>& _BgColors, const CTexFont *_Font, int _WidthMax);
    int                     ComputeLabelsWidth(const CTexFont *_Font);
    int                     ComputeValuesWidth(const CTexFont *_Font);
    void                    DrawHierHandle();

    enum EValuesWidthFit    { VALUES_WIDTH_FIT = -5555 };
  
    // RotoSlider
    struct  CPoint 
    {
        int                 x, y;
                            CPoint() {}
                            CPoint(int _X, int _Y):x(_X), y(_Y) {}
        const CPoint        operator+ (const CPoint& p) const { return CPoint(x+p.x, y+p.y); }
        const CPoint        operator- (const CPoint& p) const { return CPoint(x-p.x, y-p.y); }
    };
    struct CRotoSlider
    {
                            CRotoSlider();
        CTwVarAtom *        m_Var;
        double              m_PreciseValue;
        double              m_CurrentValue;
        double              m_Value0;
        double              m_ValueAngle0;
        bool                m_Active;
        bool                m_ActiveMiddle;
        CPoint              m_Origin;
        CPoint              m_Current;
        bool                m_HasPrevious;
        CPoint              m_Previous;
        double              m_Angle0;
        double              m_AngleDT;
        int                 m_Subdiv;
    };
    CRotoSlider             m_Roto;
    int                     m_RotoMinRadius;
    int                     m_RotoNbSubdiv; // number of steps for one turn
    void                    RotoDraw();
    void                    RotoOnMouseMove(int _X, int _Y);
    void                    RotoOnLButtonDown(int _X, int _Y);
    void                    RotoOnLButtonUp(int _X, int _Y);
    void                    RotoOnMButtonDown(int _X, int _Y);
    void                    RotoOnMButtonUp(int _X, int _Y);
    double                  RotoGetValue() const;
    void                    RotoSetValue(double _Val);
    double                  RotoGetMin() const;
    double                  RotoGetMax() const;
    double                  RotoGetStep() const;
    double                  RotoGetSteppedValue() const;

    // Edit-in-place
    struct CEditInPlace
    {
                            CEditInPlace();
                            ~CEditInPlace();
        CTwVarAtom *        m_Var;
        bool                m_Active;
        std::string         m_String;
        void *              m_EditTextObj;
        void *              m_EditSelTextObj;
        int                 m_CaretPos;
        int                 m_SelectionStart;
        int                 m_X, m_Y;
        int                 m_Width;
        int                 m_FirstChar;
        std::string         m_Clipboard;
    };
    CEditInPlace            m_EditInPlace;
    void                    EditInPlaceDraw();
    bool                    EditInPlaceAcceptVar(const CTwVarAtom* _Var);
    bool                    EditInPlaceIsReadOnly();
    void                    EditInPlaceStart(CTwVarAtom* _Var, int _X, int _Y, int _Width);
    void                    EditInPlaceEnd(bool _Commit);
    bool                    EditInPlaceKeyPressed(int _Key, int _Modifiers);
    bool                    EditInPlaceEraseSelect();
    bool                    EditInPlaceMouseMove(int _X, int _Y, bool _Select);
    bool                    EditInPlaceSetClipboard(const std::string& _String);
    bool                    EditInPlaceGetClipboard(std::string *_OutString);

    struct CCustomRecord
    {
        int                 m_IndexMin;
        int                 m_IndexMax;
        int                 m_XMin, m_XMax;
        int                 m_YMin, m_YMax; // Y visible range
        int                 m_Y0, m_Y1;     // Y widget range
        CTwVarGroup *       m_Var;
    };
    typedef std::map<CTwMgr::CStructProxy*, CCustomRecord> CustomMap;
    CustomMap               m_CustomRecords;
    CTwMgr::CStructProxy *  m_CustomActiveStructProxy;

    friend struct CTwMgr;
};

void DrawArc(int _X, int _Y, int _Radius, float _StartAngleDeg, float _EndAngleDeg, color32 _Color);

//  ---------------------------------------------------------------------------


#endif // !defined ANT_TW_BAR_INCLUDED