Click here to Skip to main content
15,881,281 members
Articles / Mobile Apps / Windows Phone 7

Using Label Class to Display Text in XNA, WPXNA (7)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
14 Jul 2013CPOL2 min read 14.2K   4   1
Using Label class to display text in XNA

The original post can be found here.

Introduction/Catalog

I have developed some games on Windows Phone. Here, I'll share my experiences and gradually upload some classes, no good name, I just call it WPXNA. (Some example code may not be stringent enough.)

  • Label
  • Example

Label

In the game, we need some labels to display information to the user, such as the name of player, scores and so on. At this time, you can use the Label class which inherits from the Making. Here are some fields and properties of Label:

C#
private float blink;
internal float Alpha = 1;
internal readonly float Rotation;

internal string Text;
internal float FontScale;
protected SpriteFont font;
protected Color color;

protected Vector2 location;
public Vector2 Location
{
 get { return this.location; }
 set { this.location = value; }
}

Field Alpha represents transparency of the label, 1 is opaque, 0 is completely transparent. Value of blink should be between -1 and 0, if blink is not 0, the label will blink.

Field Rotation is angle of the label, the property Location indicates location of the label.

Field Text represents the text, field FontScale represents the scale of the font, field font represents text font, color represents the color of the text.

C#
internal Label ( string name, string resourceName, string text, Vector2 location, 
int width, int height, float fontScale, Color color, float blink, float alpha, int angle )
 : base ( name, resourceName )
{

 if ( null == text )
  throw new ArgumentNullException ( "text", "text can't be null" );

 if ( width > 0 )
  this.Width = width;

 if ( height > 0 )
  this.Height = height;

 this.Text = text;
 this.location = location;
 this.FontScale = fontScale <= 0 ? 1 : fontScale;
 this.color = color;
 this.blink = blink;
 this.Alpha = alpha < 0 || alpha > 1 ? 1 : alpha;
 this.Rotation = Calculator.Radian ( angle );
}

In the constructor of the Label, in addition to the above-mentioned fields, parameter name is the name of label, resourceName parameter is the font resource that used by the label, parameters width and height for the size of the label and can be ignored. You can call the method InitSize to get the size of the label.

C#
internal static void InitSize ( Label label, bool isForce )
{

 if ( null == label )
  return;

 if ( label.Width == 0 || isForce )
  label.Width = ( int ) ( label.font.MeasureString ( label.Text ).X * label.FontScale );

 if ( label.Height == 0 || isForce )
  label.Height = ( int ) ( label.font.LineSpacing * label.FontScale );

}

You can use the Draw method to draw the label.

C#
internal static void Draw ( Label label, SpriteBatch batch )
{

 if ( !label.isVisible )
  return;

 Color color = label.color;

 if ( label.blink != 0 )
 {
  label.Alpha += label.blink;

  if ( label.Alpha <= 0.5 || label.Alpha >= 1 )
   label.blink = -label.blink;

 }

 if ( label.Alpha != 1 )
  color = color * label.Alpha;

 batch.DrawString ( label.font, label.Text, label.location * World.Scale, 
 color, label.Rotation, Vector2.Zero, label.FontScale * ( label.Rotation == 0 ? 
 World.Scale : World.FlipScale ), SpriteEffects.None, 0 );
}

In the Draw method, we will adjust the Alpha field according to the field blink, and transparency will be between 0.5 and 1.

Example

First of all, we need to use the ResourceManager to manage resources, in addition, we defined two labels.

C#
private readonly ResourceManager resourceManager;
private readonly Label label1;
private readonly Label label2;

In the constructor, we initialize the ResourceManager and Label, ResourceManager will contain a font resource that is included in the resource project, and the font resource is named peg.

Now, we created two labels. The first label is light green, font scaling for twice times as large, the second label is vertical, and can blink.

C#
public World ( Color backgroundColor )
 : base ( )
{
 // ...

 this.resourceManager = new ResourceManager ( new Resource[] {
  new Resource ( "peg", ResourceType.Font, @"font\myfont" )
 } );
 this.resourceManager.World = this;

 this.label1 = new Label ( "l1", 
 "Hello windows phone!", 2f, Color.LightGreen, 0f );
 this.label2 = new Label ( "l2", "peg", 
 "Nothing!", new Vector2 ( 50, 300 ), 0, 0, 1f, Color.White, -0.01f, 1f, -90 );
}

After the page loaded, we will load the required resources.

C#
protected override void OnNavigatedTo ( NavigationEventArgs e )
{
 // ...

 this.resourceManager.LoadContent ( );
 this.label1.InitResource ( this.resourceManager );
 this.label2.InitResource ( this.resourceManager );

 base.OnNavigatedTo ( e );
}

In the OnUpdate method, we let the second label show the game time.

C#
private void OnUpdate ( object sender, GameTimerEventArgs e )
{
 this.label2.Text = e.TotalTime.ToString ( );
}

In the OnDraw method, we used the Draw method to draw the two labels.

C#
private void OnDraw ( object sender, GameTimerEventArgs e )
{
 // ...

 this.spiritBatch.Begin ( );
 Label.Draw ( this.label1, this.spiritBatch );
 Label.Draw ( this.label2, this.spiritBatch );
 this.spiritBatch.End ( );
}

Get the code from here, for more contents, please visit WPXNA.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Amir Mohammad Nasrollahi25-Jul-13 20:53
professionalAmir Mohammad Nasrollahi25-Jul-13 20:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.