Skip to main content

History: Simple text in MOGRE

Preview of version: 6

Here is a snippet how to show a simple text with Mogre.

Discussion thread: here

How to use it


in the SceneCreation stuff :

Copy to clipboard
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();


The class


All needed code is included to this class.
Copy and paste this to a *.cs file.

You can get the fonts here:
Liberation Sans
DaFont: Liberation Mono

Or alter the code to use any other truetype fonts.

Copy to clipboard
using System; using System.Collections.Generic; using System.Text; using Mogre; namespace Ogre.Modules { public class TextManager { OverlayContainer _OverlayPanel; Overlay _TextOverlay; private uint textCounter; public TextManager() { this.Initalize(); } private void Initalize() { // Create the font resources // add the path to your resources.cfg // for example: // Default | ../../../Media/fonts | ../../../Media/fonts.zip Load("LiberationSans-Regular.ttf", Font.Default, 26); Load("LiberationSans-Bold.ttf", Font.DefaultBold, 28); Load("LiberationSans-Italic.ttf", Font.DefaultItalic, 26); Load("LiberationMono-Regular.ttf", Font.Console, 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(); } //returns a texthandle 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); } } 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.White; } 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 string Caption { set { _TextArea.Caption = value; } get { return _TextArea.Caption; } } 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 DefaultItalic = "FONT.DEFAULT.ITALIC"; public const string Console = "FONT.CONSOLE"; } }

See also

History

Information Version
Thu 06 of Dec, 2012 18:55 GMT-0000 fargerio 9
Thu 06 of Dec, 2012 18:35 GMT-0000 fargerio 8
Thu 06 of Dec, 2012 18:31 GMT-0000 fargerio 7
Thu 06 of Dec, 2012 18:13 GMT-0000 fargerio different AddText and RemoveText functions, changed the fonts to free fonts. 6
Wed 04 of Apr, 2012 21:14 GMT-0000 materialDefender Fixed memory leak (RemoveText not destroying OverlayElement instances) 5
Sat 05 of Jun, 2010 16:31 GMT-0000 jacmoe 4
Sat 22 of May, 2010 17:27 GMT-0000 jacmoe 3
Sat 22 of May, 2010 17:26 GMT-0000 jacmoe 2
Sat 09 of Jan, 2010 13:17 GMT-0000 Beauty NEW PAGE (code snippet) 1