public override OgmoValueDefinition ReadJson(JsonReader reader, Type objectType, OgmoValueDefinition existingValue, bool hasExistingValue, JsonSerializer serializer) { JObject obj = JObject.Load(reader); switch ((string)obj["definition"]) { case "String": var strValue = new OgmoStringValueDefinition(); strValue.Name = (string)obj["name"]; strValue.Default = (string)obj["defaults"]; strValue.MaxLength = (int)obj["maxLength"]; strValue.TrimWhitespace = (bool)obj["trimWhitespace"]; return(strValue); case "Text": var textValue = new OgmoTextValueDefinition(); textValue.Name = (string)obj["name"]; textValue.Default = (string)obj["defaults"]; return(textValue); case "Enum": var enumValue = new OgmoEnumValueDefinition(); enumValue.Name = (string)obj["name"]; var arr = obj["choices"].Children().ToArray(); var finalChoices = new string[arr.Length]; for (int i = 0; i < arr.Length; i++) { finalChoices[i] = (string)arr[i]; } enumValue.Choices = finalChoices; enumValue.Default = (int)obj["defaults"]; return(enumValue); case "Integer": var intValue = new OgmoIntegerValueDefinition(); intValue.Name = (string)obj["name"]; intValue.Default = (int)obj["defaults"]; intValue.Bounded = (bool)obj["bounded"]; intValue.Min = (int)obj["min"]; intValue.Max = (int)obj["max"]; return(intValue); case "Float": var floatValue = new OgmoFloatValueDefinition(); floatValue.Name = (string)obj["name"]; floatValue.Default = (float)obj["defaults"]; floatValue.Bounded = (bool)obj["bounded"]; floatValue.Min = (float)obj["min"]; floatValue.Max = (float)obj["max"]; return(floatValue); case "Color": var colValue = new OgmoColorValueDefinition(); colValue.Name = (string)obj["name"]; colValue.Default = ColorExt.HexToColor((string)obj["defaults"]); colValue.IncludeAlpha = (bool)obj["includeAlpha"]; return(colValue); } return(null); }
public static Color ParseColor(XAttribute xColor) { if (xColor == null) { return(Color.White); } var colorStr = ((string)xColor).TrimStart("#".ToCharArray()); return(ColorExt.HexToColor(colorStr)); }
public override OgmoEntity ReadJson(JsonReader reader, Type objectType, OgmoEntity existingValue, bool hasExistingValue, JsonSerializer serializer) { JObject obj = JObject.Load(reader); OgmoProject project = serializer.Context.Context as OgmoProject; OgmoValueDefinition[] defs = null; var ent = new OgmoEntity(); ent.Target = (string)obj["name"]; for (int i = 0; i < project.EntityDefinitions.Length; i++) { if (project.EntityDefinitions[i].Name == ent.Target) { defs = project.EntityDefinitions[i].ValueDefinitions; break; } } ent.ExportID = (string)obj["_eid"]; ent.WorldID = (int)obj["id"]; ent.Position = new Point((int)obj["x"], (int)obj["y"]); ent.Origin = new Point((int)obj["originX"], (int)obj["originY"]); var valArray = obj["values"] as JObject; ent.Values = new Dictionary <string, OgmoValue>(); if (defs == null || valArray == null) { if (defs == null) { Debug.Warn($"Definition member was not found for type {ent.Target}"); } return(ent); } foreach (var pair in valArray) { for (int x = 0; x < defs.Length; x++) { if (defs[x].Name == pair.Key) { var definition = defs[x]; switch (definition) { case OgmoColorValueDefinition color: var colorVal = new OgmoColorValue(); colorVal.Name = pair.Key; colorVal.Definition = color; colorVal.Value = ColorExt.HexToColor((string)pair.Value); ent.Values.Add(pair.Key, colorVal); break; case OgmoStringValueDefinition str: var stringVal = new OgmoStringValue(); stringVal.Name = pair.Key; stringVal.Definition = str; stringVal.Value = (string)pair.Value; ent.Values.Add(pair.Key, stringVal); break; case OgmoEnumValueDefinition enu: var enumVal = new OgmoEnumValue(); enumVal.Name = pair.Key; enumVal.Definition = enu; enumVal.Value = (int)pair.Value; ent.Values.Add(pair.Key, enumVal); break; case OgmoFloatValueDefinition flo: var floatVal = new OgmoFloatValue(); floatVal.Name = pair.Key; floatVal.Definition = flo; floatVal.Value = (float)pair.Value; ent.Values.Add(pair.Key, floatVal); break; case OgmoTextValueDefinition text: var textValue = new OgmoTextValue(); textValue.Name = pair.Key; textValue.Definition = text; textValue.Value = (string)pair.Value; ent.Values.Add(pair.Key, textValue); break; } } } } return(ent); }
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 = default; // 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); }
public override OgmoLevel ReadJson(JsonReader reader, Type objectType, OgmoLevel existingValue, bool hasExistingValue, JsonSerializer serializer) { JObject obj = JObject.Load(reader); OgmoProject project = serializer.Context.Context as OgmoProject; OgmoValueDefinition[] defs = project.LevelValueDefinitions; OgmoLevel level = new OgmoLevel(); level.LevelSize = new Point((int)obj["width"], (int)obj["height"]); level.OgmoVersion = (string)obj["ogmoVersion"]; // Layers. var layerList = obj["layers"].Children().ToArray(); level.Layers = new OgmoLayer[layerList.Length]; for (int x = 0; x < layerList.Length; x++) { OgmoLayer result = (OgmoLayer)OgmoLayerConverter.Instance.ReadJson(layerList[x].CreateReader(), typeof(OgmoLayer), null, serializer); level.Layers[x] = result; } // Values. var valArray = obj["values"] as JObject; level.Values = new Dictionary <string, OgmoValue>(); if (valArray == null) { return(level); } foreach (var pair in valArray) { for (int x = 0; x < defs.Length; x++) { if (defs[x].Name == pair.Key) { var definition = defs[x]; switch (definition) { case OgmoColorValueDefinition color: var colorVal = new OgmoColorValue(); colorVal.Name = pair.Key; colorVal.Definition = color; colorVal.Value = ColorExt.HexToColor((string)pair.Value); level.Values.Add(pair.Key, colorVal); break; case OgmoStringValueDefinition str: var stringVal = new OgmoStringValue(); stringVal.Name = pair.Key; stringVal.Definition = str; stringVal.Value = (string)pair.Value; level.Values.Add(pair.Key, stringVal); break; case OgmoEnumValueDefinition enu: var enumVal = new OgmoEnumValue(); enumVal.Name = pair.Key; enumVal.Definition = enu; var enumstring = (string)pair.Value; enumVal.Value = Array.IndexOf(enu.Choices, enumstring); level.Values.Add(pair.Key, enumVal); break; case OgmoFloatValueDefinition flo: var floatVal = new OgmoFloatValue(); floatVal.Name = pair.Key; floatVal.Definition = flo; floatVal.Value = (float)pair.Value; level.Values.Add(pair.Key, floatVal); break; case OgmoTextValueDefinition text: var textValue = new OgmoTextValue(); textValue.Name = pair.Key; textValue.Definition = text; textValue.Value = (string)pair.Value; level.Values.Add(pair.Key, textValue); break; } } } } return(level); }