Advertisement
Yassine_Abbani

Rating [Tools], [Custom Control]

May 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 25.00 KB | None | 0 0
  1. #region Derectives
  2. using System;
  3. using System.Windows.Forms;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. #endregion
  8.  
  9. #region Copyright & Contact
  10. //>-------------------------------------------------------------------------------<
  11. // Tool Name    : Rating Strip                                                     *
  12. // From Project : Creator Eye                                                      *
  13. // Creator      : Yassine Abbani                                                   *
  14. // Facebook     : https://www.facebook.com/YassineAbbani.user                      *
  15. // Pastebin     : https://pastebin.com/u/Yassine_Abbani                            *
  16. // Youtube      : https://www.youtube.com/channel/UCqvOCRs3HWbPH4yuZuTx8mw         *
  17. // Version      : 1.0 Beta                                                         *
  18. // Color        : Multi Color                                                      *
  19. // Style        : Star                                                             *
  20. //>-------------------------------------------------------------------------------<
  21. #endregion
  22. #region Browse
  23. //>-------------------------------------------------------------------------------<
  24. // Costume Propority                                                               *
  25. // Settings    :                                                                   *
  26. //   "Tools"      *      "Border"    *        "Star"            "Shape"            *
  27. // star spacing   *   Normal Color   *     Normal Color   *    Normal              *
  28. // star rating    *   Hover  Color   *     Hover  Color   *    Detailed            *
  29. // srat counting  *      Width       *        Width       *    Faat                *
  30. //>-------------------------------------------------------------------------------<
  31. #endregion
  32.  
  33. #region StarRatingStrip
  34. class Ce_Rating : Control
  35. {
  36.     #region Enums
  37.     public enum StarType { Normal, Fat, Detailed };
  38.     #endregion
  39.     #region Variables
  40.     private StarType _starType = StarType.Fat;
  41.     private BufferedGraphics _bufGraphics;
  42.     private readonly BufferedGraphicsContext _bufContext = BufferedGraphicsManager.Current;
  43.     private Pen _starStroke = new Pen(Color.FromArgb(70, 130, 180), 1f);
  44.     private Pen _starDullStroke = new Pen(Color.FromArgb(70, 130, 180), 3f);
  45.     private SolidBrush _starBrush = new SolidBrush(Color.FromArgb(70, 130, 180));
  46.     private SolidBrush _starDullBrush = new SolidBrush(Color.Transparent);
  47.     private int _starCount = 5;
  48.     private int _starSpacing = 23;
  49.     private int _starWidth = 40;
  50.     private float _mouseOverIndex = -1;
  51.     private float _rating;
  52.     private bool _allowHalfStarRating, _settingRating;
  53.     #endregion
  54.     #region Event Args
  55.  
  56.     protected override void OnMouseMove(MouseEventArgs e)
  57.     {
  58.         base.OnMouseMove(e);
  59.         if (_rating > 0) return;
  60.         float index = GetHoveredStarIndex(e.Location);
  61.  
  62.         if (index != _mouseOverIndex)
  63.         {
  64.             _mouseOverIndex = index;
  65.             OnStarsPanned();
  66.             this.Invalidate();
  67.         }
  68.     }
  69.  
  70.     protected override void OnMouseClick(MouseEventArgs e)
  71.     {
  72.         base.OnMouseClick(e);
  73.  
  74.         if (_rating == 0f)
  75.         {
  76.             _settingRating = true;
  77.             Rating = (_allowHalfStarRating) ? _mouseOverIndex : _mouseOverIndex + 1f;
  78.             _settingRating = false;
  79.             this.Invalidate();
  80.         }
  81.     }
  82.  
  83.     protected override void OnMouseLeave(EventArgs e)
  84.     {
  85.         base.OnMouseLeave(e);
  86.         if (_rating > 0) return;
  87.         _mouseOverIndex = -1; // No stars will be highlighted
  88.         this.Invalidate();
  89.     }
  90.  
  91.     protected override void OnSizeChanged(EventArgs e)
  92.     {
  93.         UpdateSize();
  94.         UpdateGraphicsBuffer();
  95.     }
  96.     #endregion
  97.     #region  Constructors
  98.     public Ce_Rating()
  99.     {
  100.         this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint |
  101.             ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true);
  102.  
  103.         SetPenBrushDefaults();
  104.         this.Size = new Size(200, 100);
  105.         UpdateGraphicsBuffer();
  106.     }
  107.     #endregion
  108.     #region Draw Control
  109.     #region Float
  110.     private static float RoundToNearestHalf(float f)
  111.     {
  112.         return (float)Math.Round(f / 5.0, 1) * 5f;
  113.     }
  114.     private float GetHoveredStarIndex(Point pos)
  115.     {
  116.         if (_allowHalfStarRating)
  117.         {
  118.             float widthSection = this.Width / (float)_starCount / 2f;
  119.  
  120.             for (float i = 0f; i < _starCount; i += 0.5f)
  121.             {
  122.                 float starX = i * widthSection * 2f;
  123.  
  124.                 // If cursor is within the x region of the iterated star
  125.                 if (pos.X >= starX && pos.X <= starX + widthSection)
  126.                 {
  127.                     return i + 0.5f;
  128.                 }
  129.             }
  130.  
  131.             return -1;
  132.         }
  133.         else
  134.         {
  135.             int widthSection = (int)(this.Width / (double)_starCount + 0.5);
  136.  
  137.             for (int i = 0; i < _starCount; i++)
  138.             {
  139.                 float starX = i * widthSection;
  140.  
  141.                 // If cursor is within the x region of the iterated star
  142.                 if (pos.X >= starX && pos.X <= starX + widthSection)
  143.                 {
  144.                     return i;
  145.                 }
  146.             }
  147.         }
  148.  
  149.         return -1;
  150.     }
  151.  
  152.     #endregion
  153.     #region Void
  154.     private void DrawDullStars()
  155.     {
  156.         float height = this.Height - _starStroke.Width;
  157.         float lastX = _starStroke.Width / 2f; // Start off at stroke size and increment
  158.         float width = (this.Width - TotalSpacing - TotalStrokeWidth) / (float)_starCount;
  159.  
  160.         // Draw stars
  161.         for (int i = 0; i < _starCount; i++)
  162.         {
  163.             RectangleF rect = new RectangleF(lastX, _starStroke.Width / 2f, width, height);
  164.             PointF[] polygon = GetStarPolygon(rect);
  165.             _bufGraphics.Graphics.FillPolygon(_starDullBrush, polygon);
  166.             _bufGraphics.Graphics.DrawPolygon(_starDullStroke, polygon);
  167.             lastX += _starWidth + _starSpacing + _starStroke.Width;
  168.             _bufGraphics.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
  169.             _bufGraphics.Graphics.FillPolygon(_starDullBrush, polygon);
  170.             _bufGraphics.Graphics.DrawPolygon(_starDullStroke, polygon);
  171.             _bufGraphics.Graphics.PixelOffsetMode = PixelOffsetMode.Default;
  172.         }
  173.     }
  174.     private void DrawIllumStars()
  175.     {
  176.         float height = this.Height - _starStroke.Width;
  177.         float lastX = _starStroke.Width / 2f; // Start off at stroke size and increment
  178.         float width = (this.Width - TotalSpacing - TotalStrokeWidth) / _starCount;
  179.  
  180.         if (_allowHalfStarRating)
  181.         {
  182.             // Draw stars
  183.             for (int i = 0; i < _starCount; i++)
  184.             {
  185.                 RectangleF rect = new RectangleF(lastX, _starStroke.Width / 2f, width, height);
  186.  
  187.                 if (i < _mouseOverIndex - 0.5f)
  188.                 {
  189.                     PointF[] polygon = GetStarPolygon(rect);
  190.                     _bufGraphics.Graphics.FillPolygon(_starBrush, polygon);
  191.                     _bufGraphics.Graphics.DrawPolygon(_starStroke, polygon);
  192.                 }
  193.                 else if (i == _mouseOverIndex - 0.5f)
  194.                 {
  195.                     PointF[] polygon = GetSemiStarPolygon(rect);
  196.                     _bufGraphics.Graphics.FillPolygon(_starBrush, polygon);
  197.                     _bufGraphics.Graphics.DrawPolygon(_starStroke, polygon);
  198.                 }
  199.                 else
  200.                 {
  201.                     break;
  202.                 }
  203.  
  204.                 lastX += _starWidth + _starSpacing + _starStroke.Width;
  205.             }
  206.         }
  207.         else
  208.         {
  209.             // Draw stars
  210.             for (int i = 0; i < _starCount; i++)
  211.             {
  212.                 RectangleF rect = new RectangleF(lastX, _starStroke.Width / 2f, width, height);
  213.                 PointF[] polygon = GetStarPolygon(rect);
  214.  
  215.                 if (i <= _mouseOverIndex)
  216.                 {
  217.                     _bufGraphics.Graphics.FillPolygon(_starBrush, polygon);
  218.                     _bufGraphics.Graphics.DrawPolygon(_starStroke, polygon);
  219.                 }
  220.                 else
  221.                 {
  222.                     break;
  223.                 }
  224.  
  225.                 lastX += _starWidth + _starSpacing + _starStroke.Width;
  226.             }
  227.         }
  228.     }
  229.     private void UpdateSize()
  230.     {
  231.         int height = (int)(_starWidth + _starStroke.Width + 0.5);
  232.         int width = (int)(TotalStarWidth + TotalSpacing + TotalStrokeWidth + 0.5);
  233.         this.Size = new Size(width, height);
  234.     }
  235.     private void UpdateGraphicsBuffer()
  236.     {
  237.         if (this.Width > 0 && this.Height > 0)
  238.         {
  239.             _bufContext.MaximumBuffer = new Size(this.Width + 1, this.Height + 1);
  240.             _bufGraphics = _bufContext.Allocate(this.CreateGraphics(), this.ClientRectangle);
  241.             _bufGraphics.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  242.         }
  243.     }
  244.     private void SetPenBrushDefaults()
  245.     {
  246.         _starStroke.LineJoin = LineJoin.Round;
  247.         _starStroke.Alignment = PenAlignment.Outset;
  248.         _starDullStroke.LineJoin = LineJoin.Round;
  249.         _starDullStroke.Alignment = PenAlignment.Outset;
  250.     }
  251.     protected virtual void OnStarsPanned()
  252.     {
  253.         if (StarsPanned != null)
  254.         {
  255.             StarsPanned(this, EventArgs.Empty);
  256.         }
  257.     }
  258.     protected virtual void OnRatingChanged()
  259.     {
  260.         if (RatingChanged != null)
  261.         {
  262.             RatingChanged(this, EventArgs.Empty);
  263.         }
  264.     }
  265.     #endregion
  266.     #region Piont
  267.     /// <summary>
  268.     /// Gets Star Polygon as a point[]
  269.     /// </summary>
  270.     private PointF[] GetStarPolygon(RectangleF rect)
  271.     {
  272.         switch (_starType)
  273.         {
  274.             case StarType.Normal: return GetNormalStar(rect);
  275.             case StarType.Fat: return GetFatStar(rect);
  276.             case StarType.Detailed: return GetDetailedStar(rect);
  277.             default: return null;
  278.         }
  279.     }
  280.     /// <summary>
  281.     /// Gets Semi star polygon as a point[]
  282.     /// </summary>
  283.     private PointF[] GetSemiStarPolygon(RectangleF rect)
  284.     {
  285.         switch (_starType)
  286.         {
  287.             case StarType.Normal: return GetNormalSemiStar(rect);
  288.             case StarType.Fat: return GetFatSemiStar(rect);
  289.             case StarType.Detailed: return GetDetailedSemiStar(rect);
  290.             default: return null;
  291.         }
  292.     }
  293.     /// <summary>
  294.     /// Gets a typical thin star polygon as a point[]
  295.     /// </summary>
  296.     private static PointF[] GetNormalStar(RectangleF rect)
  297.     {
  298.         return new[]
  299.             {
  300.                 new PointF(rect.X + rect.Width * 0.5f, rect.Y + rect.Height * 0f),
  301.                 new PointF(rect.X + rect.Width * 0.38f, rect.Y + rect.Height * 0.38f),
  302.                 new PointF(rect.X + rect.Width * 0f, rect.Y + rect.Height * 0.38f),
  303.                 new PointF(rect.X + rect.Width * 0.31f, rect.Y + rect.Height * 0.61f),
  304.                 new PointF(rect.X + rect.Width * 0.19f, rect.Y + rect.Height * 1f),
  305.                 new PointF(rect.X + rect.Width * 0.5f, rect.Y + rect.Height * 0.77f),
  306.                 new PointF(rect.X + rect.Width * 0.8f, rect.Y + rect.Height * 1f),
  307.                 new PointF(rect.X + rect.Width * 0.69f, rect.Y + rect.Height * 0.61f),
  308.                 new PointF(rect.X + rect.Width * 1f, rect.Y + rect.Height * 0.38f),
  309.                 new PointF(rect.X + rect.Width * 0.61f, rect.Y + rect.Height * 0.38f)
  310.              };
  311.     }
  312.     /// <summary>
  313.     /// Gets half of a typical thin star polygon as a point[]
  314.     /// </summary>
  315.     private static PointF[] GetNormalSemiStar(RectangleF rect)
  316.     {
  317.         return new[]
  318.             {
  319.                 new PointF(rect.X + rect.Width * 0.5f, rect.Y + rect.Height * 0f),
  320.                 new PointF(rect.X + rect.Width * 0.38f, rect.Y + rect.Height * 0.38f),
  321.                 new PointF(rect.X + rect.Width * 0f, rect.Y + rect.Height * 0.38f),
  322.                 new PointF(rect.X + rect.Width * 0.31f, rect.Y + rect.Height * 0.61f),
  323.                 new PointF(rect.X + rect.Width * 0.19f, rect.Y + rect.Height * 1f),
  324.                 new PointF(rect.X + rect.Width * 0.5f, rect.Y + rect.Height * 0.77f),
  325.              };
  326.     }
  327.     /// <summary>
  328.     /// Gets a fat star polygon as a point[]
  329.     /// </summary>
  330.     private static PointF[] GetFatStar(RectangleF rect)
  331.     {
  332.         return new[]
  333.             {
  334.                 new PointF(rect.X + rect.Width * 0.31f, rect.Y + rect.Height * 0.33f),
  335.                 new PointF(rect.X + rect.Width * 0f, rect.Y + rect.Height * 0.37f),
  336.                 new PointF(rect.X + rect.Width * 0.25f, rect.Y + rect.Height * 0.62f),
  337.                 new PointF(rect.X + rect.Width * 0.19f, rect.Y + rect.Height * 1f),
  338.                 new PointF(rect.X + rect.Width * 0.5f, rect.Y + rect.Height * 0.81f),
  339.                 new PointF(rect.X + rect.Width * 0.81f, rect.Y + rect.Height * 1f),
  340.                 new PointF(rect.X + rect.Width * 0.75f, rect.Y + rect.Height * 0.62f),
  341.                 new PointF(rect.X + rect.Width * 1f, rect.Y + rect.Height * 0.37f),
  342.                 new PointF(rect.X + rect.Width * 0.69f, rect.Y + rect.Height * 0.33f),
  343.                 new PointF(rect.X + rect.Width * 0.5f, rect.Y + rect.Height * 0f)
  344.             };
  345.     }
  346.     /// <summary>
  347.     /// Gets half of a fat star polygon as a point[]
  348.     /// </summary>
  349.     private static PointF[] GetFatSemiStar(RectangleF rect)
  350.     {
  351.         return new[]
  352.             {
  353.                 new PointF(rect.X + rect.Width * 0.31f, rect.Y + rect.Height * 0.33f),
  354.                 new PointF(rect.X + rect.Width * 0f, rect.Y + rect.Height * 0.37f),
  355.                 new PointF(rect.X + rect.Width * 0.25f, rect.Y + rect.Height * 0.62f),
  356.                 new PointF(rect.X + rect.Width * 0.19f, rect.Y + rect.Height * 1f),
  357.                 new PointF(rect.X + rect.Width * 0.5f, rect.Y + rect.Height * 0.81f),
  358.                 new PointF(rect.X + rect.Width * 0.5f, rect.Y + rect.Height * 0f)
  359.             };
  360.     }
  361.     /// <summary>
  362.     /// Gets a detailed star polygon as a point[]
  363.     /// </summary>
  364.     private static PointF[] GetDetailedStar(RectangleF rect)
  365.     {
  366.         return new[]
  367.             {
  368.                 new PointF(rect.X + rect.Width * 0.5f, rect.Y + rect.Height * 0f),
  369.                 new PointF(rect.X + rect.Width * 0.6f, rect.Y + rect.Height * 0.27f),
  370.                 new PointF(rect.X + rect.Width * 0.83f, rect.Y + rect.Height * 0.17f),
  371.                 new PointF(rect.X + rect.Width * 0.73f, rect.Y + rect.Height * 0.4f),
  372.                 new PointF(rect.X + rect.Width * 1f, rect.Y + rect.Height * 0.5f),
  373.                 new PointF(rect.X + rect.Width * 0.73f, rect.Y + rect.Height * 0.6f),
  374.                 new PointF(rect.X + rect.Width * 0.83f, rect.Y + rect.Height * 0.83f),
  375.                 new PointF(rect.X + rect.Width * 0.6f, rect.Y + rect.Height * 0.73f),
  376.                 new PointF(rect.X + rect.Width * 0.5f, rect.Y + rect.Height * 1f),
  377.                 new PointF(rect.X + rect.Width * 0.4f, rect.Y + rect.Height * 0.73f),
  378.                 new PointF(rect.X + rect.Width * 0.17f, rect.Y + rect.Height * 0.83f),
  379.                 new PointF(rect.X + rect.Width * 0.27f, rect.Y + rect.Height * 0.6f),
  380.                 new PointF(rect.X + rect.Width * 0f, rect.Y + rect.Height * 0.5f),
  381.                 new PointF(rect.X + rect.Width * 0.27f, rect.Y + rect.Height * 0.4f),
  382.                 new PointF(rect.X + rect.Width * 0.17f, rect.Y + rect.Height * 0.17f),
  383.                 new PointF(rect.X + rect.Width * 0.4f, rect.Y + rect.Height * 0.27f)
  384.             };
  385.     }
  386.     /// <summary>
  387.     /// Gets half of the detailed star polygon as a point[]
  388.     /// </summary>
  389.     private static PointF[] GetDetailedSemiStar(RectangleF rect)
  390.     {
  391.         return new[]
  392.             {
  393.                 new PointF(rect.X + rect.Width * 0.5f, rect.Y + rect.Height * 0f),
  394.                 new PointF(rect.X + rect.Width * 0.5f, rect.Y + rect.Height * 1f),
  395.                 new PointF(rect.X + rect.Width * 0.4f, rect.Y + rect.Height * 0.73f),
  396.                 new PointF(rect.X + rect.Width * 0.17f, rect.Y + rect.Height * 0.83f),
  397.                 new PointF(rect.X + rect.Width * 0.27f, rect.Y + rect.Height * 0.6f),
  398.                 new PointF(rect.X + rect.Width * 0f, rect.Y + rect.Height * 0.5f),
  399.                 new PointF(rect.X + rect.Width * 0.27f, rect.Y + rect.Height * 0.4f),
  400.                 new PointF(rect.X + rect.Width * 0.17f, rect.Y + rect.Height * 0.17f),
  401.                 new PointF(rect.X + rect.Width * 0.4f, rect.Y + rect.Height * 0.27f)
  402.             };
  403.     }
  404.     #endregion
  405.     #region Paint
  406.     protected override void OnPaint(PaintEventArgs e)
  407.     {
  408.         _bufGraphics.Graphics.Clear(this.BackColor);
  409.         DrawDullStars();
  410.         DrawIllumStars();
  411.         _bufGraphics.Render(e.Graphics);
  412.     }
  413.     #endregion
  414.     #endregion
  415.     #region Properties
  416.     #region Display
  417.     /// <summary>
  418.     /// does not include mouseleave un-ilum
  419.     /// </summary>
  420.     [Description("Occurs when a different number of stars are illuminated (does not include mouseleave un-ilum)")]
  421.     public event EventHandler StarsPanned;
  422.     /// <summary>
  423.     /// Typically by a click operation
  424.     /// </summary>
  425.     [Description("Occurs when the star rating of the strip has changed (Typically by a click operation)")]
  426.     public event EventHandler RatingChanged;
  427.  
  428.     [Browsable(false)]
  429.     public SolidBrush StarBrush
  430.     {
  431.         get { return _starBrush; }
  432.         set { _starBrush = value; }
  433.     }
  434.  
  435.     [Browsable(false)]
  436.     public SolidBrush StarDullBrush
  437.     {
  438.         get { return _starDullBrush; }
  439.         set { _starDullBrush = value; }
  440.     }
  441.  
  442.     [Browsable(false)]
  443.     public Pen StarStroke
  444.     {
  445.         get { return _starStroke; }
  446.         set { _starStroke = value; }
  447.     }
  448.  
  449.     [Browsable(false)]
  450.     public Pen StarDullStroke
  451.     {
  452.         get { return _starDullStroke; }
  453.         set { _starDullStroke = value; }
  454.     }
  455.  
  456.     [Browsable(false)]
  457.     public float MouseOverStarIndex
  458.     {
  459.         get { return _mouseOverIndex; }
  460.     }
  461.  
  462.     /// <summary>
  463.     /// Gets all of the spacing between the stars
  464.     /// </summary>
  465.     private int TotalSpacing
  466.     {
  467.         get { return (_starCount - 1) * _starSpacing; }
  468.     }
  469.  
  470.     /// <summary>
  471.     /// Gets the sum of the width of the stroke for each star
  472.     /// </summary>
  473.     private float TotalStrokeWidth
  474.     {
  475.         get { return _starCount * _starStroke.Width; }
  476.     }
  477.  
  478.     /// <summary>
  479.     /// Gets the sum of all star widths
  480.     /// </summary>
  481.     private int TotalStarWidth
  482.     {
  483.         get { return _starCount * _starWidth; }
  484.     }
  485.  
  486.     #region Allow Half StarRating
  487.     /// <summary>
  488.     /// in Maintunance
  489.     /// </summary>
  490.     [Description("Determines whether the user can rate with a half a star of specificity")]
  491.     [Category("Behavior")]
  492.     [DefaultValue(false)]
  493.     public bool AllowHalfStarRating
  494.     {
  495.         get { return _allowHalfStarRating; }
  496.         set
  497.         {
  498.             bool disabled = (!value && _allowHalfStarRating);
  499.             _allowHalfStarRating = value;
  500.  
  501.             if (disabled) // Only set rating if half star was enabled and now disabled
  502.             {
  503.                 Rating = (int)(Rating + 0.5);
  504.             }
  505.         }
  506.     }
  507.     #endregion
  508.     #endregion
  509.     #region Setting
  510.     #region NormalColor
  511.     /// <summary>
  512.     /// The color to use for the stars when they are not illuminated
  513.     /// </summary>
  514.     [Description("The color to use for the stars when they are not illuminated")]
  515.     [Category("Setting")]
  516.     [DefaultValue(typeof(Color), "Silver")]
  517.     public Color NormalColor
  518.     {
  519.         get { return _starDullBrush.Color; }
  520.         set
  521.         {
  522.             _starDullBrush.Color = value;
  523.             this.Invalidate();
  524.         }
  525.     }
  526.     #endregion
  527.     #region On Hover Color
  528.     /// <summary>
  529.     /// The color to use for the star when they are illuminated
  530.     /// </summary>
  531.     [Description("The color to use for the star when they are illuminated")]
  532.     [Category("Setting")]
  533.     [DefaultValue(typeof(Color), "Blue")]
  534.     public Color OnHoverColor
  535.     {
  536.         get { return _starBrush.Color; }
  537.         set
  538.         {
  539.             _starBrush.Color = value;
  540.             this.Invalidate();
  541.         }
  542.     }
  543.     #endregion
  544.     #region Width
  545.     /// <summary>
  546.     /// The width and height of the star in pixels (not including the border)
  547.     /// </summary>
  548.     [Description("The width and height of the star in pixels (not including the border)")]
  549.     [Category("Setting")]
  550.     [DefaultValue(25)]
  551.     public int WidthValue
  552.     {
  553.         get { return _starWidth; }
  554.         set
  555.         {
  556.             _starWidth = _starWidth < 1 ? 1 : value;
  557.             UpdateSize();
  558.             this.Invalidate();
  559.         }
  560.     }
  561.     #endregion
  562.     #region Star Spacing
  563.     /// <summary>
  564.     /// The amount of space between each star
  565.     /// </summary>
  566.     [Description("The amount of space between each star")]
  567.     [Category("Setting")]
  568.     [DefaultValue(1)]
  569.     public int Spacing
  570.     {
  571.         get { return _starSpacing; }
  572.         set
  573.         {
  574.             _starSpacing = _starSpacing < 0 ? 0 : value;
  575.             UpdateSize();
  576.             this.Invalidate();
  577.         }
  578.     }
  579.     #endregion
  580.     #region Rating
  581.     /// <summary>
  582.     /// "The number of stars selected (Note: 0 is considered un-rated")
  583.     /// </summary>
  584.     [Description("The number of stars selected (Note: 0 is considered un-rated")]
  585.     [Category("Setting")]
  586.     [DefaultValue(0f)]
  587.     public float Rating
  588.     {
  589.         get { return _rating; }
  590.         set
  591.         {
  592.             if (value > _starCount) value = _starCount; // bounds check
  593.             else if (value < 0) value = 0;
  594.             else
  595.             {
  596.                 // Rounding to whole number or near .5 appropriately
  597.                 if (_allowHalfStarRating) value = RoundToNearestHalf(value);
  598.                 else value = (int)(value + 0.5f);
  599.             }
  600.  
  601.             bool changed = value != _rating;
  602.             _rating = value;
  603.  
  604.             if (changed)
  605.             {
  606.                 if (!_settingRating)
  607.                 {
  608.                     _mouseOverIndex = _rating;
  609.                     if (!_allowHalfStarRating) _mouseOverIndex -= 1f;
  610.                 }
  611.  
  612.                 OnRatingChanged();
  613.                 this.Invalidate();
  614.             }
  615.         }
  616.     }
  617.     #endregion
  618.     #region Star Count
  619.     /// <summary>
  620.     /// Rounds precise numbers to a number no more precise than .5
  621.     /// </summary>
  622.     [Description("The number of stars to display")]
  623.     [Category("Setting")]
  624.     [DefaultValue(5)]
  625.     public int Count
  626.     {
  627.         get { return _starCount; }
  628.         set
  629.         {
  630.             bool changed = _starCount != value;
  631.             _starCount = value;
  632.  
  633.             if (changed)
  634.             {
  635.                 UpdateSize();
  636.                 this.Invalidate();
  637.             }
  638.         }
  639.     }
  640.     #endregion
  641.     #endregion
  642.     #region Border
  643.     #region Normal Color
  644.     /// <summary>
  645.     /// The color to use for the star borders when they are not illuminated
  646.     /// </summary>
  647.     [Description("The color to use for the star borders when they are not illuminated")]
  648.     [Category("Border")]
  649.     [DefaultValue(typeof(Color), "Gray")]
  650.     public Color BorderColor
  651.     {
  652.         get { return _starDullStroke.Color; }
  653.         set
  654.         {
  655.             _starDullStroke.Color = value;
  656.             this.Invalidate();
  657.         }
  658.     }
  659.     #endregion
  660.     #region On Hover Color
  661.     /// <summary>
  662.     /// The color to use for the star borders when they are illuminated
  663.     /// </summary>
  664.     [Description("The color to use for the star borders when they are illuminated")]
  665.     [Category("Border")]
  666.     [DefaultValue(typeof(Color), "Gold")]
  667.     public Color BorderHoverColor
  668.     {
  669.         get { return _starStroke.Color; }
  670.         set
  671.         {
  672.             _starStroke.Color = value;
  673.             this.Invalidate();
  674.         }
  675.     }
  676.     #endregion
  677.     #region  Width
  678.     /// <summary>
  679.     /// Gets or sets the width of the border around the star (including the dull version)
  680.     /// </summary>
  681.     [Description("The width of the star border")]
  682.     [Category("Border")]
  683.     [DefaultValue(3f)]
  684.     public float BorderWidth
  685.     {
  686.         get { return _starStroke.Width; }
  687.         set
  688.         {
  689.             _starStroke.Width = value;
  690.             _starDullStroke.Width = value;
  691.             UpdateSize();
  692.             this.Invalidate();
  693.         }
  694.     }
  695.     #endregion
  696.     #endregion
  697.     #region Shape
  698.     #region TypeOfStar
  699.     /// <summary>
  700.     /// Gets or sets the preset appearance of the star
  701.     /// </summary>
  702.     [Description("The star style to use")]
  703.     [Category("Shape")]
  704.     [DefaultValue(StarType.Fat)]
  705.     public StarType TypeOfStar
  706.     {
  707.         get { return _starType; }
  708.         set
  709.         {
  710.             _starType = value;
  711.             this.Invalidate();
  712.         }
  713.     }
  714.     #endregion
  715.     #endregion
  716.     #endregion
  717.  
  718. }
  719. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement