第二人生的源码分析(八十七)LLButton类实现按钮功能

在前面介绍了登录过程的界面里有一个退出按钮,如果当用户不想登录时,就可以随时按这个按钮退出登录的过程,它的界面如下面,那么这个按钮是怎么样实现的呢?
从上图右下角可以看到Quit按钮,它的功能实现是由类LLButton来实现的,这个类的声明如下:
#001class LLButton
#002: public LLUICtrl
#003{
#004public:
按钮的构造函数,主要设置显示的字符串,如果想使用这个类显示中文是不行的,因为这个函数里的LLString是单字节的字符串,而不是宽字节字符串,因此想把第二人生改为中文显示需要添加一个构造函数可以保存UNICODE的字符串。
#005 // simple button with text label
#006 LLButton(const LLString& name, const LLRect &rect, const LLString& control_name = LLString(),
#007 void (*on_click)(void*) = NULL, void *data = NULL);
#008
#009 LLButton(const LLString& name, const LLRect& rect,
#010 const LLString &unselected_image,
#011 const LLString &selected_image,
#012 const LLString& control_name,
#013 void (*click_callback)(void*),
#014 void *callback_data = NULL,
#015 const LLFontGL* mGLFont = NULL,
#016 const LLString& unselected_label = LLString::null,
#017 const LLString& selected_label = LLString::null );
#018
#019 virtual ~LLButton();
初始化一个按钮的回调函数,回调的数据,显示的字体。
#020 void init(void (*click_callback)(void*), void *callback_data, const LLFontGL* font, const LLString& control_name);
#021 virtual EWidgetType getWidgetType() const;
#022 virtual LLString getWidgetTag() const;
#023
#024 void addImageAttributeToXML(LLXMLNodePtr node, const LLString& imageName,
#025 const LLUUID&
#026imageID,const LLString& xmlTagName) const;
#027 virtual LLXMLNodePtr getXML(bool save_children = true) const;
#028 static LLView* fromXML(LLXMLNodePtr node, LLView *parent, LLUICtrlFactory *factory);
#029
#030 virtual BOOL handleUnicodeCharHere(llwchar uni_char, BOOL called_from_parent);
处理按钮的按钮消息。
#031 virtual BOOL handleKeyHere(KEY key, MASK mask, BOOL called_from_parent);
#032 virtual BOOL handleMouseDown(S32 x, S32 y, MASK mask);
#033 virtual BOOL handleMouseUp(S32 x, S32 y, MASK mask);
#034 virtual BOOL handleHover(S32 x, S32 y, MASK mask);
显示这个按钮的函数。
#035 virtual void draw();
#036
#037 virtual void onMouseCaptureLost();
#038
#039 // HACK: "committing" a button is the same as clicking on it.
#040 virtual void onCommit();
#041
#042 void setUnselectedLabelColor( const LLColor4& c ) { mUnselectedLabelColor = c; }
#043 void setSelectedLabelColor( const LLColor4& c ) { mSelectedLabelColor = c; }
#044
#045 void setClickedCallback( void (*cb)(void *data), void* data = NULL ); // mouse down and up within button
#046 void setMouseDownCallback( void (*cb)(void *data) ) { mMouseDownCallback = cb; } // mouse down
#047within button
#048 void setMouseUpCallback( void (*cb)(void *data) ) { mMouseUpCallback = cb; }
#049// mouse up, EVEN IF NOT IN BUTTON
#050 void setHeldDownCallback( void (*cb)(void *data) ) { mHeldDownCallback = cb; }
#051// Mouse button held down and in button
#052 void setHeldDownDelay( F32 seconds, S32 frames = 0) { mHeldDownDelay = seconds;
#053mHeldDownFrameDelay = frames; }
#054
#055 F32 getHeldDownTime() const
#056 { return mMouseDownTimer.getElapsedTimeF32(); }
#057
#058 BOOL getIsToggle() const { return mIsToggle; }
#059 void setIsToggle(BOOL is_toggle) { mIsToggle = is_toggle; }
#060 BOOL toggleState();
#061 BOOL getToggleState() const{ return mToggleState; }
#062 void setToggleState(BOOL b);
#063
#064 void setFlashing( BOOL b );
#065 BOOL getFlashing() const { return mFlashing; }
#066
#067 void setHAlign( LLFontGL::HAlign align ) { mHAlign = align; }
#068 LLFontGL::HAlign getHAlign() const { return mHAlign; }
#069 void setLeftHPad( S32 pad ) { mLeftHPad =
#070pad; }
#071 void setRightHPad( S32 pad ) { mRightHPad =
#072pad; }
#073
#074 const LLStringgetLabelUnselected() const { return wstring_to_utf8str(mUnselectedLabel); }
#075 const LLStringgetLabelSelected() const { return wstring_to_utf8str(mSelectedLabel); }
#076
#077 void setImageColor(const LLString& color_control);
#078 void setImageColor(const LLColor4& c);
#079 virtual void setColor(const LLColor4& c);
#080
#081 void setImages(const LLString &image_name, const LLString &selected_name);
#082 void setDisabledImages(const LLString &image_name, const LLString &selected_name);
#083 void setDisabledImages(const LLString &image_name, const LLString &selected_name, const LLColor4& c);
#084
#085 void setHoverImages(const LLString &image_name, const LLString &selected_name);
#086
#087 void setDisabledImageColor(const LLColor4& c) { mDisabledImageColor = c; }
#088
#089 void setDisabledSelectedLabelColor( const LLColor4& c ) { mDisabledSelectedLabelColor = c; }
#090
#091 void setImageOverlay(const LLString &image_name, LLFontGL::HAlign alignment = LLFontGL::HCENTER, const LLColor4&
#092color = LLColor4::white);
#093 LLPointer<LLUIImage> getImageOverlay() { return mImageOverlay; }
#094
#095
#096 virtual void setValue(const LLSD& value );
#097 virtual LLSD getValue() const;
#098
#099 void setLabel( const LLStringExplicit& label);
#100 virtual BOOL setLabelArg( const LLString& key, const LLStringExplicit& text );
#101 void setLabelUnselected(const LLStringExplicit& label);
#102 void setLabelSelected(const LLStringExplicit& label);
#103 void setDisabledLabel(const LLStringExplicit& disabled_label);
#104 void setDisabledSelectedLabel(const LLStringExplicit& disabled_label);
#105 void setDisabledLabelColor( const LLColor4& c ) { mDisabledLabelColor = c; }
#106
#107 void setFont(const LLFontGL *font)
#108 { mGLFont = ( font ? font : LLFontGL::sSansSerif); }
#109 void setScaleImage(BOOL scale) { mScaleImage = scale; }
#110 BOOL getScaleImage() const { return mScaleImage; }
#111
#112 void setDropShadowedText(BOOL b) { mDropShadowedText = b; }
#113
#114 void setBorderEnabled(BOOL b) {
#115mBorderEnabled = b; }
#116
#117 static void onHeldDown(void *userdata);// to be called by gIdleCallbacks
#118
#119 void setHoverGlowStrength(F32 strength) { mHoverGlowStrength = strength; }
#120
#121 void setImageUnselected(const LLString &image_name);
#122 const LLString& getImageUnselectedName() const { return mImageUnselectedName; }
#123 void setImageSelected(const LLString &image_name);
#124 const LLString& getImageSelectedName() const { return mImageSelectedName; }
#125 void setImageHoverSelected(const LLString &image_name);
#126 void setImageHoverUnselected(const LLString &image_name);
#127 void setImageDisabled(const LLString &image_name);
#128 void setImageDisabledSelected(const LLString &image_name);
#129
#130 void setImageUnselected(LLPointer<LLUIImage> image);
#131 void setImageSelected(LLPointer<LLUIImage> image);
#132 void setImageHoverSelected(LLPointer<LLUIImage> image);
#133 void setImageHoverUnselected(LLPointer<LLUIImage> image);
#134 void setImageDisabled(LLPointer<LLUIImage> image);
#135 void setImageDisabledSelected(LLPointer<LLUIImage> image);
#136
#137 void setCommitOnReturn(BOOL commit) { mCommitOnReturn = commit; }
#138 BOOL getCommitOnReturn() const { return mCommitOnReturn; }
#139
#140 void setHelpURLCallback(const LLString &help_url);
#141 const LLString& getHelpURL() const { return mHelpURL; }
#142
#143protected:
#144
#145 virtual void drawBorder(const LLColor4& color, S32 size);
#146
#147 void setImageUnselectedID(const LLUUID &image_id);
#148 const LLUUID& getImageUnselectedID() const { return mImageUnselectedID; }
#149 void setImageSelectedID(const LLUUID &image_id);
#150 const LLUUID& getImageSelectedID() const { return mImageSelectedID; }
#151 void setImageHoverSelectedID(const LLUUID &image_id);
#152 void setImageHoverUnselectedID(const LLUUID &image_id);
#153 void setImageDisabledID(const LLUUID &image_id);
#154 void setImageDisabledSelectedID(const LLUUID &image_id);
#155 const LLPointer<LLUIImage>& getImageUnselected() const{ return mImageUnselected; }
#156 const LLPointer<LLUIImage>& getImageSelected() const { return mImageSelected; }
#157
#158 LLFrameTimer mMouseDownTimer;
#159
#160private:
#161
#162 void (*mClickedCallback)(void* data );
#163 void (*mMouseDownCallback)(void *data);
#164 void (*mMouseUpCallback)(void *data);
#165 void (*mHeldDownCallback)(void *data);
#166
#167 const LLFontGL*mGLFont;
#168
#169 S32 mMouseDownFrame;
#170 F32 mHeldDownDelay; // seconds, after which held-down callbacks get
#171called
#172 S32 mHeldDownFrameDelay; // frames, after which held-down callbacks get called
#173
#174 LLPointer<LLUIImage> mImageOverlay;
#175 LLFontGL::HAlign mImageOverlayAlignment;
#176 LLColor4 mImageOverlayColor;
#177
#178 LLPointer<LLUIImage> mImageUnselected;
#179 LLUIString mUnselectedLabel;
#180 LLColor4 mUnselectedLabelColor;
#181
#182 LLPointer<LLUIImage> mImageSelected;
#183 LLUIString mSelectedLabel;
#184 LLColor4 mSelectedLabelColor;
#185
#186 LLPointer<LLUIImage> mImageHoverSelected;
#187
#188 LLPointer<LLUIImage> mImageHoverUnselected;
#189
#190 LLPointer<LLUIImage> mImageDisabled;
#191 LLUIString mDisabledLabel;
#192 LLColor4 mDisabledLabelColor;
#193
#194 LLPointer<LLUIImage> mImageDisabledSelected;
#195 LLUIString mDisabledSelectedLabel;
#196 LLColor4 mDisabledSelectedLabelColor;
#197
#198 LLUUID mImageUnselectedID;
#199 LLUUID mImageSelectedID;
#200 LLUUID mImageHoverSelectedID;
#201 LLUUID mImageHoverUnselectedID;
#202 LLUUID mImageDisabledID;
#203 LLUUID mImageDisabledSelectedID;
#204 LLString mImageUnselectedName;
#205 LLString mImageSelectedName;
#206 LLString mImageHoverSelectedName;
#207 LLString mImageHoverUnselectedName;
#208 LLString mImageDisabledName;
#209 LLString mImageDisabledSelectedName;
#210
#211 LLColor4 mHighlightColor;
#212 LLColor4 mUnselectedBgColor;
#213 LLColor4 mSelectedBgColor;
#214
#215 LLColor4 mImageColor;
#216 LLColor4 mDisabledImageColor;
#217
#218 BOOL mIsToggle;
#219 BOOL mToggleState;
#220 BOOL mScaleImage;
#221
#222 BOOL mDropShadowedText;
#223
#224 BOOL mBorderEnabled;
#225
#226 BOOL mFlashing;
#227
#228 LLFontGL::HAlign mHAlign;
#229 S32 mLeftHPad;
#230 S32 mRightHPad;
#231
#232 F32 mHoverGlowStrength;
#233 F32 mCurGlowStrength;
#234
#235 BOOL mNeedsHighlight;
#236 BOOL mCommitOnReturn;
#237
#238 LLString mHelpURL;
#239
#240 LLPointer<LLUIImage> mImagep;
#241
#242 LLFrameTimer mFlashingTimer;
#243};
通过设置这个按钮的名称,按钮处理事件的回调函数,就基本上可以生成一个上面图片的按钮了。
阅读更多

没有更多推荐了,返回首页