Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

Canvas size for text output is too small.

Skilled Illusionist
Joined
Apr 19, 2008
Messages
305
Reaction score
20
/title

Hey there peeps, the title says it all.

My text size is too big for it's canvas. How can i fix this?

Code snippet:

Textboard base

Code:
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);

    }

}

What i'm trying to put in (Ignore the sprite / just press 1 to...)

Code:
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()
    {    
    }
}

Image of what the actual result is:



Thanks in advance :)

Joël

Edit: The GXPEngine is provided by my school, for this project obviously.
 
[emoji848]
Legend
Joined
Dec 3, 2011
Messages
2,232
Reaction score
1,518
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.
 
Skilled Illusionist
Joined
Apr 19, 2008
Messages
305
Reaction score
20
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.

Thanks alot Future, i'll try this asap. :)
 
Back
Top