GAM456-SpaceInvaders / SpaceInvaders / 6_Font / FontMan.cs
FontMan.cs
Raw
using System;
using System.Diagnostics;


namespace SpaceInvaders
{
    public class FontMan : Manager
    {
        //-------------------------------------------------------------------
        //  FIELDS
        //-------------------------------------------------------------------

        private static readonly Font poCompare = new Font();
        private static FontMan poInstance = null;
        private static FontMan pActiveMan = null;


        //-------------------------------------------------------------------
        //  CONSTRUCTION
        //-------------------------------------------------------------------

        public FontMan(int numReserve = 3, int numGrow = 1)
            : base(new DLinkMan(), new DLinkMan(), numReserve, numGrow)
        {
            FontMan.pActiveMan = null;
        }


        //-------------------------------------------------------------------
        //  PUBLIC METHODS
        //-------------------------------------------------------------------

        public static void Create()
        {
            if(FontMan.poInstance == null)
            {
                FontMan.poInstance = new FontMan();
                Debug.Assert(FontMan.poInstance != null);
            }
        }

        public static void Destroy()
        { }

        public static void SetActiveMan(FontMan pFontMan)
        {
            FontMan pManager = FontMan.GetInstance();
            Debug.Assert(pManager != null);

            Debug.Assert(pFontMan != null);
            FontMan.pActiveMan = pFontMan;
        }

        public static void UnactivateMan()
        {
            FontMan pManager = FontMan.GetInstance();
            Debug.Assert(pManager != null);

            FontMan.pActiveMan = null;
        }

        public static Font AddFont(Font.FontName name, SpriteBatch.BatchName batchName, string pMessage, Glyph.GlyphName glyphName, float x, float y)
        {
            FontMan pManager = FontMan.pActiveMan;
            Debug.Assert(pManager != null);

            Font pNode = (Font)pManager.AddToFront();
            Debug.Assert(pNode != null);

            pNode.SetFont(name, pMessage, glyphName, x, y);

            //add to batch
            SpriteBatch pBatch = SpriteBatchMan.FindBatch(batchName);
            Debug.Assert(pBatch != null);
            Debug.Assert(pNode.pSpriteFont != null);
            pBatch.AttachBatch(pNode.pSpriteFont);

            return pNode;
        }

        public static Font AddFont(Font.FontName fontName, Glyph.GlyphName glyphName, SpriteBatch.BatchName batchName, string message, float x, float y, float sx = 1, float sy = 1, float spacing = 2, Azul.Color color = null)
        {
            FontMan pManager = FontMan.pActiveMan;
            Debug.Assert(pManager != null);

            //pull node from reserve pool
            Font pNode = (Font)pManager.AddToFront();
            Debug.Assert(pNode != null);

            //set font data
            pNode.SetFont(fontName, glyphName, message, x, y, sx, sy, spacing, color);

            //add to batch
            SpriteBatch pBatch = SpriteBatchMan.FindBatch(batchName);
            Debug.Assert(pBatch != null);
            Debug.Assert(pNode.pSpriteFont != null);
            pBatch.AttachBatch(pNode.pSpriteFont);

            return pNode;
        }

        public static Font FindFont(Font.FontName name)
        {
            FontMan pManager = FontMan.pActiveMan;
            Debug.Assert(pManager != null);

            FontMan.poCompare.name = name;
            Font pFont = (Font)pManager.Find(FontMan.poCompare);

            return pFont;
        }

        public static void RemoveAllFont()
        {
            FontMan pManager = FontMan.pActiveMan;
            Debug.Assert(pManager != null);

            Iterator pIterator = pManager.GetActiveIterator();
            Debug.Assert(pIterator != null);

            Font pNode = (Font)pIterator.First();
            Font pNextNode;

            while(!pIterator.IsDone())
            {
                pNextNode = (Font)pIterator.Next();

                pManager.Remove(pNode);

                pNode = pNextNode;
            }
        }

        public static void RemoveFont(Font pNode)
        {
            Debug.Assert(pNode != null);

            FontMan pManager = FontMan.pActiveMan;
            Debug.Assert(pManager != null);

            pManager.Remove(pNode);
        }

        public static void PrintFont()
        {
            FontMan pManager = FontMan.pActiveMan;
            Debug.Assert(pManager != null);

            pManager.PrintStats();
        }


        //-------------------------------------------------------------------
        //  PROTECTED METHODS
        //-------------------------------------------------------------------

        protected override Node derivedCreate()
        {
            Node pNode = new Font();
            Debug.Assert(pNode != null);

            return pNode;
        }


        //-------------------------------------------------------------------
        //  PRIVATE METHODS
        //-------------------------------------------------------------------

        private static FontMan GetInstance()
        {
            if (FontMan.poInstance == null)
            {
                Create();
                Debug.Assert(FontMan.poInstance != null);
            }

            return FontMan.poInstance;
        }


    } // end class

} // end namespace