프로젝트

일반

사용자정보

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

markus / MarkupToPDF / Controls / Etc / SymControl.cs @ a6f7f9b6

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

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

    
19
namespace MarkupToPDF.Controls.Etc
20
{
21
    [TemplatePart(Name = "PART_SymPath", Type = typeof(Path))]
22
    [TemplatePart(Name = "PART_ViewBox", Type = typeof(Viewbox))]
23

    
24
    public class SymControl : CommentUserInfo, IDisposable, INotifyPropertyChanged, IPath, IViewBox, IMarkupCommonData
25
    {
26
        #region 초기선언
27
        public enum SymStyleSet { None, Fill };
28
        private const string PART_ViewBox = "PART_ViewBox";
29
        private const string PART_SymPath = "PART_SymPath";
30
        public Viewbox Base_ViewBox = null;
31
        public Path Base_SymPath = null;
32
        #endregion
33

    
34
        static SymControl()
35
        {
36
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SymControl), new FrameworkPropertyMetadata(typeof(SymControl)));
37
            //Application.Current.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("/MarkupToPDF;Component/Themes/generic.xaml")) as ResourceDictionary);
38
            //ResourceDictionary dictionary = new ResourceDictionary();
39
            //dictionary.Source = new Uri("/MarkupToPDF;component/themes/generic.xaml", UriKind.RelativeOrAbsolute);
40
            //Application.Current.Resources.MergedDictionaries.Add(dictionary);
41
            //System.Diagnostics.Debug.WriteLine("resource Count :" + Application.Current.Resources.MergedDictionaries.Count);
42
        }
43

    
44
        public void Dispose()
45
        {
46
            //GC.Collect();
47
            //GC.SuppressFinalize(this);
48
            this.Base_SymPath = null;
49
            this.Base_ViewBox = null;
50
        }
51
        public event PropertyChangedEventHandler PropertyChanged;
52
        protected void RaisePropertyChanged(string propName)
53
        {
54
            if (PropertyChanged != null)
55
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
56
        }
57
        #region Dependency Properties
58
        public static readonly DependencyProperty UserIDProperty = DependencyProperty.Register(
59
                "UserID", typeof(string), typeof(SymControl), new PropertyMetadata(null));
60
        public static readonly DependencyProperty LineSizeProperty = DependencyProperty.Register(
61
                "LineSize", typeof(double), typeof(SymControl), new PropertyMetadata((Double)1));
62
        public static readonly DependencyProperty StrokeColorProperty = DependencyProperty.Register(
63
                "StrokeColor", typeof(SolidColorBrush), typeof(SymControl), new PropertyMetadata(new SolidColorBrush(Colors.Red)));
64
        public static readonly DependencyProperty PathDataProperty = DependencyProperty.Register(
65
                "PathData", typeof(Geometry), typeof(SymControl), null);
