在第二人生里,很多界面的创建是基于XML文件来创建,比如前面提到的登录界面。在一个界面里由很多类型组件组成的,这里就介绍其中最常用的组件是按钮组件了。比如登录界面的连接或者退出按钮,都是从XML里分析出按钮的类型,然后创建按钮的。下面就来分析一下LLButton的XML处理代码,如下: #001 LLView* LLButton::fromXML(LLXMLNodePtr node, LLView *parent, LLUICtrlFactory *factory) #002 {
从XML里获取按钮的名称。 #003 LLString name("button"); #004 node->getAttributeString("name", name); #005
获取按钮显示名称。 #006 LLString label = name; #007 node->getAttributeString("label", label); #008
获取按钮的选中显示名称。 #009 LLString label_selected = label; #010 node->getAttributeString("label_selected", label_selected); #011
获取需要显示的字体。 #012 LLFontGL* font = selectFont(node); #013
获取未选中的图片。 #014 LLString image_unselected; #015 if (node->hasAttribute("image_unselected")) node->getAttributeString("image_unselected",image_unselected); #016
获取选中的图片。 #017 LLString image_selected; #018 if (node->hasAttribute("image_selected")) node->getAttributeString("image_selected",image_selected); #019
获取当有鼠标在按钮上面时的图片。 #020 LLString image_hover_selected; #021 if (node->hasAttribute("image_hover_selected")) node->getAttributeString("image_hover_selected",image_hover_selected); #022 #023 LLString image_hover_unselected; #024 if (node->hasAttribute("image_hover_unselected")) node->getAttributeString("image_hover_unselected",image_hover_unselected); #025 #026 LLString image_disabled_selected; #027 if (node->hasAttribute("image_disabled_selected")) node->getAttributeString("image_disabled_selected",image_disabled_selected); #028 #029 LLString image_disabled; #030 if (node->hasAttribute("image_disabled")) node->getAttributeString("image_disabled",image_disabled); #031 #032 LLString image_overlay; #033 node->getAttributeString("image_overlay", image_overlay); #034
获取图片的排列方式。 #035 LLFontGL::HAlign image_overlay_alignment = LLFontGL::HCENTER; #036 LLString image_overlay_alignment_string; #037 if (node->hasAttribute("image_overlay_alignment")) #038 { #039 node->getAttributeString("image_overlay_alignment", image_overlay_alignment_string); #040 image_overlay_alignment = LLFontGL::hAlignFromName(image_overlay_alignment_string); #041 } #042 #043
根据XML里获取到的信息生成按钮。 #044 LLButton *button = new LLButton(name, #045 LLRect(), #046 image_unselected, #047 image_selected, #048 "", #049 NULL, #050 parent, #051 font,
这里是按钮显示的名称,如果想调试按钮是否显示中文,就可以查看这里。 #052 label, #053 label_selected); #054
设置按钮一些属性。 #055 node->getAttributeS32("pad_right", button->mRightHPad); #056 node->getAttributeS32("pad_left", button->mLeftHPad); #057 #058 BOOL is_toggle = button->getIsToggle(); #059 node->getAttributeBOOL("toggle", is_toggle); #060 button->setIsToggle(is_toggle); #061 #062 if(image_hover_selected != LLString::null) button->setImageHoverSelected(image_hover_selected); #063 #064 if(image_hover_unselected != LLString::null) button->setImageHoverUnselected(image_hover_unselected); #065 #066 if(image_disabled_selected != LLString::null) button->setImageDisabledSelected(image_disabled_selected ); #067 #068 if(image_disabled != LLString::null) button->setImageDisabled(image_disabled); #069 #070 if(image_overlay != LLString::null) button->setImageOverlay(image_overlay, image_overlay_alignment); #071 #072 if (node->hasAttribute("halign")) #073 { #074 LLFontGL::HAlign halign = selectFontHAlign(node); #075 button->setHAlign(halign); #076 } #077 #078 if (node->hasAttribute("scale_image")) #079 { #080 BOOL needsScale = FALSE; #081 node->getAttributeBOOL("scale_image",needsScale); #082 button->setScaleImage( needsScale ); #083 } #084
没有名称显示时的处理。 #085 if(label.empty()) #086 { #087 button->setLabelUnselected(node->getTextContents()); #088 } #089 if (label_selected.empty()) #090 { #091 button->setLabelSelected(node->getTextContents()); #092 } #093 #094 if (node->hasAttribute("help_url")) #095 { #096 LLString help_url; #097 node->getAttributeString("help_url",help_url); #098 button->setHelpURLCallback(help_url); #099 } #100
调用基类处理XML的属性。 #101 button->initFromXML(node, parent); #102 #103 return button; #104 }
上面的函数是通过XML里保存的属性和名称来创建按钮。
|