using SFML.Graphics; using SFML.System; using SFML.Window; using System.Collections.Generic; namespace Otter { /// /// Graphic that renders as a simple gradient between 4 points. /// public class Gradient : Graphic { #region Private Fields List colors = new List(); List baseColors = new List(); #endregion #region Constructors /// /// Create a new Gradient using 4 Colors for each corner. /// /// The width of the gradient. /// The height of the gradient. /// The Color of the top left corner. /// The Color of the top right corner. /// The Color of the bottom right corner. /// The Color of the bottom left corner. public Gradient(int width, int height, Color TopLeft, Color TopRight, Color BottomRight, Color BottomLeft) { baseColors.Add(TopLeft); baseColors.Add(TopRight); baseColors.Add(BottomRight); baseColors.Add(BottomLeft); colors.Add(TopLeft); colors.Add(TopRight); colors.Add(BottomRight); colors.Add(BottomLeft); Width = width; Height = height; } /// /// Create a new Gradient using another Gradient. /// /// The source Gradient to copy. public Gradient(Gradient copy) : this(copy.Width, copy.Height, copy.GetColor(ColorPosition.TopLeft), copy.GetColor(ColorPosition.TopRight), copy.GetColor(ColorPosition.BottomRight), copy.GetColor(ColorPosition.BottomLeft)) { } #endregion #region Private Methods protected override void UpdateDrawable() { base.UpdateDrawable(); SFMLVertices.Clear(); var finalColors = new List() { Color.None, Color.None, Color.None, Color.None}; for (int i = 0; i < baseColors.Count; i++) { finalColors[i] = new Color(baseColors[i]); finalColors[i] *= Color; finalColors[i].A *= Alpha; } SFMLVertices.Append(new Vertex(new Vector2f(0, 0), finalColors[0].SFMLColor)); SFMLVertices.Append(new Vertex(new Vector2f(Width, 0), finalColors[1].SFMLColor)); SFMLVertices.Append(new Vertex(new Vector2f(Width, Height), finalColors[2].SFMLColor)); SFMLVertices.Append(new Vertex(new Vector2f(0, Height), finalColors[3].SFMLColor)); } #endregion #region Public Methods /// /// Set the Color of a specific position. /// /// The new Color. /// The position to change the Color on. public void SetColor(Color color, ColorPosition position) { colors[(int)position] = color; NeedsUpdate = true; } /// /// Get the Color of a specific position. /// /// The position to get the Color of. /// public Color GetColor(ColorPosition position) { return colors[(int)position]; } #endregion #region Enum public enum ColorPosition { TopLeft, TopRight, BottomRight, BottomLeft } #endregion } }