using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.ComponentModel; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; namespace Tango.BL.Entities { public partial class Segment { private double _lastLength; private LinearGradientBrush _brush; public override void Save(ObservablesContext context) { for (int i = 0; i < BrushStops.Count; i++) { BrushStops[i].StopIndex = i; } base.Save(context); } protected override void RaisePropertyChanged(string propName) { base.RaisePropertyChanged(propName); if (propName == nameof(Length) && _lastLength != Length) { BrushStops.ToList().ForEach(x => x.RaiseOffsetChanged()); _lastLength = Length; } if (propName == nameof(BrushStops)) { if (BrushStops != null) { BrushStops.CollectionChanged -= BrushStops_CollectionChanged; BrushStops.CollectionChanged += BrushStops_CollectionChanged; foreach (var stop in BrushStops) { stop.RaiseOffsetChanged(); } RaiseSegmentBrushChanged(); } } } private void BrushStops_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { foreach (var stop in BrushStops) { stop.RaiseOffsetChanged(); } if (BrushStops.Count > 0) { BrushStops.First().OffsetPercent = 0; } if (BrushStops.Count > 1) { BrushStops.Last().OffsetPercent = 100; } RaiseSegmentBrushChanged(); } private TimeSpan _remainingTime; [NotMapped] [JsonIgnore] public TimeSpan RemainingTime { get { return _remainingTime; } set { _remainingTime = value; RaisePropertyChangedAuto(); } } private bool _started; [NotMapped] [JsonIgnore] public bool Started { get { return _started; } set { _started = value; RaisePropertyChangedAuto(); } } private bool _completed; [NotMapped] [JsonIgnore] public bool Completed { get { return _completed; } set { _completed = value; RaisePropertyChangedAuto(); } } [NotMapped] [JsonIgnore] public Brush SegmentBrush { get { return GetSegmentBrush(); } } private bool _isInterSegment; [NotMapped] [JsonIgnore] public bool IsInterSegment { get { return _isInterSegment; } set { _isInterSegment = value; RaisePropertyChangedAuto(); } } [NotMapped] public bool HasOutOfGamutBrushStop { get { return BrushStops.Any(x => x.IsOutOfGamut); } } internal void RaiseHasOutOfGamutBrushStop() { RaisePropertyChanged(nameof(HasOutOfGamutBrushStop)); } public override Segment Clone() { Segment cloned = base.Clone(); cloned.BrushStops = BrushStops.Select(x => x.Clone()).ToObservableCollection(); return cloned; } public Segment Clone(Job job) { Segment cloned = base.Clone(); cloned.BrushStops = BrushStops.Select(x => x.Clone(cloned)).ToObservableCollection(); cloned.Job = job; cloned.JobGuid = job.Guid; return cloned; } public override void DefferedDelete(ObservablesContext context) { BrushStops.ToList().ForEach(x => x.DefferedDelete(context)); BrushStops.Clear(); base.DefferedDelete(context); } public LinearGradientBrush GetSegmentBrush() { if (_brush == null || _brush.GradientStops.Count != BrushStops.Count) { GradientStopCollection stops = new GradientStopCollection(); foreach (var stop in BrushStops.OrderBy(x => x.StopIndex)) { stops.Add(new GradientStop(stop.Color, stop.OffsetPercent / 100d)); } LinearGradientBrush brush = new LinearGradientBrush(); brush.StartPoint = new Point(0, 0); brush.EndPoint = new Point(1, 0); brush.GradientStops = stops; _brush = brush; return brush; } else { for (int i = 0; i < BrushStops.Count; i++) { _brush.GradientStops[i].Color = BrushStops[i].Color; _brush.GradientStops[i].Offset = BrushStops[i].OffsetPercent / 100d; } return _brush; } } public System.Drawing.Brush CreateGdiBrush(int width, int height) { if (BrushStops.Count > 1) { System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.PointF(0, 0), new System.Drawing.Point(width, height), System.Drawing.Color.Black, System.Drawing.Color.Black); System.Drawing.Drawing2D.ColorBlend blend = new System.Drawing.Drawing2D.ColorBlend(); List colors = new List(); List offsets = new List(); foreach (var stop in BrushStops.OrderBy(x => x.OffsetPercent)) { colors.Add(stop.Color.ToGdiColor()); offsets.Add((float)stop.OffsetPercent / 100f); } blend.Colors = colors.ToArray(); blend.Positions = offsets.ToArray(); brush.InterpolationColors = blend; return brush; } else if (BrushStops.Count == 1) { return new System.Drawing.SolidBrush(BrushStops.First().Color.ToGdiColor()); } else { return System.Drawing.Brushes.Black; } } public void RaiseSegmentBrushChanged() { RaisePropertyChanged(nameof(SegmentBrush)); } public BrushStop AddBrushStop() { BrushStop stop = new BrushStop(); if (Job.ColorSpace != null) { stop.ColorSpace = Job.ColorSpace; } else { stop.ColorSpaceGuid = Job.ColorSpaceGuid; } if (BrushStops.Count > 0) { stop.StopIndex = BrushStops.Max(x => x.StopIndex) + 1; stop.OffsetPercent = 100; } else { stop.StopIndex = 1; } stop.Segment = this; stop.Color = Colors.Black; BrushStops.Add(stop); return stop; } } }