using SFML.Graphics;
using SFML.System;
using SFML.Window;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Xml;
namespace Otter {
///
/// Various extensions for classes are in here.
///
public static class Extensions {
#region XML
///
/// Parse an attribute from an XmlElement as an int.
///
/// The XmlElement to parse.
/// The name of the attribute.
/// An value as an int.
public static int AttributeInt(this XmlNode xml, string name) {
return int.Parse(xml.Attributes[name].Value);
}
///
/// Parse an attribute from an XmlElement as an int.
///
/// The XmlElement to parse.
/// The name of the attribute.
/// The default value to return if that attribute doesn't exist.
/// The value as an int.
public static int AttributeInt(this XmlNode xml, string name, int returnOnNull) {
if (xml == null) return returnOnNull;
if (xml.Attributes[name] == null) return returnOnNull;
return int.Parse(xml.Attributes[name].Value);
}
///
/// Parse an attibute from an XmlElement as a string.
///
/// The XmlElement to parse.
/// The name of the attribute.
/// The value as a string.
public static string AttributeString(this XmlNode xml, string name) {
return xml.Attributes[name].Value;
}
///
/// Parse an attibute from an XmlElement as a string.
///
/// The XmlElement to parse.
/// The name of the attribute.
/// The default value to return if that attribute doesn't exist.
/// The value as a string.
public static string AttributeString(this XmlNode xml, string name, string returnOnNull) {
if (xml == null) return returnOnNull;
if (xml.Attributes[name] == null) return returnOnNull;
return xml.Attributes[name].Value;
}
///
/// Parse an attribute from an XmlElement as a float.
///
/// The XmlElement to parse.
/// The name of the attribute.
/// The value as a float.
public static float AttributeFloat(this XmlNode xml, string name) {
return float.Parse(xml.Attributes[name].Value);
}
///
/// Parse an attribute from an XmlElement as a float.
///
/// The XmlElement to parse.
/// The name of the attribute.
/// The default value to return if that attribute doesn't exist.
/// The value as a float.
public static float AttributeFloat(this XmlNode xml, string name, float returnOnNull) {
if (xml == null) return returnOnNull;
if (xml.Attributes[name] == null) return returnOnNull;
return float.Parse(xml.Attributes[name].Value);
}
///
/// Parse an attribute from an XmlElement as a bool.
///
/// The XmlElement to parse.
/// The name of the attribute.
/// The value as a bool.
public static bool AttributeBool(this XmlNode xml, string name) {
return bool.Parse(xml.Attributes[name].Value);
}
///
/// Parse an attribute from an XmlElement as a bool.
///
/// The XmlElement to parse.
/// The name of the attribute.
/// The default value to return if that attribute doesn't exist.
/// The value as a bool.
public static bool AttributeBool(this XmlNode xml, string name, bool returnOnNull) {
if (xml == null) return returnOnNull;
if (xml.Attributes[name] == null) return returnOnNull;
return bool.Parse(xml.Attributes[name].Value);
}
///
/// Parse an attribute from an XmlElement as a Color.
///
/// The XmlElement to parse.
/// The name of the attribute.
/// The value as a Color.
public static Color AttributeColor(this XmlNode xml, string name) {
return new Color(xml.Attributes[name].Value);
}
///
/// Parse an attribute from an XmlElement as a Color.
///
/// The XmlElement to parse.
/// The name of the attribute.
/// The default value to return if that attribute doesn't exist.
/// The value as a Color.
public static Color AttributeColor(this XmlNode xml, string name, Color returnOnNull) {
if (xml == null) return returnOnNull;
if (xml.Attributes[name] == null) return returnOnNull;
return new Color(xml.Attributes[name].Value);
}
///
/// Parse the inner text of an XmlElement as an int.
///
/// The XmlElement to parse.
/// The value as an int.
public static int InnerInt(this XmlNode xml) {
return int.Parse(xml.InnerText);
}
public static int InnerInt(this XmlNode xml, int returnOnNull) {
if (xml != null) return xml.InnerInt();
return returnOnNull;
}
///
/// Parse the inner text of an XmlElement as a float.
///
///
/// The value as a float.
public static float InnerFloat(this XmlNode xml) {
return float.Parse(xml.InnerText);
}
public static float InnerFloat(this XmlNode xml, float returnOnNull) {
if (xml != null) return xml.InnerFloat();
return returnOnNull;
}
public static string InnerText(this XmlNode xml, string returnOnNull) {
if (xml != null) return xml.InnerText;
return returnOnNull;
}
///
/// Parse the inner text of an XmlElement as a bool.
///
///
/// The value as a bool.
public static bool InnerBool(this XmlNode xml) {
return bool.Parse(xml.InnerText);
}
///
/// Parse the inner text of an XmlElement as a Color.
///
///
/// The value as a Color.
public static Color InnerColor(this XmlNode xml) {
return new Color(xml.InnerText);
}
///
/// Parse an int from an attribute collection.
///
///
/// The attribute to parse.
/// The value to return if null.
/// The attribute as an int.
public static int Int(this XmlAttributeCollection a, string name, int returnOnNull) {
if (a[name] == null) return returnOnNull;
return int.Parse(a[name].Value);
}
#endregion
#region String
// String Extensions
static public string ClearWhitespace(this string str) {
return Regex.Replace(str, @"\s+", "");
}
#endregion
#region Dictionary
///
/// Get a value out of a Dictionary of strings as an int, and return a default value if the key
/// is not present in the Dictionary.
///
/// The Dictionary.
/// The key to search for.
/// The value to return if that key is not found.
/// The value from the Dictionary as an int.
static public int ValueAsInt(this Dictionary d, string key, int onNull = 0) {
if (d.ContainsKey(key)) {
return int.Parse(d[key]);
}
return onNull;
}
///
/// Get a value out of a Dictionary of strings as a a float, and return a default value if the key
/// is not present in the Dictionary.
///
/// The Dictionary.
/// The key to search for.
/// The value to return if that key is not found.
/// The value from the Dictionary as an float.
static public float ValueAsFloat(this Dictionary d, string key, float onNull = 0) {
if (d.ContainsKey(key)) {
return float.Parse(d[key]);
}
return onNull;
}
///
/// Get a value out of a Dictionary of strings as a a bool, and return a default value if the key
/// is not present in the Dictionary.
///
/// The Dictionary.
/// The key to search for.
/// The value to return if that key is not found.
/// The value from the Dictionary as a bool.
static public bool ValueAsBool(this Dictionary d, string key, bool onNull = false) {
if (d.ContainsKey(key)) {
return bool.Parse(d[key]);
}
return onNull;
}
///
/// Get the value out of a Dictionary of strings as a Color, and return a default value if the key
/// is not present in the Dictionary.
///
/// The Dictionary.
/// The key to search for.
/// The value to return if that key is not found.
/// The value fro the Dictionary as a Color.
static public Color ValueAsColor(this Dictionary d, string key, Color onNull = null) {
if (d.ContainsKey(key)) {
return new Color(d[key]);
}
return onNull;
}
static public void AddOrUpdate(this Dictionary d, TKey key, TValue value) {
if (d.ContainsKey(key)) d[key] = value;
else d.Add(key, value);
}
static public TValue GetOrAdd(this Dictionary d, TKey key, TValue value = default(TValue)) {
if (d.ContainsKey(key)) return d[key];
d.Add(key, value);
return value;
}
#endregion
#region List
///
/// Removes an item from a list only if the list contains that item.
///
///
///
/// The element to remove.
/// True if the element was removed successfully.
static public bool RemoveIfContains(this List l, T element) {
if (l.Contains(element)) {
l.Remove(element);
return true;
}
return false;
}
///
/// Add multiple items to a list.
///
/// The type of items.
/// The list.
/// The elements to add.
static public void Add(this List l, params T[] elements) {
foreach (var i in elements) {
l.Add(i);
}
}
static public void MoveForward(this List list, T item) {
var oldIndex = list.IndexOf(item);
list.Remove(item);
list.InsertOrAdd(oldIndex - 1, item);
}
public static void MoveBackward(this List list, T item) {
var oldIndex = list.IndexOf(item);
list.Remove(item);
list.InsertOrAdd(oldIndex + 1, item);
}
public static void MoveToFront(this List list, T item) {
list.Remove(item);
list.InsertOrAdd(0, item);
}
public static void MoveToBack(this List list, T item) {
list.Remove(item);
list.Add(item);
}
#endregion
#region SFML
public static void Append(this VertexArray vertices, float x, float y, Color color, float tx, float ty) {
vertices.Append(new Vertex(new Vector2f(x, y), color.SFMLColor, new Vector2f(tx, ty)));
}
public static void Append(this VertexArray vertices, float x, float y, Color color = null) {
if (color == null) color = Color.White;
vertices.Append(new Vertex(new Vector2f(x, y), color.SFMLColor));
}
public static void Append(this VertexArray vertices, double x, double y, Color color) {
vertices.Append(new Vertex(new Vector2f((float)x, (float)y), color.SFMLColor));
}
public static void Append(this VertexArray vertices, Vert vert) {
vertices.Append(vert.X, vert.Y, vert.Color, vert.U, vert.V);
}
#endregion
#region Other
///
/// Converts a RGB or RGBA uint value to a Color.
///
/// The uint.
/// A new Color from the uint.
public static Color ToColor(this uint i) {
return new Color(i);
}
///
/// Converts a RGB or RGBA int value to a Color.
///
/// The int.
/// A new Color from the int.
public static Color ToColor(this int i) {
return new Color((uint)i);
}
///
/// Check to see if a flags enumeration has a specific flag set.
///
/// Flags enumeration to check
/// Flag to check for
/// True if the Enum contains the flag.
public static bool HasFlag(this Enum variable, Enum value) {
// http://stackoverflow.com/questions/4108828/generic-extension-method-to-see-if-an-enum-contains-a-flag
if (variable == null)
return false;
if (value == null)
throw new ArgumentNullException("value");
// Not as good as the .NET 4 version of this function, but should be good enough
if (!Enum.IsDefined(variable.GetType(), value)) {
throw new ArgumentException(string.Format(
"Enumeration type mismatch. The flag is of type '{0}', was expecting '{1}'.",
value.GetType(), variable.GetType()));
}
ulong num = Convert.ToUInt64(value);
return ((Convert.ToUInt64(variable) & num) == num);
}
#endregion
}
}