66
        public static readonly DependencyProperty EndPointProperty = DependencyProperty.Register(
67
                "EndPoint", typeof(Point), typeof(SymControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
68
        public static readonly DependencyProperty StartPointProperty = DependencyProperty.Register(
69
                "StartPoint", typeof(Point), typeof(SymControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
70
        public static readonly DependencyProperty TopRightPointProperty = DependencyProperty.Register(
71
                "TopRightPoint", typeof(Point), typeof(SymControl), new PropertyMetadata(new Point(0, 0), TRPointValueChanged));
72
        public static readonly DependencyProperty LeftBottomPointProperty = DependencyProperty.Register(
73
                 "LeftBottomPoint", typeof(Point), typeof(SymControl), new PropertyMetadata(new Point(0, 0), LBPointValueChanged));
74
        public static readonly DependencyProperty PointSetProperty = DependencyProperty.Register(
75
                 "PointSet", typeof(List<Point>), typeof(SymControl), new PropertyMetadata(new List<Point>()));
76
        public static readonly DependencyProperty AngleProperty = DependencyProperty.Register("Angle", typeof(double), typeof(SymControl),
77
            new PropertyMetadata((double)0.0, new PropertyChangedCallback(AngleValueChanged)));
78
        public static readonly DependencyProperty CenterXProperty = DependencyProperty.Register("CenterX", typeof(double), typeof(SymControl),
79
            new PropertyMetadata((double)0, OnCenterXYChanged));
80
        public static readonly DependencyProperty CenterYProperty = DependencyProperty.Register("CenterY", typeof(double), typeof(SymControl),
81
            new PropertyMetadata((double)0, OnCenterXYChanged));
82
        public static readonly DependencyProperty PaintProperty = DependencyProperty.Register(
83
                "Paint", typeof(PaintSet), typeof(SymControl), new PropertyMetadata(PaintSet.None));
84

    
85
        public static readonly DependencyProperty IsSelectedProperty =
86
     DependencyProperty.Register("IsSelected", typeof(bool), typeof(SymControl), new FrameworkPropertyMetadata(false, IsSelectedChanged));
87

    
88
        public static readonly DependencyProperty ControlTypeProperty =
89
            DependencyProperty.Register("ControlType", typeof(ControlType), typeof(SymControl), new FrameworkPropertyMetadata(ControlType.Symbol));
90
        #endregion
91
        #region PropertyChanged Method
92
        public static void PointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
93
        {
94
            var instance = (SymControl)sender;
95
            if (e.OldValue != e.NewValue && instance.Base_SymPath != null)
96
            {
97
                instance.SetValue(e.Property, e.NewValue);
98
                instance.SetSymPath();
99
            }
100
        }
101

    
102
        public static void TRPointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
103
        {
104
            var instance = (SymControl)sender;
105
            if (e.OldValue != e.NewValue && instance.Base_SymPath != null)
106
            {
107
                instance.SetValue(e.Property, e.NewValue);
108
                instance.SetSymPath();
109
            }
110
        }
111
        public static void IsSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
112
        {
113

    
114
        }
115
        public static void OnCenterXYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
116
        {
117
            var instance = (SymControl)sender;
118
            if (e.OldValue != e.NewValue && instance.Base_SymPath != null)
119
            {
120
                instance.SetValue(e.Property, e.NewValue);
121
                instance.SetSymPath();
122
            }
123
        }
124

    
125
        public static void LBPointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
126
        {
127
            var instance = (SymControl)sender;
128
            if (e.OldValue != e.NewValue && instance.Base_SymPath != null)
129
            {
130
                instance.SetValue(e.Property, e.NewValue);
131
                //instance.EndPoint = new Point(instance.EndPoint.X, ((Point)e.NewValue).Y);
132
                //instance.StartPoint = new Point(((Point)e.NewValue).X, instance.StartPoint.Y);
133
                ////instance.PointSet[0] = instance.StartPoint;
134
                ////instance.PointSet[2] = instance.EndPoint;
135
                instance.SetSymPath();
136
            }
137
        }
138

    
139
        public static void AngleValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
140
        {
141
            var instance = (SymControl)sender;
142
            if (e.OldValue != e.NewValue && instance.Base_SymPath != null)
143
            {
144
                instance.SetValue(e.Property, e.NewValue);
145
                instance.SetSymPath();
146
            }
147
        }
148
        #endregion
149
        #region Properties
150
        public string UserID
151
        {
152
            get { return (string)GetValue(UserIDProperty); }
153
            set
154
            {
155
                if (this.UserID != value)
156
                {
157
                    SetValue(UserIDProperty, value);
158
                    RaisePropertyChanged("UserID");
159
                }
160
            }
161
        }
162
        public Double LineSize
163
        {
164
            get { return (Double)GetValue(LineSizeProperty); }
165
            set
166
            {
167
                if (this.LineSize != value)
168
                {
169
                    SetValue(LineSizeProperty, value);
170
                }
171
            }
172
        }
173
        public PaintSet Paint
174
        {
175
            get { return (PaintSet)GetValue(PaintProperty); }
176
            set
177
            {
178
                if (this.Paint != value)
179
                {
180
                    SetValue(PaintProperty, value);
181
                }
182
            }
183
        }
184
        public List<Point> PointSet
185
        {
186
            get { return (List<Point>)GetValue(PointSetProperty); }
187
            set { SetValue(PointSetProperty, value); }
188
        }
189

    
190
        public override double Angle
191
        {
192
            get { return (double)GetValue(AngleProperty); }
193
            set
194
            {
195
                if (this.Angle != value)
196
                {
197
                    SetValue(AngleProperty, value);
198
                }
199
            }
200
        }
201
        public SolidColorBrush StrokeColor
202
        {
203
            get { return (SolidColorBrush)GetValue(StrokeColorProperty); }
204
            set
205
            {
206
                if (this.StrokeColor != value)
207
                {
208
                    SetValue(StrokeColorProperty, value);
209
                }
210
            }
211
        }
212
        public Geometry PathData
213
        {
214
            get { return (Geometry)GetValue(PathDataProperty); }
215
            set
216
            {
217
                SetValue(PathDataProperty, value);
218
                RaisePropertyChanged("PathData");
219
            }
220
        }
221
        public Point TopRightPoint
222
        {
223
            get { return (Point)GetValue(TopRightPointProperty); }
224
            set
225
            {
226
                SetValue(TopRightPointProperty, value);
227
                RaisePropertyChanged("TopRightPoint");
228
            }
229
        }
230
        public Point LeftBottomPoint
231
        {
232
            get { return (Point)GetValue(LeftBottomPointProperty); }
233
            set
234
            {
235
                SetValue(LeftBottomPointProperty, value);
236
                RaisePropertyChanged("LeftBottomPoint");
237
            }
238
        }
239
        public Point EndPoint
240
        {
241
            get { return (Point)GetValue(EndPointProperty); }
242
            set
243
            {
244
                SetValue(EndPointProperty, value);
245
                RaisePropertyChanged("EndPoint");
246
            }
247
        }
248
        public Point StartPoint
249
        {
250
            get { return (Point)GetValue(StartPointProperty); }
251
            set
252
            {
253
                SetValue(StartPointProperty, value);
254
                RaisePropertyChanged("StartPoint");
255
            }
256
        }
257
        public double CenterX
258
        {
259
            get { return (double)GetValue(CenterXProperty); }
260
            set { SetValue(CenterXProperty, value); }
261
        }
262
        public double CenterY
263
        {
264
            get { return (double)GetValue(CenterYProperty); }
265
            set { SetValue(CenterYProperty, value); }
266
        }
267
        public double AngleValue
268
        {
269
            get { return (double)GetValue(AngleProperty); }
270
            set { SetValue(AngleProperty, value); }
271
        }
272
        public override bool IsSelected
273
        {
274
            get
275
            {
276
                return (bool)GetValue(IsSelectedProperty);
277
            }
278
            set
279
            {
280
                SetValue(IsSelectedProperty, value);
281
            }
282
        }
283

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

    
296
        #endregion
297
        public override void OnApplyTemplate()
298
        {
299
            base.OnApplyTemplate();
300
            Base_ViewBox = GetTemplateChild(PART_ViewBox) as Viewbox;
301
            Base_SymPath = GetTemplateChild(PART_SymPath) as Path;
302
            SetSymPath();
303
        }
304

    
305
        private void SetSymPath()
306
        {
307
            this.ApplyTemplate();
308
            switch (this.Paint)
309
            {
310
                case PaintSet.None:
311
                    Base_SymPath.Fill = null;
312
                    break;
313
                case PaintSet.Fill:
314
                    Base_SymPath.Fill = this.StrokeColor;
315
                    break;
316
                default:
317
                    break;
318
            }
319

    
320
            Point mid = MathSet.FindCentroid(new List<Point>()
321
            {
322
                this.StartPoint,
323
                this.LeftBottomPoint,
324
                this.EndPoint,
325
                this.TopRightPoint,
326
            });
327

    
328
            double AngleData = this.Angle * -1;
329

    
330
            PathFigure pathFigure = new PathFigure();
331
            pathFigure.StartPoint = MathSet.RotateAbout(mid, this.StartPoint, AngleData);
332

    
333
            LineSegment lineSegment0 = new LineSegment();
334
            lineSegment0.Point = MathSet.RotateAbout(mid, this.StartPoint, AngleData);
335
            pathFigure.Segments.Add(lineSegment0);
336

    
337
            LineSegment lineSegment1 = new LineSegment();
338
            lineSegment1.Point = MathSet.RotateAbout(mid, this.LeftBottomPoint, AngleData);
339
            pathFigure.Segments.Add(lineSegment1);
340

    
341
            LineSegment lineSegment2 = new LineSegment();
342
            lineSegment2.Point = MathSet.RotateAbout(mid, this.EndPoint, AngleData);
343
            pathFigure.Segments.Add(lineSegment2);
344

    
345
            LineSegment lineSegment3 = new LineSegment();
346
            lineSegment3.Point = MathSet.RotateAbout(mid, this.TopRightPoint, AngleData);
347
            pathFigure.Segments.Add(lineSegment3);
348

    
349
            PathGeometry pathGeometry = new PathGeometry();
350
            pathGeometry.Figures = new PathFigureCollection();
351
            pathFigure.IsClosed = true;
352
            pathGeometry.Figures.Add(pathFigure);
353
            this.Base_ViewBox.Width = pathGeometry.Bounds.Width;
354
            this.Base_ViewBox.Height = pathGeometry.Bounds.Height; ;
355
            this.Tag = pathGeometry;
356

    
357
            Canvas.SetLeft(this, MathSet.RotateAbout(mid, mid, AngleData).X - this.Base_ViewBox.Width / 2);
358
            Canvas.SetTop(this, MathSet.RotateAbout(mid, mid, AngleData).Y - this.Base_ViewBox.Height / 2);
359

    
360
        }
361

    
362
        public override void UpdateControl()
363
        {
364
            this.StartPoint = new Point(this.PointSet[0].X, this.PointSet[0].Y);
365
            this.LeftBottomPoint = new Point(this.PointSet[1].X, this.PointSet[1].Y);
366
            this.EndPoint = new Point(this.PointSet[2].X, this.PointSet[2].Y);
367
            this.TopRightPoint = new Point(this.PointSet[3].X, this.PointSet[3].Y);
368
            this.SetSymPath();
369
        }
370

    
371

    
372
        public void ChangePaint(PaintSet state)
373
        {
374
            this.Paint = state;
375
            this.SetSymPath();
376
        }
377

    
378
        /// <summary>
379
        /// call when mouse is moving while drawing control
380
        /// </summary>
381
        /// <author>humkyung</author>
382
        /// <param name="pt"></param>
383
        /// <param name="bAxisLocked"></param>
384
        public override void OnCreatingMouseMove(Point pt, bool bAxisLocked, bool bShiftKeyPressed)
385
        {
386
            this.EndPoint = pt;
387
            this.LeftBottomPoint = new Point(this.StartPoint.X, this.EndPoint.Y);
388
            this.TopRightPoint = new Point(this.EndPoint.X, this.StartPoint.Y);
389
            
390
            this.StrokeColor = new SolidColorBrush(Colors.Red);
391

    
392
            if (this.StartPoint != this.EndPoint)
393
            {
394
                if (this.PathData == null)
395
                {
396
                    using (StringToPathConverter Convert = new StringToPathConverter())
397
                    {
398
                        this.PathData = Convert.Convert("M-5,5L0,0L20,30L40,-20 ");
399
                    }
400
                }
401
            }
402

    
403
            this.PointSet = new List<Point>
404
            {
405
                this.StartPoint,
406
                this.LeftBottomPoint,
407
                this.EndPoint,
408
                this.TopRightPoint,
409
            };
410
        }
411

    
412
        /// <summary>
413
        /// move control point has same location of given pt along given delta
414
        /// </summary>
415
        /// <author>humkyung</author>
416
        /// <date>2019.06.20</date>
417
        /// <param name="pt"></param>
418
        /// <param name="dx"></param>
419
        /// <param name="dy"></param>
420
        public override void OnMoveCtrlPoint(Point pt, double dx, double dy)
421
        {
422
            IPath path = (this as IPath);
423

    
424
            Point selected = MathSet.getNearPoint(path.PointSet, pt);
425
            selected.X += dx;
426
            selected.Y += dy;
427
            int i = 0;
428
            for (i = 0; i < (this as IPath).PointSet.Count; i++)
429
            {
430
                if (pt.Equals((this as IPath).PointSet[i]))
431
                {
432
                    path.PointSet[i] = selected;
433
                    break;
434
                }
435
            }
436

    
437
            var ReverseP = (i + path.PointSet.Count / 2) % path.PointSet.Count;
438
            var PreviousP = (i + (path.PointSet.Count - 1)) % path.PointSet.Count;
439
            var NextP = (i + 1) % path.PointSet.Count;
440

    
441
            var distance = MathSet.DistanceTo(path.PointSet[ReverseP], path.PointSet[i]);
442

    
443
            var PreviousV = MathSet.GetNormVectorBetween(path.PointSet[ReverseP], path.PointSet[PreviousP]);
444
            var l = MathSet.DotProduct(PreviousV.X, PreviousV.Y, path.PointSet[i].X - path.PointSet[ReverseP].X,
445
                path.PointSet[i].Y - path.PointSet[ReverseP].Y);
446
            path.PointSet[PreviousP] = new Point(path.PointSet[ReverseP].X + PreviousV.X * l, path.PointSet[ReverseP].Y + PreviousV.Y * l);
447

    
448
            var NextV = MathSet.GetNormVectorBetween(path.PointSet[ReverseP], path.PointSet[NextP]);
449
            l = MathSet.DotProduct(NextV.X, NextV.Y, path.PointSet[i].X - path.PointSet[ReverseP].X, path.PointSet
450
                [i].Y - path.PointSet[ReverseP].Y);
451
            path.PointSet[NextP] = new Point(path.PointSet[ReverseP].X + NextV.X * l, path.PointSet[ReverseP].Y + NextV.Y * l);
452

    
453
            this.UpdateControl();
454
        }
455

    
456
        /// <summary>
457
        /// return SymControl's area
458
        /// </summary>
459
        /// <author>humkyung</author>
460
        /// <date>2019.06.13</date>
461
        public override Rect ItemRect
462
        {
463
            get
464
            {
465
                double dMinX = Math.Min(this.StartPoint.X, this.EndPoint.X);
466
                double dMinY = Math.Min(this.StartPoint.Y, this.EndPoint.Y);
467
                double dMaxX = Math.Max(this.StartPoint.X, this.EndPoint.X);
468
                double dMaxY = Math.Max(this.StartPoint.Y, this.EndPoint.Y);
469

    
470
                return new Rect(new Point(dMinX, dMinY), new Point(dMaxX, dMaxY));
471
            }
472
        }
473

    
474
        /// <summary>
475
        /// Serialize this
476
        /// </summary>
477
        /// <param name="sUserId"></param>
478
        /// <returns></returns>
479
        public override string Serialize()
480
        {
481
            using (S_SymControl STemp = new S_SymControl())
482
            {
483
                STemp.TransformPoint = "0|0";
484
                STemp.PointSet = this.PointSet;
485
                STemp.UserID = this.UserID;
486
                STemp.SizeSet = String.Format("{0}", this.LineSize.ToString());
487
                STemp.PaintState = this.Paint;
488
                STemp.PathInfo = this.PathData.ToString();
489
                STemp.StrokeColor = this.StrokeColor.Color.ToString();
490
                STemp.StartPoint = this.StartPoint;
491
                STemp.Angle = this.Angle;
492
                STemp.EndPoint = this.EndPoint;
493
                STemp.LB = this.LeftBottomPoint;
494
                STemp.TR = this.TopRightPoint;
495
                STemp.Opac = this.Opacity;
496
                STemp.Name = this.GetType().Name.ToString();
497

    
498
                return "|DZ|" + JsonSerializerHelper.CompressString((STemp.JsonSerialize()));
499
            }
500
        }
501

    
502
        /// <summary>
503
        /// create a symcontrol from given string
504
        /// </summary>
505
        /// <param name="str"></param>
506
        /// <returns></returns>
507
        public static SymControl FromString(string str, SolidColorBrush brush, string sProjectNo)
508
        {
509
            SymControl instance = null;
510
            using (S_SymControl s = JsonSerializerHelper.JsonDeserialize<S_SymControl>(str))
511
            {
512
                string[] data2 = s.SizeSet.Split(delimiterChars2, StringSplitOptions.RemoveEmptyEntries);
513
                Common.StringToPathConverter sm = new Common.StringToPathConverter();
514
                instance = new SymControl
515
                {
516
                    LineSize = Convert.ToDouble(data2.First()),
517
                    PointSet = s.PointSet,
518
                    Paint = s.PaintState,
519
                    StartPoint = s.StartPoint,
520
                    StrokeColor = brush,
521
                    EndPoint = s.EndPoint,
522
                    Angle = s.Angle,
523
                    UserID = s.UserID,
524
                    LeftBottomPoint = s.LB,
525
                    TopRightPoint = s.TR,
526
                    PathData = sm.Convert(s.PathInfo.ToString()),
527
                    Opacity = s.Opac,
528
                    Memo = s.Memo
529
                };
530
            }
531

    
532
            return instance;
533
        }
534
    }
535
}
클립보드 이미지 추가 (최대 크기: 500 MB)