Simple text in MOGRE

TaylorMouse

29-12-2009 09:01:33

Hi,

I've been breaking my head for a couple of hours now and don't seem to find anything as of "out of the box" to display some siple text on screen.

In XNA it is

Text text = new Text();
text.Text = "Hello World !";
text.Draw();

In other 3D engine, it is actually the same ( more or less ) but nothing in MOGRE ??

Any ideas ?


I already tried the Overlay and TextOverlay stuff, but that is just not it !! --> BAD ME, this is IT ! 8)

T.

TaylorMouse

29-12-2009 14:29:17

YES !! I found it - after a lot of searching and googling and stuff ( I found it in the OGRE section, so I needed to convert it to c# !! )

make sure you have the following :

a folder structure for your font stuff :
Media\fonts\consola.ttf

in your code ( you can copy paste it to the SceneCreating ( OgreWindow win ) method


// Name the font we want to use
string fontFileName = @"consola.ttf";

// Create the font resource
ResourceGroupManager.Singleton.AddResourceLocation("Media/fonts", "FileSystem");
ResourcePtr font = FontManager.Singleton.Create("MyFont", ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME);
font.SetParameter("type", "truetype");
font.SetParameter("source", fontFileName);
font.SetParameter("size", "26");
font.SetParameter("resolution", "96");
font.Load();

// Create the overlay panel
OverlayContainer panel = OverlayManager.Singleton.CreateOverlayElement("Panel", "OverlayPanelName") as OverlayContainer;
panel.MetricsMode = GuiMetricsMode.GMM_PIXELS;
panel.SetPosition(10, 10);
panel.SetDimensions(300, 120);

// Create the text area
TextAreaOverlayElement textArea = OverlayManager.Singleton.CreateOverlayElement("TextArea", "TextAreaName") as TextAreaOverlayElement;
textArea.MetricsMode = GuiMetricsMode.GMM_PIXELS;
textArea.SetPosition(0, 0);
textArea.SetDimensions(300, 120);
textArea.CharHeight = 26;
textArea.FontName = "MyFont";
textArea.Caption = "Hello World !\nMerry X Mas !!";
textArea.ColourBottom = ColourValue.Black;
textArea.ColourTop = ColourValue.Blue;
// Create the Overlay to link all
Overlay textOverlay = OverlayManager.Singleton.Create("The Text Overlay");
textOverlay.Add2D(panel);
panel.AddChild(textArea);

textOverlay.Show();


T.

TaylorMouse

29-12-2009 16:14:01

I made a little class for this :

using System;
using System.Collections.Generic;
using System.Text;
using Mogre;

namespace AdditionalClasses
{
public class TextManager
{
OverlayContainer _OverlayPanel;
Overlay _TextOverlay;

public TextManager()
{
this.Initalize();

}

private void Initalize()
{
// Create the font resources
ResourceGroupManager.Singleton.AddResourceLocation("Media/fonts", "FileSystem");
Load("Default.ttf", Font.Default, 26);
Load("DefaultBold.ttf", Font.DefaultBold, 28);
Load("Torchlight.ttf", Font.Torchlight, 36);
Load("Consolas.ttf", Font.Consolas, 26);

// Create the overlay panel
_OverlayPanel = OverlayManager.Singleton.CreateOverlayElement("Panel", new Guid().ToString()) as OverlayContainer;
_OverlayPanel.MetricsMode = GuiMetricsMode.GMM_PIXELS;
_OverlayPanel.SetPosition(10, 10);
_OverlayPanel.SetDimensions(300, 120);

_TextOverlay = OverlayManager.Singleton.Create(new Guid().ToString());

_TextOverlay.Add2D(_OverlayPanel);

}

private void Load(string fontFileName, string fontRef, int size)
{
ResourcePtr font = FontManager.Singleton.Create(fontRef, ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME);
font.SetParameter("type", "truetype");
font.SetParameter("source", fontFileName);
font.SetParameter("size", size.ToString());
font.SetParameter("resolution", "96");
font.Load();
}

public void HideAllText()
{
_TextOverlay.Hide();
}
public void ShowAllText()
{
_TextOverlay.Show();
}

public void AddText(Text text)
{
_OverlayPanel.AddChild(text._TextArea);
}
public void RemoveText(Text text)
{
_OverlayPanel.RemoveChild(text.TextName);
}
public void RemoveText(string textName)
{
_OverlayPanel.RemoveChild(textName);

}

}

public class Text
{
public TextAreaOverlayElement _TextArea;
public string TextName = Guid.NewGuid().ToString();

public Text(string caption)
{
_TextArea = OverlayManager.Singleton.CreateOverlayElement("TextArea", TextName) as TextAreaOverlayElement;
_TextArea.MetricsMode = GuiMetricsMode.GMM_PIXELS;
_TextArea.SetPosition(0, 0);
_TextArea.SetDimensions(300, 120);
_TextArea.CharHeight = 20;
_TextArea.FontName = Font.Default;
_TextArea.Caption = caption;
_TextArea.Colour = ColourValue.Black;
}

public void Hide()
{
_TextArea.Hide();
}
public void Show()
{
_TextArea.Show();
}

public System.Drawing.Point Position
{
set { _TextArea.SetPosition(value.X, value.Y); }
get { return new System.Drawing.Point((int)_TextArea._getLeft(), (int)_TextArea._getTop()); }
}
public string FontName
{
set { _TextArea.FontName = value; }
get { return _TextArea.FontName; }
}
public float CharacterHeight
{
set { _TextArea.CharHeight = value; }
get { return _TextArea.CharHeight; }
}
public ColourValue Color
{
set { _TextArea.Colour = value; }
get { return _TextArea.Colour; }
}
public ColourValue ColorTop
{
set { _TextArea.ColourTop = value; }
get { return _TextArea.ColourTop; }
}
public ColourValue ColorBottom
{
set { _TextArea.ColourBottom = value; }
get { return _TextArea.ColourBottom; }
}
}

public class Font
{
public const string Default = "FONT.DEFAULT";
public const string DefaultBold = "FONT.DEFAULT.BOLD";
public const string Torchlight = "FONT.TORCHLIGHT";
public const string Consolas = "FONT.CONSOLAS";
}
}


Copy and paste this in a file.cs, how to use it :

in the SceneCreation stuff :


TextManager textManager = new TextManager();
textManager.AddText(new Text("Hello World !") { Position = new System.Drawing.Point(10,10) });
textManager.AddText(new Text("Merry X Mas") { Position = new System.Drawing.Point(10,30) });
textManager.AddText(new Text("And A happy New YEAR !") { Position = new System.Drawing.Point(10, 50) });
textManager.ShowAllText();



T.

Beauty

09-01-2010 11:55:19

Thanks for giving us your code.
I suppose, it's useful for other people.
I will add it to the wiki :wink:

Beauty

09-01-2010 14:40:10

Ok, I created the page in the new wiki:
http://test.ogitor.org/tiki/Simple+text+in+MOGRE

I suppose the URL of the wiki will change in future, but the page name will be the same.
If somebody have improvements for this code, please add it to the wiki page :wink:

andyhebear1

21-05-2010 14:07:10

good

Beauty

22-11-2010 01:23:31

I tried your nice class, but get an exception, because the class needs some fonts in the application resources.
The names are hard coded in the method Initialize().
Default.ttf, DefaultBold.ttf, Torchlight.ttf, Consolas.ttf.

Can you please attach the needed resources to the wiki page?
(When you are logged-in, then on the page bottom is an attach link, which displays an upload form.)
Also for the resources you should write a note for the licences of the fonts.

fargerio

27-11-2012 16:04:15

Thank you, that class is really useful.

fargerio

01-12-2012 15:58:27

Hey,
after using your class for a bit, i noticed that removing Text is not working as expected, because _OverlayPanel.AddChild() uses text._TextArea.Name as handle, and the RemoveText function tries to use text.TextName as handle, which results in an exception.

So i suggest these changes to the TextManager Class:

//returns texthandle to facilitate later removal
public string AddText(Text text)
{
_OverlayPanel.AddChild(text._TextArea);
return text._TextArea.Name;
}
public void RemoveText(Text text)
{
_OverlayPanel.RemoveChild(text._TextArea.Name);
OverlayManager.Singleton.DestroyOverlayElement(text._TextArea.Name);
}
public void RemoveText(string textAreaName)
{
_OverlayPanel.RemoveChild(textAreaName);
OverlayManager.Singleton.DestroyOverlayElement(textAreaName);
}


This way you can simply remember the handle that AddText() returns, and use it to remove the text again later.

Beauty

01-12-2012 21:48:40

Hi Fargerio,

thank you very much for publishing your bug report and improvement. :D

It would be nice, when you add it to the wiki page, too.
Then it's on common place.
http://www.ogre3d.org/tikiwiki/Simple+text+in+MOGRE

You can log-in to the wiki with the username and password of the MAIN forum.
In the case that you aren't registered in the main forum, you need to do it before.

Nice would be if you take a screenshot for the wiki page.
You can send it to me and I'll add it to the wiki. (adding pictures is a little bit tricky)
My mail address is: beautyod at gmx.de
Add the word "Mogre" to the subject of your e-mail. So it shouldn't get lost in the spam folder.