11. 按钮基类UIButtonBase.cs和按钮类UIButton.cs

下面几个教程需要创建按钮、复选框和单选框,这几个控件的行为是类似的,所以比较好的方法是创建一个按钮基类,而按钮类,复选框类和单选框类从这个基类继承,所以需要将这三个类的共有功能抽象出来封装成一个基类,先归纳一下按钮需要实现的功能:

而复选框和单选框的功能是类似的,不同之处在于背景图像位于文字的左侧而不是在文字的下面。 提取出共性后,按钮基类代码如下:

namespace StunEngine.SceneNodes.UI
{
    /// <summary>
    /// 按钮控件基类,按钮、复选框、单选框都是从这个类继承的
    /// </summary>
    public abstract class UIButtonBase:UISceneNode
    {
        /// <summary>
        /// 控件上显示的文字
        /// </summary>
        protected string text;

        /// <summary>
        /// 按钮文字颜色
        /// </summary>
        protected Color textColor;

        /// <summary>
        /// 按钮文字高亮颜色
        /// </summary>
        protected Color textColorHighlight;

        /// <summary>
        /// 文字使用的字体
        /// </summary>
        protected SpriteFont font;        

        /// <summary>
        /// 控件普通状态的源矩形
        /// </summary>
        protected Rectangle rectSourceOrigin;

        /// <summary>
        /// 控件高亮状态的源矩形
        /// </summary>
        protected Rectangle rectSourceHighlight;

        /// <summary>
        /// 创建一个新UIButtonBase对象,内部方法        
        /// </summary>
        /// <param name="engine">引擎</param>
        /// <param name="setScene">此控件所属的scene</param>
        /// <param name="setPosition">2D屏幕位置</param>
        /// <param name="setTextureName">包含路径的背景纹理的名称</param>
        /// <param name="setSize">2D屏幕大小</param>
        /// <param name="setText">按钮上的文字</param>
        /// <param name="setFont">字体</param>
        /// <param name="setTextColor">普通状态的文字颜色</param>
        /// <param name="setTextColorHighlight">高亮状态的文字颜色</param>        
        /// <param name="setRectOrigin">普通状态图像源矩形</param>
        /// <param name="setRectHighlight">高亮状态的图像源矩形</param>        
        /// <param name="setRectClicked">鼠标点击状态的图像源矩形</param>        
        internal UIButtonBase(StunXnaGE engine, Scene setScene, Vector2 setPosition, string setTextureName, Vector2 setSize, string setText, SpriteFont setFont, Color setTextColor, Color setTextColorHighlight, Rectangle setRectOrigin, Rectangle setRectHighlight)
            : base(engine, setScene, setPosition, setTextureName)
        {
            this.size = setSize;
            this.text = setText;
            this.font = setFont;            
            this.textColor = setTextColor;
            this.textColorHighlight = setTextColorHighlight;
            this.rectSourceOrigin = setRectOrigin;
            this.rectSourceHighlight = setRectHighlight;            
        }

        属性        
    }
}

在UISceneNode类的基础上,UIButton类添加了一个文字对齐方式的textAlign变量和表示鼠标点击时背景图片矩形的rectSourceClicked变量,当然还包括绘制按钮的具体代码:

namespace StunEngine.SceneNodes.UI
{
    /// <summary>
    /// 文字对齐方式枚举
    /// </summary>
    public enum TextAlign
    {
        MiddleLeft,
        MiddleCenter
    }
    
    /// <summary>
    /// 按钮控件
    /// </summary>
    public class UIButton:UIButtonBase 
    {
        成员变量和构造函数

        属性

        /// <summary>
        /// 绘制按钮
        /// </summary>
        public override void Draw(GameTime gameTime,bool useReflection)
        {
            if (isVisible)
            {
                //如果按钮背景图片不为null,则绘制背景
                if (ColorTextureName != null)
                {
                    //获取图像淡入淡出的透明颜色
                    alphaTextureColor = new Color(color, scene.TransitionAlpha);
                    
                    //根据鼠标移入还是按下设置图像源矩阵
                    Rectangle rect = isMouseInside || (uiManager.ActiveControl == this) ? ((Input.MouseLeftButtonPressed&&isMouseInside)?rectSourceClicked: rectSourceHighlight): rectSourceOrigin;
                    //绘制按钮背景图像
                    StunXnaGE.SpriteBatch.Draw(material.Textures[0], new Rectangle((int)position.X, (int)position.Y, (int)size.X, (int)size.Y), rect, alphaTextureColor, rotation, origin, spriteEffect, layerDepth);
                }

                //如果按钮文字不为null,则绘制文字
                if (text != null)
                {
                    Color textColor = isMouseInside || (uiManager.ActiveControl == this) ? textColorHighlight : TextColor;
                    // 设置淡入淡出的透明度
                    Color alphaTextColor = new Color(textColor.R, textColor.G, textColor.B, scene.TransitionAlpha);

                    //如果文字的对齐方式为MiddleCenter
                    if (textAlign == TextAlign.MiddleCenter)
                        StunXnaGE.SpriteBatch.DrawString(font, text, new Vector2(position.X + size.X / 2 - font.MeasureString(text).X / 2, position.Y + size.Y / 2 - font.MeasureString(text).Y / 2), alphaTextColor, rotation, origin, 1.0f, spriteEffect, layerDepth);
                    //如果文字的对齐方式为MiddleLeft
                    else if (textAlign == TextAlign.MiddleLeft)
                        StunXnaGE.SpriteBatch.DrawString(font, text, new Vector2(position.X, position.Y + size.Y / 2 - font.MeasureString(text).Y / 2), alphaTextColor, rotation, origin, 1.0f, spriteEffect, layerDepth);
                }
            }            
        }

        单元测试
    }
}

运行单元测试的程序截图如下:

程序截图


发布时间:2010/1/18 8:50:28  阅读次数:7073

2006 - 2024,推荐分辨率1024*768以上,推荐浏览器Chrome、Edge等现代浏览器,截止2021年12月5日的访问次数:1872万9823 站长邮箱

沪ICP备18037240号-1

沪公网安备 31011002002865号