-
Notifications
You must be signed in to change notification settings - Fork 1
/
ScreenManager.cs
210 lines (158 loc) · 6.14 KB
/
ScreenManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Diagnostics;
using Microsoft.Xna.Framework.Audio;
namespace PROJECT_RPG
{
public class ScreenManager : DrawableGameComponent
{
#region Fields and Properties
Texture2D blankTexture;
SpriteFont font;
SpriteBatch spriteBatch;
SoundEffect menuItemSound;
InputState input = new InputState();
bool isInitialized;
bool screenTraceEnabled;
List<GameScreen> screens = new List<GameScreen>();
List<GameScreen> screensToUpdate = new List<GameScreen>();
// All of the properties are shared throughout all screens.
public SpriteFont Font { get { return font; } }
public SoundEffect MenuItemSound { get { return menuItemSound; } }
public SpriteBatch SpriteBatch { get { return spriteBatch; } }
public bool ScreenTraceEnabled { get { return screenTraceEnabled; } set { screenTraceEnabled = value; } }
#endregion
#region Initialization
public ScreenManager(Game game) : base(game)
{ }
public override void Initialize()
{
base.Initialize();
isInitialized = true;
}
protected override void LoadContent()
{
ContentManager content = Game.Content;
spriteBatch = new SpriteBatch(GraphicsDevice);
// To do: Load content shared by all screens (texture, font, ...)
font = content.Load<SpriteFont>("ct_font_alpha3");
//menuItemSound = content.Load<SoundEffect>("ffvii-messagesent");
blankTexture = content.Load<Texture2D>("blank");
// Tell each of the screens to load their content.
foreach (GameScreen screen in screens)
{
screen.LoadContent();
}
}
protected override void UnloadContent()
{
// Tell each of the screens to unload their content.
foreach (GameScreen screen in screens)
{
screen.UnloadContent();
}
}
#endregion
#region Update and Draw
public override void Update(GameTime gameTime)
{
// Read the keyboard.
input.Update();
// Make a copy of the master screen list, to avoid confusion if
// the process of updating one screen adds or removes others.
screensToUpdate.Clear();
foreach (GameScreen screen in screens)
screensToUpdate.Add(screen);
bool otherScreenHasFocus = !Game.IsActive;
bool coveredByOtherScreen = false;
// Loop as long as there are screens waiting to be updated.
while (screensToUpdate.Count > 0)
{
// Pop the topmost screen off the waiting list.
GameScreen screen = screensToUpdate[screensToUpdate.Count - 1];
screensToUpdate.RemoveAt(screensToUpdate.Count - 1);
// Update the screen.
screen.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
if (screen.ScreenState == ScreenState.TransitionOn ||
screen.ScreenState == ScreenState.Active)
{
// If this is the first active screen we came across,
// let it handle input.
if (!otherScreenHasFocus)
{
screen.HandleInput(input);
otherScreenHasFocus = true;
}
// If this is an active non-popup, inform any subsequent
// screens that they are covered by it.
if (!screen.IsPopup)
coveredByOtherScreen = true;
}
}
// Used for VS10 console output debugging.
if (screenTraceEnabled)
TraceScreens();
}
// Used for debugging screens, seeing when one transitions on, off, etc.
void TraceScreens()
{
List<string> screenNames = new List<string>();
foreach (GameScreen screen in screens)
screenNames.Add(screen.GetType().Name);
Debug.WriteLine(string.Join(", ", screenNames.ToArray()));
}
public override void Draw(GameTime gameTime)
{
foreach (GameScreen screen in screens)
{
if (screen.ScreenState == ScreenState.Hidden)
continue;
screen.Draw(gameTime);
}
}
#endregion
#region Public Methods
public void AddScreen(GameScreen screen)
{
screen.ScreenManager = this;
screen.IsExiting = false;
// If we have a graphics device, tell the screen to load content.
if (isInitialized)
{
screen.LoadContent();
}
screens.Add(screen);
}
public void RemoveScreen(GameScreen screen)
{
// If we have a graphics device, tell the screen to unload content.
if (isInitialized)
{
screen.UnloadContent();
}
screens.Remove(screen);
screensToUpdate.Remove(screen);
}
// Grabs an array of all the screens. This returns a -copy- of the list in array form.
public GameScreen[] GetScreens()
{
return screens.ToArray();
}
// Helps draw a black screen for fading screens in and out, darkening for pause screens, etc.
public void FadeBackBufferToBlack(float alpha)
{
Viewport viewport = GraphicsDevice.Viewport;
spriteBatch.Begin();
spriteBatch.Draw(blankTexture,
new Rectangle(0, 0, viewport.Width, viewport.Height),
Color.Black * alpha);
spriteBatch.End();
}
#endregion
}
}