帮酷LOGO
0 0 评论
  • 显示原文与译文双语对照的内容
文章标签:BOX  XNA  notification  


介绍

在互联网上找到代码,让你轻松地显示通知框,以及你想要的大小和大小是令人惊讶的。 现在,我已经生成了一个类,它可以解决所有这些问题。

使用代码

这个类很简单,使用它,你只需要改变一些小的东西。 这些是:

  • FontLocation - 用于绘制文本的spriteFont的位置
  • DisplayedText - 简单地做'DisplayedText ='在框中添加一行文本

我的代码被写入,以便通知框显示为一个时间长度,然后退出直到它透明。 添加更多文本时再次显示。 代码自动将文本拆分为行,这样就不会在指定的rectangle 边缘溢出。 执行'DisplayedText ='时,代码将它的添加到当前文本的开头,将文本拆分为行,然后显示。 代码最难的部分将文本拆分成线条,否则将会有一串无限的文本在通知框之外。 我使用的代码部分来自MSDN网站,但修改得更灵活,因此它实现了我真正想要的。 最后的代码如下所示:

privatestring parseText(string SomeText)
{
 //Create a string to return that is emptyString returnString = String.Empty;
 //Create a list of lines that are already in the text //so that they remain separated String[] ExistingLines = SomeText.Split("n".ToCharArray());
 //Create a list to contain the new lines of text. List<String> Lines = new List<String>();
 //For every existing line, check its length and split it up if it's too long.foreach (String ALine in ExistingLines)
 {
 //Current line that is being split upString line = String.Empty;
 //Lines that this existing line has been split up into List<String> CLines = new List<String>();
 //Words in this existing line String[] wordArray = ALine.Split(' ');
 //For each word, check if it will fit on the current line, //if not create a new line and add the old one to the CLines object.foreach (String word in wordArray)
 {
 //Check to see if word will fit on current line.if (TheFont.MeasureString(line + word + "").Length() > 
 SurroundingBox.Width)
 {
 //If not, add the line to CLines with a new line  character on the end to make sure that the text is 
 split when it is drawn.
 CLines.Add(line + "n");
 //Reset the current line to blank. line = String.Empty;
 }
 //Add the word to the current line. line = line + word + ' ';
 }
 //Add the current line to Clines as it won't already have been added. CLines.Add(line + "n");
 //For every line this existing line has been split up into, add it to the final set of lines.
 foreach (string TheLine in CLines)
 {
 Lines.Add(TheLine);
 }
 }
 //Remove the first line until the number of lines is less than the maximum.while(Lines.Count > MaxLines)
 {
 Lines.RemoveAt(0);
 }
 //Add the final set of lines to the return string.for (int i = 0; i < Lines.Count; i++)
 {
 returnString += Lines[i];
 }
 return returnString;
}

如你所见,它查看已经传递的文本,将它的拆分成现有行,看看是否有太长的时间。 如果是,代码将分割它们,并添加新行代替原始行。 最后,它将所有这些行连接到一个 string 中并返回 string

就是这样除了调用画。更新和调整大小函数,你需要做的就是给框一些文本。

Points of Interest

我发现它既有趣又令人厌烦,虽然 spriteBatch.DrawString 函数识别了转义字符 n,但在框中没有绘制 string

历史记录

  • 28th 2010年08月: 初始帖子


文章标签:notification  BOX  XNA  

Copyright © 2011 HelpLib All rights reserved.    知识分享协议 京ICP备05059198号-3  |  如果智培  |  酷兔英语