source: 2011/23/sijoseha/Fera Proelia/Fera Proelia/Fera Proelia/ScreenManager.cs @ 2144

Revision 2144, 3.6 KB checked in by sijoseha, 12 years ago (diff)

Basic HUD stuff implemented

Line 
1using System;
2using System.Collections.Generic;
3using Microsoft.Xna.Framework;
4using Microsoft.Xna.Framework.Content;
5using Microsoft.Xna.Framework.Graphics;
6using Microsoft.Xna.Framework.Input;
7using Microsoft.Xna.Framework.Input.Touch;
8
9namespace Fera_Proelia
10{
11    public class ScreenManager : DrawableGameComponent
12    {
13        List<GameScreen> screens = new List<GameScreen>();
14        List<GameScreen> screensToUpdate = new List<GameScreen>();
15
16        InputState input = new InputState();
17
18        SpriteBatch spriteBatch;
19        SpriteFont menuFont, hudFont;
20
21        bool isInitialized;
22
23        public SpriteBatch SpriteBatch { get { return spriteBatch; } }
24        public SpriteFont MenuFont { get { return menuFont; } }
25        public SpriteFont HudFont { get { return hudFont; } }
26
27        public ScreenManager(Game game)
28            : base(game)
29        {
30            TouchPanel.EnabledGestures = GestureType.None;
31        }
32
33        public override void Initialize()
34        {
35            base.Initialize();
36            isInitialized = true;
37        }
38
39        protected override void LoadContent()
40        {
41            ContentManager content = Game.Content;
42            spriteBatch = new SpriteBatch(GraphicsDevice);
43            menuFont = content.Load<SpriteFont>("menuFont");
44            hudFont = content.Load<SpriteFont>("hudFont");
45            foreach (GameScreen screen in screens)
46            {
47                screen.LoadContent();
48            }
49        }
50
51        protected override void UnloadContent()
52        {
53            foreach (GameScreen screen in screens)
54            {
55                screen.UnloadContent();
56            }
57        }
58
59        public override void Update(GameTime gameTime)
60        {
61            input.Update();
62            screensToUpdate.Clear();
63            foreach (GameScreen screen in screens)
64                screensToUpdate.Add(screen);
65
66            bool otherScreenHasFocus = !Game.IsActive;
67            bool coveredByOtherScreen = false;
68
69            while (screensToUpdate.Count > 0)
70            {
71                GameScreen screen = screensToUpdate[screensToUpdate.Count - 1];
72                screensToUpdate.RemoveAt(screensToUpdate.Count - 1);
73                screen.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
74                if (screen.ScreenState == ScreenState.Active)
75                {
76                    if (!otherScreenHasFocus)
77                    {
78                        screen.HandleInput(input);
79                        otherScreenHasFocus = true;
80                    }
81                }
82            }
83
84            base.Update(gameTime);
85        }
86
87        public override void Draw(GameTime gameTime)
88        {
89            foreach (GameScreen screen in screens)
90            {
91                screen.Draw(gameTime);
92            }
93            base.Draw(gameTime);
94        }
95
96        public void AddScreen(GameScreen screen)
97        {
98            screen.ScreenManager = this;
99            if (isInitialized)
100            {
101                screen.LoadContent();
102            }
103            screens.Add(screen);
104            TouchPanel.EnabledGestures = screen.EnabledGestures;
105        }
106
107        public void RemoveScreen(GameScreen screen)
108        {
109            if (isInitialized)
110            {
111                screen.UnloadContent();
112            }
113
114            screens.Remove(screen);
115            screensToUpdate.Remove(screen);
116        }
117
118        public GameScreen[] GetScreens()
119        {
120            return screens.ToArray();
121        }
122    }
123}
Note: See TracBrowser for help on using the repository browser.