프로젝트

일반

사용자정보

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

markus / MarkupToPDF / Controls / Polygon / InkControl.cs @ d2114d3b

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

1
using MarkupToPDF.Common;
2
using MarkupToPDF.Controls.Common;
3
using MarkupToPDF.Serialize.Core;
4
using MarkupToPDF.Serialize.S_Control;
5
using System;
6
using System.Collections.Generic;
7
using System.ComponentModel;
8
using System.Linq;
9
using System.Text;
10
using System.Threading.Tasks;
11
using System.Windows;
12
using System.Windows.Controls;
13
using System.Windows.Media;
14
using System.Windows.Shapes;
15

    
16
namespace MarkupToPDF.Controls.Polygon
17
{
18
    public class InkControl : CommentUserInfo, IDisposable, INotifyPropertyChanged, IShapeControl, IMarkupControlData, IDashControl
19
    {
20
        #region Constructure
21

    
22
        static InkControl()
23
        {
24
            DefaultStyleKeyProperty.OverrideMetadata(typeof(InkControl), new FrameworkPropertyMetadata(typeof(InkControl)));
25
            ResourceDictionary dictionary = new ResourceDictionary();
26
            dictionary.Source = new Uri("/MarkupToPDF;component/themes/generic.xaml", UriKind.RelativeOrAbsolute);
27
            Application.Current.Resources.MergedDictionaries.Add(dictionary);
28
        }
29

    
30
        public InkControl()
31
        {
32
            this.DefaultStyleKey = typeof(InkControl);
33
        }
34

    
35
        #endregion
36

    
37
        #region Variable
38
        private const string PART_PolyPath = "PART_PolyPath";
39

    
40
        public Path Base_PolyPath = null;
41

    
42
        #endregion
43

    
44
        #region Internal Method
45
        public override void OnApplyTemplate()
46
        {
47
            base.OnApplyTemplate();
48

    
49
            Base_PolyPath = GetTemplateChild(PART_PolyPath) as Path;
50

    
51
            if (Base_PolyPath == null)
52
                return;
53

    
54
            this.SetPolyPath();
55
        }
56

    
57

    
58
        #endregion
59

    
60
        #region Method
61

    
62
        public override void ApplyOverViewData()
63
        {
64
            this.OverViewPathData = this.PathData;
65
        }
66

    
67
        #endregion
68

    
69
        #region Dependency Properties
70

    
71
        public static readonly DependencyProperty UserIDProperty = DependencyProperty.Register(
72
                "UserID", typeof(string), typeof(InkControl), new PropertyMetadata(null));
73

    
74
        public static readonly DependencyProperty LineSizeProperty = DependencyProperty.Register(
75
              "LineSize", typeof(double), typeof(InkControl), new PropertyMetadata((Double)3));
76

    
77
        public static readonly DependencyProperty StrokeColorProperty = DependencyProperty.Register(
78
               "StrokeColor", typeof(SolidColorBrush), typeof(InkControl),
79
               new PropertyMetadata(new SolidColorBrush(Colors.Red)));
80

    
81
        public static readonly DependencyProperty PathDataProperty = DependencyProperty.Register(
82
                "PathData", typeof(Geometry), typeof(InkControl), null);
83

    
84
        //강인구 추가
85
        public static readonly DependencyProperty DashSizeProperty = DependencyProperty.Register(
86
                "DashSize", typeof(DoubleCollection), typeof(InkControl), new PropertyMetadata(new DoubleCollection { 99999999 }, PointValueChanged));
87

    
88
        public static readonly DependencyProperty OverViewPathDataProperty = DependencyProperty.Register(
89
                "OverViewPathData", typeof(Geometry), typeof(InkControl), null);
90
        //강인구 추가
91
        public static readonly DependencyProperty PaintProperty = DependencyProperty.Register(
92
        "Paint", typeof(PaintSet), typeof(InkControl), new PropertyMetadata(PaintSet.None, PointValueChanged));
93

    
94
        public static readonly DependencyProperty IsCompletedProperty = DependencyProperty.Register(
95
               "IsCompleted", typeof(bool), typeof(InkControl), null);
96

    
97
        public static readonly DependencyProperty StartPointProperty = DependencyProperty.Register(
98
               "StartPoint", typeof(Point), typeof(InkControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
99

    
100
        public static readonly DependencyProperty EndPointProperty = DependencyProperty.Register(
101
               "EndPoint", typeof(Point), typeof(InkControl), new PropertyMetadata(new Point(100, 100), PointValueChanged));
102

    
103
        public static readonly DependencyProperty PointSetProperty = DependencyProperty.Register(
104
                "PointSet", typeof(List<Point>), typeof(InkControl), new PropertyMetadata(new List<Point>(), PointValueChanged));
105

    
106
        /// <summary>
107
        /// StylusPointSet을 List<Point>로 대체하면 PointValueChanged가 작동안한다.
108
        /// </summary>
109
        /// 강인구 추가 Ink Control
110
        public static readonly DependencyProperty StylusPointSetProperty = DependencyProperty.Register(
111
                "PointC", typeof(List<StylusPointSet>), typeof(InkControl), new PropertyMetadata(new List<StylusPointSet>(), PointValueChanged));
112

    
113
        public static readonly DependencyProperty AngleProperty =
114
            DependencyProperty.Register("Angle", typeof(double), typeof(InkControl), new PropertyMetadata((double)0.0, new PropertyChangedCallback
115
                (AngleValueChanged)));
116

    
117
        public static readonly DependencyProperty CenterXProperty =
118
            DependencyProperty.Register("CenterX", typeof(double), typeof(InkControl), new PropertyMetadata((double)0, OnCenterXYChanged));
119

    
120
        public static readonly DependencyProperty CenterYProperty =
121
            DependencyProperty.Register("CenterY", typeof(double), typeof(InkControl), new PropertyMetadata((double)0, OnCenterXYChanged));
122

    
123
        public static readonly DependencyProperty IsSelectedProperty =
124
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(InkControl), new FrameworkPropertyMetadata(false, IsSelectedChanged));
125

    
126
        public static readonly DependencyProperty ControlTypeProperty =
127
            DependencyProperty.Register("ControlType", typeof(ControlType), typeof(InkControl), new FrameworkPropertyMetadata(ControlType.Ink));
128

    
129
        public static readonly DependencyProperty CanvasXProperty = DependencyProperty.Register(
130
                "CanvasX", typeof(double), typeof(InkControl), new PropertyMetadata((double)0, OnSetCansvasChanged));
131

    
132
        public static readonly DependencyProperty CanvasYProperty = DependencyProperty.Register(
133
                "CanvasY", typeof(double), typeof(InkControl), new PropertyMetadata((double)0, OnSetCansvasChanged));
134

    
135
        #endregion
136

    
137
        #region PropertyChanged Method
138
        public static void OnSetCansvasChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
139
        {
140
            var instance = (InkControl)sender;
141

    
142
            if (e.OldValue != e.NewValue && instance != null)
143
            {
144
                instance.SetValue(e.Property, e.NewValue);
145
                //Canvas.SetLeft(instance, instance.CanvasX);
146
                //Canvas.SetTop(instance, instance.CanvasY);
147
            }
148
        }
149

    
150
        public static void PointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
151
        {
152
            var instance = (InkControl)sender;
153

    
154
            if (e.OldValue != e.NewValue && instance != null)
155
            {
156
                instance.SetValue(e.Property, e.NewValue);
157
                //강인구 추가
158
                instance.SetPolyPath();
159
                //instance.SetPolyPath(); 주석처리
160

    
161
            }
162
        }
163
        public static void AngleValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
164
        {
165
            var instance = (InkControl)sender;
166

    
167
            if (e.OldValue != e.NewValue && instance != null)
168
            {
169
                instance.SetValue(e.Property, e.NewValue);
170
                instance.SetPolyPath();
171
            }
172
        }
173
        public static void OnCenterXYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
174
        {
175
            var instance = (InkControl)sender;
176
            if (e.OldValue != e.NewValue && instance != null)
177
            {
178
                instance.SetValue(e.Property, e.NewValue);
179
                instance.SetPolyPath();
180
            }
181
        }
182

    
183
        public static void IsSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
184
        {
185
            //var instance = (InkControl)sender;
186

    
187
            //if (e.OldValue != e.NewValue && instance.Base_PolyPath != null)
188
            //{
189
            //    instance.SetValue(e.Property, e.NewValue);
190

    
191
            //    if (instance.IsSelected)
192
            //    {
193
            //        instance.Base_PolyPath.Stroke = new SolidColorBrush(Colors.Blue);
194
            //    }
195
            //    else
196
            //    {
197
            //        instance.Base_PolyPath.Stroke = new SolidColorBrush(Colors.Transparent);
198
            //    }
199
            //}
200
        }
201

    
202
        #endregion
203

    
204
        #region Properties
205

    
206

    
207
        public bool IsCompleted
208
        {
209
            get { return (bool)GetValue(IsCompletedProperty); }
210
            set
211
            {
212
                SetValue(IsCompletedProperty, value);
213
                OnPropertyChanged("IsCompleted");
214
            }
215
        }
216

    
217

    
218
        public Geometry OverViewPathData
219
        {
220
            get { return (Geometry)GetValue(OverViewPathDataProperty); }
221
            set
222
            {
223
                SetValue(OverViewPathDataProperty, value);
224
                OnPropertyChanged("OverViewPathData");
225
            }
226
        }
227

    
228
        public double CanvasX
229
        {
230
            get { return (double)GetValue(CanvasXProperty); }
231
            set
232
            {
233
                if (this.CanvasX != value)
234
                {
235
                    SetValue(CanvasXProperty, value);
236
                    OnPropertyChanged("CanvasX");
237
                }
238
            }
239
        }
240

    
241
        public double CanvasY
242
        {
243
            get { return (double)GetValue(CanvasYProperty); }
244
            set
245
            {
246
                if (this.CanvasY != value)
247
                {
248
                    SetValue(CanvasYProperty, value);
249
                    OnPropertyChanged("CanvasY");
250
                }
251
            }
252
        }
253

    
254
        public override bool IsSelected
255
        {
256
            get
257
            {
258
                return (bool)GetValue(IsSelectedProperty);
259
            }
260
            set
261
            {
262
                SetValue(IsSelectedProperty, value);
263
                OnPropertyChanged("IsSelected");
264
            }
265
        }
266

    
267
        public PaintSet Paint
268
        {
269
            get { return (PaintSet)GetValue(PaintProperty); }
270
            set
271
            {
272
                if (this.Paint != value)
273
                {
274
                    SetValue(PaintProperty, value);
275
                    OnPropertyChanged("Paint");
276
                }
277
            }
278
        }
279

    
280
        public override ControlType ControlType
281
        {
282
            set
283
            {
284
                SetValue(ControlTypeProperty, value);
285
                OnPropertyChanged("ControlType");
286
            }
287
            get
288
            {
289
                return (ControlType)GetValue(ControlTypeProperty);
290
            }
291
        }
292

    
293
        public Double LineSize
294
        {
295
            get { return (Double)GetValue(LineSizeProperty); }
296
            set
297
            {
298
                if (this.LineSize != value)
299
                {
300
                    SetValue(LineSizeProperty, value);
301
                    OnPropertyChanged("LineSize");
302
                }
303
            }
304
        }
305

    
306
        public DoubleCollection DashSize
307
        {
308
            get { return (DoubleCollection)GetValue(DashSizeProperty); }
309
            set
310
            {
311
                if (this.DashSize != value)
312
                {
313
                    SetValue(DashSizeProperty, value);
314
                    OnPropertyChanged("DashSize");
315
                }
316
            }
317
        }
318
        public string UserID
319
        {
320
            get { return (string)GetValue(UserIDProperty); }
321
            set
322
            {
323
                if (this.UserID != value)
324
                {
325
                    SetValue(UserIDProperty, value);
326
                    OnPropertyChanged("UserID");
327
                }
328
            }
329
        }
330
        public List<Point> PointSet
331
        {
332
            get { return (List<Point>)GetValue(PointSetProperty); }
333
            set
334
            {
335
                SetValue(PointSetProperty, value);
336
                OnPropertyChanged("PointSet");
337
            }
338
        }
339
        //강인구 추가 InkControl
340
        public List<StylusPointSet> PointC
341
        {
342
            get { return (List<StylusPointSet>)GetValue(StylusPointSetProperty); }
343
            set
344
            {
345
                SetValue(StylusPointSetProperty, value);
346
                OnPropertyChanged("PointC");
347
            }
348
        }
349

    
350

    
351
        public double CenterX
352
        {
353
            get { return (double)GetValue(CenterXProperty); }
354
            set
355
            {
356
                SetValue(CenterXProperty, value);
357
                OnPropertyChanged("CenterX");
358
            }
359
        }
360
        public double CenterY
361
        {
362
            get
363
            {
364
                return (double)GetValue(CenterYProperty);
365
            }
366
            set
367
            {
368
                SetValue(CenterYProperty, value);
369
                OnPropertyChanged("CenterY");
370
            }
371
        }
372
        public override SolidColorBrush StrokeColor
373
        {
374
            get { return (SolidColorBrush)GetValue(StrokeColorProperty); }
375
            set
376
            {
377
                if (this.StrokeColor != value)
378
                {
379
                    SetValue(StrokeColorProperty, value);
380
                    OnPropertyChanged("StrokeColor");
381
                }
382
            }
383
        }
384
        public Geometry PathData
385
        {
386
            get { return (Geometry)GetValue(PathDataProperty); }
387
            set
388
            {
389
                SetValue(PathDataProperty, value);
390
                OnPropertyChanged("PathData");
391
            }
392
        }
393

    
394
        public double Angle
395
        {
396
            get { return (double)GetValue(AngleProperty); }
397
            set
398
            {
399
                if (this.Angle != value)
400
                {
401
                    SetValue(AngleProperty, value);
402
                    OnPropertyChanged("Angle");
403
                }
404
            }
405
        }
406

    
407
        public Point EndPoint
408
        {
409
            get { return (Point)GetValue(EndPointProperty); }
410
            set
411
            {
412
                SetValue(EndPointProperty, value);
413
                OnPropertyChanged("EndPoint");
414
            }
415
        }
416
        public Point StartPoint
417
        {
418
            get { return (Point)GetValue(StartPointProperty); }
419
            set
420
            {
421
                SetValue(StartPointProperty, value);
422
                OnPropertyChanged("StartPoint");
423
            }
424
        }
425
        #endregion
426

    
427
        public void updateControl()
428
        {
429
            this.PointSet = new List<Point>();
430

    
431
            //this.PointSet.Clear();
432

    
433
            //this.PointSet.AddRange(PointC.pointSet);
434
            //this.PointSet.AddRange(PointSet);
435

    
436
            //강인구 추가(Ink Control)
437

    
438

    
439
            foreach (var item in PointC)
440
            {
441
                PointSet.AddRange(item.pointSet);
442
            }
443

    
444
            ////////////////////////////////////////
445

    
446
            this.StartPoint = new Point(this.PointSet[0].X, this.PointSet[0].Y);
447

    
448
            this.EndPoint = new Point(this.PointSet[this.PointSet.Count - 1].X, this.PointSet[this.PointSet.Count - 1].Y);
449

    
450
            //SetPolyPath();
451
        }
452

    
453
        private void SetPolyPath()
454
        {
455
            this.ApplyTemplate();
456

    
457
            if (Base_PolyPath != null)
458
            {
459
                Base_PolyPath.StrokeDashArray.Clear();
460

    
461
                if (DashSize != null)
462
                {
463
                    foreach (var item in this.DashSize)
464
                    {
465
                        Base_PolyPath.StrokeDashArray.Add(item);
466
                    }
467
                }
468

    
469
                PathGeometry pathGeometry = new PathGeometry();
470
                if (PointC != null && PointC.Count > 0)
471
                {
472
                    foreach (var item in PointC)
473
                    {
474
                        PathFigure pathFigure_ = new PathFigure();
475
                        pathFigure_.StartPoint = item.pointSet[0];
476
                        System.Diagnostics.Debug.WriteLine("SP : " + pathFigure_.StartPoint);
477
                        PolyLineSegment instance_ = new PolyLineSegment();
478
                        foreach (var Inneritem in item.pointSet)
479
                        {
480
                            instance_.Points.Add(Inneritem);
481
                        }
482
                        pathFigure_.Segments.Add(instance_);
483
                        pathGeometry.Figures.Add(pathFigure_);
484
                    }
485
                    this.PathData = pathGeometry;
486
                    return;
487
                }
488

    
489
                PathFigure pathFigure = new PathFigure();
490
                PolyLineSegment instance = new PolyLineSegment();
491

    
492

    
493
                foreach (var Inneritem in PointSet)
494
                {
495
                    instance.Points.Add(Inneritem);
496
                }
497

    
498

    
499
                StartPoint = instance.Points.First();
500
                pathFigure.StartPoint = StartPoint;
501
                EndPoint = instance.Points.Last();
502
                pathFigure.Segments.Add(instance);
503
                pathGeometry.Figures.Add(pathFigure);
504

    
505
                //강인구 추가(Chain이 아닌 Polygon일때만 채우기 및 빗금 하기)
506
                if (ControlType == ControlType.Ink)
507
                {
508
                    switch (this.Paint)
509
                    {
510
                        case PaintSet.None:
511
                            {
512
                                //강인구 추가
513
                                Base_PolyPath.Fill = null;
514
                            }
515
                            break;
516
                        case PaintSet.Fill:
517
                            {
518
                                Base_PolyPath.Fill = this.StrokeColor;
519
                            }
520
                            break;
521
                        case PaintSet.Hatch:
522
                            {
523
                                Base_PolyPath.Fill = HatchMake.CreateHatchBrush(this.StrokeColor, this.LineSize * 0.1);
524
                            }
525
                            break;
526
                        default:
527
                            break;
528
                    }
529
                }
530

    
531
                this.PathData = pathGeometry;
532
                this.OverViewPathData = PathData;
533
            }
534
        }
535

    
536
        public void ChangePaint(PaintSet state)
537
        {
538

    
539
        }
540

    
541
        /// <summary>
542
        /// move control point has same location of given pt along given delta
543
        /// </summary>
544
        /// <author>humkyung</author>
545
        /// <date>2019.06.20</date>
546
        /// <param name="pt"></param>
547
        /// <param name="dx"></param>
548
        /// <param name="dy"></param>
549
        public override void OnMoveCtrlPoint(Point pt, double dx, double dy)
550
        {
551
            Point selected = MathSet.getNearPoint((this as IPath).PointSet, pt);
552
            selected.X += dx;
553
            selected.Y += dy;
554
            for (int i = 0; i < (this as IPath).PointSet.Count; i++)
555
            {
556
                if (pt.Equals((this as IPath).PointSet[i]))
557
                {
558
                    (this as IPath).PointSet[i] = selected;
559
                    break;
560
                }
561
            }
562
            this.updateControl();
563
        }
564

    
565
        /// <summary>
566
        /// Serialize this
567
        /// </summary>
568
        /// <param name="sUserId"></param>
569
        /// <returns></returns>
570
        public override string Serialize()
571
        {
572
            using (S_PolyControl STemp = new S_PolyControl())
573
            {
574
                STemp.TransformPoint = "0|0";
575
                STemp.SizeSet = String.Format("{0}", this.LineSize);
576
                STemp.StrokeColor = this.StrokeColor.Color.ToString();
577
                //STemp.StrokeColor = "#FF000FFF";
578
                STemp.Name = this.GetType().Name.ToString();
579
                //STemp.Toler = this.Toler;
580
                STemp.PaintState = this.Paint;
581
                STemp.Opac = this.Opacity;
582
                STemp.UserID = this.UserID;
583
                STemp.PaintState = this.Paint;
584
                //강인구 추가(Chain인지 Polygon인지 구분)
585
                STemp.Type = this.ControlType;
586
                //STemp.IsTrans = this.isTransOn;
587
                //STemp.IsChain = this.isChain;
588
                STemp.PointSet = new List<Point>();
589
                STemp.DashSize = this.DashSize;
590
                STemp.IsCompleted = this.IsCompleted;
591
                STemp.StartPoint = this.StartPoint;
592
                STemp.EndPoint = this.EndPoint;
593
                foreach (var point in this.PointSet)
594
                {
595
                    STemp.PointSet.Add(point);
596
                }
597
                ///강인구 추가(2017.11.02)
598
                ///Memo 추가
599
                STemp.Memo = this.Memo;
600

    
601
                return "|DZ|" + JsonSerializerHelper.CompressString((STemp.JsonSerialize()));
602
            };
603
        }
604

    
605
        /// <summary>
606
        /// create a inkcontrol from given string
607
        /// </summary>
608
        /// <param name="str"></param>
609
        /// <returns></returns>
610
        public static InkControl FromString(string str, SolidColorBrush brush, string sProjectNo)
611
        {
612
            using (S_PolyControl s = JsonSerializerHelper.JsonDeserialize<S_PolyControl>(str))
613
            {
614
                string[] data2 = s.SizeSet.Split(delimiterChars2, StringSplitOptions.RemoveEmptyEntries);
615
                return new InkControl
616
                {
617
                    LineSize = Convert.ToDouble(data2.First()),
618
                    //Toler = s.Toler,
619
                    IsCompleted = s.IsCompleted,
620
                    //PointSet = new List<Point>(),
621
                    Opacity = s.Opac,
622
                    StrokeColor = brush,
623
                    //강인구 추가(Chain인지 Polygon인지 구분)
624
                    ControlType = s.Type,
625
                    DashSize = s.DashSize,
626
                    StartPoint = s.StartPoint,
627
                    PointSet = s.PointSet,
628
                    UserID = s.UserID,
629
                    Paint = s.PaintState,
630
                    EndPoint = s.EndPoint,
631
                    //PointC = s.PointC,
632
                    Memo = s.Memo
633
                };
634
            }
635
        }
636
        //public PaintSet Paint { get; set; }
637

    
638

    
639
        #region Dispose
640
        public void Dispose()
641
        {
642
            GC.Collect();
643
            GC.SuppressFinalize(this);
644
        }
645
        #endregion
646

    
647
        #region INotifyPropertyChanged
648
        private void OnPropertyChanged(string name)
649
        {
650
            if (PropertyChanged != null)
651
            {
652
                PropertyChanged(this, new PropertyChangedEventArgs(name));
653
            }
654
        }
655

    
656
        public event PropertyChangedEventHandler PropertyChanged;
657
        #endregion
658
    }
659

    
660
    //public class StylusPointSet : INotifyPropertyChanged
661
    //{
662
    //    public StylusPointSet()
663
    //    {
664
    //        if (pointSet == null)
665
    //            pointSet = new List<Point>();
666
    //    }
667

    
668
    //    public List<Point> _pointSet;
669

    
670
    //    public List<Point> pointSet
671
    //    {
672
    //        get
673
    //        {
674
    //            return _pointSet;
675
    //        }
676
    //        set
677
    //        {
678
    //            _pointSet = value;
679
    //            OnPropertyChanged("pointSet");
680
    //        }
681
    //    }
682

    
683
    //    #region INotifyPropertyChanged
684
    //    private void OnPropertyChanged(string name)
685
    //    {
686
    //        if (PropertyChanged != null)
687
    //        {
688
    //            PropertyChanged(this, new PropertyChangedEventArgs(name));
689
    //        }
690
    //    }
691

    
692
    //    public event PropertyChangedEventHandler PropertyChanged;
693
    //    #endregion
694
    //}
695
}
클립보드 이미지 추가 (최대 크기: 500 MB)