Click here to Skip to main content
12,402,889 members (74,501 online)
Click here to Skip to main content
Add your own
alternative version

Stats

9.8K views
4 bookmarked
Posted

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

, 14 Jul 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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.

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.

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.

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.

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.

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.

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.

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.

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)

Share

About the Author

zoyobar
United States United States
No Biography provided

You may also be interested in...

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 15 Jul 2013
Article Copyright 2013 by zoyobar
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid