Skip to content

Commit

Permalink
Made previou changes compatible with Nez formatting/naming policy.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsusini committed Apr 21, 2017
1 parent 49d63d7 commit b0c4068
Show file tree
Hide file tree
Showing 4 changed files with 795 additions and 787 deletions.
34 changes: 17 additions & 17 deletions Nez.PipelineImporter/UISkin/JsonDictionaryConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@

namespace Nez.UISkinImporter
{
class JsonDictionaryConverter : CustomCreationConverter<IDictionary<string, object>>
{
class JsonDictionaryConverter : CustomCreationConverter<IDictionary<string, object>>
{

public override IDictionary<string, object> Create(Type objectType)
{
return new Dictionary<string, object>();
}
public override IDictionary<string, object> Create( Type objectType )
{
return new Dictionary<string, object>();
}

public override bool CanConvert(Type objectType)
{
return objectType == typeof(object) || base.CanConvert(objectType);
}
public override bool CanConvert( Type objectType )
{
return objectType == typeof( object ) || base.CanConvert( objectType );
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.StartObject || reader.TokenType == JsonToken.Null)
return base.ReadJson(reader, objectType, existingValue, serializer);
public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer )
{
if( reader.TokenType == JsonToken.StartObject || reader.TokenType == JsonToken.Null )
return base.ReadJson( reader, objectType, existingValue, serializer );

return serializer.Deserialize(reader);
}
}
return serializer.Deserialize( reader );
}
}
}
280 changes: 140 additions & 140 deletions Nez.PipelineImporter/UISkin/UISkinProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,140 +1,140 @@
using Microsoft.Xna.Framework.Content.Pipeline;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Microsoft.Xna.Framework;
using Nez.UI;
using System;

namespace Nez.UISkinImporter
{
[ContentProcessor( DisplayName = "UISkin Processor" )]
public class UISkinProcessor : ContentProcessor<Dictionary<string,object>, UISkinConfig>
{
public override UISkinConfig Process( Dictionary<string,object> input, ContentProcessorContext context )
{
var skinConfig = new UISkinConfig();
var styleConfig = new UISkinStyleConfig();

foreach( var key in input.Keys )
{
// special cases first
if( key == "colors" )
{
skinConfig.colors = parseColors( input[key] as Dictionary<string, object> );
}
else if( key == "libGdxAtlases" )
{
var jArr = input[key] as JArray;
skinConfig.libGdxAtlases = jArr.ToObject<string[]>();
UISkinImporter.logger.LogMessage( "added {0} LibGdxAtlases\n", jArr.Count );
}
else if( key == "textureAtlases" )
{
var jArr = input[key] as JArray;
skinConfig.textureAtlases = jArr.ToObject<string[]>();
UISkinImporter.logger.LogMessage( "added {0} TextureAtlases\n", jArr.Count );
}
else
{
UISkinImporter.logger.LogMessage( "adding style type: {0}", key );
styleConfig.Add( key, input[key] );
}
}

if( styleConfig.Keys.Count > 0 )
skinConfig.styles = styleConfig;

return skinConfig;
}


Dictionary<string,Color> parseColors(Dictionary<string, object> colors )
{
if (colors.Count == 0)
return null;

var result = new Dictionary<string,Color>( colors.Count );
foreach (var key in colors.Keys)
{
var obj = colors[key];
UISkinImporter.logger.LogMessage("adding color: {0}", key);
Color color;

// Allow the passing of strings (hex), arrays and objects (r:, g:, b:) to
// represent colors. Also detect the usage of integers or normalized floats
if (obj is string)
{
var val = obj as string;

// we could have hex or hex
if (val.StartsWith("#"))
color = ColorExt.hexToColor(val.Substring(1));
else if (val.StartsWith("0x"))
color = ColorExt.hexToColor(val.Substring(2));
else
{
UISkinImporter.logger.LogMessage("unsupported color definition {0}: {1}", key, val);
continue;
}
}
else if (obj is JArray)
{
var jArr = obj as JArray;
if (jArr.Count < 3)
{
UISkinImporter.logger.LogMessage("unsupported color definition {0}: color array requires at least 3 members", key);
continue;
}

if (jArr[0].Type == JTokenType.Integer)
{
var arr = jArr.ToObject<int[]>();
color = new Color(arr[0], arr[1], arr[2]);
if (arr.Length == 4)
color = new Color(color, arr[3]);
}
else if(jArr[0].Type == JTokenType.Float)
{
var arr = jArr.ToObject<float[]>();
color = new Color(arr[0], arr[1], arr[2]);
if (arr.Length == 4)
color = new Color(color, arr[3]);
}
else
{
UISkinImporter.logger.LogMessage("unsupported color definition {0}: unknown type", key);
continue;
}
}
else if (obj is Dictionary<string, object>)
{
var dict = obj as Dictionary<string, object>;
if (dict.Count < 3)
{
UISkinImporter.logger.LogMessage("unsupported color definition {0}: color object requires at least 3 members", key);
continue;
}

if (dict["r"] is long)
{
color = new Color(Convert.ToInt32(dict["r"]), Convert.ToInt32(dict["g"]), Convert.ToInt32(dict["b"]));
if (dict.Count == 4)
color = new Color(color, Convert.ToInt32(dict["a"]));
}
else if (dict["r"] is double)
{
color = new Color(Convert.ToSingle(dict["r"]), Convert.ToSingle(dict["g"]), Convert.ToSingle(dict["b"]));
if (dict.Count == 4)
color = new Color(color, Convert.ToSingle(dict["a"]));
}
}

result.Add(key, color);
}

UISkinImporter.logger.LogMessage( "" );
return result;
}
}
}

using Microsoft.Xna.Framework.Content.Pipeline;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Microsoft.Xna.Framework;
using Nez.UI;
using System;

