Simple text in MOGRE        
Print

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

Table of contents

Discussion thread: here(external link)

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();

 

The class

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

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";
   }
}

 

See also

 


Contributors to this page: materialDefender252 points  , jacmoe133512 points  and Beauty10198 points  .
Page last modified on Wednesday 04 of April, 2012 21:14:31 UTC by materialDefender252 points .


The content on this page is licensed under the terms of the Creative Commons Attribution-ShareAlike License.
As an exception, any source code contributed within the content is released into the Public Domain.