Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- {
- /// <summary>
- /// This is a game component that implements IUpdateable.
- /// </summary>
- public class Mode4 : Microsoft.Xna.Framework.DrawableGameComponent
- {
- private SpriteBatch spriteBatch;
- SharedResourceList<string> listTest = new SharedResourceList<string>();
- SharedResourceDictionary<int, string> dictTest = new SharedResourceDictionary<int, string>();
- 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
- }
- /// <summary>
- /// 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.
- /// </summary>
- public override void Initialize()
- {
- base.Initialize();
- this.spriteBatch = new SpriteBatch(Game.GraphicsDevice);
- }
- /// <summary>
- /// Allows the game component to update itself.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- 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<SharedResourceDictionary<int,string>>(reader, null);
- //this.listTest = IntermediateSerializer.Deserialize<SharedResourceList<string>>(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<int,string> pair in dictTest)
- {
- this.spriteBatch.DrawString(
- Game.Content.Load<SpriteFont>("Font"),
- "Pair { " + pair.Key + " , " + pair.Value + " }",
- new Vector2(100, y += 30),
- Color.White);
- }
- //foreach (string name in listTest)
- //{
- // this.spriteBatch.DrawString(
- // Game.Content.Load<SpriteFont>("Font"),
- // name,
- // new Vector2(200, y += 30),
- // Color.White);
- //}
- this.spriteBatch.End();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement