프로젝트

일반

사용자정보

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

markus / MarkupToPDF / Controls / Shape / CircleControl.cs @ a5b465dc

이력 | 보기 | 이력해설 | 다운로드 (19.6 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.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
        public SolidColorBrush StrokeColor
218
        {
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
        public override bool IsSelected
300
        {
301
            get
302
            {
303
                return (bool)GetValue(IsSelectedProperty);
304
            }
305
            set
306
            {
307
                SetValue(IsSelectedProperty, value);
308
            }
309
        }
310

    
311
        public override ControlType ControlType
312
        {
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
        private void SetCircle()
363
        {
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
        public void updateControl()
416
        {
417
            this.StartPoint = new Point(this.PointSet[0].X, this.PointSet[0].Y);
418
            this.LeftBottomPoint = new Point(this.PointSet[1].X, this.PointSet[1].Y);
419
            this.TopRightPoint = new Point(this.PointSet[3].X, this.PointSet[3].Y);
420
            this.EndPoint = new Point(this.PointSet[2].X, this.PointSet[2].Y);
421
        }
422

    
423
        public void SetCenterXY()
424
        {
425
            CenterX = MathSet.getMiddlePoint(this.StartPoint, this.EndPoint).X;
426
            CenterY = MathSet.getMiddlePoint(this.StartPoint, this.EndPoint).Y;
427
        }
428

    
429
        public void ApplyOverViewData()
430
        {
431
            this.OverViewPathData = this.PathData;
432
        }
433

    
434
        public void ChangePaint(PaintSet state)
435
        {
436
            this.Paint = state;
437
            this.SetCircle();
438
        }
439

    
440
        /// <summary>
441
        /// return circlecontrols' area
442
        /// </summary>
443
        /// <author>humkyung</author>
444
        /// <date>2019.06.13</date>
445
        public override Rect ItemRect
446
        {
447
            get
448
            {
449
                double dMinX = Math.Min(this.StartPoint.X, this.EndPoint.X);
450
                double dMinY = Math.Min(this.StartPoint.Y, this.EndPoint.Y);
451
                double dMaxX = Math.Max(this.StartPoint.X, this.EndPoint.X);
452
                double dMaxY = Math.Max(this.StartPoint.Y, this.EndPoint.Y);
453

    
454
                return new Rect(new Point(dMinX, dMinY), new Point(dMaxX, dMaxY));
455
            }
456
        }
457

    
458
        /// <summary>
459
        /// Serialize this
460
        /// </summary>
461
        /// <param name="sUserId"></param>
462
        /// <returns></returns>
463
        public override string Serialize()
464
        {
465
            using (S_CircleControl STemp = new S_CircleControl())
466
            {
467
                STemp.TransformPoint = "0|0";
468
                STemp.SizeSet = String.Format("{0}", this.LineSize);
469
                STemp.PaintState = this.Paint;
470
                //STemp.StrokeColor = "#FF00FF00";
471
                STemp.StrokeColor = this.StrokeColor.Color.ToString();
472
                if (this.FillColor != null)
473
                {
474
                    STemp.FillColor = this.FillColor.Color.ToString();
475
                }
476
                STemp.StartPoint = this.StartPoint;
477
                STemp.UserID = this.UserID;
478
                STemp.EndPoint = this.EndPoint;
479
                STemp.TRP = this.TopRightPoint;
480
                STemp.LBP = this.LeftBottomPoint;
481
                STemp.Opac = this.Opacity;
482
                STemp.Angle = this.Angle;
483
                STemp.PointSet = this.PointSet;
484
                STemp.DashSize = this.DashSize;
485
                STemp.Name = this.GetType().Name.ToString();
486
                ///강인구 추가(2017.11.02)
487
                ///Memo 추가
488
                STemp.Memo = this.Memo;
489

    
490
                return "|DZ|" + JsonSerializerHelper.CompressString((STemp.JsonSerialize()));
491
            };
492
        }
493

    
494
        /// <summary>
495
        /// create a circlecontrol from given string
496
        /// </summary>
497
        /// <param name="str"></param>
498
        /// <returns></returns>
499
        public static CircleControl FromString(string str, SolidColorBrush brush, string sProjectNo)
500
        {
501
            using (S_CircleControl s = JsonSerializerHelper.JsonDeserialize<S_CircleControl>(str))
502
            {
503
                string[] data2 = s.SizeSet.Split(CommentUserInfo.delimiterChars2, StringSplitOptions.RemoveEmptyEntries);
504
                return new CircleControl
505
                {
506
                    LineSize = Convert.ToDouble(data2.First()),
507
                    Paint = s.PaintState,
508
                    StartPoint = s.StartPoint,
509
                    EndPoint = s.EndPoint,
510
                    LeftBottomPoint = s.LBP,
511
                    TopRightPoint = s.TRP,
512
                    Opacity = s.Opac,
513
                    Angle = s.Angle,
514
                    DashSize = s.DashSize,
515
                    PointSet = s.PointSet,
516
                    StrokeColor = brush,
517
                    UserID = s.UserID,
518
                    Memo = s.Memo
519
                };
520
            }
521
        }
522
    }
523
}
클립보드 이미지 추가 (최대 크기: 500 MB)