프로젝트

일반

사용자정보

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

markus / MarkupToPDF / Controls / Shape / CircleControl.cs @ 0d00f9c8

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

1 787a4489 KangIngu
using MarkupToPDF.Common;
2
using MarkupToPDF.Controls.Common;
3 036650a0 humkyung
using MarkupToPDF.Serialize.Core;
4
using MarkupToPDF.Serialize.S_Control;
5 787a4489 KangIngu
using System;
6
using System.Collections.Generic;
7
using System.ComponentModel;
8 661b7416 humkyung
using System.Linq;
9 787a4489 KangIngu
using System.Windows;
10
using System.Windows.Media;
11
using System.Windows.Shapes;
12
13
namespace MarkupToPDF.Controls.Shape
14
{
15
    [TemplatePart(Name = "PART_CirclePath", Type = typeof(Path))]
16
    public class CircleControl : CommentUserInfo, IDisposable, IPath, INotifyPropertyChanged, IMarkupCommonData, IShapeControl, ICircleControl, IDashControl
17
    {
18
        private const string PART_CirclePath = "PART_CirclePath";
19
        public Path Base_CirclePath = null;
20
21
        static CircleControl()
22
        {
23
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CircleControl), new FrameworkPropertyMetadata(typeof(CircleControl)));
24
            //Application.Current.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("/MarkupToPDF;Component/Themes/generic.xaml")) as ResourceDictionary);
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
        #region Dependency Properties
31
32
        public static readonly DependencyProperty IsSelectedProperty =
33
      DependencyProperty.Register("IsSelected", typeof(bool), typeof(CircleControl), new FrameworkPropertyMetadata(false, IsSelectedChanged));
34
35
        public static readonly DependencyProperty ControlTypeProperty =
36
    DependencyProperty.Register("ControlType", typeof(ControlType), typeof(CircleControl), new FrameworkPropertyMetadata(ControlType.Circle));
37
38
39
40
41
        public static readonly DependencyProperty OverViewPathDataProperty = DependencyProperty.Register(
42
                "OverViewPathData", typeof(Geometry), typeof(CircleControl), new PropertyMetadata(null));
43
44
        public static readonly DependencyProperty LineSizeProperty = DependencyProperty.Register(
45
                "LineSize", typeof(double), typeof(CircleControl), new PropertyMetadata((Double)3));
46
        public static readonly DependencyProperty UserIDProperty = DependencyProperty.Register(
47
                "UserID", typeof(string), typeof(CircleControl), new PropertyMetadata(null));
48
				
49
        public static readonly DependencyProperty FillColorProperty = DependencyProperty.Register(
50
                "FillColor", typeof(SolidColorBrush), typeof(CircleControl), new PropertyMetadata(new SolidColorBrush(Colors.Red)));
51
52
        public static readonly DependencyProperty StrokeColorProperty = DependencyProperty.Register(
53
                "StrokeColor", typeof(SolidColorBrush), typeof(CircleControl), new PropertyMetadata(new SolidColorBrush(Colors.Red)));
54
55
        public static readonly DependencyProperty PathDataProperty = DependencyProperty.Register(
56
                "PathData", typeof(Geometry), typeof(CircleControl), null);
57
        //강인구 추가
58
        public static readonly DependencyProperty PaintProperty = DependencyProperty.Register(
59
                "Paint", typeof(PaintSet), typeof(CircleControl), new PropertyMetadata(PaintSet.None, PointValueChanged));
60
61
        public static readonly DependencyProperty EndPointProperty = DependencyProperty.Register(
62
                "EndPoint", typeof(Point), typeof(CircleControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
63
64
        //강인구 추가
65
        public static readonly DependencyProperty DashSizeProperty = DependencyProperty.Register(
66
                "DashSize", typeof(DoubleCollection), typeof(CircleControl), new PropertyMetadata(new DoubleCollection { 1, 1 }, PointValueChanged));
67
68
        public static readonly DependencyProperty TopRightPointProperty = DependencyProperty.Register(
69
              "TopRightPoint", typeof(Point), typeof(CircleControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
70
71
        public static readonly DependencyProperty LeftBottomPointProperty = DependencyProperty.Register(
72
                 "LeftBottomPoint", typeof(Point), typeof(CircleControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
73
74
        public static readonly DependencyProperty StartPointProperty = DependencyProperty.Register(
75
                "StartPoint", typeof(Point), typeof(CircleControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
76
        public static readonly DependencyProperty PointSetProperty = DependencyProperty.Register(
77
                 "PointSet", typeof(List<Point>), typeof(CircleControl), new PropertyMetadata(new List<Point>()));
78
        public static readonly DependencyProperty AngleProperty = DependencyProperty.Register("Angle", typeof(double), typeof(CircleControl),
79
            new PropertyMetadata((double)0.0, new PropertyChangedCallback(AngleValueChanged)));
80
        public static readonly DependencyProperty CenterXProperty = DependencyProperty.Register("CenterX", typeof(double), typeof(CircleControl),
81
            new PropertyMetadata((double)0, OnCenterXYChanged));
82
        public static readonly DependencyProperty CenterYProperty = DependencyProperty.Register("CenterY", typeof(double), typeof(CircleControl),
83
            new PropertyMetadata((double)0, OnCenterXYChanged));
84
        public static readonly DependencyProperty mousemodeProperty =
85
                        DependencyProperty.Register("mousemode", typeof(MouseMode), typeof(CircleControl), new PropertyMetadata(MouseMode.None, PointValueChanged));
86
        #endregion
87
        #region PropertyChanged Method
88
89
         public static void IsSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
90
        {
91
            //var instance = (CircleControl)sender;
92
93
            //if (e.OldValue != e.NewValue && instance.Base_CirclePath != null)
94
            //{
95
96
            //    instance.SetValue(e.Property, e.NewValue);
97
98
            //    if (instance.IsSelected)
99
            //    {
100
            //        instance.Base_CirclePath.Stroke = new SolidColorBrush(Colors.Blue);
101
            //    }
102
            //    else
103
            //    {
104
            //        instance.Base_CirclePath.Stroke = new SolidColorBrush(Colors.Red);
105
            //    }
106
            //}
107
        }
108
109
        public static void PointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
110
        {
111
            var instance = (CircleControl)sender;
112
            if (e.OldValue != e.NewValue && instance.Base_CirclePath != null)
113
            {
114
                instance.SetValue(e.Property, e.NewValue);
115
                instance.SetCircle();
116
            }
117
        }
118
119
        public static void OnCenterXYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
120
        {
121
            var instance = (CircleControl)sender;
122
            if (e.OldValue != e.NewValue && instance.Base_CirclePath != null)
123
            {
124
                instance.SetValue(e.Property, e.NewValue);
125
                instance.SetCircle();
126
            }
127
        }
128
129
130
        public static void AngleValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
131
        {
132
            var instance = (CircleControl)sender;
133
            if (e.OldValue != e.NewValue && instance.Base_CirclePath != null)
134
            {
135
                instance.SetValue(e.Property, e.NewValue);
136
                instance.SetCircle();
137
            }
138
        }
139
        #endregion
140
        #region Properties
141
        public string UserID
142
        {
143
            get { return (string)GetValue(UserIDProperty); }
144
            set
145
            {
146
                if (this.UserID != value)
147
                {
148
                    SetValue(UserIDProperty, value);
149
                    OnPropertyChanged("UserID");
150
                }
151
            }
152
        }
153
        public MouseMode mousemode
154
        {
155
            get
156
            {
157
                return (MouseMode)GetValue(mousemodeProperty);
158
            }
159
            set
160
            {
161
                SetValue(mousemodeProperty, value);
162
                OnPropertyChanged("mousemode");
163
            }
164
        }
165
166
        public Double LineSize
167
        {
168
            get { return (Double)GetValue(LineSizeProperty); }
169
            set
170
            {
171
                if (this.LineSize != value)
172
                {
173
                    SetValue(LineSizeProperty, value);
174
                }
175
            }
176
        }
177
        public DoubleCollection DashSize
178
        {
179
            get { return (DoubleCollection)GetValue(DashSizeProperty); }
180
            set
181
            {
182
                if (this.DashSize != value)
183
                {
184
                    SetValue(DashSizeProperty, value);
185
                }
186
            }
187
        }
188
        public SolidColorBrush FillColor
189
        {
190
            get { return (SolidColorBrush)GetValue(FillColorProperty); }
191
            set
192
            {
193
                if (this.FillColor != value)
194
                {
195
                    SetValue(FillColorProperty, value);
196
                }
197
            }
198
        }
199
        public Point TopRightPoint
200
        {
201
            get { return (Point)GetValue(TopRightPointProperty); }
202
            set
203
            {
204
                SetValue(TopRightPointProperty, value);
205
                OnPropertyChanged("TopRightPoint");
206
            }
207
        }
208
        public Point LeftBottomPoint
209
        {
210
            get { return (Point)GetValue(LeftBottomPointProperty); }
211
            set
212
            {
213
                SetValue(LeftBottomPointProperty, value);
214
                OnPropertyChanged("LeftBottomPoint");
215
            }
216
        }
217 4913851c humkyung
        public override SolidColorBrush StrokeColor
218 787a4489 KangIngu
        {
219
            get { return (SolidColorBrush)GetValue(StrokeColorProperty); }
220
            set
221
            {
222
                if (this.StrokeColor != value)
223
                {
224
                    SetValue(StrokeColorProperty, value);
225
                }
226
            }
227
        }
228
        public Geometry PathData
229
        {
230
            get { return (Geometry)GetValue(PathDataProperty); }
231
            set { SetValue(PathDataProperty, value); }
232
        }
233
234
        public Geometry OverViewPathData
235
        {
236
            get
237
            {
238
                return (Geometry)GetValue(OverViewPathDataProperty);
239
            }
240
            set
241
            {
242
                SetValue(OverViewPathDataProperty, value);
243
                OnPropertyChanged("OverViewPathData");
244
            }
245
        }
246
        public PaintSet Paint
247
        {
248
            get { return (PaintSet)GetValue(PaintProperty); }
249
            set
250
            {
251
                if (this.Paint != value)
252
                {
253
                    SetValue(PaintProperty, value);
254
                }
255
            }
256
        }
257
        public Point EndPoint
258
        {
259
            get { return (Point)GetValue(EndPointProperty); }
260
            set { SetValue(EndPointProperty, value); }
261
        }
262
        public Point StartPoint
263
        {
264
            get { return (Point)GetValue(StartPointProperty); }
265
            set { SetValue(StartPointProperty, value); }
266
        }
267
        //public double CenterX
268
        //{
269
        //    get { return (double)GetValue(CenterXProperty); }
270
        //    set { SetValue(CenterXProperty, value); }
271
        //}
272
        //public double CenterY
273
        //{
274
        //    get { return (double)GetValue(CenterYProperty); }
275
        //    set { SetValue(CenterYProperty, value); }
276
        //}
277
        public double AngleValue
278
        {
279
            get { return (double)GetValue(AngleProperty); }
280
            set { SetValue(AngleProperty, value); }
281
        }
282
        public double Angle
283
        {
284
            get { return (double)GetValue(AngleProperty); }
285
            set
286
            {
287
                if (this.Angle != value)
288
                {
289
                    SetValue(AngleProperty, value);
290
                }
291
            }
292
        }
293
        public List<Point> PointSet
294
        {
295
            get { return (List<Point>)GetValue(PointSetProperty); }
296
            set { SetValue(PointSetProperty, value); }
297
        }
298
299 959b3ef2 humkyung
        public override bool IsSelected
300 787a4489 KangIngu
        {
301
            get
302
            {
303
                return (bool)GetValue(IsSelectedProperty);
304
            }
305
            set
306
            {
307
                SetValue(IsSelectedProperty, value);
308
            }
309
        }
310
311 5529d2a2 humkyung
        public override ControlType ControlType
312 787a4489 KangIngu
        {
313
            get
314
            {
315
                return (ControlType)GetValue(ControlTypeProperty);
316
            }
317
            set
318
            {
319
                SetValue(ControlTypeProperty, value);
320
            }
321
        }
322
323
        public double CenterX
324
        {
325
            get { return (double)GetValue(CenterXProperty); }
326
            set { SetValue(CenterXProperty, value); }
327
        }
328
        public double CenterY
329
        {
330
            get { return (double)GetValue(CenterYProperty); }
331
            set { SetValue(CenterYProperty, value); }
332
        }
333
        #endregion
334
        #region Object & Variable
335
        EllipseGeometry instance = new EllipseGeometry();
336
        GeometryGroup CircleGroup = new GeometryGroup();
337
338
        #endregion
339
        public override void OnApplyTemplate()
340
        {
341
            base.OnApplyTemplate();
342
            Base_CirclePath = GetTemplateChild(PART_CirclePath) as Path;
343
344
            if (Base_CirclePath == null)
345
            {
346
                return;
347
            }
348
            this.SetCircle();
349
        }
350
        public void Dispose()
351
        {
352
            GC.Collect();
353
            GC.SuppressFinalize(this);
354
        }
355
356
        public event PropertyChangedEventHandler PropertyChanged;
357
        protected void OnPropertyChanged(string propName)
358
        {
359
            if (PropertyChanged != null)
360
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
361
        }
362 661b7416 humkyung
        private void SetCircle()
363 787a4489 KangIngu
        {
364
            Base_CirclePath.StrokeDashArray.Clear();
365
            foreach (var item in this.DashSize)
366
            {
367
                Base_CirclePath.StrokeDashArray.Add(item);
368
            }
369
            Base_CirclePath.StrokeDashCap = PenLineCap.Square;
370
371
            Point middle = MathSet.getMiddlePoint(this.EndPoint, this.StartPoint);
372
373
            instance.RadiusX = (MathSet.DistanceTo(this.LeftBottomPoint, this.EndPoint) / 2);
374
            instance.RadiusY = (MathSet.DistanceTo(this.EndPoint, this.TopRightPoint) / 2);
375
            instance.Center = middle;
376
            switch (this.Paint)
377
            {
378
                case PaintSet.None:
379
                    this.FillColor = null;
380
                    Base_CirclePath.Fill = this.FillColor;
381
                    break;
382
                case PaintSet.Fill:
383
                    this.FillColor = this.StrokeColor;
384
                    Base_CirclePath.Fill = this.FillColor;
385
                    break;
386
                case PaintSet.Hatch:
387
                    Base_CirclePath.Fill = HatchMake.CreateHatchBrush(this.StrokeColor);
388
                    break;
389
                default:
390
                    break;
391
            }
392
393
394
            CircleGroup.Children.Clear();
395
            CircleGroup.FillRule = FillRule.Nonzero;
396
            CircleGroup.Children.Add(instance);
397
398
            try
399
            {                
400
                //강인구 수정(원 테두리가 잘리는것 방지)
401
                //this.Width = Math.Abs(this.CircleGroup.Bounds.Right + 2);
402
                //this.Height = Math.Abs(this.CircleGroup.Bounds.Bottom + 2);
403
                this.Width = Math.Abs(this.CircleGroup.Bounds.Right + 20);
404
                this.Height = Math.Abs(this.CircleGroup.Bounds.Bottom + 20);
405
            }
406
            catch (Exception)
407
            {
408
                
409
            }            
410
            CenterX = Math.Abs(this.CircleGroup.Bounds.X / 2);
411
            CenterY = Math.Abs(this.CircleGroup.Bounds.Y / 2);
412
            this.PathData = CircleGroup;
413
            ApplyOverViewData();
414
        }
415 0d00f9c8 humkyung
416
        public override void UpdateControl()
417 787a4489 KangIngu
        {
418
            this.StartPoint = new Point(this.PointSet[0].X, this.PointSet[0].Y);
419
            this.LeftBottomPoint = new Point(this.PointSet[1].X, this.PointSet[1].Y);
420
            this.TopRightPoint = new Point(this.PointSet[3].X, this.PointSet[3].Y);
421
            this.EndPoint = new Point(this.PointSet[2].X, this.PointSet[2].Y);
422
        }
423
424
        public void SetCenterXY()
425
        {
426
            CenterX = MathSet.getMiddlePoint(this.StartPoint, this.EndPoint).X;
427
            CenterY = MathSet.getMiddlePoint(this.StartPoint, this.EndPoint).Y;
428
        }
429
430 f513c215 humkyung
        public override void ApplyOverViewData()
431 787a4489 KangIngu
        {
432
            this.OverViewPathData = this.PathData;
433
        }
434
435
        public void ChangePaint(PaintSet state)
436
        {
437
            this.Paint = state;
438
            this.SetCircle();
439
        }
440 036650a0 humkyung
441
        /// <summary>
442 a6272c57 humkyung
        /// call when mouse is moving while drawing control
443
        /// </summary>
444
        /// <param name="pt"></param>
445
        /// <param name="bAxisLocked"></param>
446
        public override void OnCreatingMouseMove(Point pt, bool bAxisLocked, bool bShiftKeyPressed)
447
        {
448
            this.EndPoint = bShiftKeyPressed ? this.GetSquareEndPoint(this.StartPoint, pt) : pt;
449
            this.LeftBottomPoint = new Point(this.StartPoint.X, this.EndPoint.Y);
450
            this.TopRightPoint = new Point(this.EndPoint.X, this.StartPoint.Y);
451
452
            this.PointSet = new List<Point>
453
            {
454
                this.StartPoint,
455
                this.LeftBottomPoint,
456
                this.EndPoint,
457
                this.TopRightPoint,
458
            };
459
        }
460
461
        /// <summary>
462 d2114d3b humkyung
        /// move control point has same location of given pt along given delta
463
        /// </summary>
464
        /// <author>humkyung</author>
465
        /// <date>2019.06.20</date>
466
        /// <param name="pt"></param>
467
        /// <param name="dx"></param>
468
        /// <param name="dy"></param>
469
        public override void OnMoveCtrlPoint(Point pt, double dx, double dy)
470
        {
471
            IPath path = (this as IPath);
472
            Point selected = MathSet.getNearPoint(path.PointSet, pt);
473
            selected.X += dx;
474
            selected.Y += dy;
475
            int i = 0;
476
            for (i = 0; i < (this as IPath).PointSet.Count; i++)
477
            {
478
                if (pt.Equals((this as IPath).PointSet[i]))
479
                {
480
                    (this as IPath).PointSet[i] = selected;
481
                    break;
482
                }
483
            }
484
485
            List<Point> newPointSet = new List<Point> { };
486
            Point middle = new Point(path.PathData.Bounds.X + path.PathData.Bounds.Width * 0.5, path.PathData.Bounds.Y + path.PathData.Bounds.Height * 0.5);
487
            foreach (Point _pt in path.PointSet)
488
            {
489
                newPointSet.Add(_pt);
490
            }
491
            var ReverseP = (i + newPointSet.Count() / 2) % newPointSet.Count();
492
            var PreviousP = (i + (newPointSet.Count() - 1)) % newPointSet.Count();
493
            var NextP = (i + 1) % newPointSet.Count();
494
            var distance = MathSet.DistanceTo(newPointSet[ReverseP], newPointSet[i]);
495
            var PreviousV = MathSet.GetNormVectorBetween(newPointSet[ReverseP], newPointSet[PreviousP]);
496
            var l = MathSet.DotProduct(PreviousV.X, PreviousV.Y, newPointSet[i].X - newPointSet[ReverseP].X,
497
                newPointSet[i].Y - newPointSet[ReverseP].Y);
498
            newPointSet[PreviousP] = new Point(newPointSet[ReverseP].X + PreviousV.X * l, newPointSet[ReverseP].Y
499
                + PreviousV.Y * l);
500
501
            var NextV = MathSet.GetNormVectorBetween(newPointSet[ReverseP], newPointSet[NextP]);
502
            l = MathSet.DotProduct(NextV.X, NextV.Y, newPointSet[i].X - newPointSet[ReverseP].X, newPointSet[i].Y
503
                - newPointSet[ReverseP].Y);
504
            newPointSet[NextP] = new Point(newPointSet[ReverseP].X + NextV.X * l, newPointSet[ReverseP].Y + NextV.Y * l);
505
506
            path.PointSet = newPointSet;
507 0d00f9c8 humkyung
            this.UpdateControl();
508 d2114d3b humkyung
        }
509
510
        /// <summary>
511 91efe37a humkyung
        /// return circlecontrols' area
512
        /// </summary>
513
        /// <author>humkyung</author>
514
        /// <date>2019.06.13</date>
515
        public override Rect ItemRect
516
        {
517
            get
518
            {
519
                double dMinX = Math.Min(this.StartPoint.X, this.EndPoint.X);
520
                double dMinY = Math.Min(this.StartPoint.Y, this.EndPoint.Y);
521
                double dMaxX = Math.Max(this.StartPoint.X, this.EndPoint.X);
522
                double dMaxY = Math.Max(this.StartPoint.Y, this.EndPoint.Y);
523
524
                return new Rect(new Point(dMinX, dMinY), new Point(dMaxX, dMaxY));
525
            }
526
        }
527
528
        /// <summary>
529 036650a0 humkyung
        /// Serialize this
530
        /// </summary>
531
        /// <param name="sUserId"></param>
532
        /// <returns></returns>
533
        public override string Serialize()
534
        {
535
            using (S_CircleControl STemp = new S_CircleControl())
536
            {
537
                STemp.TransformPoint = "0|0";
538
                STemp.SizeSet = String.Format("{0}", this.LineSize);
539
                STemp.PaintState = this.Paint;
540
                //STemp.StrokeColor = "#FF00FF00";
541
                STemp.StrokeColor = this.StrokeColor.Color.ToString();
542
                if (this.FillColor != null)
543
                {
544
                    STemp.FillColor = this.FillColor.Color.ToString();
545
                }
546
                STemp.StartPoint = this.StartPoint;
547
                STemp.UserID = this.UserID;
548
                STemp.EndPoint = this.EndPoint;
549
                STemp.TRP = this.TopRightPoint;
550
                STemp.LBP = this.LeftBottomPoint;
551
                STemp.Opac = this.Opacity;
552
                STemp.Angle = this.Angle;
553
                STemp.PointSet = this.PointSet;
554
                STemp.DashSize = this.DashSize;
555
                STemp.Name = this.GetType().Name.ToString();
556
                ///강인구 추가(2017.11.02)
557
                ///Memo 추가
558
                STemp.Memo = this.Memo;
559
560
                return "|DZ|" + JsonSerializerHelper.CompressString((STemp.JsonSerialize()));
561
            };
562
        }
563 661b7416 humkyung
564
        /// <summary>
565
        /// create a circlecontrol from given string
566
        /// </summary>
567
        /// <param name="str"></param>
568
        /// <returns></returns>
569
        public static CircleControl FromString(string str, SolidColorBrush brush, string sProjectNo)
570
        {
571
            using (S_CircleControl s = JsonSerializerHelper.JsonDeserialize<S_CircleControl>(str))
572
            {
573
                string[] data2 = s.SizeSet.Split(CommentUserInfo.delimiterChars2, StringSplitOptions.RemoveEmptyEntries);
574
                return new CircleControl
575
                {
576
                    LineSize = Convert.ToDouble(data2.First()),
577
                    Paint = s.PaintState,
578
                    StartPoint = s.StartPoint,
579
                    EndPoint = s.EndPoint,
580
                    LeftBottomPoint = s.LBP,
581
                    TopRightPoint = s.TRP,
582
                    Opacity = s.Opac,
583
                    Angle = s.Angle,
584
                    DashSize = s.DashSize,
585
                    PointSet = s.PointSet,
586
                    StrokeColor = brush,
587
                    UserID = s.UserID,
588
                    Memo = s.Memo
589
                };
590
            }
591
        }
592 787a4489 KangIngu
    }
593
}
클립보드 이미지 추가 (최대 크기: 500 MB)