namespace Nez.UISkinImporter
{
[ContentProcessor( DisplayName = "UISkin Processor" )]
public class UISkinProcessor : ContentProcessor<Dictionary<string, object>, UISkinConfig>
{
public override UISkinConfig Process( Dictionary<string, object> input, ContentProcessorContext context )
{
var skinConfig = new UISkinConfig();
var styleConfig = new UISkinStyleConfig();

foreach( var key in input.Keys )
{
// special cases first
if( key == "colors" )
{
skinConfig.colors = parseColors( input[key] as Dictionary<string, object> );
}
else if( key == "libGdxAtlases" )
{
var jArr = input[key] as JArray;
skinConfig.libGdxAtlases = jArr.ToObject<string[]>();
UISkinImporter.logger.LogMessage( "added {0} LibGdxAtlases\n", jArr.Count );
}
else if( key == "textureAtlases" )
{
var jArr = input[key] as JArray;
skinConfig.textureAtlases = jArr.ToObject<string[]>();
UISkinImporter.logger.LogMessage( "added {0} TextureAtlases\n", jArr.Count );
}
else
{
UISkinImporter.logger.LogMessage( "adding style type: {0}", key );
styleConfig.Add( key, input[key] );
}
}

if( styleConfig.Keys.Count > 0 )
skinConfig.styles = styleConfig;

return skinConfig;
}


Dictionary<string, Color> parseColors( Dictionary<string, object> colors )
{
if( colors.Count == 0 )
return null;

var result = new Dictionary<string, Color>( colors.Count );
foreach( var key in colors.Keys )
{
var obj = colors[key];
UISkinImporter.logger.LogMessage( "adding color: {0}", key );
Color color;

// Allow the passing of strings (hex), arrays and objects (r:, g:, b:) to
// represent colors. Also detect the usage of integers or normalized floats
if( obj is string )
{
var val = obj as string;

// we could have hex or hex
if( val.StartsWith( "#" ) )
color = ColorExt.hexToColor( val.Substring( 1 ) );
else if( val.StartsWith( "0x" ) )
color = ColorExt.hexToColor( val.Substring( 2 ) );
else
{
UISkinImporter.logger.LogMessage( "unsupported color definition {0}: {1}", key, val );
continue;
}
}
else if( obj is JArray )
{
var jArr = obj as JArray;
if( jArr.Count < 3 )
{
UISkinImporter.logger.LogMessage( "unsupported color definition {0}: color array requires at least 3 members", key );
continue;
}

if( jArr[0].Type == JTokenType.Integer )
{
var arr = jArr.ToObject<int[]>();
color = new Color( arr[0], arr[1], arr[2] );
if( arr.Length == 4 )
color = new Color( color, arr[3] );
}
else if( jArr[0].Type == JTokenType.Float )
{
var arr = jArr.ToObject<float[]>();
color = new Color( arr[0], arr[1], arr[2] );
if( arr.Length == 4 )
color = new Color( color, arr[3] );
}
else
{
UISkinImporter.logger.LogMessage( "unsupported color definition {0}: unknown type", key );
continue;
}
}
else if( obj is Dictionary<string, object> )
{
var dict = obj as Dictionary<string, object>;
if( dict.Count < 3 )
{
UISkinImporter.logger.LogMessage( "unsupported color definition {0}: color object requires at least 3 members", key );
continue;
}

if( dict["r"] is long )
{
color = new Color( Convert.ToInt32( dict["r"] ), Convert.ToInt32( dict["g"] ), Convert.ToInt32( dict["b"] ) );
if( dict.Count == 4 )
color = new Color( color, Convert.ToInt32( dict["a"] ) );
}
else if( dict["r"] is double )
{
color = new Color( Convert.ToSingle( dict["r"] ), Convert.ToSingle( dict["g"] ), Convert.ToSingle( dict["b"] ) );
if( dict.Count == 4 )
color = new Color( color, Convert.ToSingle( dict["a"] ) );
}
}

result.Add( key, color );
}

UISkinImporter.logger.LogMessage( "" );
return result;
}
}
}
26 changes: 13 additions & 13 deletions Nez.Portable/PipelineRuntime/UISkin/UISkinConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Nez.UI
{
public class UISkinConfig
{
public Dictionary<string,Color> colors;
public Dictionary<string, Color> colors;
public string[] textureAtlases;
public string[] libGdxAtlases;
public UISkinStyleConfig styles;
Expand All @@ -20,39 +20,39 @@ public bool containsColor( string name )


public class UISkinStyleConfig : Dictionary<string, object>
{
{
/// <summary>
/// gets all the style class names included in the config object
/// </summary>
/// <returns>The styles.</returns>
public List<string> getStyleClasses()
{
return new List<string>( Keys );
}


}


/// <summary>
/// gets all the style names in the config object for the given styleType
/// </summary>
/// <returns>The style names.</returns>
/// <param name="styleType">Style type.</param>
public List<string> getStyleNames( string styleType )
{
var type = this[styleType] as Dictionary<string, object>;
{
var type = this[styleType] as Dictionary<string, object>;
return new List<string>( type.Keys );
}


}


/// <summary>
/// gets a style config dict for the styleType -> styleName
/// </summary>
/// <returns>The style.</returns>
/// <param name="styleType">Style type.</param>
/// <param name="styleName">Style name.</param>
public Dictionary<string, object> getStyleDict( string styleType, string styleName )
{
var styleDict = this[styleType] as Dictionary<string, object>;
return styleDict[styleName] as Dictionary<string, object>;
{
var styleDict = this[styleType] as Dictionary<string, object>;
return styleDict[styleName] as Dictionary<string, object>;
}
}
}
Expand Down

0 comments on commit b0c4068

Please sign in to comment.