using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using JD.Kernel.StaticHelpers; using JD.Sequencers.Times; using JD.Sequencers.Windowlesses; namespace JD.Sequencers.Controls { /// /// A single item in a track. /// public abstract class TrackItem : WindowlessControl, IMovable { #region Variables private Color m_color = Color.DarkSlateBlue; private bool m_useGradient = true; private Track m_track; private bool m_isMoveAllowed = true; #endregion #region Constructor protected TrackItem() { Selectable = true; } #endregion #region Properties public Color Color { get { return m_color; } set { if (m_color != value) { m_color = value; Invalidate(); } } } public Color GradientColor { get { return Color.FromArgb(Color.A, ModifyBrightness(Color, 1.25f)); } } public bool UseGradient { get { return m_useGradient; } set { if (m_useGradient != value) { m_useGradient = value; Invalidate(); } } } public Track Track { get { return m_track; } internal set { Throw.InvalidOperationIf(m_track != null && value != null, "Item is already owned by another track"); m_track = value; } } public Timeline Timeline { get { return Track != null ? Track.Timeline : null; } } public TrackView TrackView { get { return Parents.OfType().FirstOrDefault(); } } /// /// Whether this movable is movable :) /// public bool IsMoveAllowed { get { return m_isMoveAllowed; } set { m_isMoveAllowed = value; } } #endregion #region Methods public virtual void Initialize() { } #region Painting protected override void OnPaintSelf(System.Windows.Forms.PaintEventArgs e) { base.OnPaintSelf(e); Rectangle borderBounds = Bounds; borderBounds.Height -= (int)Pens.White.Width; borderBounds.Width -= (int)Pens.White.Width; using (Brush fillBrush = CreateFillBrush()) { e.Graphics.FillRectangle(fillBrush, Bounds); e.Graphics.DrawRectangle(Selected ? Pens.White : Pens.Black, borderBounds); } } protected virtual Brush CreateFillBrush() { if (UseGradient) { return new LinearGradientBrush(new Point(Bounds.Left, Bounds.Top), new Point(Bounds.Left, Bounds.Bottom), GradientColor, Color); } return new SolidBrush(Color); } #endregion #region Layout public abstract void OnVisibleRangeChanged(); public virtual bool HitTestFilter(Rectangle localZone) { return true; } #endregion private Color ModifyBrightness(Color c1, float factor) { float r = ((c1.R * factor) > 255) ? 255 : (c1.R * factor); float g = ((c1.G * factor) > 255) ? 255 : (c1.G * factor); float b = ((c1.B * factor) > 255) ? 255 : (c1.B * factor); Color c = Color.FromArgb(c1.A, (int)r, (int)g, (int)b); return c; } #endregion #region Implementation of IMovable public abstract void Snap_Self(float originalDelta, ref float snappedDelta, ref float diff); public abstract void Snap_Bounds(float originalDelta, ref float snappedDelta, ref float diff); public abstract void Move(float delta); #endregion } }