프로젝트

일반

사용자정보

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

markus / MarkupToPDF / Controls / Etc / SignControl.cs @ 4a54bb3a

이력 | 보기 | 이력해설 | 다운로드 (22.9 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.Collections.Generic;
12
using System.ComponentModel;
13
using System.Linq;
14
using MarkupToPDF.Controls.Common;
15
using MarkupToPDF.Common;
16
using MarkupToPDF.Serialize.Core;
17
using MarkupToPDF.Serialize.S_Control;
18
using System.Windows.Media.Imaging;
19
using System.Threading.Tasks;
20
using static System.Windows.Forms.AxHost;
21

    
22
//강인구 추가
23
namespace MarkupToPDF.Controls.Etc
24
{
25
    [TemplatePart(Name = "PART_Image", Type = typeof(Image))]
26
    public class SignControl : CommentUserInfo, IDisposable, INormal, INotifyPropertyChanged, IViewBox
27
    {
28
        #region 초기선언
29
        public string ProjectNO { get; set; }
30

    
31
        private const string PART_Image = "PART_Image";
32
        public Image Base_Image = null;
33
        #endregion
34

    
35
        static SignControl()
36
        {
37
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SignControl), new FrameworkPropertyMetadata(typeof(SignControl)));
38
        }
39

    
40
        public override void Copy(CommentUserInfo lhs)
41
        {
42
            if(lhs is SignControl item)
43
            {
44
                this.CommentAngle = item.CommentAngle;
45
                this.StartPoint = new Point(item.StartPoint.X, item.StartPoint.Y);
46
                this.TopRightPoint = new Point(item.TopRightPoint.X, item.TopRightPoint.Y);
47
                this.EndPoint = new Point(item.EndPoint.X, item.EndPoint.Y);
48
                this.LeftBottomPoint = new Point(item.LeftBottomPoint.X, item.LeftBottomPoint.Y);
49
                this.PointSet = item.PointSet.ConvertAll(x => new Point(x.X, x.Y));
50
                this.Opacity = item.Opacity;
51
                this.SignImage = item.SignImage;
52
                this.UserID = item.UserID;
53
                this.UserNumber = item.UserNumber;
54
                this.Memo = item.Memo;
55
                this.ZIndex = item.ZIndex;
56
                this.GroupID = item.GroupID;
57
            }
58
        }
59

    
60
        public override CommentUserInfo Clone()
61
        {
62
            var clone = new SignControl();
63
            clone.Copy(this);
64
            return clone;
65
        }
66

    
67
        public void Dispose()
68
        {
69
            //GC.Collect();
70
            ////GC.SuppressFinalize(this);
71
            this.Base_Image = null;
72
        }
73
        #region Dependency Properties
74
        public static readonly DependencyProperty UserIDProperty = DependencyProperty.Register(
75
                "UserID", typeof(string), typeof(SignControl), new PropertyMetadata(null));
76
        public static readonly DependencyProperty SignImageProperty = DependencyProperty.Register(
77
            "SignImage", typeof(ImageSource), typeof(SignControl), new PropertyMetadata(null));
78
        public static readonly DependencyProperty StartPointProperty = DependencyProperty.Register(
79
            "StartPoint", typeof(Point), typeof(SignControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
80
        public static readonly DependencyProperty EndPointProperty = DependencyProperty.Register(
81
            "EndPoint", typeof(Point), typeof(SignControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
82
        public static readonly DependencyProperty PointSetProperty = DependencyProperty.Register(
83
            "PointSet", typeof(List<Point>), typeof(SignControl), new PropertyMetadata(new List<Point>()));
84
        public static readonly DependencyProperty UserNubmerProperty = DependencyProperty.Register(
85
            "UserNumber", typeof(string), typeof(SignControl), new PropertyMetadata(null));
86
        public static readonly DependencyProperty TopRightPointProperty = DependencyProperty.Register(
87
             "TopRightPoint", typeof(Point), typeof(SignControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
88
        public static readonly DependencyProperty LeftBottomPointProperty = DependencyProperty.Register(
89
              "LeftBottomPoint", typeof(Point), typeof(SignControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
90
        public static readonly DependencyProperty AngleProperty = DependencyProperty.Register("Angle", typeof(double), typeof(SignControl),
91
                        new PropertyMetadata((double)0.0, new PropertyChangedCallback(AngleValueChanged)));
92
        public static readonly DependencyProperty CenterXProperty = DependencyProperty.Register("CenterX", typeof(double), typeof(SignControl),
93
                        new PropertyMetadata((double)0, OnCenterXYChanged));
94
        public static readonly DependencyProperty CenterYProperty = DependencyProperty.Register("CenterY", typeof(double), typeof(SignControl),
95
                        new PropertyMetadata((double)0, OnCenterXYChanged));
96
        public static readonly DependencyProperty IsSelectedProperty =
97
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(SignControl), new FrameworkPropertyMetadata(false, IsSelectedChanged));
98

    
99
        public static readonly DependencyProperty ControlTypeProperty =
100
            DependencyProperty.Register("ControlType", typeof(ControlType), typeof(SignControl), new FrameworkPropertyMetadata(ControlType.Sign));
101
        #endregion
102
        #region PropertyChanged Method
103
        public static void PointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
104
        {
105
            var instance = (SignControl)sender;
106
            if (e.OldValue != e.NewValue && instance.Base_Image != null)
107
            {
108
                instance.SetValue(e.Property, e.NewValue);
109
                instance.SetImage();
110
            }
111
        }
112
        public static void IsSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
113
        {
114

    
115
        }
116
            public static void OnCenterXYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
117
        {
118
            var instance = (SignControl)sender;
119
            if (e.OldValue != e.NewValue && instance.Base_Image != null)
120
            {
121
                instance.SetValue(e.Property, e.NewValue);
122
                instance.SetImage();
123
            }
124
        }
125
        public static void AngleValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
126
        {
127
            var instance = (SignControl)sender;
128
            if (e.OldValue != e.NewValue && instance.Base_Image != null)
129
            {
130
                instance.SetValue(e.Property, e.NewValue);
131
                instance.SetImage();
132
            }
133
        }
134
        #endregion
135
        #region Properties
136
        public string UserID
137
        {
138
            get { return (string)GetValue(UserIDProperty); }
139
            set
140
            {
141
                if (this.UserID != value)
142
                {
143
                    SetValue(UserIDProperty, value);
144
                    RaisePropertyChanged("UserID");
145
                }
146
            }
147
        }
148

    
149
        public double CenterX
150
        {
151
            get { return (double)GetValue(CenterXProperty); }
152
            set { SetValue(CenterXProperty, value); }
153
        }
154
        public double CenterY
155
        {
156
            get { return (double)GetValue(CenterYProperty); }
157
            set { SetValue(CenterYProperty, value); }
158
        }
159
        public Point EndPoint
160
        {
161
            get { return (Point)GetValue(EndPointProperty); }
162
            set
163
            {
164
                if (this.EndPoint != value)
165
                {
166
                    SetValue(EndPointProperty, value);
167
                    RaisePropertyChanged("EndPoint");
168
                }
169
            }
170
        }
171
        public List<Point> PointSet
172
        {
173
            get { return (List<Point>)GetValue(PointSetProperty); }
174
            set { SetValue(PointSetProperty, value); }
175
        }
176
        public Point StartPoint
177
        {
178
            get { return (Point)GetValue(StartPointProperty); }
179
            set
180
            {
181
                if (this.StartPoint != value)
182
                {
183
                    SetValue(StartPointProperty, value);
184
                    RaisePropertyChanged("StartPoint");
185
                }
186
            }
187
        }
188
        public string UserNumber
189
        {
190
            get { return (string)GetValue(UserNubmerProperty); }
191
            set
192
            {
193
                if (this.UserNumber != value)
194
                {
195
                    SetValue(UserNubmerProperty, value);
196
                    RaisePropertyChanged("UserNumber");
197
                }
198
            }
199
        }
200
        public Point TopRightPoint
201
        {
202
            get { return (Point)GetValue(TopRightPointProperty); }
203
            set
204
            {
205
                SetValue(TopRightPointProperty, value);
206
                RaisePropertyChanged("TopRightPoint");
207
            }
208
        }
209
        public Point LeftBottomPoint
210
        {
211
            get { return (Point)GetValue(LeftBottomPointProperty); }
212
            set
213
            {
214
                SetValue(LeftBottomPointProperty, value);
215
                RaisePropertyChanged("LeftBottomPoint");
216
            }
217
        }
218
        public override double CommentAngle
219
        {
220
            get { return (double)GetValue(AngleProperty); }
221
            set
222
            {
223
                if (this.CommentAngle != value)
224
                {
225
                    SetValue(AngleProperty, value);
226
                }
227
            }
228
        }
229
        public ImageSource SignImage
230
        {
231
            get { return (ImageSource)this.GetValue(SignImageProperty); }
232
            set { this.SetValue(SignImageProperty, value); }
233
        }
234

    
235
        public override bool IsSelected
236
        {
237
            get
238
            {
239
                return (bool)GetValue(IsSelectedProperty);
240
            }
241
            set
242
            {
243
                SetValue(IsSelectedProperty, value);
244
            }
245
        }
246

    
247
        public override ControlType ControlType
248
        {
249
            set
250
            {
251
                SetValue(ControlTypeProperty, value);
252
            }
253
            get
254
            {
255
                return (ControlType)GetValue(ControlTypeProperty);
256
            }
257
        }
258

    
259
        #endregion
260
        public override void OnApplyTemplate()
261
        {
262
            base.OnApplyTemplate();
263
            Base_Image = GetTemplateChild(PART_Image) as Image;
264
            SetImage();
265
        }
266

    
267
        private void SetImage()
268
        {
269
            this.ApplyTemplate();
270

    
271
            Point mid = MathSet.FindCentroid(new List<Point>()
272
            {
273
                this.StartPoint,
274
                this.LeftBottomPoint,
275
                this.EndPoint,
276
                this.TopRightPoint,                
277
            });
278

    
279
            double AngleData = this.CommentAngle * -1;
280

    
281
            PathFigure pathFigure = new PathFigure();
282
            pathFigure.StartPoint = MathSet.RotateAbout(mid, this.StartPoint, AngleData);
283

    
284
            LineSegment lineSegment0 = new LineSegment();
285
            lineSegment0.Point = MathSet.RotateAbout(mid, this.StartPoint, AngleData);
286
            pathFigure.Segments.Add(lineSegment0);
287

    
288
            LineSegment lineSegment1 = new LineSegment();
289
            lineSegment1.Point = MathSet.RotateAbout(mid, this.LeftBottomPoint, AngleData);
290
            pathFigure.Segments.Add(lineSegment1);
291

    
292
            LineSegment lineSegment2 = new LineSegment();
293
            lineSegment2.Point = MathSet.RotateAbout(mid, this.EndPoint, AngleData);
294
            pathFigure.Segments.Add(lineSegment2);
295

    
296
            LineSegment lineSegment3 = new LineSegment();
297
            lineSegment3.Point = MathSet.RotateAbout(mid, this.TopRightPoint, AngleData);
298
            pathFigure.Segments.Add(lineSegment3);
299

    
300
            PathGeometry pathGeometry = new PathGeometry();
301
            pathGeometry.Figures = new PathFigureCollection();
302
            pathFigure.IsClosed = true;
303
            pathGeometry.Figures.Add(pathFigure);
304

    
305
            this.Base_Image.Width = Math.Abs(pathGeometry.Bounds.Width);
306
            this.Base_Image.Height = Math.Abs(pathGeometry.Bounds.Height);
307

    
308
            this.Tag = pathGeometry;
309

    
310
            Canvas.SetLeft(this, MathSet.RotateAbout(mid, mid, AngleData).X - this.Base_Image.Width / 2);
311
            Canvas.SetTop(this, MathSet.RotateAbout(mid, mid, AngleData).Y - this.Base_Image.Height / 2);
312
        }
313
        public event PropertyChangedEventHandler PropertyChanged;
314
        protected void RaisePropertyChanged(string propName)
315
        {
316
            if (PropertyChanged != null)
317
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
318
        }
319

    
320
        public override void UpdateControl()
321
        {
322
            this.StartPoint = new Point(this.PointSet[0].X, this.PointSet[0].Y);
323
            this.TopRightPoint = new Point(this.PointSet[3].X, this.PointSet[3].Y);
324
            this.LeftBottomPoint = new Point(this.PointSet[1].X, this.PointSet[1].Y);
325
            this.EndPoint = new Point(this.PointSet[2].X, this.PointSet[2].Y);
326
        }
327

    
328
        /// <summary>
329
        /// call when mouse is moving while drawing control
330
        /// </summary>
331
        /// <author>humkyung</author>
332
        /// <param name="pt"></param>
333
        /// <param name="bAxisLocked"></param>
334
        public override  void OnCreatingMouseMove(Point pt, bool bAxisLocked)
335
        {
336
            if (bAxisLocked)
337
            {
338
                double _dx = pt.X - this.StartPoint.X;
339
                double _dy = pt.Y - this.StartPoint.Y;
340
                double dist = Math.Max(Math.Abs(_dx) , Math.Abs(_dy));
341
                var dir = new Vector(_dx, _dy);
342
                dir.Normalize();
343

    
344
                this.LeftBottomPoint = new Point(this.StartPoint.X, this.StartPoint.Y + (dir.Y > 0 ? 1 : -1) * dist);
345
                this.TopRightPoint = new Point(this.StartPoint.X + (dir.X > 0 ? 1 : -1) * dist, this.StartPoint.Y);
346
                this.EndPoint = new Point(this.TopRightPoint.X, this.LeftBottomPoint.Y);
347
            }
348
            else
349
            {
350
                this.EndPoint = pt;
351
                this.LeftBottomPoint = new Point(this.StartPoint.X, this.EndPoint.Y);
352
                this.TopRightPoint = new Point(this.EndPoint.X, this.StartPoint.Y);
353
            }
354

    
355
            var _sign = Application.Current.FindResource("UserSign");
356

    
357
            if (this.StartPoint != this.EndPoint && this.SignImage == null && _sign != null)
358
            {
359
                byte[] imageBytes = System.Convert.FromBase64String(_sign.ToString());
360

    
361
                using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
362
                {
363
                    stream.Write(imageBytes, 0, imageBytes.Length);
364
                    stream.Position = 0;
365

    
366
                    BitmapImage returnImage = new BitmapImage();
367
                    returnImage.BeginInit();
368
                    using (System.Drawing.Image img = System.Drawing.Image.FromStream(stream))
369
                    {
370
                        System.IO.MemoryStream ms = new System.IO.MemoryStream();
371
                        img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
372
                        ms.Seek(0, System.IO.SeekOrigin.Begin);
373
                        returnImage.StreamSource = ms;
374
                        returnImage.EndInit();
375
                    }
376
                    stream.Close();
377

    
378
                    this.SignImage = returnImage;
379
                }
380
            }
381

    
382
            this.PointSet = new List<Point>
383
            {
384
                this.StartPoint,
385
                this.LeftBottomPoint,
386
                this.EndPoint,
387
                this.TopRightPoint,
388
            };
389
        }
390

    
391
        /// <summary>
392
        /// move control point has same location of given pt along given delta
393
        /// </summary>
394
        /// <author>humkyung</author>
395
        /// <date>2019.06.20</date>
396
        /// <param name="pt"></param>
397
        /// <param name="dx"></param>
398
        /// <param name="dy"></param>
399
        public override void OnMoveCtrlPoint(Point pt, double dx, double dy, bool bAxisLocked = false)
400
        {
401
            IPath path = (this as IPath);
402

    
403
            Point selected = MathSet.getNearPoint(path.PointSet, pt);
404
            selected.X += dx;
405
            selected.Y += dy;
406

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

    
409
            var OppositeP = (idx + path.PointSet.Count / 2) % path.PointSet.Count;
410
            var PreviousP = (idx + (path.PointSet.Count - 1)) % path.PointSet.Count;
411
            var NextP = (idx + 1) % path.PointSet.Count;
412

    
413
            if (bAxisLocked)
414
            {
415
                var PrevV = path.PointSet[PreviousP] - path.PointSet[OppositeP];
416
                double PrevLength = PrevV.Length;
417
                PrevV.Normalize();
418

    
419
                var NextV = path.PointSet[NextP] - path.PointSet[OppositeP];
420
                double NextVLength = NextV.Length;
421
                NextV.Normalize();
422

    
423
                double _dx = selected.X - path.PointSet[OppositeP].X;
424
                double _dy = selected.Y - path.PointSet[OppositeP].Y;
425
                var dir = new Vector(_dx, _dy);
426
                if (PrevLength > NextVLength)
427
                {
428
                    double ratio = NextVLength / PrevLength;
429

    
430
                    double dot = MathSet.DotProduct(PrevV.X, PrevV.Y, dir.X, dir.Y);
431

    
432
                    path.PointSet[PreviousP] = path.PointSet[OppositeP] + PrevV * dot;
433
                    path.PointSet[NextP] = path.PointSet[OppositeP] + NextV * dot * ratio;
434
                    path.PointSet[idx] = path.PointSet[OppositeP] + PrevV * dot + NextV * dot * ratio;
435
                }
436
                else
437
                {
438
                    double ratio = PrevLength / NextVLength;
439

    
440
                    double dot = MathSet.DotProduct(NextV.X, NextV.Y, dir.X, dir.Y);
441

    
442
                    path.PointSet[PreviousP] = path.PointSet[OppositeP] + PrevV * dot * ratio;
443
                    path.PointSet[NextP] = path.PointSet[OppositeP] + NextV * dot;
444
                    path.PointSet[idx] = path.PointSet[OppositeP] + PrevV * dot * ratio + NextV * dot;
445
                }
446
            }
447
            else
448
            {
449
                var PreviousV = MathSet.GetNormVectorBetween(path.PointSet[OppositeP], path.PointSet[PreviousP]);
450
                var l = MathSet.DotProduct(PreviousV.X, PreviousV.Y, path.PointSet[idx].X - path.PointSet[OppositeP].X,
451
                    path.PointSet[idx].Y - path.PointSet[OppositeP].Y);
452
                path.PointSet[PreviousP] = new Point(path.PointSet[OppositeP].X + PreviousV.X * l, path.PointSet[OppositeP].Y + PreviousV.Y * l);
453

    
454
                var NextV = MathSet.GetNormVectorBetween(path.PointSet[OppositeP], path.PointSet[NextP]);
455
                l = MathSet.DotProduct(NextV.X, NextV.Y, path.PointSet[idx].X - path.PointSet[OppositeP].X, path.PointSet
456
                    [idx].Y - path.PointSet[OppositeP].Y);
457
                path.PointSet[NextP] = new Point(path.PointSet[OppositeP].X + NextV.X * l, path.PointSet[OppositeP].Y + NextV.Y * l);
458

    
459
                path.PointSet[idx] = selected;
460
            }
461

    
462
            this.UpdateControl();
463
        }
464

    
465
        /// <summary>
466
        /// return SignControl's area
467
        /// </summary>
468
        /// <author>humkyung</author>
469
        /// <date>2019.06.13</date>
470
        public override Rect ItemRect
471
        {
472
            get
473
            {
474
                double dMinX = Math.Min(this.StartPoint.X, this.EndPoint.X);
475
                double dMinY = Math.Min(this.StartPoint.Y, this.EndPoint.Y);
476
                double dMaxX = Math.Max(this.StartPoint.X, this.EndPoint.X);
477
                double dMaxY = Math.Max(this.StartPoint.Y, this.EndPoint.Y);
478

    
479
                return new Rect(new Point(dMinX, dMinY), new Point(dMaxX, dMaxY));
480
            }
481
        }
482

    
483
        /// <summary>
484
        /// Serialize this
485
        /// </summary>
486
        /// <param name="sUserId"></param>
487
        /// <returns></returns>
488
        public override string Serialize()
489
        {
490
            using (S_SignControl ctrl = new S_SignControl())
491
            {
492
                ctrl.Angle = this.CommentAngle;
493
                ctrl.EndPoint = this.EndPoint;
494
                ctrl.UserID = this.UserID;
495
                ctrl.LB = this.LeftBottomPoint;
496
                ctrl.Name = this.GetType().Name;
497
                ctrl.PointSet = this.PointSet;
498
                ctrl.StartPoint = this.StartPoint;
499
                ctrl.Opac = this.Opacity;
500
                ctrl.TR = this.TopRightPoint;
501
                ctrl.UserNumber = this.UserNumber == null ? this.UserID: this.UserNumber;
502

    
503
                ///강인구 추가(2017.11.02)
504
                ///Memo 추가
505
                ctrl.Memo = this.Memo;
506

    
507
                ctrl.ZIndex = this.ZIndex;
508
                ctrl.GroupID = this.GroupID;
509

    
510
                return "|DZ|" + JsonSerializerHelper.CompressString((ctrl.JsonSerialize()));
511
            }
512
        }
513

    
514
        /// <summary>
515
        /// create a signcontrol from given string
516
        /// </summary>
517
        /// <param name="str"></param>
518
        /// <returns></returns>
519
        public static SignControl FromString(string str, SolidColorBrush brush, string sProjectNo)
520
        {
521
            SignControl instance = null;
522
            using (S_SignControl s = JsonSerializerHelper.JsonDeserialize<S_SignControl>(str))
523
            {
524
                instance = new SignControl
525
                {
526
                    CommentAngle = s.Angle,
527
                    StartPoint = s.StartPoint,
528
                    TopRightPoint = s.TR,
529
                    EndPoint = s.EndPoint,
530
                    LeftBottomPoint = s.LB,
531
                    PointSet = s.PointSet,
532
                    Opacity = s.Opac,
533
                    SignImage = null,
534
                    UserID = s.UserID,
535
                    UserNumber = s.UserNumber,
536
                    Memo = s.Memo,
537
                    ZIndex = s.ZIndex,
538
                    GroupID = s.GroupID
539
                };
540

    
541
                if (s.UserNumber != null)
542
                { 
543
                    var _signItem = MarkupContext.GetUserSignItem(s.UserNumber);
544

    
545
                    if (_signItem != null)
546
                    {
547
                        byte[] imageBytes = System.Convert.FromBase64String(_signItem.Data);
548

    
549
                        BitmapImage returnImage = new BitmapImage();
550

    
551
                        using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
552
                        {
553
                            stream.WriteAsync(imageBytes, 0, imageBytes.Length);
554

    
555
                            stream.Position = 0;
556
                            System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
557
                            returnImage.BeginInit();
558
                            System.IO.MemoryStream ms = new System.IO.MemoryStream();
559
                            img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
560
                            ms.Seek(0, System.IO.SeekOrigin.Begin);
561
                            returnImage.StreamSource = ms;
562
                            returnImage.EndInit();
563
                            stream.Close();
564
                        }
565

    
566
                        instance.SignImage = returnImage;
567
                    }
568
                }
569
            }
570

    
571
            return instance;
572
        }
573

    
574
        public double LineSize
575
        {
576
            get;
577
            set;
578
        }
579
        public Geometry PathData
580
        {
581
            get;
582
            set;
583
        }
584
    }
585
}
클립보드 이미지 추가 (최대 크기: 500 MB)