Source

SharpHammer / DarkMotion / BlendView.cs

Full commit
using System;
using System.Drawing;
using System.Windows.Forms;

namespace DarkMotion
{
	public partial class BlendView : UserControl
	{
		private float barThickness = 10;

		private float blend = 0;

		private Pen blendLine = new Pen(Color.OrangeRed, 2f);

		private SolidBrush fillTris;

		private int items = 0;

		private StringFormat strFormat = new StringFormat();

		public BlendView()
		{
			InitializeComponent();
			var c = Color.MediumTurquoise;
			fillTris = new SolidBrush(Color.FromArgb(64, c.R, c.G, c.B));
		}

		public float BarThickness
		{
			get
			{
				return barThickness;
			}
			set
			{
				barThickness = value;
				Invalidate();
			}
		}

		public float Blend
		{
			get
			{
				return blend;
			}
			set
			{
				blend = value;
				blend = SharpHammer.MathUtil.Clamp(blend, 0, items - 1f);
				Invalidate();
			}
		}

		public int Items
		{
			get
			{
				return items;
			}
			set
			{
				items = value;
				blend = SharpHammer.MathUtil.Clamp(blend, 0, items - 1f);
				Invalidate();
			}
		}

		private float seperation
		{
			get
			{
				return Width / (items - 1f);
			}
		}

		protected override void OnMouseDown(MouseEventArgs e)
		{
			base.OnMouseDown(e);
			if (e.Button == MouseButtons.Left)
				Blend = e.X / seperation;
		}

		protected override void OnMouseMove(MouseEventArgs e)
		{
			base.OnMouseMove(e);
			if (e.Button == MouseButtons.Left)
				Blend = e.X / seperation;
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint(e);
			e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
			if (items > 1) {
				for (int i = 0; i < items; i++) {
					var x = i * seperation;

					float diff = Math.Abs(i - blend);
					if (diff < 1)
						e.Graphics.FillRectangle(Brushes.DarkGray,
							new RectangleF((i * seperation) - barThickness / 2, diff * Height, barThickness, (1 - diff) * Height));

					var points = new PointF[] { new PointF(x - seperation, Height), new PointF(x, 0), new PointF(x + seperation, Height) };

					e.Graphics.DrawPolygon(Pens.Silver, points);
					e.Graphics.FillPolygon(fillTris, points);
				}

				int blendPos = (int)(blend * seperation);
				e.Graphics.DrawLine(blendLine, new Point(blendPos - 1, 0), new Point(blendPos - 1, Height));
				strFormat.Alignment = (blendPos > Width / 2) ? StringAlignment.Far : StringAlignment.Near;
				e.Graphics.DrawString(blend.ToString("0.00"), Font,
					Brushes.OrangeRed, new PointF(blendPos, Height - Font.Height), strFormat);
			}
		}
	}
}