12.复选框UICheckBox类
再次声明:以下所有控件的图片素材来自于NeoForce Controls(http://www.tomshane.cz/neoforce/default.aspx),好像它没有公开源代码,所以代码参考自xWinForms 3.0(http://sourceforge.net/projects/xwinforms/)。
CheckBox和Button的区别在于:
- CheckBox的图像绘制在文字的左边而不是下面。
- CheckBox点击后切换的图像不会随鼠标移出发生变动。
- 有一个checked变量表示复选框是否被选中,还要添加一个事件:CheckedChanged,当选中项发生改变时引发。
- 有一个canUncheck变量表示复选框是否可以不被选中,这主要是用于下一个教程中的单选框控件,因为很多情况下单选框是不可以通过点击它取消选中状态的。
控件使用的图像如下:
以下是复选框的代码:
namespace StunEngine.SceneNodes.UI
...{
/**//// <summary>
/// 复选框控件
/// </summary>
public class UICheckBox:UIButtonBase
...{
成员变量和构造函数#region 成员变量和构造函数
//默认值
private static readonly Vector2 DefaultCheckBoxImageSize = new Vector2(16, 16);
private static readonly Rectangle DefaultRectSource = new Rectangle(0, 0, 13, 13);
private static readonly Rectangle DefaultRectHighlight = new Rectangle(13, 0, 13, 13);
private static readonly Rectangle DefaulRectChecked = new Rectangle(0, 39, 13, 13);
private static readonly Rectangle DefaulRectHighlightChecked = new Rectangle(13, 39, 13, 13);
private static readonly Color DefaultTextColor = Color.White;
private static readonly Color DefaultTextHighlight = Color.Red;
/**//// <summary>
/// 文字与左侧图像框的水平距离,默认为5.
/// </summary>
protected float textOffsetX = 5.0f;
/**//// <summary>
/// 复选框选中时的纹理的源矩形
/// </summary>
protected Rectangle rectSourceChecked;
/**//// <summary>
/// 复选框选中时的高亮状态的纹理源矩形
/// </summary>
protected Rectangle rectSourceHighlightChecked;
/**//// <summary>
/// 复选框是否被选中
/// </summary>
protected bool _checked = false;
/**//// <summary>
/// 复选框是否可以被Uncheck
/// </summary>
protected bool canUncheck = true;
/**//// <summary>
/// 左侧图像的大小
/// </summary>
private Vector2 imageSize;
/**//// <summary>
/// 创建一个新UICheckBox对象,默认文字为"CheckBox",默认文字颜色为白色。
/// </summary>
/// <param name="engine">引擎</param>
/// <param name="setScene">所属的Scene</param>
public UICheckBox(StunXnaGE engine, Scene setScene)
: this(engine, setScene, Vector2.Zero, "Textures/UI/UICheckBox", DefaultCheckBoxImageSize, "CheckBox", engine.DefaultFont, DefaultTextColor, DefaultTextHighlight, DefaultRectSource, DefaultRectHighlight, DefaulRectChecked, DefaulRectHighlightChecked)
...{
}
/**//// <summary>
/// 主要用于RadioButton继承
/// </summary>
/// <param name="engine">引擎</param>
/// <param name="setScene">选择框所属的scene</param>
/// <param name="setText">选择框上的文字</param>
/// <param name="setTextureName">选择框所使用的纹理名称</param>
internal UICheckBox(StunXnaGE engine, Scene setScene,string setText,string setTextureName)
: this(engine, setScene, Vector2.Zero, setTextureName, DefaultCheckBoxImageSize, setText, engine.DefaultFont, DefaultTextColor, DefaultTextHighlight, DefaultRectSource, DefaultRectHighlight, DefaulRectChecked, DefaulRectHighlightChecked)
...{ }
/**//// <summary>
/// 创建一个新UICheckBox对象,内部方法,只能在引擎内部调用。
/// </summary>
/// <param name="engine">引擎</param>
/// <param name="setScene">所属的Scene</param>
/// <param name="setPosition">文字的2D屏幕位置</param>
/// <param name="setTextureName">选择框所使用的纹理名称</param>
/// <param name="setText">文字</param>
/// <param name="setSize">选择框左侧的图像大小</param>
/// <param name="setFont">字体</param>
/// <param name="setTextColor">文字普通状态的颜色</param>
/// <param name="setTextColorHighlight">文字高亮状态的颜色</param>
/// <param name="setRectOrigin">普通状态的纹理矩形</param>
/// <param name="setRectHighlight">高亮状态的纹理矩形</param>
/// <param name="setRectChecked">选中时普通状态的纹理矩形</param>
/// <param name="setRectHighlightChecked">选中时高亮状态的纹理矩形</param>
internal UICheckBox(StunXnaGE engine, Scene setScene, Vector2 setPosition, string setTextureName, Vector2 setSize, string setText, SpriteFont setFont, Color setTextColor, Color setTextColorHighlight, Rectangle setRectOrigin, Rectangle setRectHighlight, Rectangle setRectChecked, Rectangle setRectHighlightChecked)
: base(engine, setScene, setPosition, setTextureName, setSize, setText, setFont, setTextColor, setTextColorHighlight, setRectOrigin, setRectHighlight)
...{
this.rectSourceChecked = setRectChecked;
this.rectSourceHighlightChecked = setRectHighlightChecked;
imageSize = setSize;
if (setText != null)
this.size = new Vector2(setSize.X + textOffsetX + font.MeasureString(setText).X, font.MeasureString(setText).Y > setSize.Y ? font.MeasureString(setText).Y : setSize.Y);
else
this.size = imageSize;
}
#endregion
属性#region 属性
/**//// <summary>
/// 获取或设置控件的文字
/// </summary>
public override string Text
...{
get
...{
return base.Text;
}
set
...{
base.Text = value;
//改变文字后需要重新设置复选框的大小
this.size = new Vector2(imageSize.X + textOffsetX + font.MeasureString(text).X, font.MeasureString(text).Y > size.Y ? font.MeasureString(text).Y : size.Y);
}
}
/**//// <summary>
/// 获取或设置文字与图像框的水平距离
/// </summary>
public float TextOffset
...{
get ...{ return textOffsetX; }
set ...{ textOffsetX = value; }
}
/**//// <summary>
/// 获取或设置控件选中时的纹理的源矩形
/// </summary>
public Rectangle RectSourceChecked
...{
get ...{ return rectSourceChecked; }
set ...{ rectSourceChecked = value; }
}
/**//// <summary>
/// 获取或设置控件选中时的高亮状态的纹理源矩形
/// </summary>
public Rectangle RectSourceHighlightChecked
...{
get ...{ return rectSourceHighlightChecked; }
set ...{ rectSourceHighlightChecked = value; }
}
/**//// <summary>
/// 获取或设置控件是否被选中
/// </summary>
public bool Checked ...{ get ...{ return _checked; } set ...{ _checked = value; } }
/**//// <summary>
/// 获取或设置字体,字体改变时复选框的大小也会发生改变
/// </summary>
public override SpriteFont Font
...{
get
...{
return base.Font;
}
set
...{
base.Font = value;
//如果复选框包含文字,则复选框的大小长为imageSize.X + textOffsetX + value.MeasureString(text).X,高为value.MeasureString(text).Y和imageSize.Y中的大的那个
if (text != null)
size = new Vector2(imageSize.X + textOffsetX + value.MeasureString(text).X, value.MeasureString(text).Y > imageSize.Y ? font.MeasureString(text).Y : imageSize.Y);
//如果复选框不包含文字,则它的大小就是左侧图像框的大小
else
this.size = imageSize;
}
}
/**//// <summary>
/// 获取或设置复选框是否可以被Uncheck
/// </summary>
public bool CanUncheck ...{ get ...{ return canUncheck; } set ...{ canUncheck = value; } }
#endregion
事件#region 事件
/**//// <summary>
/// 当选中项改变时发生
/// </summary>
public event EventHandler CheckedChanged;
/**//// <summary>
/// 引发CheckedChanged事件。
/// OnCheckedChanged方法允许继承的类处理这个事件。
/// 注意:当在继承类中覆写OnCheckedChanged时,一定要调用基类的OnCheckedChanged方法才可以注册委托
/// </summary>
/// <param name="e"></param>
public virtual void OnCheckedChanged(EventArgs e)
...{
if (_checked && canUncheck)
_checked = false;
else if (!_checked)
_checked = true;
if (CheckedChanged != null)
...{
CheckedChanged(this, e);
}
}
#endregion
/**//// <summary>
/// 绘制复选框
/// </summary>
/// <param name="gameTime"></param>
public override void Draw(GameTime gameTime,bool useReflection)
...{
if (isVisible == true)
...{
//如果文字不为null,则绘制文字
if (text != null)
...{
Color textColor = IsMouseInside || (uiManager.ActiveControl == this) ? textColorHighlight : TextColor;
// 设置文本的淡入淡出的透明度
Color alphaTextColor = new Color(textColor.R, textColor.G, textColor.B, scene.TransitionAlpha);
//绘制文本
StunXnaGE.SpriteBatch.DrawString(font, text, new Vector2(position.X+imageSize .X+textOffsetX , position.Y + size.Y / 2 - font.MeasureString(text).Y / 2), alphaTextColor, rotation, origin, 1.0f, spriteEffect, layerDepth);
}
//获取图像淡入淡出的透明颜色
alphaTextureColor = new Color(color, scene.TransitionAlpha);
if (!_checked)
...{
//根据鼠标移入还是按下设置图像源矩阵
Rectangle rect = IsMouseInside || (uiManager.ActiveControl == this) ? rectSourceHighlight: rectSourceOrigin;
//绘制按钮背景图像
StunXnaGE.SpriteBatch.Draw(material.Textures[0], new Rectangle((int)position.X, (int)(position.Y + size.Y / 2-imageSize.Y /2), (int)imageSize.X, (int)imageSize.Y), rect, alphaTextureColor, rotation, origin, spriteEffect, layerDepth);
}
else
...{
//根据鼠标移入还是按下设置图像源矩阵
Rectangle rect = IsMouseInside || (uiManager.ActiveControl == this) ? rectSourceHighlightChecked : rectSourceChecked;
//绘制按钮背景图像
StunXnaGE.SpriteBatch.Draw(material.Textures[0], new Rectangle((int)position.X, (int)(position.Y + size.Y / 2 - imageSize.Y / 2), (int)imageSize.X, (int)imageSize.Y), rect, alphaTextureColor, rotation, origin, spriteEffect, layerDepth);
}
//鼠标点击切换选中状态
if (IsMouseInside && Input.MouseLeftButtonJustPressed)
...{
this.OnCheckedChanged(EventArgs .Empty );
}
}
}
单元测试#region 单元测试
#if DEBUG
/**//// <summary>
/// 测试UICheck类
/// </summary>
public static void TestUICheckBox()
...{
UICheckBox chk1=null;
UICheckBox chk2=null;
//显示相关信息
UILabel lblMessage=null;
TestGame.Start("测试UICheckBox类",
delegate
...{
chk1 = new UICheckBox(TestGame.engine, TestGame.scene);
chk1.Position = new Vector2(100, 100);
chk1.Text = "First";
//将按钮颜色调整为灰色
chk1.Color = Color.DimGray;
TestGame.scene.AddNode(chk1);
chk2 = new UICheckBox(TestGame.engine, TestGame.scene);
chk2.Position = new Vector2(100, 150);
chk2.Text = "Second";
//将文字颜色和文字高亮颜色分别设置为绿色和蓝色
chk2.TextColor = Color.Green;
chk2.TextColorHighlight = Color.Blue ;
TestGame.scene.AddNode(chk2);
lblMessage = new UILabel(TestGame.engine, TestGame.scene);
lblMessage.Position = new Vector2(100, 200);
TestGame.scene.AddNode(lblMessage);
//关闭相机控制器
TestGame.scene.fpsCamCtrl.Enabled = false;
},
delegate
...{
lblMessage.Text = "You Select CheckBox:"+((UICheckBox)TestGame.scene.UiManager.ActiveControl).Text+",Checked:"+((UICheckBox)TestGame.scene.UiManager.ActiveControl).Checked ;
}
);
}
#endif
#endregion
}
}
单元测试截图如下:
发布时间:2010/1/18 上午11:38:57 阅读次数:7674