Beispiel #1
0
        /// <summary>
        /// Static utility method to cut off a string to fit in a text area.
        /// </summary>
        public static void FitCaptionToArea(string caption, ref Mogre.TextAreaOverlayElement area, float maxWidth)
        {
            Mogre.FontPtr font = null;
            if (Mogre.FontManager.Singleton.ResourceExists(area.FontName))
            {
                font = (Mogre.FontPtr)Mogre.FontManager.Singleton.GetByName(area.FontName);
                if (!font.IsLoaded)
                {
                    font.Load();
                }
            }
            else
            {
                OGRE_EXCEPT("this font:", area.FontName, "is not exist");
            }
            Mogre.FontPtr f = font;
            string        s = DisplayStringToString(caption);
            //int nl = s.find('\n');
            //if (nl != string.npos)
            //	s = s.substr(0, nl);
            int nl = s.IndexOf('\n');

            if (nl != -1)
            {
                s = s.Substring(0, nl);
            }

            float width = 0;

            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == ' ' && area.SpaceWidth != 0)
                {
                    width += area.SpaceWidth;
                }
                else
                {
                    width += f.GetGlyphAspectRatio(s[i]) * area.CharHeight;
                }
                if (width > maxWidth)
                {
                    s = s.Substring(0, i);
                    break;
                }
            }

            area.Caption = (s);
        }
Beispiel #2
0
        //public static Vector2 cursorOffset(Mogre.OverlayContainer containerElement,Vector2 cursorPos)
        //{
        //    Mogre.OverlayManager om = Mogre.OverlayManager.Singleton;
        //    return new Mogre.Vector2(cursorPos.x - (containerElement._getDerivedLeft() * om.ViewportWidth + containerElement.Width / 2), cursorPos.y - (containerElement._getDerivedTop() * om.ViewportHeight + containerElement.Height / 2f));
        //}


        /// <summary>
        /// Static utility method used to get the width of a caption in a text area.
        /// </summary>
        protected static float GetCaptionWidth(string caption, ref TextAreaOverlayElement area)
        {
            Mogre.FontPtr font = null;
            if (Mogre.FontManager.Singleton.ResourceExists(area.FontName))
            {
                font = (Mogre.FontPtr)Mogre.FontManager.Singleton.GetByName(area.FontName);
                if (!font.IsLoaded)
                {
                    font.Load();
                }
            }
            else
            {
                OGRE_EXCEPT("this font:", area.FontName, "is not exist");
            }
            //Font font = new Font(ft.Creator, ft.Name, ft.Handle, ft.Group, ft.IsManuallyLoaded);
            string current   = DisplayStringToString(caption);
            float  lineWidth = 0f;

            for (int i = 0; i < current.Length; i++)
            {
                // be sure to provide a line width in the text area
                if (current[i] == ' ')
                {
                    if (area.SpaceWidth != 0)
                    {
                        lineWidth += area.SpaceWidth;
                    }
                    else
                    {
                        lineWidth += font.GetGlyphAspectRatio(' ') * area.CharHeight;
                    }
                }
                else if (current[i] == '\n')
                {
                    break;
                }
                // use glyph information to calculate line width
                else
                {
                    lineWidth += font.GetGlyphAspectRatio(current[i]) * area.CharHeight;
                }
            }

            return(lineWidth);
        }
Beispiel #3
0
        protected static float GetCaptionHeight(string caption, ref TextAreaOverlayElement area)
        {
            Mogre.FontPtr font = null;
            if (Mogre.FontManager.Singleton.ResourceExists(area.FontName))
            {
                font = (Mogre.FontPtr)Mogre.FontManager.Singleton.GetByName(area.FontName);
                if (!font.IsLoaded)
                {
                    font.Load();
                }
            }
            else
            {
                OGRE_EXCEPT("this font:", area.FontName, "is not exist");
            }
            //Font font = new Font(ft.Creator, ft.Name, ft.Handle, ft.Group, ft.IsManuallyLoaded);
            string current    = DisplayStringToString(caption);
            float  lineHeight = 0f;

            for (int i = 0; i < current.Length; i++)
            {
                if (current[i] == '\n')
                {
                    var aspectRatio = font.GetGlyphAspectRatio(current[i]);
                    if (aspectRatio <= 0)
                    {
                        lineHeight += aspectRatio * area.CharHeight;
                    }
                    else
                    {
                        lineHeight += area.CharHeight;
                    }
                }
                else if (i == current.Length - 1)
                {
                    lineHeight += area.CharHeight;
                }
            }

            return(lineHeight);
        }