Commit 524f6e4e authored by Jesse Freeman's avatar Jesse Freeman

Removed use of ISave and ILoad interface and code from each chip and fixed...

Removed use of ISave and ILoad interface and code from each chip and fixed ControllerChip's namespace to PixelVisoinSDK.Chips.
parent 169c6807
......@@ -100,47 +100,47 @@ namespace PixelVisionSDK.Chips
active = false;
}
/// <summary>
/// This methods sets up the foundation for serializing a component as
/// JSON. It automatically creates a json wrapper before calling
/// CustomSerializedData().
/// </summary>
/// <returns>string</returns>
public virtual string SerializeData()
{
var sb = new StringBuilder();
sb.Append("{");
CustomSerializedData(sb);
sb.Append("}");
return sb.ToString();
}
/// <summary>
/// Override this to add custom json data to the serialized string
/// passed in by the StringBuilder reference.
/// </summary>
/// <param name="sb">
/// A StringBuilder reference to add additional json string properties
/// to.
/// </param>
public virtual void CustomSerializedData(StringBuilder sb)
{
// Override to add custom data
}
/// <summary>
/// Override this to method to handle your own custom de-serialized
/// logic. It expects a Dictionary with a string as the key and a
/// generic object as the value.
/// </summary>
/// <param name="data">
/// A Dictionary with a string as the key and a generic object as the
/// value.
/// </param>
public virtual void DeserializeData(Dictionary<string, object> data)
{
throw new NotImplementedException();
}
// /// <summary>
// /// This methods sets up the foundation for serializing a component as
// /// JSON. It automatically creates a json wrapper before calling
// /// CustomSerializedData().
// /// </summary>
// /// <returns>string</returns>
// public virtual string SerializeData()
// {
// var sb = new StringBuilder();
// sb.Append("{");
// CustomSerializedData(sb);
// sb.Append("}");
// return sb.ToString();
// }
//
// /// <summary>
// /// Override this to add custom json data to the serialized string
// /// passed in by the StringBuilder reference.
// /// </summary>
// /// <param name="sb">
// /// A StringBuilder reference to add additional json string properties
// /// to.
// /// </param>
// public virtual void CustomSerializedData(StringBuilder sb)
// {
// // Override to add custom data
// }
//
// /// <summary>
// /// Override this to method to handle your own custom de-serialized
// /// logic. It expects a Dictionary with a string as the key and a
// /// generic object as the value.
// /// </summary>
// /// <param name="data">
// /// A Dictionary with a string as the key and a generic object as the
// /// value.
// /// </param>
// public virtual void DeserializeData(Dictionary<string, object> data)
// {
// throw new NotImplementedException();
// }
public virtual void Init()
{
......
......@@ -17,7 +17,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PixelVisionSDK.Chips
{
......@@ -29,7 +28,7 @@ namespace PixelVisionSDK.Chips
/// managing the life-cycle of chips as they are created and destroy in the
/// engine.
/// </summary>
public class ChipManager : IGameLoop, ISave, ILoad
public class ChipManager : IGameLoop
{
protected Dictionary<string, AbstractChip> chips = new Dictionary<string, AbstractChip>();
......@@ -92,7 +91,7 @@ namespace PixelVisionSDK.Chips
}
/// <summary>
/// Loops throgh all chips and calls reset on them.
/// Loops through all chips and calls reset on them.
/// </summary>
public void Reset()
{
......@@ -102,81 +101,6 @@ namespace PixelVisionSDK.Chips
}
}
/// <summary>
/// This method reads a Dictionary's properties and applies them to any
/// <see cref="chips" /> registered in the manager.
/// </summary>
/// <param name="data">
/// A Dictionary with a string for the key and a generic object for the
/// value.
/// </param>
public void DeserializeData(Dictionary<string, object> data)
{
// Disable any active chips
DeactivateChips();
foreach (var entry in data)
{
var chipName = entry.Key;
var chipData = entry.Value as Dictionary<string, object>;
UpdateChip(chipName, chipData);
}
// Removed any active chips not reserialized
RemoveInactiveChips();
}
/// <summary>
/// This method serializes all of the chips in the
/// manager. Each chip is added to a json object allowing the
/// ChipManager to be easily reserialized at run-time.
/// </summary>
/// <returns name="String">
/// Returns a JSON string containing all of the chips in
/// the manager.
/// </returns>
public string SerializeData()
{
// Create a new string builder instance
var sb = new StringBuilder();
// Start the json string
sb.Append("{");
CustomSerializedData(sb);
sb.Append("}");
return sb.ToString();
}
/// <summary>
/// Loops through each of the <see cref="chips" /> flagged for export and
/// adds them to the StringBuilder instance.
/// </summary>
/// <param name="sb">
/// Reference of a StringBuilder used by the SerializeData() method.
/// </param>
public void CustomSerializedData(StringBuilder sb)
{
// Select the chips that are flagged to be exported
var chipsToSave = chips.Where(c => c.Value.export).ToArray();
// Get total number of chips to export
var total = chipsToSave.Length;
// Loop through all the chips to export
for (var i = 0; i < total; i++)
{
var item = chipsToSave.ElementAt(i);
sb.Append("\"" + item.Key + "\":");
sb.Append(item.Value.SerializeData());
if (i < total - 1)
sb.Append(",");
}
}
/// <summary>
/// This method tests to see if a chip exists in the manager.
/// </summary>
......@@ -227,7 +151,8 @@ namespace PixelVisionSDK.Chips
}
// TODO create a chip
var chipInstance = Activator.CreateInstance(Type.GetType(id)) as AbstractChip;
var type = Type.GetType(id);
var chipInstance = Activator.CreateInstance(type) as AbstractChip;
ActivateChip(id, chipInstance);
......@@ -247,10 +172,10 @@ namespace PixelVisionSDK.Chips
/// exist.
/// </param>
/// <param name="data">
/// A Dictonary with a string key and generic object value. Used to pass
/// A Dictionary with a string key and generic object value. Used to pass
/// in new properties to the chip.
/// </param>
public void UpdateChip(string id, Dictionary<string, object> data)
public virtual void UpdateChip(string id, Dictionary<string, object> data)
{
var chip = GetChip(id);
......@@ -258,7 +183,7 @@ namespace PixelVisionSDK.Chips
return;
chip.Activate(engine);
chip.DeserializeData(data);
}
/// <summary>
......
......@@ -185,65 +185,7 @@ namespace PixelVisionSDK.Chips
engine.currentGame = null;
}
/// <summary>
/// Converts the internal save data into JSON.
/// </summary>
/// <param name="sb"></param>
public override void CustomSerializedData(StringBuilder sb)
{
sb.Append("\"maxSize\":");
sb.Append(maxSize);
sb.Append(",");
sb.Append("\"saveSlots\":");
sb.Append(saveSlots);
sb.Append(",");
sb.Append("\"savedData\": {");
for (var i = savedData.Count - 1; i >= 0; i--)
{
var item = savedData.ElementAt(i);
sb.Append("\"");
sb.Append(item.Key);
sb.Append("\": \"");
sb.Append(item.Value);
sb.Append("\"");
if (i > 0)
{
sb.Append(",");
}
}
sb.Append("}");
}
/// <summary>
/// Attempts to convert a supplied Dictionary into internal saved data.
/// </summary>
/// <param name="data">A Dictionary with a string for the key and a generic object for the data.</param>
public override void DeserializeData(Dictionary<string, object> data)
{
// loop through all data and save it to the game's memory
if (data.ContainsKey("maxSize"))
{
maxSize = (int) (long) data["maxSize"];
}
if (data.ContainsKey("saveSlots"))
{
saveSlots = (int) (long) data["saveSlots"];
}
if (data.ContainsKey("savedData"))
{
foreach (var entry in data["savedData"] as Dictionary<string, object>)
{
var name = entry.Key;
var value = entry.Value as string;
SaveData(name, value);
}
}
}
}
......
......@@ -229,78 +229,7 @@ namespace PixelVisionSDK.Chips
pages = MathUtil.CeilToInt(total / colorsPerPage);
}
/// <summary>
/// This method converts the chip's supportedColors and defaultColors into a json object.
/// </summary>
/// <param name="sb"></param>
public override void CustomSerializedData(StringBuilder sb)
{
sb.Append("\"pages\":");
sb.Append(pages);
sb.Append(",");
sb.Append("\"colorsPerPage\":");
sb.Append(colorsPerPage);
sb.Append(",");
sb.Append("\"transparent\":\"");
sb.Append(transparent);
sb.Append("\",");
// Default Colors
sb.Append("\"colors\":[");
var total = _colors.Length;
for (var i = 0; i < total; i++)
{
var color = _colors[i];
if (color != _transparent)
{
sb.Append("\"");
sb.Append(color);
sb.Append("\"");
sb.Append(",");
}
}
if (sb.ToString().EndsWith(","))
sb.Length -= 1;
sb.Append("]");
}
/// <summary>
/// This method reads the supplied Dictionary and attempts to set the supportedColors value and rebuild the
/// defaultColors array. Once done, it calls RevertColors().
/// </summary>
/// <param name="data"></param>
public override void DeserializeData(Dictionary<string, object> data)
{
if (data.ContainsKey("colorsPerPage"))
{
_colorsPerPage = (int) (long) data["colrosPerPage"];
}
if (data.ContainsKey("transparent"))
{
_transparent = (string) data["transparent"];
}
// Make sure we have data to parse
if (data.ContainsKey("colors"))
{
// Pull out the color data
var colors = (List<object>) data["colors"];
var newTotal = colors.Count;
RebuildColorPages(newTotal);
Clear();
for (var i = 0; i < newTotal; i++)
{
UpdateColorAt(i, (string) colors[i]);
}
}
}
}
......
......@@ -246,52 +246,7 @@ namespace PixelVisionSDK.Chips
engine.displayChip = null;
}
public override void CustomSerializedData(StringBuilder sb)
{
// Wrap Mode Value
sb.Append("\"wrapMode\":");
sb.Append(Convert.ToInt32(wrapMode));
sb.Append(",");
// Width Value
sb.Append("\"width\":");
sb.Append(_width);
sb.Append(",");
// Height Value
sb.Append("\"height\":");
sb.Append(_height);
sb.Append(",");
// AutoClear Value
sb.Append("\"autoClear\":");
sb.Append(Convert.ToInt32(autoClear));
sb.Append(",");
// Max Sprite Count Value
sb.Append("\"maxSpriteCount\":");
sb.Append(maxSpriteCount);
}
public override void DeserializeData(Dictionary<string, object> data)
{
if (data.ContainsKey("wrapMode"))
wrapMode = Convert.ToBoolean((int) (long) data["wrapMode"]);
if (data.ContainsKey("width"))
_width = (int) (long) data["width"];
if (data.ContainsKey("height"))
_height = (int) (long) data["height"];
ResetResolution(_width, _height);
if (data.ContainsKey("autoClear"))
autoClear = Convert.ToBoolean((long) data["autoClear"]);
if (data.ContainsKey("maxSpriteCount"))
maxSpriteCount = (int) (long) data["maxSpriteCount"];
}
}
......
......@@ -299,7 +299,7 @@ namespace PixelVisionSDK.Chips
return string.Join("\n", split);
}
}
}
\ No newline at end of file
......@@ -314,47 +314,6 @@ namespace PixelVisionSDK.Chips
Invalidate();
}
/// <summary>
/// This converts the ScreenBuffer's settings into a JSON string. It
/// stores the <see cref="viewPortWidth" /> and viewPortHeight.
/// </summary>
/// <param name="sb"></param>
public override void CustomSerializedData(StringBuilder sb)
{
sb.Append("\"backgroundColor\":");
sb.Append(backgroundColor);
sb.Append(",");
// Width Value
sb.Append("\"viewPortWidth\":");
sb.Append(viewPortWidth);
sb.Append(",");
// Height Value
sb.Append("\"viewPortHeight\":");
sb.Append(viewPortHeight);
}
/// <summary>
/// This converts values from a Dictionary with a sting as the key and
/// an object as the value into values the
/// <see cref="ScreenBufferChip" /> can use.
/// </summary>
/// <param name="data">
/// A Dictionary with a sting as the key and an object.
/// </param>
public override void DeserializeData(Dictionary<string, object> data)
{
if (data.ContainsKey("backgroundColor"))
backgroundColor = (int) (long) data["backgroundColor"];
if (data.ContainsKey("viewPortWidth"))
viewPortWidth = (int) (long) data["viewPortWidth"];
if (data.ContainsKey("viewPortHeight"))
viewPortHeight = (int) (long) data["viewPortHeight"];
}
public override void Deactivate()
{
base.Deactivate();
......
......@@ -258,94 +258,6 @@ namespace PixelVisionSDK.Chips
SpriteChipUtil.ClearTextureData(ref _texture);
}
/// <summary>
/// This serializes the chip's data as JSON. It saves out the size of
/// the internal TextureData, the sprite's dimensions,
/// and if set to compressed mode, will save all the pixel data in the
/// as well.
/// </summary>
/// <param name="sb"></param>
public override void CustomSerializedData(StringBuilder sb)
{
// Mode
sb.Append("\"spriteWidth\":");
sb.Append(width);
sb.Append(",");
sb.Append("\"spriteHeight\":");
sb.Append(height);
sb.Append(",");
sb.Append("\"pages\":");
sb.Append(pages);
sb.Append(",");
// Height
sb.Append("\"cps\":");
sb.Append(colorsPerSprite);
sb.Append(",");
//TODO this should just use the TextureData's
// serializePixelData
sb.Append("\"serializePixelData\":");
sb.Append(Convert.ToInt32(serializePixelData));
if (serializePixelData)
{
sb.Append(",");
sb.Append("\"pixels\":");
sb.Append(_texture.SerializeData());
}
}
/// <summary>
/// This will go through and attempt to deserialize any
/// <paramref name="data" /> passed into it. It requires a dictionary
/// with a string as the key and a generic object for the value.
/// </summary>
/// <param name="data">
/// A Dictionary with a string as the key and a generic object as the
/// value.
/// </param>
public override void DeserializeData(Dictionary<string, object> data)
{
if (data.ContainsKey("spriteWidth"))
{
width = (int) (long) data["spriteWidth"];
}
if (data.ContainsKey("spriteHeight"))
{
height = (int) (long) data["spriteHeight"];
}
if (data.ContainsKey("cps"))
{
colorsPerSprite = (int) (long) data["cps"];
}
if (data.ContainsKey("pages"))
{
pages = (int) (long) data["pages"];
}
Resize(pageWidth, pageHeight * pages);
if (data.ContainsKey("pixels"))
{
var pixelData = (Dictionary<string, object>) data["pixels"];
_texture.DeserializeData(pixelData);
}
if (data.ContainsKey("serializePixelData"))
{
serializePixelData = Convert.ToBoolean((int) (long) data["serializePixelData"]);
}
}
/// <summary>
/// Returns an array of ints that represent a sprite. Each
/// int should be mapped to a color
......@@ -407,7 +319,7 @@ namespace PixelVisionSDK.Chips
/// range of colors.
/// </summary>
/// <param name="offset">
/// Anint to use as the offset value.
/// An int to use as the offset value.
/// </param>
public void ShiftColorIndex(int offset)
{
......
......@@ -72,80 +72,7 @@ namespace PixelVisionSDK.Chips
/// </summary>
public int rows { get; private set; }
/// <summary>
/// This creates a json string for the TileMapChip's properties. It will serialize the columns, rows & totalFlags.
/// </summary>
/// <param name="sb"></param>
public override void CustomSerializedData(StringBuilder sb)
{
// Columns
sb.Append("\"columns\":");
sb.Append(columns);
sb.Append(",");
// Rows
sb.Append("\"rows\":");
sb.Append(rows);
sb.Append(",");
// Total Flags
sb.Append("\"totalFlags\":");
sb.Append(totalFlags);
if (serializeTileData)
{
// Disabled for now while I break the files out
sb.Append(",");
sb.Append("\"spriteIDs\":[" + string.Join(",", spriteIDs.Select(x => x.ToString()).ToArray()) + "],");
sb.Append("\"paletteIDs\":[" + string.Join(",", paletteIDs.Select(x => x.ToString()).ToArray()) + "],");
sb.Append("\"flags\":[" + string.Join(",", flags.Select(x => x.ToString()).ToArray()) + "]");
}
}
/// <summary>
/// Attempts to rebuild the TileMap from a Dictionary with a string as
/// the key and an object for the value. It looks for columns, rows,
/// <see cref="totalFlags" /> and an array of sprite ids which represent
/// the tiles.
/// </summary>
/// <param name="data">
/// A Dictionary with a string for the key and an object for the value.
/// </param>
public override void DeserializeData(Dictionary<string, object> data)
{
var columns = 32;
var rows = 30;
if (data.ContainsKey("columns"))
columns = (int) (long) data["columns"];
if (data.ContainsKey("rows"))
rows = (int) (long) data["rows"];
if (data.ContainsKey("totalFlags"))
rows = (int) (long) data["totalFlags"];
Resize(columns, rows);
if (data.ContainsKey("spriteIDs") && data.ContainsKey("paletteIDs") && data.ContainsKey("flags"))
{
var sIDs = (List<object>) data["spriteIDs"];
var pIDs = (List<object>) data["paletteIDs"];
var f = (List<object>) data["flags"];
//Debug.Log("Deserilize Tile Map");
var total = columns * rows;
for (var i = 0; i < total; i++)
{
spriteIDs[i] = (int) (long) sIDs[i];
paletteIDs[i] = (int) (long) pIDs[i];
flags[i] = (int) (long) f[i];
}
}
}
/// <summary>
/// Reads the current tile and output the spriteID,
......
......@@ -23,7 +23,7 @@ namespace PixelVisionSDK.Chips
/// implements the <see cref="ILoad" /> and <see cref="ISave" /> interfaces
/// for serialization.
/// </summary>
public interface IChip : ISave, ILoad
public interface IChip
{
/// <summary>
......
......@@ -101,7 +101,7 @@ namespace PixelVisionSDK.Chips
FontChip fontChip { get; set; }
/// <summary>
/// The music chip represents a squencer that can be used to play back
/// The music chip represents a sequencer that can be used to play back
/// sound data for songs.
/// </summary>
MusicChip musicChip { get; set; }
......
......@@ -14,6 +14,8 @@
// Shawn Rakowski - @shwany
//
using PixelVisionSDK.Chips;
namespace PixelVisionSDK
{
......
......@@ -15,9 +15,8 @@
//
using System;
using PixelVisionSDK.Chips;
namespace PixelVisionSDK
namespace PixelVisionSDK.Chips
{
public enum Buttons
......