AxiosEngine-old 

AxiosEngine-old Mercurial Source Tree


Root/axios/ScreenSystem/ScreenManagerComponent.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
 
namespace FarseerPhysics.SamplesFramework
{
    /// <summary>
    /// The screen manager is a component which manages one or more GameScreen
    /// instances. It maintains a stack of screens, calls their Update and Draw
    /// methods at the appropriate times, and automatically routes input to the
    /// topmost active screen.
    /// </summary>
    public class ScreenManager : DrawableGameComponent
    {
        private AssetCreator _assetCreator;
        private ContentManager _contentManager;
 
        private InputHelper _input;
        private bool _isInitialized;
        private LineBatch _lineBatch;
 
        private List<GameScreen> _screens;
        private List<GameScreen> _screensToUpdate;
 
        private SpriteBatch _spriteBatch;
 
        /// <summary>
        /// Contains all the fonts avaliable for use.
        /// </summary>
        private SpriteFonts _spriteFonts;
 
        private List<RenderTarget2D> _transitions;
 
        /// <summary>
        /// Constructs a new screen manager component.
        /// </summary>
        public ScreenManager(Game game)
            : base(game)
        {
            // we must set EnabledGestures before we can query for them, but
            // we don't assume the game wants to read them.
            //game.Components.
            TouchPanel.EnabledGestures = GestureType.None;
            _contentManager = game.Content;
            _contentManager.RootDirectory = "Content";
            _input = new InputHelper(this);
 
            _screens = new List<GameScreen>();
            _screensToUpdate = new List<GameScreen>();
            _transitions = new List<RenderTarget2D>();
        }
 
        /// <summary>
        /// A default SpriteBatch shared by all the screens. This saves
        /// each screen having to bother creating their own local instance.
        /// </summary>
        public SpriteBatch SpriteBatch
        {
            get { return _spriteBatch; }
        }
 
        public LineBatch LineBatch
        {
            get { return _lineBatch; }
        }
 
        public ContentManager Content
        {
            get { return _contentManager; }
        }
 
        public SpriteFonts Fonts
        {
            get { return _spriteFonts; }
        }
 
        public AssetCreator Assets
        {
            get { return _assetCreator; }
        }
 
        /// <summary>
        /// Initializes the screen manager component.
        /// </summary>
        public override void Initialize()
        {
            if (!Axios.Settings.ScreenSaver)
                _spriteFonts = new SpriteFonts(_contentManager);
            base.Initialize();
 
            _isInitialized = true;
        }
 
        /// <summary>
        /// Load your graphics content.
        /// </summary>
        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);
            _lineBatch = new LineBatch(GraphicsDevice);
 
            if (!Axios.Settings.ScreenSaver)
            {
                _assetCreator = new AssetCreator(GraphicsDevice);
                _assetCreator.LoadContent(_contentManager);
 
                _input.LoadContent();
            }
            // Tell each of the screens to load their content.
            foreach (GameScreen screen in _screens)
            {
                screen.LoadContent();
            }
        }
 
        /// <summary>
        /// Unload your graphics content.
        /// </summary>
        protected override void UnloadContent()
        {
            // Tell each of the screens to unload their content.
            foreach (GameScreen screen in _screens)
            {
                screen.UnloadContent();
            }
        }
 
        /// <summary>
        /// Allows each screen to run logic.
        /// </summary>
        public override void Update(GameTime gameTime)
        {
            // Read the keyboard and gamepad.
            _input.Update(gameTime);
 
            // Make a copy of the master screen list, to avoid confusion if
            // the process of updating one screen adds or removes others.
            _screensToUpdate.Clear();
 
            if (_screens.Count == 0 || (_screens.Count == 1 && _screens[0] is BackgroundScreen))
                //I'm done, exit
                this.Game.Exit();
 
            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,
                    // give it a chance to handle input.
                    if (!otherScreenHasFocus || screen.AlwaysHasFocus)
                    {
 
                        if (!otherScreenHasFocus)
                        {
                            _input.ShowCursor = screen.HasCursor;
                            _input.EnableVirtualStick = screen.HasVirtualStick;
 
                            otherScreenHasFocus = true;
                        }
                        screen.HandleInput(_input, gameTime);
                    }
 
                    // If this is an active non-popup, inform any subsequent
                    // screens that they are covered by it.
                    if (!screen.IsPopup)
                    {
                        coveredByOtherScreen = true;
                    }
                }
            }
        }
 
        /// <summary>
        /// Tells each screen to draw itself.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            int transitionCount = 0;
            foreach (GameScreen screen in _screens)
            {
                if (screen.ScreenState == ScreenState.TransitionOn ||
                    screen.ScreenState == ScreenState.TransitionOff)
                {
                    ++transitionCount;
                    if (_transitions.Count < transitionCount)
                    {
                        PresentationParameters _pp = GraphicsDevice.PresentationParameters;
                        _transitions.Add(new RenderTarget2D(GraphicsDevice, _pp.BackBufferWidth, _pp.BackBufferHeight,
                                                            false,
                                                            SurfaceFormat.Color, _pp.DepthStencilFormat,
                                                            _pp.MultiSampleCount,
                                                            RenderTargetUsage.DiscardContents));
                    }
                    GraphicsDevice.SetRenderTarget(_transitions[transitionCount - 1]);
                    GraphicsDevice.Clear(Color.Transparent);
                    screen.Draw(gameTime);
                    GraphicsDevice.SetRenderTarget(null);
                }
            }
 
            //GraphicsDevice.Clear(Color.Black);
 
            transitionCount = 0;
            foreach (GameScreen screen in _screens)
            {
                if (screen.ScreenState == ScreenState.Hidden)
                {
                    continue;
                }
 
                if (screen.ScreenState == ScreenState.TransitionOn ||
                    screen.ScreenState == ScreenState.TransitionOff)
                {
                    _spriteBatch.Begin(0, BlendState.AlphaBlend);
                    _spriteBatch.Draw(_transitions[transitionCount], Vector2.Zero, Color.White * screen.TransitionAlpha);
                    _spriteBatch.End();
 
                    ++transitionCount;
                }
                else
                {
                    screen.Draw(gameTime);
                }
            }
            _input.Draw();
        }
 
        /// <summary>
        /// Adds a new screen to the screen manager.
        /// </summary>
        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);
 
            // update the TouchPanel to respond to gestures this screen is interested in
            TouchPanel.EnabledGestures = screen.EnabledGestures;
        }
 
        /// <summary>
        /// Removes a screen from the screen manager. You should normally
        /// use GameScreen.ExitScreen instead of calling this directly, so
        /// the screen can gradually transition off rather than just being
        /// instantly removed.
        /// </summary>
        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);
 
            // if there is a screen still in the manager, update TouchPanel
            // to respond to gestures that screen is interested in.
            if (_screens.Count > 0)
            {
                TouchPanel.EnabledGestures = _screens[_screens.Count - 1].EnabledGestures;
            }
        }
 
        /// <summary>
        /// Expose an array holding all the screens. We return a copy rather
        /// than the real master list, because screens should only ever be added
        /// or removed using the AddScreen and RemoveScreen methods.
        /// </summary>
        public GameScreen[] GetScreens()
        {
            return _screens.ToArray();
        }
    }
}
Source at commit 6526c5e6050c created 12 years 9 months ago.
By nathan@daedalus, Fixing issue with UI objects and collision with mouse pointer

Archive Download this file

Page rendered in 0.81905s using 11 queries.