프로젝트

일반

사용자정보

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

markus / MarkupToPDF / Controls / Etc / SymControlN.cs @ 49a6d7c3

이력 | 보기 | 이력해설 | 다운로드 (22.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
using MarkupToPDF.Common;
15
using MarkupToPDF.Serialize.Core;
16
using MarkupToPDF.Serialize.S_Control;
17

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

    
22

    
23
    public class SymControlN : CommentUserInfo, INotifyPropertyChanged , IViewBox, IMarkupCommonData
24
    {
25
        private const string PART_ViewBox = "PART_ViewBox";
26
        public Viewbox Base_ViewBox = null;
27
        public string STAMP { get; set; }
28
        public Dictionary<string,string> STAMP_Contents { get; set; }
29

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

    
40
        public override void Copy(CommentUserInfo lhs)
41
        {
42
            if (lhs is SymControlN symControlN)
43
            {
44
                this.PointSet = symControlN.PointSet.ConvertAll(x => new Point(x.X, x.Y));
45
                this.StartPoint = new Point(symControlN.StartPoint.X, symControlN.StartPoint.Y);
46
                this.TopRightPoint = new Point(symControlN.TopRightPoint.X, symControlN.TopRightPoint.Y);
47
                this.EndPoint = new Point(symControlN.EndPoint.X, symControlN.EndPoint.Y);
48
                this.LeftBottomPoint = new Point(symControlN.LeftBottomPoint.X, symControlN.LeftBottomPoint.Y);
49
                this.PointSet = symControlN.PointSet.ConvertAll(x => new Point(x.X, x.Y));
50
                this.CommentAngle = symControlN.CommentAngle;                                
51
                this.Opacity = symControlN.Opacity;
52
                this.PathXathData = symControlN.PathXathData;
53
                this.Memo = symControlN.Memo;
54
            }
55
        }
56

    
57
        public override CommentUserInfo Clone()
58
        {
59
            var clone = new SymControlN();
60
            clone.Copy(this);
61
            return clone;
62
        }
63

    
64
        public event PropertyChangedEventHandler PropertyChanged;
65
        protected void RaisePropertyChanged(string propName)
66
        {
67
            if (PropertyChanged != null)
68
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
69
        }
70
        #region 내장 프로퍼티
71
        public static readonly DependencyProperty UserIDProperty = DependencyProperty.Register(
72
                "UserID", typeof(string), typeof(SymControlN), new PropertyMetadata(null));
73
         public static readonly DependencyProperty PathXathDataProperty = DependencyProperty.Register(
74
                "PathXathData", typeof(string), typeof(SymControlN), new PropertyMetadata(null));
75
        public static readonly DependencyProperty LineSizeProperty = DependencyProperty.Register(
76
                "LineSize", typeof(double), typeof(SymControlN), new PropertyMetadata((Double)1));
77
         public static readonly DependencyProperty EndPointProperty = DependencyProperty.Register(
78
                "EndPoint", typeof(Point), typeof(SymControlN), new PropertyMetadata(new Point(0, 0), PointValueChanged));
79
        public static readonly DependencyProperty StartPointProperty = DependencyProperty.Register(
80
                "StartPoint", typeof(Point), typeof(SymControlN), new PropertyMetadata(new Point(0, 0), PointValueChanged));
81
        public static readonly DependencyProperty TopRightPointProperty = DependencyProperty.Register(
82
                "TopRightPoint", typeof(Point), typeof(SymControlN), new PropertyMetadata(new Point(0, 0), PointValueChanged));
83
        public static readonly DependencyProperty StrokeColorProperty = DependencyProperty.Register(
84
                 "StrokeColor", typeof(SolidColorBrush), typeof(SymControlN), new PropertyMetadata(new SolidColorBrush(Colors.Red)));        
85
        public static readonly DependencyProperty LeftBottomPointProperty = DependencyProperty.Register(
86
                 "LeftBottomPoint", typeof(Point), typeof(SymControlN), new PropertyMetadata(new Point(0, 0), PointValueChanged));
87
        public static readonly DependencyProperty PointSetProperty = DependencyProperty.Register(
88
                 "PointSet", typeof(List<Point>), typeof(SymControlN), new PropertyMetadata(new List<Point>()));
89
        public static readonly DependencyProperty PathDataProperty = DependencyProperty.Register(
90
                 "PathData", typeof(Geometry), typeof(SymControlN), null);
91
        public static readonly DependencyProperty AngleProperty = DependencyProperty.Register("Angle", typeof(double), typeof(SymControlN),
92
          new PropertyMetadata((double)0.0, new PropertyChangedCallback(AngleValueChanged)));
93
        public static readonly DependencyProperty CenterXProperty = DependencyProperty.Register("CenterX", typeof(double), typeof(SymControlN),
94
            new PropertyMetadata((double)0, OnCenterXYChanged));
95
        public static readonly DependencyProperty CenterYProperty = DependencyProperty.Register("CenterY", typeof(double), typeof(SymControlN),
96
            new PropertyMetadata((double)0, OnCenterXYChanged));
97

    
98
        public static readonly DependencyProperty IsSelectedProperty =
99
     DependencyProperty.Register("IsSelected", typeof(bool), typeof(SymControlN), new FrameworkPropertyMetadata(false, IsSelectedChanged));
100

    
101
        public static readonly DependencyProperty ControlTypeProperty =
102
            DependencyProperty.Register("ControlType", typeof(ControlType), typeof(SymControlN), new FrameworkPropertyMetadata(ControlType.Stamp));
103
        #endregion
104

    
105
        #region PropertyChanged Method
106

    
107
        public static void IsSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
108
        {
109

    
110
        }
111

    
112
        public static void PointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
113
        {
114
            var instance = (SymControlN)sender;
115
            if (e.OldValue != e.NewValue && instance.Base_ViewBox != null)
116
            {
117
                instance.SetValue(e.Property, e.NewValue);
118
                instance.SetViewBox();
119
            }
120
        }
121
        public static void OnCenterXYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
122
        {
123
            var instance = (SymControlN)sender;
124
            if (e.OldValue != e.NewValue && instance.Base_ViewBox != null)
125
            {
126
                instance.SetValue(e.Property, e.NewValue);
127
                instance.SetViewBox();
128
            }
129
        }
130
        public static void AngleValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
131
        {
132
            var instance = (SymControlN)sender;
133
            if (e.OldValue != e.NewValue && instance.Base_ViewBox != null)
134
            {
135
                instance.SetValue(e.Property, e.NewValue);
136
                instance.SetViewBox();
137
            }
138
        }
139
        #endregion
140

    
141
        #region Properties
142
        public string UserID
143
        {
144
            get { return (string)GetValue(UserIDProperty); }
145
            set
146
            {
147
                if (this.UserID != value)
148
                {
149
                    SetValue(UserIDProperty, value);
150
                    RaisePropertyChanged("UserID");
151
                }
152
            }
153
        }
154
        public SolidColorBrush StrokeColor
155
        {
156
            get { return (SolidColorBrush)GetValue(StrokeColorProperty); }
157
            set
158
            {
159
                if (this.StrokeColor != value)
160
                {
161
                    SetValue(StrokeColorProperty, value);
162
                }
163
            }
164
        }
165

    
166
        public string PathXathData
167
        {
168
            get
169
            {
170
                return (string)GetValue(PathXathDataProperty);
171
            }
172
            set
173
            {
174
                SetValue(PathXathDataProperty, value);
175
                RaisePropertyChanged("PathXathData");
176
            }
177
            
178
        }
179
        public List<Point> PointSet
180
        {
181
            get { return (List<Point>)GetValue(PointSetProperty); }
182
            set { SetValue(PointSetProperty, value); }
183
        }
184
        public Point TopRightPoint
185
        {
186
            get { return (Point)GetValue(TopRightPointProperty); }
187
            set
188
            {
189
                SetValue(TopRightPointProperty, value);
190
                RaisePropertyChanged("TopRightPoint");
191
            }
192
        }
193
        public Point LeftBottomPoint
194
        {
195
            get { return (Point)GetValue(LeftBottomPointProperty); }
196
            set
197
            {
198
                SetValue(LeftBottomPointProperty, value);
199
                RaisePropertyChanged("LeftBottomPoint");
200
            }
201
        }
202
        public Point EndPoint
203
        {
204
            get { return (Point)GetValue(EndPointProperty); }
205
            set
206
            {
207
                SetValue(EndPointProperty, value);
208
                RaisePropertyChanged("EndPoint");
209
            }
210
        }
211
        public Point StartPoint
212
        {
213
            get { return (Point)GetValue(StartPointProperty); }
214
            set
215
            {
216
                SetValue(StartPointProperty, value);
217
                RaisePropertyChanged("StartPoint");
218
            }
219
        }
220
        #endregion
221

    
222
        public override void OnApplyTemplate()
223
        {
224
            base.OnApplyTemplate();
225
            Base_ViewBox = GetTemplateChild(PART_ViewBox) as Viewbox;
226
            SetViewBox();
227
        }
228

    
229
        public void SetViewBox()
230
        {
231
            this.ApplyTemplate();
232
            Point mid = MathSet.FindCentroid(new List<Point>()
233
            {
234
                this.StartPoint,
235
                this.LeftBottomPoint,
236
                this.EndPoint,
237
                this.TopRightPoint,                
238
            });
239
            double AngleData = this.CommentAngle * -1;
240

    
241
            PathFigure pathFigure = new PathFigure();
242
            pathFigure.StartPoint = MathSet.RotateAbout(mid, this.StartPoint, AngleData);
243

    
244
            LineSegment lineSegment0 = new LineSegment();
245
            lineSegment0.Point = MathSet.RotateAbout(mid, this.StartPoint, AngleData);
246
            pathFigure.Segments.Add(lineSegment0);
247

    
248
            LineSegment lineSegment1 = new LineSegment();
249
            lineSegment1.Point = MathSet.RotateAbout(mid, this.LeftBottomPoint, AngleData);
250
            pathFigure.Segments.Add(lineSegment1);
251

    
252
            LineSegment lineSegment2 = new LineSegment();
253
            lineSegment2.Point = MathSet.RotateAbout(mid, this.EndPoint, AngleData);
254
            pathFigure.Segments.Add(lineSegment2);
255

    
256
            LineSegment lineSegment3 = new LineSegment();
257
            lineSegment3.Point = MathSet.RotateAbout(mid, this.TopRightPoint, AngleData);
258
            pathFigure.Segments.Add(lineSegment3);
259

    
260
            PathGeometry pathGeometry = new PathGeometry();
261
            pathGeometry.Figures = new PathFigureCollection();
262
            pathFigure.IsClosed = true;
263
            pathGeometry.Figures.Add(pathFigure);
264
            this.Base_ViewBox.Width = pathGeometry.Bounds.Width;
265
            this.Base_ViewBox.Height = pathGeometry.Bounds.Height;
266
            this.Tag = pathGeometry;
267

    
268
            if(Base_ViewBox.Child == null)
269
            {
270
                SetChild();
271
            }
272

    
273
            Canvas.SetLeft(this, MathSet.RotateAbout(mid, mid, AngleData).X - this.Base_ViewBox.Width / 2);
274
            Canvas.SetTop(this, MathSet.RotateAbout(mid, mid, AngleData).Y - this.Base_ViewBox.Height / 2);
275
        }
276

    
277
        public void SetChild()
278
        {            
279
            if(this.PathXathData != null)
280
            {
281
                var xamlData = MarkupToPDF.Serialize.Core.JsonSerializerHelper.UnCompressStamp(this.PathXathData);
282
                System.IO.MemoryStream stream = new System.IO.MemoryStream();
283
                System.IO.StreamWriter writer = new System.IO.StreamWriter(stream);
284
                writer.Write(xamlData);
285
                writer.Flush();
286
                stream.Position = 0;
287

    
288
                object obj = System.Windows.Markup.XamlReader.Load(stream);
289
                UIElement ob = obj as UIElement;
290
                                
291
                Base_ViewBox.Child = ob;
292
            }            
293
        }
294

    
295
        public override bool IsSelected
296
        {
297
            get
298
            {
299
                return (bool)GetValue(IsSelectedProperty);
300
            }
301
            set
302
            {
303
                SetValue(IsSelectedProperty, value);
304
            }
305
        }
306

    
307
        public override ControlType ControlType
308
        {
309
            set
310
            {
311
                SetValue(ControlTypeProperty, value);
312
            }
313
            get
314
            {
315
                return (ControlType)GetValue(ControlTypeProperty);
316
            }
317
        }
318

    
319
        public override double CommentAngle
320
        {
321
            get { return (double)GetValue(AngleProperty); }
322
            set
323
            {
324
                if (this.CommentAngle != value)
325
                {
326
                    SetValue(AngleProperty, value);
327
                }
328
            }
329
        }
330

    
331

    
332
        public Double LineSize
333
        {
334
            get { return (Double)GetValue(LineSizeProperty); }
335
            set
336
            {
337
                if (this.LineSize != value)
338
                {
339
                    SetValue(LineSizeProperty, value);
340
                }
341
            }
342
        }
343
        public Geometry PathData
344
        {
345
            get { return (Geometry)GetValue(PathDataProperty); }
346
            set
347
            {
348
                SetValue(PathDataProperty, value);
349
                RaisePropertyChanged("PathData");
350
            }
351
        }
352

    
353
        public override void UpdateControl()
354
        {
355
            this.StartPoint = new Point(this.PointSet[0].X, this.PointSet[0].Y);
356
            this.LeftBottomPoint = new Point(this.PointSet[1].X, this.PointSet[1].Y);            
357
            this.TopRightPoint = new Point(this.PointSet[3].X, this.PointSet[3].Y);
358
            this.EndPoint = new Point(this.PointSet[2].X, this.PointSet[2].Y);
359
            this.SetViewBox();
360
        }
361

    
362
        /// <summary>
363
        /// call when mouse is moving while drawing control
364
        /// </summary>
365
        /// <author>humkyung</author>
366
        /// <param name="pt"></param>
367
        /// <param name="bAxisLocked"></param>
368
        public override void OnCreatingMouseMove(Point pt, bool bAxisLocked)
369
        {
370
            if (this.StartPoint == this.EndPoint)
371
            {
372
                var xamlData = MarkupToPDF.Serialize.Core.JsonSerializerHelper.UnCompressStamp(this.STAMP);
373

    
374
                if (STAMP_Contents?.Count > 0)
375
                {
376
                    foreach (var item in STAMP_Contents)
377
                    {
378
                        xamlData = xamlData.Replace(item.Key, System.Security.SecurityElement.Escape(item.Value));
379
                    }
380
                }
381

    
382
                System.IO.MemoryStream stream = new System.IO.MemoryStream();
383
                System.IO.StreamWriter writer = new System.IO.StreamWriter(stream);
384
                writer.Write(xamlData);
385
                writer.Flush();
386
                stream.Position = 0;
387

    
388
                this.StrokeColor = new SolidColorBrush(Colors.Red);
389
                object obj = System.Windows.Markup.XamlReader.Load(stream);
390
                UIElement ob = obj as UIElement;
391

    
392
                this.SetViewBox();
393
                this.PathXathData = this.STAMP;
394
                this.Base_ViewBox.Child = ob;
395
            }
396

    
397
            if (bAxisLocked)
398
            {
399
                double _dx = pt.X - this.StartPoint.X;
400
                double _dy = pt.Y - this.StartPoint.Y;
401
                double dist = Math.Max(Math.Abs(_dx), Math.Abs(_dy));
402
                var dir = new Vector(_dx, _dy);
403
                dir.Normalize();
404

    
405
                this.LeftBottomPoint = new Point(this.StartPoint.X, this.StartPoint.Y + (dir.Y > 0 ? 1 : -1) * dist);
406
                this.TopRightPoint = new Point(this.StartPoint.X + (dir.X > 0 ? 1 : -1) * dist, this.StartPoint.Y);
407
                this.EndPoint = new Point(this.TopRightPoint.X, this.LeftBottomPoint.Y);
408
            }
409
            else
410
            {
411
                this.EndPoint = pt;
412
                this.LeftBottomPoint = new Point(this.StartPoint.X, this.EndPoint.Y);
413
                this.TopRightPoint = new Point(this.EndPoint.X, this.StartPoint.Y);
414
            }
415

    
416
            this.PointSet = new List<Point>
417
            {
418
                this.StartPoint,
419
                this.LeftBottomPoint,
420
                this.EndPoint,
421
                this.TopRightPoint,
422
            };
423
        }
424

    
425
        /// <summary>
426
        /// move control point has same location of given pt along given delta
427
        /// </summary>
428
        /// <author>humkyung</author>
429
        /// <date>2019.06.20</date>
430
        /// <param name="pt"></param>
431
        /// <param name="dx"></param>
432
        /// <param name="dy"></param>
433
        public override void OnMoveCtrlPoint(Point pt, double dx, double dy, bool bAxisLocked = false)
434
        {
435
            IPath path = (this as IPath);
436

    
437
            Point selected = MathSet.getNearPoint(path.PointSet, pt);
438
            selected.X += dx;
439
            selected.Y += dy;
440

    
441
            int idx = (this as IPath).PointSet.FindIndex(x => x.Equals(pt));
442

    
443
            var OppositeP = (idx + path.PointSet.Count / 2) % path.PointSet.Count;
444
            var PreviousP = (idx + (path.PointSet.Count - 1)) % path.PointSet.Count;
445
            var NextP = (idx + 1) % path.PointSet.Count;
446
            if (bAxisLocked)
447
            {
448
                var PrevV = path.PointSet[PreviousP] - path.PointSet[OppositeP];
449
                double PrevLength = PrevV.Length;
450
                PrevV.Normalize();
451

    
452
                var NextV = path.PointSet[NextP] - path.PointSet[OppositeP];
453
                double NextVLength = NextV.Length;
454
                NextV.Normalize();
455

    
456
                double _dx = selected.X - path.PointSet[OppositeP].X;
457
                double _dy = selected.Y - path.PointSet[OppositeP].Y;
458
                var dir = new Vector(_dx, _dy);
459
                if (PrevLength > NextVLength)
460
                {
461
                    double ratio = NextVLength / PrevLength;
462

    
463
                    double dot = MathSet.DotProduct(PrevV.X, PrevV.Y, dir.X, dir.Y);
464

    
465
                    path.PointSet[PreviousP] = path.PointSet[OppositeP] + PrevV * dot;
466
                    path.PointSet[NextP] = path.PointSet[OppositeP] + NextV * dot * ratio;
467
                    path.PointSet[idx] = path.PointSet[OppositeP] + PrevV * dot + NextV * dot * ratio;
468
                }
469
                else
470
                {
471
                    double ratio = PrevLength / NextVLength;
472

    
473
                    double dot = MathSet.DotProduct(NextV.X, NextV.Y, dir.X, dir.Y);
474

    
475
                    path.PointSet[PreviousP] = path.PointSet[OppositeP] + PrevV * dot * ratio;
476
                    path.PointSet[NextP] = path.PointSet[OppositeP] + NextV * dot;
477
                    path.PointSet[idx] = path.PointSet[OppositeP] + PrevV * dot * ratio + NextV * dot;
478
                }
479
            }
480
            else
481
            {
482
                var PreviousV = MathSet.GetNormVectorBetween(path.PointSet[OppositeP], path.PointSet[PreviousP]);
483
                var l = MathSet.DotProduct(PreviousV.X, PreviousV.Y, path.PointSet[idx].X - path.PointSet[OppositeP].X,
484
                    path.PointSet[idx].Y - path.PointSet[OppositeP].Y);
485
                path.PointSet[PreviousP] = new Point(path.PointSet[OppositeP].X + PreviousV.X * l, path.PointSet[OppositeP].Y + PreviousV.Y * l);
486

    
487
                var NextV = MathSet.GetNormVectorBetween(path.PointSet[OppositeP], path.PointSet[NextP]);
488
                l = MathSet.DotProduct(NextV.X, NextV.Y, path.PointSet[idx].X - path.PointSet[OppositeP].X, path.PointSet
489
                    [idx].Y - path.PointSet[OppositeP].Y);
490
                path.PointSet[NextP] = new Point(path.PointSet[OppositeP].X + NextV.X * l, path.PointSet[OppositeP].Y + NextV.Y * l);
491

    
492
                path.PointSet[idx] = selected;
493
            }
494

    
495
            this.UpdateControl();
496
        }
497

    
498
        /// <summary>
499
        /// return SymControlN's area
500
        /// </summary>
501
        /// <author>humkyung</author>
502
        /// <date>2019.06.13</date>
503
        public override Rect ItemRect
504
        {
505
            get
506
            {
507
                double dMinX = Math.Min(this.StartPoint.X, this.EndPoint.X);
508
                double dMinY = Math.Min(this.StartPoint.Y, this.EndPoint.Y);
509
                double dMaxX = Math.Max(this.StartPoint.X, this.EndPoint.X);
510
                double dMaxY = Math.Max(this.StartPoint.Y, this.EndPoint.Y);
511

    
512
                return new Rect(new Point(dMinX, dMinY), new Point(dMaxX, dMaxY));
513
            }
514
        }
515

    
516
        /// <summary>
517
        /// Serialize this
518
        /// </summary>
519
        /// <param name="sUserId"></param>
520
        /// <returns></returns>
521
        public override string Serialize()
522
        {
523
            using (S_SymControlN STemp = new S_SymControlN())
524
            {
525
                STemp.TransformPoint = "0|0";
526
                STemp.PointSet = this.PointSet;
527
                //STemp.XamlData = this.PathXathData;
528
                STemp.UserID = this.UserID;
529
                STemp.DBData = this.PathXathData;
530
                //STemp.StrokeColor = this.StrokeColor.Color.ToString();
531
                STemp.StartPoint = this.StartPoint;
532
                STemp.Angle = this.CommentAngle;
533
                STemp.EndPoint = this.EndPoint;
534
                STemp.LB = this.LeftBottomPoint;
535
                STemp.TR = this.TopRightPoint;
536
                STemp.Opac = this.Opacity;
537
                STemp.Name = this.GetType().Name.ToString();
538

    
539
                return "|DZ|" + JsonSerializerHelper.CompressString((STemp.JsonSerialize()));
540
            }
541
        }
542

    
543
        /// <summary>
544
        /// create a symcontroln from given string
545
        /// </summary>
546
        /// <param name="str"></param>
547
        /// <returns></returns>
548
        public static SymControlN FromString(string str, SolidColorBrush brush, string sProjectNo)
549
        {
550
            using (S_SymControlN s = JsonSerializerHelper.JsonDeserialize<S_SymControlN>(str))
551
            {
552
                return new SymControlN()
553
                {
554
                    PointSet = s.PointSet,
555
                    StartPoint = s.StartPoint,
556
                    EndPoint = s.EndPoint,
557
                    CommentAngle = s.Angle,
558
                    LeftBottomPoint = s.LB,
559
                    TopRightPoint = s.TR,
560
                    Opacity = s.Opac,
561
                    PathXathData = s.DBData,
562
                    Memo = s.Memo
563
                };
564
            }
565
        }
566
    }
567
}
568

    
569

    
클립보드 이미지 추가 (최대 크기: 500 MB)