aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.BL/EntitiesExtensions/Segment.cs
blob: 63356345c400333db7cac62ea35da7783d41a9aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
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;
                RaisePropertyChanged(nameof(LengthWithFactor));
            }

            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); }
        }

        [NotMapped]
        [JsonIgnore]
        public double LengthWithFactor
        {
            get { return Job != null && !IsInterSegment ? (Length + Length * (Job.LengthPercentageFactor / 100)) : Length; }
        }

        internal void RaiseHasOutOfGamutBrushStop()
        {
            RaisePropertyChanged(nameof(HasOutOfGamutBrushStop));
        }

        public override Segment Clone()
        {
            Segment cloned = base.Clone();

            cloned.BrushStops = BrushStops.Select(x => x.Clone()).ToObservableCollection();

            foreach (var stop in cloned.BrushStops)
            {
                stop.SegmentGuid = cloned.Guid;
                stop.Segment = cloned;
            }

            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<System.Drawing.Color> colors = new List<System.Drawing.Color>();
                List<float> offsets = new List<float>();

                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 void RaiseLengthWithFactorChanged()
        {
            RaisePropertyChanged(nameof(LengthWithFactor));
        }

        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;
        }
    }
}