llcontrol.h

Go to the documentation of this file.
00001 
00032 #ifndef LL_LLCONTROL_H
00033 #define LL_LLCONTROL_H
00034 
00035 #include "llevent.h"
00036 #include "llnametable.h"
00037 #include "llmap.h"
00038 #include "llstring.h"
00039 #include "llrect.h"
00040 
00041 class LLVector3;
00042 class LLVector3d;
00043 class LLColor4;
00044 class LLColor3;
00045 class LLColor4U;
00046 
00047 const BOOL NO_PERSIST = FALSE;
00048 
00049 typedef enum e_control_type
00050 {
00051         TYPE_U32,
00052         TYPE_S32,
00053         TYPE_F32,
00054         TYPE_BOOLEAN,
00055         TYPE_STRING,
00056         TYPE_VEC3,
00057         TYPE_VEC3D,
00058         TYPE_RECT,
00059         TYPE_COL4,
00060         TYPE_COL3,
00061         TYPE_COL4U
00062 } eControlType;
00063 
00064 class LLControlBase : public LLSimpleListenerObservable
00065 {
00066 friend class LLControlGroup;
00067 protected:
00068         LLString                mName;
00069         LLString                mComment;
00070         eControlType    mType;
00071         BOOL                    mHasRange;
00072         BOOL                    mPersist;
00073         BOOL            mIsDefault;
00074 
00075         static  std::set<LLControlBase*>        mChangedControls;
00076         static  std::list<S32>                          mFreeIDs;//These lists are used to store the ID's of registered event listeners.
00077         static  std::list<S32>                          mUsedIDs;
00078         static  S32                                                     mTopID;//This is the index of the highest ID event listener ID. When the free pool is exhausted, new IDs are allocated from here.
00079 
00080 public:
00081         static  void                                            releaseListenerID(S32   id);
00082         static  S32                                                     allocateListenerID();
00083         static  void                                            updateAllListeners();
00084         virtual void updateListeners() = 0;
00085 
00086         LLControlBase(const LLString& name, eControlType type, const LLString& comment, BOOL persist)
00087                 : mName(name),
00088                 mComment(comment),
00089                 mType(type),
00090                 mHasRange(FALSE),
00091                 mPersist(persist),
00092                 mIsDefault(TRUE)
00093         { 
00094                 if (mPersist && mComment.empty())
00095                 {
00096                         llerrs << "Must supply a comment for control " << mName << llendl;
00097                 }
00098                 sMaxControlNameLength = llmax((U32)mName.size(), sMaxControlNameLength);
00099         }
00100 
00101         virtual ~LLControlBase();
00102 
00103         const LLString& getName() const { return mName; }
00104         const LLString& getComment() const { return mComment; }
00105 
00106         eControlType type()             { return mType; }
00107         BOOL    isType(eControlType tp) { return tp == mType; }
00108 
00109         // Defaults to no-op
00110         virtual void resetToDefault();
00111 
00112         LLSD registerListener(LLSimpleListenerObservable *listener, LLSD userdata = "");
00113 
00114         virtual LLSD get() const = 0;
00115         virtual LLSD getValue() const = 0;
00116         virtual void setValue(LLSD value) = 0;
00117         virtual void set(LLSD value) = 0;
00118 
00119         // From LLSimpleListener
00120         virtual bool handleEvent(LLPointer<LLEvent> event, const LLSD& userdata);
00121 
00122         void firePropertyChanged();
00123 
00124         static U32      sMaxControlNameLength;
00125 
00126 protected:
00127         const char* name()                      { return mName.c_str(); }
00128         const char* comment()           { return mComment.c_str(); }
00129 };
00130 
00131 class LLControl
00132 : public LLControlBase
00133 {
00134 friend class LLControlGroup;
00135 protected:
00136         LLSD mCurrent;
00137         LLSD mDefault;
00138 
00139 public: 
00140 
00141         typedef void    tListenerCallback(const LLSD&   newValue,S32    listenerID, LLControl& control);
00142         typedef struct{
00143                 S32                                     mID;
00144                 LLSD                    mNewValue;
00145                 tListenerCallback*      mCBFN;
00146         }tPropertyChangedEvent;
00147 
00148         typedef std::list<tPropertyChangedEvent>::iterator tPropertyChangedListIter;
00149         std::list<tPropertyChangedEvent>        mChangeEvents;
00150         std::list< tListenerCallback* >         mListeners;
00151         std::list< S32 >                                        mListenerIDs;
00152 
00153         virtual void                                            updateListeners();
00154         S32                                                                     addListener(tListenerCallback*  cbfn);
00155         
00156         LLControl(
00157                 const LLString& name,
00158                 eControlType type,
00159                 LLSD initial, const
00160                 LLString& comment,
00161                 BOOL persist = TRUE);
00162 
00163         void set(LLSD val)                      { setValue(val); }
00164         LLSD get()                      const   { return getValue(); } 
00165         LLSD getdefault()       const   { return mDefault; }
00166         LLSD getValue()         const   { return mCurrent; }
00167         BOOL llsd_compare(const LLSD& a, const LLSD& b);
00168 
00169         void setValue(LLSD value)                       
00170         { 
00171                 if (llsd_compare(mCurrent, value) == FALSE)
00172                 {
00173                         mCurrent = value; 
00174                         mIsDefault = llsd_compare(mCurrent, mDefault); 
00175                         firePropertyChanged(); 
00176                 }
00177         }
00178 
00179         /*virtual*/ void resetToDefault() 
00180         { 
00181                 setValue(mDefault);
00182         }
00183 
00184         virtual ~LLControl()
00185         {
00186                 //Remove and deregister all listeners..
00187                 while(mListenerIDs.size())
00188                 {
00189                         S32     id = mListenerIDs.front();
00190                         mListenerIDs.pop_front();
00191                         releaseListenerID(id);
00192                 }
00193         }
00194 };
00195 
00196 //const U32 STRING_CACHE_SIZE = 10000;
00197 class LLControlGroup
00198 {
00199 public:
00200         typedef std::map<LLString, LLPointer<LLControlBase> > ctrl_name_table_t;
00201         ctrl_name_table_t mNameTable;
00202         std::set<LLString> mWarnings;
00203         
00204 public:
00205         LLControlGroup();
00206         ~LLControlGroup();
00207         void cleanup();
00208         
00209         LLControlBase*  getControl(const LLString& name);
00210         LLSD registerListener(const LLString& name, LLSimpleListenerObservable *listener);
00211         
00212         BOOL declareControl(const LLString& name, eControlType type, const LLSD initial_val, const LLString& comment, BOOL persist);
00213         BOOL declareU32(const LLString& name, U32 initial_val, const LLString& comment, BOOL persist = TRUE);
00214         BOOL declareS32(const LLString& name, S32 initial_val, const LLString& comment, BOOL persist = TRUE);
00215         BOOL declareF32(const LLString& name, F32 initial_val, const LLString& comment, BOOL persist = TRUE);
00216         BOOL declareBOOL(const LLString& name, BOOL initial_val, const LLString& comment, BOOL persist = TRUE);
00217         BOOL declareString(const LLString& name, const LLString &initial_val, const LLString& comment, BOOL persist = TRUE);
00218         BOOL declareVec3(const LLString& name, const LLVector3 &initial_val,const LLString& comment,  BOOL persist = TRUE);
00219         BOOL declareVec3d(const LLString& name, const LLVector3d &initial_val, const LLString& comment, BOOL persist = TRUE);
00220         BOOL declareRect(const LLString& name, const LLRect &initial_val, const LLString& comment, BOOL persist = TRUE);
00221         BOOL declareColor4U(const LLString& name, const LLColor4U &initial_val, const LLString& comment, BOOL persist = TRUE);
00222         BOOL declareColor4(const LLString& name, const LLColor4 &initial_val, const LLString& comment, BOOL persist = TRUE);
00223         BOOL declareColor3(const LLString& name, const LLColor3 &initial_val, const LLString& comment, BOOL persist = TRUE);
00224         
00225         LLString        findString(const LLString& name);
00226 
00227         LLString        getString(const LLString& name);
00228         LLWString       getWString(const LLString& name);
00229         LLString        getText(const LLString& name);
00230         LLVector3       getVector3(const LLString& name);
00231         LLVector3d      getVector3d(const LLString& name);
00232         LLRect          getRect(const LLString& name);
00233         BOOL            getBOOL(const LLString& name);
00234         S32                     getS32(const LLString& name);
00235         F32                     getF32(const LLString& name);
00236         U32                     getU32(const LLString& name);
00237         LLSD            getValue(const LLString& name);
00238 
00239 
00240         // Note: If an LLColor4U control exists, it will cast it to the correct
00241         // LLColor4 for you.
00242         LLColor4        getColor(const LLString& name);
00243         LLColor4U       getColor4U(const LLString& name);
00244         LLColor4        getColor4(const LLString& name);
00245         LLColor3        getColor3(const LLString& name);
00246 
00247         void    setBOOL(const LLString& name, BOOL val);
00248         void    setS32(const LLString& name, S32 val);
00249         void    setF32(const LLString& name, F32 val);
00250         void    setU32(const LLString& name, U32 val);
00251         void    setString(const LLString&  name, const LLString& val);
00252         void    setVector3(const LLString& name, const LLVector3 &val);
00253         void    setVector3d(const LLString& name, const LLVector3d &val);
00254         void    setRect(const LLString& name, const LLRect &val);
00255         void    setColor4U(const LLString& name, const LLColor4U &val);
00256         void    setColor4(const LLString& name, const LLColor4 &val);
00257         void    setColor3(const LLString& name, const LLColor3 &val);
00258         void    setValue(const LLString& name, const LLSD& val);
00259 
00260         BOOL    controlExists(const LLString& name);
00261 
00262         // Returns number of controls loaded, 0 if failed
00263         // If require_declaration is false, will auto-declare controls it finds
00264         // as the given type.
00265         U32             loadFromFileLegacy(const LLString& filename, BOOL require_declaration = TRUE, eControlType declare_as = TYPE_STRING);
00266         U32             loadFromFile(const LLString& filename, BOOL require_declaration = TRUE, eControlType declare_as = TYPE_STRING);
00267         U32             saveToFile(const LLString& filename, BOOL skip_if_default);
00268         void    applyOverrides(const std::map<std::string, std::string>& overrides);
00269         void    resetToDefaults();
00270 
00271         // Ignorable Warnings
00272         
00273         // Add a config variable to be reset on resetWarnings()
00274         void addWarning(const LLString& name);
00275         BOOL getWarning(const LLString& name);
00276         void setWarning(const LLString& name, BOOL val);
00277         
00278         // Resets all ignorables
00279         void resetWarnings();
00280 };
00281 
00282 #endif

Generated on Thu Jul 1 06:08:23 2010 for Second Life Viewer by  doxygen 1.4.7