프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / MarkupToPDF / Controls / Shape / TriControl.cs @ 37eadd3f

이력 | 보기 | 이력해설 | 다운로드 (22.2 KB)

1
using System;
2
using System.Net;
3
using System.Windows;
4
using System.Windows.Controls;
5
using System.Windows.Documents;
6
using System.Windows.Ink;
7
using System.Windows.Input;
8
using System.Windows.Media;
9
using System.Windows.Media.Animation;
10
using System.Windows.Shapes;
11
using System.ComponentModel;
12
using System.Collections.Generic;
13
using MarkupToPDF.Controls.Common;
14
using MarkupToPDF.Common;
15
using MarkupToPDF.Serialize.Core;
16
using MarkupToPDF.Serialize.S_Control;
17
using System.Linq;
18

    
19
namespace MarkupToPDF.Controls.Shape
20
{
21
    //[TemplatePart(Name = "PART_TriPath", Type = typeof(Path))]
22
    public class TriControl : CommentUserInfo, IDisposable, INotifyPropertyChanged, IShapeControl, IDashControl
23
    {
24
        public Path Base_TriPath { get; set; }
25
        //public enum PaintSet { None, Fill, Hatch };
26
        private const string PART_TriPath = "PART_TriPath";
27

    
28
        //public Path Base_TriPath = null;
29

    
30
        static TriControl()
31
        {
32
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TriControl), new FrameworkPropertyMetadata(typeof(TriControl)));
33
            //ResourceDictionary dictionary = new ResourceDictionary();
34
            //dictionary.Source = new Uri("/MarkupToPDF;component/themes/generic.xaml", UriKind.RelativeOrAbsolute);
35
            //Application.Current.Resources.MergedDictionaries.Add(dictionary);
36
        }
37

    
38
        public override void Copy(CommentUserInfo lhs)
39
        {
40
            if(lhs is TriControl item)
41
            {
42
                this.LineSize = item.LineSize;
43
                this.MidPoint = new Point(item.MidPoint.X, item.MidPoint.Y);
44
                this.StartPoint = new Point(item.StartPoint.X, item.StartPoint.Y);
45
                this.Paint = item.Paint;
46
                this.EndPoint = new Point(item.EndPoint.X, item.EndPoint.Y);
47
                this.Opacity = item.Opacity;
48
                this.CommentAngle = item.CommentAngle;
49
                this.DashSize = item.DashSize;
50
                this.PointSet = item.PointSet.ConvertAll(x => new Point(x.X, x.Y));
51
                this.StrokeColor = item.StrokeColor;
52
                this.UserID = item.UserID;
53
                this.Memo = item.Memo;
54
            }
55
        }
56

    
57
        public override CommentUserInfo Clone()
58
        {
59
            var clone = new TriControl();
60
            clone.Copy(this);
61
            return clone;
62
        }
63

    
64
        #region Dependency Properties
65

    
66
        public static readonly DependencyProperty OverViewPathDataProperty = DependencyProperty.Register(
67
        "OverViewPathData", typeof(Geometry), typeof(TriControl), new PropertyMetadata(null));
68

    
69
        public static readonly DependencyProperty IsSelectedProperty =
70
      DependencyProperty.Register("IsSelected", typeof(bool), typeof(TriControl), new FrameworkPropertyMetadata(false, IsSelectedChanged));
71

    
72
        public static readonly DependencyProperty ControlTypeProperty =
73
    DependencyProperty.Register("ControlType", typeof(ControlType), typeof(TriControl), new FrameworkPropertyMetadata(ControlType.Triangle));
74

    
75
        public static readonly DependencyProperty LineSizeProperty = DependencyProperty.Register(
76
         "LineSize", typeof(double), typeof(TriControl), new PropertyMetadata((Double)3));
77
        public static readonly DependencyProperty UserIDProperty = DependencyProperty.Register(
78
                "UserID", typeof(string), typeof(TriControl), new PropertyMetadata(null));
79
        //강인구 추가
