您的位置:首页 > 其它

第二人生的源码分析(九十五)LLCheckBoxCtrl类实现复选按钮

2008-06-19 23:35 471 查看
在第二人生的登录界面里,可以看到输入密码的下面有一个复选按钮,如下图所示:



这个按钮在这里主要用来设置是否帮助用户保存密码在磁盘里的,这样可以使用户每次不用输入密码,就可以登录进去。那么这个按钮是怎么样实现的呢?现在就分析跟这个相关的代码,它的继承代码如下:
class LLCheckBoxCtrl
: public LLUICtrl
可见LLCheckBoxCtrl类也是控件类来的,它主要两部分组成:LLButton和LLTextBox。这两个类都是前面学习过的按钮类和静态文本框。在LLCheckBoxCtrl类声明如下:
#001 protected:
#002 // note: value is stored in toggle state of button
#003 LLButton* mButton;
#004 LLTextBox* mLabel;
#005 const LLFontGL* mFont;
#006 LLColor4 mTextEnabledColor;
#007 LLColor4 mTextDisabledColor;

LLCheckBoxCtrl类的显示代码,如下:
#001 void LLCheckBoxCtrl::draw()
#002 {

根据是否选择来显示文本不同的颜色。
#003 if (getEnabled())
#004 {
#005 mLabel->setColor( mTextEnabledColor );
#006 }
#007 else
#008 {
#009 mLabel->setColor( mTextDisabledColor );
#010 }
#011
#012 // Draw children
#013 LLUICtrl::draw();
#014 }

由于使用前面两个现成的组件构造一个新组件,这样不需要使用更多复杂的代码,就可以实现这样的功能了,因此就这样简单的。当然这两个组件的构造是在LLCheckBoxCtrl类构造函数里创建的,如下:
#001 LLCheckBoxCtrl::LLCheckBoxCtrl(const LLString& name, const LLRect& rect,
#002 const LLString& label,
#003 const LLFontGL* font,
#004 void (*commit_callback)(LLUICtrl* ctrl, void*
#005 userdata),
#006 void* callback_user_data,
#007 BOOL initial_value,
#008 BOOL use_radio_style,
#009 const LLString& control_which)
#010 : LLUICtrl(name, rect, TRUE, commit_callback, callback_user_data, FOLLOWS_LEFT | FOLLOWS_TOP),
#011 mTextEnabledColor( LLUI::sColorsGroup->getColor( "LabelTextColor" ) ),
#012 mTextDisabledColor( LLUI::sColorsGroup->getColor( "LabelDisabledColor" ) ),
#013 mRadioStyle( use_radio_style ),
#014 mInitialValue( initial_value ),
#015 mSetValue( initial_value )
#016 {
#017 if (font)
#018 {
#019 mFont = font;
#020 }
#021 else
#022 {
#023 mFont = LLFontGL::sSansSerifSmall;
#024 }
#025
#026 // must be big enough to hold all children
#027 setUseBoundingRect(TRUE);
#028
#029 mKeyboardFocusOnClick = TRUE;
#030
#031 // Label (add a little space to make sure text actually renders)
#032 const S32 FUDGE = 10;
#033 S32 text_width = mFont->getWidth( label ) + FUDGE;
#034 S32 text_height = llround(mFont->getLineHeight());
#035 LLRect label_rect;
#036 label_rect.setOriginAndSize(
#037 LLCHECKBOXCTRL_HPAD + LLCHECKBOXCTRL_BTN_SIZE + LLCHECKBOXCTRL_SPACING,
#038 LLCHECKBOXCTRL_VPAD + 1, // padding to get better alignment
#039 text_width + LLCHECKBOXCTRL_HPAD,
#040 text_height );
#041
#042 // *HACK Get rid of this with SL-55508...
#043 // this allows blank check boxes and radio boxes for now
#044 LLString local_label = label;
#045 if(local_label.empty())
#046 {
#047 local_label = " ";
#048 }
#049
#050 mLabel = new LLTextBox( "CheckboxCtrl Label", label_rect, local_label.c_str(), mFont );
#051 mLabel->setFollowsLeft();
#052 mLabel->setFollowsBottom();
#053 addChild(mLabel);
#054
#055 // Button
#056 // Note: button cover the label by extending all the way to the right.
#057 LLRect btn_rect;
#058 btn_rect.setOriginAndSize(
#059 LLCHECKBOXCTRL_HPAD,
#060 LLCHECKBOXCTRL_VPAD,
#061 LLCHECKBOXCTRL_BTN_SIZE + LLCHECKBOXCTRL_SPACING + text_width + LLCHECKBOXCTRL_HPAD,
#062 llmax( text_height, LLCHECKBOXCTRL_BTN_SIZE ) + LLCHECKBOXCTRL_VPAD);
#063 LLString active_true_id, active_false_id;
#064 LLString inactive_true_id, inactive_false_id;
#065 if (mRadioStyle)
#066 {
#067 active_true_id = "UIImgRadioActiveSelectedUUID";
#068 active_false_id = "UIImgRadioActiveUUID";
#069 inactive_true_id = "UIImgRadioInactiveSelectedUUID";
#070 inactive_false_id = "UIImgRadioInactiveUUID";
#071 mButton = new LLButton(
#072 "Radio control button", btn_rect,
#073 active_false_id, active_true_id, control_which,
#074 &LLCheckBoxCtrl::onButtonPress, this, LLFontGL::sSansSerif );
#075 mButton->setDisabledImages( inactive_false_id, inactive_true_id );
#076 mButton->setHoverGlowStrength(0.35f);
#077 }
#078 else
#079 {
#080 active_false_id = "UIImgCheckboxActiveUUID";
#081 active_true_id = "UIImgCheckboxActiveSelectedUUID";
#082 inactive_true_id = "UIImgCheckboxInactiveSelectedUUID";
#083 inactive_false_id = "UIImgCheckboxInactiveUUID";
#084 mButton = new LLButton(
#085 "Checkbox control button", btn_rect,
#086 active_false_id, active_true_id, control_which,
#087 &LLCheckBoxCtrl::onButtonPress, this, LLFontGL::sSansSerif );
#088 mButton->setDisabledImages( inactive_false_id, inactive_true_id );
#089 mButton->setHoverGlowStrength(0.35f);
#090 }
#091 mButton->setIsToggle(TRUE);
#092 mButton->setToggleState( initial_value );
#093 mButton->setFollowsLeft();
#094 mButton->setFollowsBottom();
#095 mButton->setCommitOnReturn(FALSE);
#096 addChild(mButton);
#097 }

这样简单就实现了一个复选框的功能。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: