프로젝트

일반

사용자정보

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

markus / MarkupToPDF_Old / Controls / Line / ArcControl.cs @ 5538eb5a

이력 | 보기 | 이력해설 | 다운로드 (18.8 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

    
15

    
16
namespace MarkupToPDF.Controls.Line
17
{
18

    
19
    public class ArcControl : Control, IDisposable, INotifyPropertyChanged, IMarkupCommonData
20
    {
21
        #region 초기선언
22
        public event PropertyChangedEventHandler PropertyChanged;
23
        private const string PART_ArcPath = "PART_ArcPath";
24
        public Path Base_ArcPath = null;
25
        #endregion
26
        static ArcControl()
27
        {
28
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ArcControl), new FrameworkPropertyMetadata(typeof(ArcControl)));
29
            //Application.Current.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("/MarkupToPDF;Component/Themes/generic.xaml")) as ResourceDictionary);
30
            ResourceDictionary dictionary = new ResourceDictionary();
31
            dictionary.Source = new Uri("/MarkupToPDF;component/themes/generic.xaml", UriKind.RelativeOrAbsolute);
32
            Application.Current.Resources.MergedDictionaries.Add(dictionary);
33
             //System.Diagnostics.Debug.WriteLine("resource Count :" + Application.Current.Resources.MergedDictionaries.Count);
34
        }
35

    
36
        public ArcControl()
37
        {
38
            this.DefaultStyleKey = typeof(ArcControl);
39
        }
40

    
41
        public void Dispose()
42
        {
43
            GC.Collect();
44
            GC.SuppressFinalize(this);
45
        }
46
        protected void OnPropertyChanged(string propName)
47
        {
48
            if (PropertyChanged != null)
49
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
50
        }
51

    
52
        #region Dependency Properties
53

    
54
        
55

    
56
        public static readonly DependencyProperty IsSelectedProperty =
57
    DependencyProperty.Register("IsSelected", typeof(bool), typeof(ArcControl), new FrameworkPropertyMetadata(false, IsSelectedChanged));
58

    
59
        public static readonly DependencyProperty ControlTypeProperty =
60
    DependencyProperty.Register("ControlType", typeof(ControlType), typeof(ArcControl), new FrameworkPropertyMetadata(ControlType.ArcLine));
61

    
62
        public static readonly DependencyProperty OverViewPathDataProperty = DependencyProperty.Register(
63
                "OverViewPathData", typeof(Geometry), typeof(ArcControl), new PropertyMetadata(null));
64

    
65
        public static readonly DependencyProperty UserIDProperty = DependencyProperty.Register(
66
                "UserID", typeof(string), typeof(ArcControl), new PropertyMetadata(null));
67
        public static readonly DependencyProperty LineSizeProperty = DependencyProperty.Register(
68
              "LineSize", typeof(double), typeof(ArcControl), new PropertyMetadata((Double)3));
69

    
70
        public static readonly DependencyProperty DashSizeProperty = DependencyProperty.Register(
71
              "DashSize", typeof(DoubleCollection), typeof(ArcControl), new PropertyMetadata(new DoubleCollection { 1, 1 }));
72

    
73
        public static readonly DependencyProperty StrokeColorProperty = DependencyProperty.Register(
74
               "StrokeColor", typeof(SolidColorBrush), typeof(ArcControl), new PropertyMetadata(new SolidColorBrush(Colors.Red)));
75

    
76
        public static readonly DependencyProperty PathDataProperty = DependencyProperty.Register(
77
              "PathData", typeof(Geometry), typeof(ArcControl), null);
78

    
79
        //public static readonly DependencyProperty ClockProperty = DependencyProperty.Register(
80
        //       "Clock", typeof(SweepDirection), typeof(ArcControl), new PropertyMetadata(SweepDirection.Clockwise));
81
        public static readonly DependencyProperty ClockProperty = DependencyProperty.Register(
82
               "Clock", typeof(bool), typeof(ArcControl), new PropertyMetadata(false));
83

    
84
        public static readonly DependencyProperty StartPointProperty = DependencyProperty.Register(
85
               "StartPoint", typeof(Point), typeof(ArcControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
86
        public static readonly DependencyProperty MidPointProperty = DependencyProperty.Register(
87
               "MidPoint", typeof(Point), typeof(ArcControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
88

    
89
        public static readonly DependencyProperty EndPointProperty = DependencyProperty.Register(
90
               "EndPoint", typeof(Point), typeof(ArcControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
91
        
92
        public static readonly DependencyProperty PointSetProperty = DependencyProperty.Register(
93
                "PointSet", typeof(List<Point>), typeof(ArcControl), new PropertyMetadata(new List<Point>(), PointValueChanged));
94

    
95
        public static readonly DependencyProperty isTransOnProperty = DependencyProperty.Register(
96
                "isTransOn", typeof(bool), typeof(ArcControl), new PropertyMetadata(false));
97

    
98
        public static readonly DependencyProperty AngleProperty =
99
            DependencyProperty.Register("AngleValue", typeof(double), typeof(ArcControl), new PropertyMetadata((double)0.0, new PropertyChangedCallback
100
                (AngleValueChanged)));
101

    
102
        public static readonly DependencyProperty CenterXProperty =
103
            DependencyProperty.Register("CenterX", typeof(double), typeof(ArcControl), new PropertyMetadata((double)0, OnCenterXYChanged));
104

    
105
        public static readonly DependencyProperty CenterYProperty =
106
            DependencyProperty.Register("CenterY", typeof(double), typeof(ArcControl), new PropertyMetadata((double)0, OnCenterXYChanged));
107
        #endregion
108
        #region PropertyChanged Method
109
        
110
        public static void IsSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
111
        {
112
            var instance = (ArcControl)sender;
113

    
114
            if (e.OldValue != e.NewValue && instance.Base_ArcPath != null)
115
            {
116

    
117
                instance.SetValue(e.Property, e.NewValue);
118

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

    
130

    
131
        public static void PointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
132
        {
133
            var instance = (ArcControl)sender;
134
            if (e.OldValue != e.NewValue && instance.Base_ArcPath != null)
135
            {
136
                instance.SetValue(e.Property, e.NewValue);
137
                instance.SetArcPath();
138
            }
139
        }
140
        public static void AngleValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
141
        {
142
            var instance = (ArcControl)sender;
143
            if (e.OldValue != e.NewValue && instance.Base_ArcPath != null)
144
            {
145
                instance.SetValue(e.Property, e.NewValue);
146
                instance.SetArcPath();
147
            }
148
        }
149
        public static void OnCenterXYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
150
        {
151
            var instance = (ArcControl)sender;
152
            if (e.OldValue != e.NewValue && instance.Base_ArcPath != null)
153
            {
154
                instance.SetValue(e.Property, e.NewValue);
155
                instance.SetArcPath();
156
            }
157
        }
158

    
159
      
160

    
161
        #endregion
162
        #region Properties
163
        public string UserID
164
        {
165
            get { return (string)GetValue(UserIDProperty); }
166
            set
167
            {
168
                if (this.UserID != value)
169
                {
170
                    SetValue(UserIDProperty, value);
171
                    OnPropertyChanged("UserID");
172
                }
173
            }
174
        }
175
        public Double LineSize
176
        {
177
            get { return (Double)GetValue(LineSizeProperty); }
178
            set
179
            {
180
                if (this.LineSize != value)
181
                {
182
                    SetValue(LineSizeProperty, value);
183
                }
184
            }
185
        }
186

    
187

    
188
        public bool Clock
189
        {
190
            get { return (bool)GetValue(ClockProperty); }
191
            set
192
            {
193
                if (this.Clock != value)
194
                {
195
                    SetValue(ClockProperty, value);
196
                    OnPropertyChanged("Clock");
197
                }
198
            }
199
        }
200

    
201
        //public SweepDirection Clock
202
        //{
203
        //    get { return (SweepDirection)GetValue(ClockProperty); }
204
        //    set
205
        //    {
206
        //        if (this.Clock != value)
207
        //        {
208
        //            SetValue(ClockProperty, value);
209
        //            OnPropertyChanged("Clock");
210
        //        }
211
        //    }
212
        //}
213
        public DoubleCollection DashSize
214
        {
215
            get { return (DoubleCollection)GetValue(DashSizeProperty); }
216
            set
217
            {
218
                if (this.DashSize != value)
219
                {
220
                    SetValue(DashSizeProperty, value);
221
                }
222
            }
223
        }
224
        public List<Point> PointSet
225
        {
226
            get { return (List<Point>)GetValue(PointSetProperty); }
227
            set { SetValue(PointSetProperty, value); }
228
        }
229
        public double CenterX
230
        {
231
            get { return (double)GetValue(CenterXProperty); }
232
            set { SetValue(CenterXProperty, value); }
233
        }
234
        public double CenterY
235
        {
236
            get { return (double)GetValue(CenterYProperty); }
237
            set { SetValue(CenterYProperty, value); }
238
        }
239
        public SolidColorBrush StrokeColor
240
        {
241
            get { return (SolidColorBrush)GetValue(StrokeColorProperty); }
242
            set
243
            {
244
                if (this.StrokeColor != value)
245
                {
246
                    SetValue(StrokeColorProperty, value);
247
                }
248
            }
249
        }
250

    
251
        public Geometry OverViewPathData
252
        {
253
            get
254
            {
255
                return (Geometry)GetValue(OverViewPathDataProperty);
256
            }
257
            set
258
            {
259
                SetValue(OverViewPathDataProperty, value);
260
                OnPropertyChanged("OverViewPathData");
261
            }
262
        }
263

    
264
        public Geometry PathData
265
        {
266
            get { return (Geometry)GetValue(PathDataProperty); }
267
            set
268
            {
269
                SetValue(PathDataProperty, value);
270
                OnPropertyChanged("PathData");
271
            }
272
        }
273
        public bool IsSelected
274
        {
275
            get
276
            {
277
                return (bool)GetValue(IsSelectedProperty);
278
            }
279
            set
280
            {
281
                SetValue(IsSelectedProperty, value);
282
            }
283
        }
284

    
285
        public ControlType ControlType
286
        {
287
            get
288
            {
289
                return (ControlType)GetValue(ControlTypeProperty);
290
            }
291
            set
292
            {
293
                SetValue(ControlTypeProperty, value);
294
                OnPropertyChanged("ControlType");
295
            }
296
        }
297
        public double AngleValue
298
        {
299
            get { return (double)GetValue(AngleProperty); }
300
            set { SetValue(AngleProperty, value); }
301
        }
302
        public double Angle
303
        {
304
            get { return (double)GetValue(AngleProperty); }
305
            set
306
            {
307
                if (this.Angle != value)
308
                {
309
                    SetValue(AngleProperty, value);
310
                }
311
            }
312
        }
313
        public Point EndPoint
314
        {
315
            get { return (Point)GetValue(EndPointProperty); }
316
            set
317
            {
318
                SetValue(EndPointProperty, value);
319
                OnPropertyChanged("EndPoint");
320
            }
321
        }
322
        public Point StartPoint
323
        {
324
            get { return (Point)GetValue(StartPointProperty); }
325
            set
326
            {
327
                SetValue(StartPointProperty, value);
328
                OnPropertyChanged("StartPoint");
329
            }
330
        }
331
        public Point MidPoint
332
        {
333
            get { return (Point)GetValue(MidPointProperty); }
334
            set
335
            {
336
                SetValue(MidPointProperty, value);
337
                OnPropertyChanged("MidPoint");
338
            }
339
        }
340
        public bool isTransOn
341
        {
342
            get { return (bool)GetValue(isTransOnProperty); }
343
            set
344
            {
345
                SetValue(isTransOnProperty, value);
346
                OnPropertyChanged("isTransOn");
347
            }
348
        }
349

    
350
        #endregion
351
        #region Object & Variable
352
        PathGeometry pathGeometry = new PathGeometry();
353
        GeometryGroup instanceGroup = new GeometryGroup();
354
        #endregion
355
        public override void OnApplyTemplate()
356
        {
357
            base.OnApplyTemplate();
358
            Base_ArcPath = GetTemplateChild(PART_ArcPath) as Path;
359
        }
360
        public void updateControl()
361
        {
362
            this.StartPoint = new Point(this.PointSet[0].X, this.PointSet[0].Y);
363
            this.MidPoint = new Point(this.PointSet[1].X, this.PointSet[1].Y);
364
            this.EndPoint = new Point(this.PointSet[2].X, this.PointSet[2].Y);
365
        }
366

    
367
        public void setClock()
368
        {
369
            if (Clock)
370
            {
371
                this.Clock = false;
372
            }
373
            else
374
            {
375
                this.Clock = true;
376
            }
377
        }
378

    
379
        //public void setClock()
380
        //{
381
        //    if(this.Clock == SweepDirection.Clockwise)
382
        //    {
383
        //        this.Clock = SweepDirection.Counterclockwise;
384
        //    }
385
        //    else
386
        //    {
387
        //        this.Clock =SweepDirection.Clockwise;
388
        //    }
389
        //}
390
        public void SetArcPath()
391
        {
392
            this.ApplyTemplate();
393

    
394
            //보류
395
            //foreach (var item in this.DashSize)
396
            //{
397
            //    Base_ArcPath.StrokeDashArray.Add(item);
398
            //}
399
            instanceGroup.Children.Clear();
400

    
401

    
402
            PathFigure pathFigure = new PathFigure();
403

    
404
            pathFigure.StartPoint = this.StartPoint;
405
            QuadraticBezierSegment qb = new QuadraticBezierSegment();
406

    
407

    
408
            double xSharp = 0;
409
            double ySharp = 0;
410

    
411
            //Point t_start = new Point(0, 0);
412
            //Point t_end = new Point(30, 10);
413

    
414
            var midP = MathSet.getMiddlePoint(StartPoint, EndPoint);
415
            Point normalV = MathSet.GetNormVectorBetween(StartPoint, EndPoint);
416
            var dis = MathSet.DistanceTo(StartPoint, EndPoint);
417

    
418
            if (!this.Clock)
419
            {
420
                xSharp = -normalV.Y;
421
                ySharp = normalV.X;
422
            }
423
            else
424
            {
425
                xSharp = normalV.Y;
426
                ySharp = -normalV.X;
427
            }
428

    
429

    
430
            var pointN = new Point(xSharp * dis, ySharp * dis);
431
            MidPoint = new Point(pointN.X + midP.X, pointN.Y + midP.Y);
432

    
433

    
434
            if (MidPoint != null)
435
            {
436
                //System.Diagnostics.Debug.WriteLine(this.MidPoint.ToString());
437
                qb.Point1 = new Point(200, 200);
438
                qb.Point1 = this.MidPoint;
439
            }
440

    
441
            qb.Point2 = this.EndPoint;
442
            pathFigure.Segments.Add(qb);
443
            pathGeometry.Figures = new PathFigureCollection();
444
            pathGeometry.Figures.Add(pathFigure);
445
            pathFigure.IsFilled = false;
446
            pathFigure.IsClosed = false;
447
            instanceGroup.Children.Add(pathGeometry);
448

    
449
            //Arc Arrow
450
            //if(ControlType == ControlType.ArcArrow)
451
            if (isTransOn) 
452
            {
453
                isTransOn = true;
454
                instanceGroup.Children.Add(SingleAllow(this.MidPoint, this.StartPoint, this.LineSize));
455
                instanceGroup.Children.Add(ConverseAllow(this.EndPoint, this.MidPoint, this.LineSize));
456
                this.Base_ArcPath.Fill = this.StrokeColor;
457
            }
458

    
459
            instanceGroup.FillRule = FillRule.Nonzero;
460
            this.PathData = instanceGroup;
461
            this.OverViewPathData = PathData;
462

    
463
        }
464

    
465
        public static PathGeometry SingleAllow(Point p2, Point p1, double lineSize)
466
        {
467
            double theta = Math.Atan2((p2.Y - p1.Y), (p2.X - p1.X)) * 180 / Math.PI;
468
            PathGeometry pathGeometry = new PathGeometry();
469
            PathFigure pathFigure = new PathFigure();
470
            pathFigure.StartPoint = p1;
471

    
472
            Point lpoint = new Point(p1.X + lineSize * 2, p1.Y + lineSize * 4);
473
            Point rpoint = new Point(p1.X - lineSize * 2, p1.Y + lineSize * 4);
474

    
475
            LineSegment seg1 = new LineSegment();
476
            seg1.Point = lpoint;
477
            pathFigure.Segments.Add(seg1);
478

    
479
            LineSegment seg2 = new LineSegment();
480
            seg2.Point = rpoint;
481
            pathFigure.Segments.Add(seg2);
482

    
483
            LineSegment seg3 = new LineSegment();
484
            seg3.Point = p1;
485
            pathFigure.Segments.Add(seg3);
486

    
487
            pathFigure.IsClosed = true;
488
            pathFigure.IsFilled = true;
489

    
490
            pathGeometry.Figures.Add(pathFigure);
491
            pathGeometry.FillRule = FillRule.Nonzero;
492
            RotateTransform transform = new RotateTransform();
493
            transform.Angle = theta - 90;
494
            transform.CenterX = p1.X;
495
            transform.CenterY = p1.Y;
496
            pathGeometry.Transform = transform;
497
            return pathGeometry;
498
        }
499

    
500
        public static PathGeometry ConverseAllow(Point p2, Point p1, double lineSize)
501
        {
502
            double theta = Math.Atan2((p2.Y - p1.Y), (p2.X - p1.X)) * 180 / Math.PI;
503
            PathGeometry pathGeometry2 = new PathGeometry();
504
            PathFigure pathFigure2 = new PathFigure();
505
            pathFigure2.StartPoint = p2;
506

    
507
            Point lpoint2 = new Point(p2.X + lineSize * 2, p2.Y + lineSize * 4);
508
            Point rpoint2 = new Point(p2.X - lineSize * 2, p2.Y + lineSize * 4);
509
            LineSegment seg1_1 = new LineSegment();
510
            seg1_1.Point = lpoint2;
511
            pathFigure2.Segments.Add(seg1_1);
512

    
513
            LineSegment seg2_1 = new LineSegment();
514
            seg2_1.Point = rpoint2;
515
            pathFigure2.Segments.Add(seg2_1);
516

    
517
            LineSegment seg3_1 = new LineSegment();
518
            seg3_1.Point = p2;
519
            pathFigure2.Segments.Add(seg3_1);
520

    
521
            LineSegment seg4_1 = new LineSegment();
522

    
523
            seg4_1.Point = new Point(lpoint2.X, lpoint2.Y);
524
            pathFigure2.Segments.Add(seg4_1);
525

    
526
            //pathFigure2.IsFilled = true;
527
            //pathFigure2.IsClosed = true;
528

    
529
            RotateTransform transform2 = new RotateTransform();
530
            transform2.Angle = theta + 90;
531
            transform2.CenterX = p2.X;
532
            transform2.CenterY = p2.Y;
533
            pathGeometry2.Figures.Add(pathFigure2);
534
            pathGeometry2.Transform = transform2;
535
            return pathGeometry2;
536
        }
537

    
538
        public void ApplyOverViewData()
539
        {
540
            this.OverViewPathData = this.PathData;
541
        }
542
    }
543
}
클립보드 이미지 추가 (최대 크기: 500 MB)