IT tutorials
 
Mobile
 

Windows Phone 7 : Designing the Game Framework (part 2) - The TextObject Class

2/25/2013 6:27:30 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

3. The TextObject Class

So we have a simple way of representing a sprite, but it would be very useful to have a corresponding mechanism for representing text. We achieve this by creating the TextObject class. Because text shares many of the same features as sprites (a position, origin, color, rotation, scaling, and more) we derive TextObject from SpriteObject as shown in Figure 2, allowing us to take advantage of all the SpriteObject properties.

Figure 2. The TextObject position in the framework project

The constructors available to this class are more text-oriented and are shown in Listing 6.

Example 6. The available constructors for the TextObject class
public TextObject(Game game)

    public TextObject(Game game, Vector2 position)

    public TextObject(Game game, Vector2 position, SpriteFont font)

    public TextObject(Game game, Vector2 position, SpriteFont font, String text)

    public TextObject(Game game, Vector2 position, SpriteFont font, String text,
                        TextAlignment horizontalAlignment, TextAlignment verticalAlignment)

					  

All the SpriteObject properties are relevant to TextObject except for SpriteTexture (we need a font instead of a texture) and SourceRect (which has no relevance to rendering text). We will ignore these, and instead add a couple of new properties of our own:

  • Font stores a reference to a SpriteFont object that will be used to render the text.

  • Text stores a text string to be displayed.

  • HorizontalAlignment and VerticalAlignment offer the creator of the object a simple way of automatically aligning the text around its position.

All four of these properties are backed by a private class variable rather than being autoimplemented using the { get; set; } syntax. This allows us to hook into their set code and perform some additional processing relating to text alignment, an example of which is shown in Listing 7.

Example 7. The implementation of the Text property
public String Text
    {
        get { return _text; }
        set
        {
            // Has the text changed from whatever we already have stored?
            if (_text != value)
            {
                // Yes, so store the new text and recalculate the origin if needed
                _text = value;
                CalculateAlignmentOrigin();
            }
        }
    }

					  

In each of the properties, if the code detects that a changed value has been provided, it calls into a function named CalculateAlignmentOrigin. This function examines the content of the HorizontalAlignment and VerticalAlignment properties, and if either is set to a value other than Manual, it automatically calculates a new Origin coordinate by calling the SpriteFont.MeasureString function . This allows the object creator to instruct the text to be left- or right-aligned, or for it to be centered (and the same options are available for vertical alignment, too). With the alignment properties set, this alignment will continue to be automatically applied whenever an update to the object is made. Alternatively, either or both of the alignment properties can be set to Manual (which is also their default), in which case the Origin coordinate can be set explicitly by the game.

Next we have the Draw method. TextObject overrides it and replaces the SpriteObject implementation completely, rendering text rather than a texture. The implementation is very simple and is shown in Listing 8.

Example 8. The TextObject Draw method
public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
    {
        // Do we have a font? And some text? If not then there is nothing to draw...
        if (Font != null && Text != null && Text.Length > 0)
        {
            // Draw the text
            spriteBatch.DrawString(Font, Text, Position, SpriteColor, Angle, Origin, Scale,
                                                        SpriteEffects.None, LayerDepth);
        }
    }

					  

Finally, the class overrides the BoundingBox function to return a box calculated from the measured text size instead of from the texture size (because we don't use a texture in the TextObject class).

Just as with the SpriteObject class, the TextObject class gives us an easy-to-use mechanism for representing all the possible properties of a piece of text that is to be displayed within a game. Game-specific classes can derive from TextObject if they want to perform their own custom processing (they might override the Update method, for example, to change the content or positioning of the text), or alternatively it might be directly instantiated if nothing else is required for display.

 
Others
 
- Windows Phone 7 : Designing the Game Framework (part 1) - The GameObjectBase Class, The SpriteObject Class
- Windows Phone 7 : Getting Started with XNA - Other Graphics Options
- iPad : Your Calendar - Adding New Calendar Appointments, Events (part 2)
- iPad : Your Calendar - Adding New Calendar Appointments, Events (part 1)
- Java ME on Symbian OS : Handling Diversity - Using Adaptive Code and Flexible Design to Handle Diversity
- Java ME on Symbian OS : Handling Diversity - Detecting Diversity using Properties
- BlackBerry Bold 9700 and 9650 Series : Email Set Up - Sync Google Contacts Using Email Setup
- BlackBerry Bold 9700 and 9650 Series : Email Set Up - Maintaining Your Email Accounts
- iPhone Programming : Other Native Platforms - MonoTouch
- iPhone Programming : Other Native Platforms - PhoneGap
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us