Skilled Illusionist
- Joined
- Apr 19, 2008
- Messages
- 305
- Reaction score
- 20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using GXPEngine;
public class TextBoard : Canvas
{
public TextBoard(int width, int height) : base(width, height)
{
SetText("");
}
public void SetText(string text)
{
//graphics.Clear(Color.Gray);
Font drawFont = new Font("ArialBlack", 15);
graphics.DrawString(text, drawFont, Brushes.Black, 0, 0);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using GXPEngine;
public class MenuDesign : Sprite
{
private TextBoard _Level1;
public MenuDesign(int PosX, int PosY, MyGame gamemanager) : base("StartSc.png")
{
Sprite SaxionLogo = new Sprite("Saxion.png");
AddChild(SaxionLogo);
SaxionLogo.scale = 0.5f;
SaxionLogo.x = ((MyGame.WIDTH / 4) * 3);
SaxionLogo.y = ((MyGame.HEIGHT/ 4) * 3);
this.x = PosX;
this.y = PosY;
_Level1 = new TextBoard(MyGame.GRID_WIDTH*2, MyGame.GRID_HEIGHT);
_Level1.x += (MyGame.WIDTH / 2);
_Level1.y += (MyGame.HEIGHT / 2);
_Level1.x += PosX;
_Level1.y += PosY;
AddChild(_Level1);
_Level1.SetText("Press 1 to start level one.");
}
public void Update()
{
}
}
public void SetText(string text)
{
Font drawFont = new Font("ArialBlack", 15); // should not be instantiated here for performance reasons
// Measure string boundaries
SizeF sSize = graphics.MeasureString(text, drawFont);
// Adjust canvas width if necessary
if (sSize.Width > this.Width)
this.Width = sSize.Width; // or alternative engine call
graphics.DrawString(text, drawFont, Brushes.Black, 0, 0);
}
A blatant answer would be: "Make it larger then"
It really depends on how you plan to use that Canvas. Should it be statically sized? If that's the case, then make it wider so possible texts will fit.
If you want the Canvas to size with the text, you have to calculate the boundaries, of the text to be drawn, in TextBoard.SetText and adjust the Canvas' size on every call. I don't know this engine your school provided, but with typical GDI+ stuff (in C#) you'd call a text length measuring method like Graphics.MeasureString. So your TextBoard.SetText should look something like this:
Code:public void SetText(string text) { Font drawFont = new Font("ArialBlack", 15); // should not be instantiated here for performance reasons // Measure string boundaries SizeF sSize = graphics.MeasureString(text, drawFont); // Adjust canvas width if necessary if (sSize.Width > this.Width) this.Width = sSize.Width; // or alternative engine call graphics.DrawString(text, drawFont, Brushes.Black, 0, 0); }
Note that the code might not work as you want it to, it's just to give you an idea of what you'd need to do. For a school project, this should do.