示例#1
0
    /// <summary>
    /// Gets a style-specific attribute value.
    /// </summary>
    /// <typeparam name="T">The type of the attribute value.</typeparam>
    /// <param name="style">The style.</param>
    /// <param name="name">The name of the attribute.</param>
    /// <param name="result">The attribute value.</param>
    /// <returns>
    /// <see langword="true"/> if the renderer can provide a value for the attribute; otherwise, 
    /// <see langword="false"/> if the renderer does not know the style or the attribute.
    /// </returns>
    /// <remarks>
    /// This method calls <see cref="OnParseAttribute{T}"/> to convert a 
    /// <see cref="ThemeAttribute"/> to a value of type <typeparamref name="T"/>.
    /// </remarks>
    public bool GetAttribute<T>(string style, string name, out T result)
    {
      result = default(T);

      if (style == null)
        return false;
      if (string.IsNullOrEmpty(name))
        return false;

      // Get style.
      ThemeStyle themeStyle;
      bool found = Theme.Styles.TryGet(style, out themeStyle);
      if (!found)
        return false;

      // Search for attribute including style inheritance.
      ThemeAttribute attribute = null;
      while (attribute == null && themeStyle != null)
      {
        if (!themeStyle.Attributes.TryGet(name, out attribute))
        {
          // Try ancestor.
          themeStyle = themeStyle.Inherits;
        }
      }

      if (attribute == null)
        return false;

      return OnParseAttribute(attribute, out result);
    }
示例#2
0
    protected bool OnParseAttribute<T>(ThemeAttribute attribute, out T result)
    {
      result = default(T);

      if (result is Vector2F)
      {
        result = (T)(object)ThemeHelper.ParseVector2F(attribute.Value);
      }
      else if (result is Vector3)
      {
        result = (T)(object)ThemeHelper.ParseVector3(attribute.Value);
      }
      else if (result is Vector4)
      {
        result = (T)(object)ThemeHelper.ParseVector4(attribute.Value);
      }
      else if (typeof(T) == typeof(string) || typeof(T) == typeof(object))
      {
        result = (T)(object)attribute.Value;
      }
      else if (result is Color)
      {
        result = (T)(object)ThemeHelper.ParseColor(attribute.Value, Color.Black);
      }

      else if (typeof(T).IsAssignableFrom(typeof(Rectangle)))
#else
      else if (typeof(T).GetTypeInfo().IsAssignableFrom(typeof(Rectangle).GetTypeInfo()))

      {
        result = (T)(object)ThemeHelper.ParseRectangle(attribute.Value);
      }

      else if (typeof(T).IsAssignableFrom(typeof(Texture2D)))
#else
      else if (typeof(T).GetTypeInfo().IsAssignableFrom(typeof(Texture2D).GetTypeInfo()))

      {
        result = (T)(object)GetTexture(attribute.Value);
      }
      else
      {
        try
        {
          result = ObjectHelper.Parse<T>(attribute.Value);
        }
        catch
        {
          return false;
        }
      }

      return true;
    }