History: Simple text in MOGRE
Source of version: 5
Copy to clipboard
Here is a snippet how to show a simple text with ((MOGRE|Mogre)).
{maketoc}
Discussion thread: [http://www.ogre3d.org/addonforums/viewtopic.php?f=8&t=11839|here]
!How to use it
in the SceneCreation stuff :
{CODE(wrap="1", colors="c#")}
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();
{CODE}
!The class
All needed code is included to this class.
Copy and paste this to a *.cs file.
{CODE(wrap="1", colors="c#")}
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);
OverlayManager.Singleton.DestroyOverlayElement(text.TextName);
}
public void RemoveText(string textName)
{
_OverlayPanel.RemoveChild(textName);
OverlayManager.Singleton.DestroyOverlayElement(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";
}
}
{CODE}
!!See also
* ((MOGRE MovableText by Billboards)) - shows text, clamped to a SceneNode
* ((MOGRE MovableText)) - shows text, clamped to a {LEX()}SceneNode{LEX}
* ((MovableTextOverlay)) - shows text, clamped to a SceneNode
* ((Easy Debug Text for MOGRE)) - shows a text, which can be updated
* ((SpriteManager2d)) | ((MOGRE SpriteManager2d)) - code snippet
* ((OgreSprites)) - similar, but without use of ''Billboard''
* ((ManualObject 2D))
* {LEX()}Overlay{LEX}
* {LEX()}Billboard{LEX}
* {LEX()}GUI{LEX} - several gui systems