第二人生的源码分析(九十四)LLTextBox类实现文本显示
在登录的画面里,可以看到程序的版本显示,那么它是怎么样显示出来的呢?现在就来分析显示这个版本号的代码,其实它是由静态文本显示框LLTextBox来显示的。比如在登录时创建版本显示的代码如下:
这里就是创建静态文本框。
#001 LLTextBox* version_text = LLUICtrlFactory::getTextBoxByName(this, "version_text");
下面是先格式化再显示版本字符串。
#002 if (version_text)
#003 {
#004 LLString version = llformat("%d.%d.%d (%d)",
#005 LL_VERSION_MAJOR,
#006 LL_VERSION_MINOR,
#007 LL_VERSION_PATCH,
#008 LL_VIEWER_BUILD );
#009 version_text->setText(version);
#010 version_text->setClickedCallback(onClickVersion);
#011 version_text->setCallbackUserData(this);
#012 }
LLTextBox类的继承关系代码如下:
class LLTextBox
: public LLUICtrl
接着来分析一下它的显示代码,如下:
#001 void LLTextBox::draw()
#002 {
查看是否可以显示。
#003 if( getVisible() )
#004 {
是否显示边框。
#005 if (mBorderVisible)
#006 {
#007 gl_rect_2d_offset_local(getLocalRect(), 2, FALSE);
#008 }
#009
是否显示边框为阴影方式。
#010 if( mBorderDropShadowVisible )
#011 {
#012 static LLColor4 color_drop_shadow = LLUI::sColorsGroup->getColor("ColorDropShadow");
#013 static S32 drop_shadow_tooltip = LLUI::sConfigGroup->getS32("DropShadowTooltip");
#014 gl_drop_shadow(0, getRect().getHeight(), getRect().getWidth(), 0,
#015 color_drop_shadow, drop_shadow_tooltip);
#016 }
#017
背景是否可见。
#018 if (mBackgroundVisible)
#019 {
#020 LLRect r( 0, getRect().getHeight(), getRect().getWidth(), 0 );
#021 gl_rect_2d( r, mBackgroundColor );
#022 }
#023
显示字符串的对齐方式。
#024 S32 text_x = 0;
#025 switch( mHAlign )
#026 {
#027 case LLFontGL::LEFT:
#028 text_x = mHPad;
#029 break;
#030 case LLFontGL::HCENTER:
#031 text_x = getRect().getWidth() / 2;
#032 break;
#033 case LLFontGL::RIGHT:
#034 text_x = getRect().getWidth() - mHPad;
#035 break;
#036 }
#037
计算字符串显示的高度。
#038 S32 text_y = getRect().getHeight() - mVPad;
#039
#040 if ( getEnabled() )
#041 {
是否有鼠标在上面,而显示不同的颜色。
#042 if(mHasHover)
#043 {
#044 drawText( text_x, text_y, mHoverColor );
#045 }
#046 else
#047 {
#048 drawText( text_x, text_y, mTextColor );
#049 }
#050 }
#051 else
#052 {
#053 drawText( text_x, text_y, mDisabledColor );
#054 }
#055
是否调试输出窗口。
#056 if (sDebugRects)
#057 {
#058 drawDebugRect();
#059 }
#060 }
#061
#062 mHasHover = FALSE; // This is reset every frame.
#063 }
上面这个函数先显示边框,然后显示背景,最后显示字符串出来,这样就实现了静态的文本显示。