80
        public static readonly DependencyProperty DashSizeProperty = DependencyProperty.Register(
81
              "DashSize", typeof(DoubleCollection), typeof(TriControl), new PropertyMetadata(new DoubleCollection { 1, 1 }, PointValueChanged));
82
        public static readonly DependencyProperty PathDataProperty = DependencyProperty.Register(
83
        "PathData", typeof(Geometry), typeof(TriControl), null);
84
        public static readonly DependencyProperty StrokeColorProperty = DependencyProperty.Register(
85
         "StrokeColor", typeof(SolidColorBrush), typeof(TriControl), new PropertyMetadata(new SolidColorBrush(Colors.Red)));
86
        public static readonly DependencyProperty FillColorProperty = DependencyProperty.Register(
87
         "FillColor", typeof(SolidColorBrush), typeof(TriControl), new PropertyMetadata(new SolidColorBrush(Colors.Red)));
88
        public static readonly DependencyProperty EndPointProperty = DependencyProperty.Register(
89
            "EndPoint", typeof(Point), typeof(TriControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
90
        public static readonly DependencyProperty MidPointProperty = DependencyProperty.Register(
91
            "MidPoint", typeof(Point), typeof(TriControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
92
        public static readonly DependencyProperty StartPointProperty = DependencyProperty.Register(
93
            "StartPoint", typeof(Point), typeof(TriControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
94
        //강인구 추가
95
        public static readonly DependencyProperty PaintProperty = DependencyProperty.Register(
96
                "Paint", typeof(PaintSet), typeof(TriControl), new PropertyMetadata(PaintSet.None, PointValueChanged));
97

    
98
        public static readonly DependencyProperty PointSetProperty = DependencyProperty.Register(
99
            "PointSet", typeof(List<Point>), typeof(TriControl), new PropertyMetadata(new List<Point>(), PointValueChanged));
100
        public static readonly DependencyProperty AngleProperty = DependencyProperty.Register("Angle", typeof(double), typeof(TriControl), new
101
            PropertyMetadata((double)0.0, new PropertyChangedCallback(AngleValueChanged)));
102
        public static readonly DependencyProperty CenterXProperty = DependencyProperty.Register("CenterX", typeof(double), typeof(TriControl),
103
            new PropertyMetadata((double)0, OnCenterXYChanged));
104

    
105
        public static readonly DependencyProperty CenterYProperty = DependencyProperty.Register("CenterY", typeof(double), typeof(TriControl),
106
            new PropertyMetadata((double)0, OnCenterXYChanged));
107

    
108
        #endregion
109
        #region PropertyChanged Method
110

    
111
        public static void IsSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
112
        {
113
            //var instance = (TriControl)sender;
114

    
115
            //if (e.OldValue != e.NewValue && instance.Base_TriPath != null)
116
            //{
117

    
118
            //    instance.SetValue(e.Property, e.NewValue);
119

    
120
            //    if (instance.IsSelected)
121
            //    {
122
            //        instance.Base_TriPath.Stroke = new SolidColorBrush(Colors.Blue);
123
            //    }
124
            //    else
125
            //    {
126
            //        instance.Base_TriPath.Stroke = new SolidColorBrush(Colors.Red);
127
            //    }
128
            //}
129
        }
130

    
131
        public static void PointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
132
        {
133
            var instance = (TriControl)sender;
134

    
135
            if (e.OldValue != e.NewValue && instance.Base_TriPath != null)
136
            {
137
                instance.SetValue(e.Property, e.NewValue);
138
                instance.SetTri();
139
            }
140
        }
141

    
142

    
143
        public static void OnCenterXYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
144
        {
145
            var instance = (TriControl)sender;
146
            if (e.OldValue != e.NewValue && instance.Base_TriPath != null)
147
            {
148
                instance.SetValue(e.Property, e.NewValue);
149
                instance.SetTri();
150
            }
151
        }
152
        public static void AngleValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
153
        {
154
            var instance = (TriControl)sender;
155
            if (e.OldValue != e.NewValue && instance.Base_TriPath != null)
156
            {
157
                instance.SetValue(e.Property, e.NewValue);
158
                instance.SetTri();
159
            }
160
        }
161
        #endregion
162
        #region Properties
163

    
164
        public Double LineSize
165
        {
166
            get { return (Double)GetValue(LineSizeProperty); }
167
            set
168
            {
169
                if (this.LineSize != value)
170
                {
171
                    SetValue(LineSizeProperty, value);
172
                }
173
            }
174
        }
175
        public string UserID
176
        {
177
            get { return (string)GetValue(UserIDProperty); }
178
            set
179
            {
180
                if (this.UserID != value)
181
                {
182
                    SetValue(UserIDProperty, value);
183
                    OnPropertyChanged("UserID");
184
                }
185
            }
186
        }
187
        public SolidColorBrush FillColor
188
        {
189
            get { return (SolidColorBrush)GetValue(FillColorProperty); }
190
            set
191
            {
192
                if (this.FillColor != value)
193
                {
194
                    SetValue(FillColorProperty, value);
195
                }
196
            }
197
        }
198
        public DoubleCollection DashSize
199
        {
200
            get { return (DoubleCollection)GetValue(DashSizeProperty); }
201
            set
202
            {
203
                if (this.DashSize != value)
204
                {
205
                    SetValue(DashSizeProperty, value);
206
                }
207
            }
208
        }
209
        public PaintSet Paint
210
        {
211
            get { return (PaintSet)GetValue(PaintProperty); }
212
            set
213
            {
214
                if (this.Paint != value)
215
                {
216
                    SetValue(PaintProperty, value);
217
                }
218
            }
219
        }
220

    
221

    
222
        public override SolidColorBrush StrokeColor
223
        {
224
            get { return (SolidColorBrush)GetValue(StrokeColorProperty); }
225
            set
226
            {
227
                if (this.StrokeColor != value)
228
                {
229
                    SetValue(StrokeColorProperty, value);
230
                }
231
            }
232
        }
233
        public Geometry OverViewPathData
234
        {
235
            get
236
            {
237
                return (Geometry)GetValue(OverViewPathDataProperty);
238
            }
239
            set
240
            {
241
                SetValue(OverViewPathDataProperty, value);
242
                OnPropertyChanged("OverViewPathData");
243
            }
244
        }
245
        public Geometry PathData
246
        {
247
            get { return (Geometry)GetValue(PathDataProperty); }
248
            set { SetValue(PathDataProperty, value); }
249
        }
250
        public Point EndPoint
251
        {
252
            get { return (Point)GetValue(EndPointProperty); }
253
            set { SetValue(EndPointProperty, value); }
254
        }
255
        public Point StartPoint
256
        {
257
            get { return (Point)GetValue(StartPointProperty); }
258
            set { SetValue(StartPointProperty, value); }
259
        }
260
        public Point MidPoint
261
        {
262
            get { return (Point)GetValue(MidPointProperty); }
263
            set { SetValue(MidPointProperty, value); }
264
        }
265
        public List<Point> PointSet
266
        {
267
            get { return (List<Point>)GetValue(PointSetProperty); }
268
            set { SetValue(PointSetProperty, value); }
269
        }
270

    
271
        public override bool IsSelected
272
        {
273
            get
274
            {
275
                return (bool)GetValue(IsSelectedProperty);
276
            }
277
            set
278
            {
279
                SetValue(IsSelectedProperty, value);
280
            }
281
        }
282

    
283
        public override ControlType ControlType
284
        {
285
            get
286
            {
287
                return (ControlType)GetValue(ControlTypeProperty);
288
            }
289
            set
290
            {
291
                SetValue(ControlTypeProperty, value);
292
            }
293
        }
294

    
295

    
296
        public double CommentAngle
297
        {
298
            get { return (double)GetValue(AngleProperty); }
299
            set
300
            {
301
                if (this.CommentAngle != value)
302
                {
303
                    SetValue(AngleProperty, value);
304
                }
305
            }
306
        }
307
        public double CenterX
308
        {
309
            get { return (double)GetValue(CenterXProperty); }
310
            set { SetValue(CenterXProperty, value); }
311
        }
312
        public double CenterY
313
        {
314
            get { return (double)GetValue(CenterYProperty); }
315
            set { SetValue(CenterYProperty, value); }
316
        }
317
        #endregion
318

    
319
        #region Dependency PropertyChanged
320
        //public static void IsSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
321
        //{
322
        //    var instance = (TriControl)sender;
323

    
324
        //    if (e.OldValue != e.NewValue && instance.Base_TriPath != null)
325
        //    {
326

    
327
        //        instance.SetValue(e.Property, e.NewValue);
328

    
329
        //        if (instance.IsSelected)
330
        //        {
331
        //            instance.Base_TriPath.Stroke = new SolidColorBrush(Colors.Blue);
332
        //        }
333
        //        else
334
        //        {
335
        //            instance.Base_TriPath.Stroke = new SolidColorBrush(Colors.Red);
336
        //        }
337
        //    }
338
        //}
339

    
340
        #endregion Dependency PropertyChanged
341

    
342
        public override void OnApplyTemplate()
343
        {
344
            base.OnApplyTemplate();
345
            Base_TriPath = GetTemplateChild(PART_TriPath) as Path;
346

    
347
            if (Base_TriPath == null)
348
            {
349
                return;
350
            }
351

    
352
            this.SetTri();
353
        }
354

    
355
        private void SetTri()
356
        {
357
            this.ApplyTemplate();
358
            Base_TriPath.StrokeDashArray.Clear();
359
            foreach (var item in this.DashSize)
360
            {
361
                Base_TriPath.StrokeDashArray.Add(item);
362
            }
363
            Base_TriPath.StrokeDashCap = PenLineCap.Square;
364
            PathFigure pathFigure = new PathFigure();
365

    
366
            //this.FillColor = this.StrokeColor;
367
            ////Base_TriPath.Fill = this.FillColor;
368
            //Base_TriPath.Fill = Brushes.Red;
369
            //pathFigure.IsFilled = true;
370

    
371
            switch (this.Paint)
372
            {
373
                case PaintSet.None:
374
                    this.FillColor = null;
375
                    pathFigure.IsFilled = false;
376
                    break;
377
                case PaintSet.Fill:
378
                    this.FillColor = this.StrokeColor;
379
                    Base_TriPath.Fill = this.FillColor;
380
                    pathFigure.IsFilled = true;
381
                    break;
382
                case PaintSet.Hatch:
383
                    Base_TriPath.Fill = HatchMake.CreateHatchBrush(this.StrokeColor);
384
                    pathFigure.IsFilled = true;
385
                    break;
386
                default:
387
                    break;
388
            }
389
            pathFigure.StartPoint = this.StartPoint;
390

    
391
            List<Point> points = new List<Point>() { this.StartPoint };
392
            if (MidPoint.X != 0 && MidPoint.Y != 0) points.Add(this.MidPoint);
393
            points.Add(this.EndPoint);
394

    
395
            PolyLineSegment polyline = new PolyLineSegment(points , true);
396
            pathFigure.Segments.Add(polyline);
397

    
398
            PathGeometry pathGeometry = new PathGeometry();
399
            pathGeometry.Figures = new PathFigureCollection();
400
            pathFigure.IsClosed = true;
401
            pathGeometry.Figures.Add(pathFigure);
402

    
403
            this.PathData = pathGeometry;
404
            ApplyOverViewData();
405

    
406
        }
407
        public void Dispose()
408
        {
409
            //GC.Collect();
410
            ////GC.SuppressFinalize(this);
411
            this.Base_TriPath = null;
412
        }
413
        public event PropertyChangedEventHandler PropertyChanged;
414
        protected void OnPropertyChanged(string propName)
415
        {
416
            if (PropertyChanged != null)
417
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
418
        }
419

    
420
        public override void ApplyOverViewData()
421
        {
422
            this.OverViewPathData = this.PathData;
423
        }
424

    
425
        public override void UpdateControl()
426
        {
427
            this.StartPoint = new Point(this.PointSet[0].X, this.PointSet[0].Y);
428
            this.MidPoint = new Point(this.PointSet[1].X, this.PointSet[1].Y);
429
            this.EndPoint = new Point(this.PointSet[2].X, this.PointSet[2].Y);
430
        }
431

    
432
        /// <summary>
433
        /// call when mouse is moving while drawing control
434
        /// </summary>
435
        /// <param name="pt"></param>
436
        /// <param name="bAxisLocked"></param>
437
        public override void OnCreatingMouseMove(Point pt, bool bAxisLocked)
438
        {
439
            this.EndPoint = pt;
440

    
441
            if (bAxisLocked)
442
            {
443
                Point tmp = this.EndPoint;
444
                string angle = MathSet.returnAngleString(this.StartPoint, ref tmp, true);
445
                this.EndPoint = tmp;
446
          
447
                List<Point> Points = this.GetRegularTrianglePoints(this.StartPoint, this.EndPoint);
448
                if (2 == Points.Count())
449
                {
450
                    this.MidPoint = Points[0];
451
                    this.EndPoint = Points[1];
452
                }
453
            }
454

    
455
            this.PointSet = new List<Point>
456
            {
457
                this.StartPoint,
458
                this.MidPoint,
459
                this.EndPoint,
460
            };
461
        }
462

    
463
        /// <summary>
464
        /// move control point has same location of given pt along given delta
465
        /// </summary>
466
        /// <author>humkyung</author>
467
        /// <date>2019.06.20</date>
468
        /// <param name="pt"></param>
469
        /// <param name="delta"></param>
470
        public override void OnMoveCtrlPoint(Point pt, double dx, double dy, bool bAxisLocked = false)
471
        {
472
            Point selected = MathSet.getNearPoint((this as IPath).PointSet, pt);    ///TODO: 위험 요소를 가지고 있음.
473
            selected.X += dx;
474
            selected.Y += dy;
475

    
476
            var vFirstPoint = (this as IPath).PointSet.FindAll(x => x == pt);
477
            int iIndex = 0;
478
            if (vFirstPoint != null && vFirstPoint.Count > 0)
479
            {
480
                iIndex = (this as IPath).PointSet.IndexOf(vFirstPoint.FirstOrDefault());
481
            }
482

    
483
            for (int i = 0; i < (this as IPath).PointSet.Count; i++)
484
            {
485
                if (i == iIndex)
486
                {
487
                    if (pt.Equals((this as IPath).PointSet[i]))
488
                    {
489
                        (this as IPath).PointSet[i] = selected;
490
                    }
491
                }
492
            }
493
            this.UpdateControl();
494
        }
495

    
496
        /// <summary>
497
        /// return tricontrols' area
498
        /// </summary>
499
        /// <author>humkyung</author>
500
        /// <date>2019.06.13</date>
501
        public override Rect ItemRect
502
        {
503
            get
504
            {
505
                double dMinX = Math.Min(this.StartPoint.X, this.EndPoint.X);
506
                dMinX = Math.Min(this.MidPoint.X, dMinX);
507
                double dMinY = Math.Min(this.StartPoint.Y, this.EndPoint.Y);
508
                dMinY = Math.Min(this.MidPoint.Y, dMinY);
509
                double dMaxX = Math.Max(this.StartPoint.X, this.EndPoint.X);
510
                dMaxX = Math.Max(this.MidPoint.X, dMaxX);
511
                double dMaxY = Math.Max(this.StartPoint.Y, this.EndPoint.Y);
512
                dMaxY = Math.Max(this.MidPoint.Y, dMaxY);
513

    
514
                return new Rect(new Point(dMinX, dMinY), new Point(dMaxX, dMaxY));
515
            }
516
        }
517

    
518
        /// <summary>
519
        /// Serialize this
520
        /// </summary>
521
        /// <param name="sUserId"></param>
522
        /// <returns></returns>
523
        public override string Serialize()
524
        {
525
            using (S_TriControl STemp = new S_TriControl())
526
            {
527
                STemp.TransformPoint = "0|0";
528
                STemp.Paint = this.Paint;
529
                STemp.SizeSet = String.Format("{0}", this.LineSize);
530
                //STemp.StrokeColor = "#FF00FF00";
531
                STemp.StrokeColor = this.StrokeColor.Color.ToString();
532
                if (this.FillColor != null)
533
                {
534
                    STemp.FillColor = this.FillColor.Color.ToString();
535
                }
536
                STemp.StartPoint = this.StartPoint;
537
                STemp.EndPoint = this.EndPoint;
538
                STemp.MidPoint = this.MidPoint;
539
                STemp.Opac = this.Opacity;
540
                STemp.UserID = this.UserID;
541
                STemp.PointSet = this.PointSet;
542
                STemp.Angle = this.CommentAngle;
543
                STemp.DashSize = this.DashSize;
544
                STemp.Name = this.GetType().Name.ToString();
545
                ///강인구 추가(2017.11.02)
546
                ///Memo 추가
547
                STemp.Memo = this.Memo;
548

    
549
                return "|DZ|" + JsonSerializerHelper.CompressString((STemp.JsonSerialize()));
550
            }
551
        }
552

    
553
        /// <summary>
554
        /// create a tricontrol from given string
555
        /// </summary>
556
        /// <param name="str"></param>
557
        /// <returns></returns>
558
        public static TriControl FromString(string str, SolidColorBrush brush, string sProjectNo)
559
        {
560
            using (S_TriControl s = JsonSerializerHelper.JsonDeserialize<S_TriControl>(str))
561
            {
562
                string[] data2 = s.SizeSet.Split(delimiterChars2, StringSplitOptions.RemoveEmptyEntries);
563
                return new TriControl
564
                {
565
                    LineSize = Convert.ToDouble(data2.First()),
566
                    MidPoint = s.MidPoint,
567
                    StartPoint = s.StartPoint,
568
                    Paint = s.Paint,
569
                    EndPoint = s.EndPoint,
570
                    Opacity = s.Opac,
571
                    CommentAngle = s.Angle,
572
                    DashSize = s.DashSize,
573
                    PointSet = s.PointSet,
574
                    StrokeColor = brush,
575
                    UserID = s.UserID,
576
                    Memo = s.Memo
577
                };
578
            }
579
        }
580

    
581
        /// <summary>
582
        /// 정삼각형을 그리기위한 두 포인트를 계산하여 넘겨줌
583
        /// </summary>
584
        /// <author>humkyung</author>
585
        /// <date>2018.04.26</date>
586
        /// <param name="StartP"></param>
587
        /// <param name="EndP"></param>
588
        /// <returns></returns>
589
        /// <history>humkyung 2018.05.11 apply axis lock</history>
590
        private List<Point> GetRegularTrianglePoints(Point StartP, Point EndP, bool bCheckAxis = false)
591
        {
592
            List<Point> res = new List<Point>();
593

    
594
            double dx = EndP.X - StartP.X;
595
            double dy = EndP.Y - StartP.Y;
596
            double length = Math.Sqrt(dx * dx + dy * dy);
597
            double baseLength = length * Math.Tan(30.0 * Math.PI / 180.0);
598
            dx /= length;
599
            dy /= length;
600
            double tmp = dx;
601
            dx = -dy; dy = tmp; /// rotate by 90 degree
602

    
603
            res.Add(new Point(EndP.X + dx * baseLength, EndP.Y + dy * baseLength));
604
            res.Add(new Point(EndP.X - dx * baseLength, EndP.Y - dy * baseLength));
605

    
606
            return res;
607
        }
608
    }
609
}
클립보드 이미지 추가 (최대 크기: 500 MB)