using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SerializationTest.Data;
using System.Xml;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate;
namespace SerializationTest.Modes
{
///
/// This is a game component that implements IUpdateable.
///
public class Mode4 : Microsoft.Xna.Framework.DrawableGameComponent
{
private SpriteBatch spriteBatch;
SharedResourceList listTest = new SharedResourceList();
SharedResourceDictionary dictTest = new SharedResourceDictionary();
int currKey = 0;
// Keyboard polling helpers
private KeyboardState currentState;
private KeyboardState previousState;
private MouseState currentMouseState;
private MouseState previousMouseState;
public Mode4(Game game)
: base(game)
{
// TODO: Construct any child components here
}
///
/// Allows the game component to perform any initialization it needs to before starting
/// to run. This is where it can query for any required services and load content.
///
public override void Initialize()
{
base.Initialize();
this.spriteBatch = new SpriteBatch(Game.GraphicsDevice);
}
///
/// Allows the game component to update itself.
///
/// Provides a snapshot of timing values.
public override void Update(GameTime gameTime)
{
// Key checking
previousState = currentState;
currentState = Keyboard.GetState();
previousMouseState = currentMouseState;
currentMouseState = Mouse.GetState();
// STEP 1: User requests save / load
// When we press the "s" key
if (currentState.IsKeyUp(Keys.S) &&
previousState.IsKeyDown(Keys.S))
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create("out.xml", settings))
{
IntermediateSerializer.Serialize(writer, dictTest, null);
//IntermediateSerializer.Serialize(writer, listTest, null);
}
}
// When we press the "l" key
if (currentState.IsKeyUp(Keys.L) &&
previousState.IsKeyDown(Keys.L))
{
XmlReaderSettings settings = new XmlReaderSettings();
using (XmlReader reader = XmlReader.Create("out.xml", settings))
{
this.dictTest = IntermediateSerializer.Deserialize>(reader, null);
//this.listTest = IntermediateSerializer.Deserialize>(reader, null);
}
}
// Make the age change so we can see
// stuff change with the save / load
if (currentMouseState.LeftButton == ButtonState.Released && previousMouseState.LeftButton == ButtonState.Pressed)
{
this.dictTest.Add(currKey, "Value" + currKey);
//this.listTest.Add("Value" + currKey);
currKey++;
}
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
base.Draw(gameTime);
int y = 100;
this.spriteBatch.Begin();
foreach(KeyValuePair pair in dictTest)
{
this.spriteBatch.DrawString(
Game.Content.Load("Font"),
"Pair { " + pair.Key + " , " + pair.Value + " }",
new Vector2(100, y += 30),
Color.White);
}
//foreach (string name in listTest)
//{
// this.spriteBatch.DrawString(
// Game.Content.Load("Font"),
// name,
// new Vector2(200, y += 30),
// Color.White);
//}
this.spriteBatch.End();
}
}
}