markus / MarkupToPDF / Controls / Etc / SymControlN.cs @ ab7fe8c0
이력 | 보기 | 이력해설 | 다운로드 (20.3 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 event PropertyChangedEventHandler PropertyChanged; |
41 |
protected void RaisePropertyChanged(string propName) |
42 |
{ |
43 |
if (PropertyChanged != null) |
44 |
PropertyChanged(this, new PropertyChangedEventArgs(propName)); |
45 |
} |
46 |
#region 내장 프로퍼티 |
47 |
public static readonly DependencyProperty UserIDProperty = DependencyProperty.Register( |
48 |
"UserID", typeof(string), typeof(SymControlN), new PropertyMetadata(null)); |
49 |
public static readonly DependencyProperty PathXathDataProperty = DependencyProperty.Register( |
50 |
"PathXathData", typeof(string), typeof(SymControlN), new PropertyMetadata(null)); |
51 |
public static readonly DependencyProperty LineSizeProperty = DependencyProperty.Register( |
52 |
"LineSize", typeof(double), typeof(SymControlN), new PropertyMetadata((Double)1)); |
53 |
public static readonly DependencyProperty EndPointProperty = DependencyProperty.Register( |
54 |
"EndPoint", typeof(Point), typeof(SymControlN), new PropertyMetadata(new Point(0, 0), PointValueChanged)); |
55 |
public static readonly DependencyProperty StartPointProperty = DependencyProperty.Register( |
56 |
"StartPoint", typeof(Point), typeof(SymControlN), new PropertyMetadata(new Point(0, 0), PointValueChanged)); |
57 |
public static readonly DependencyProperty TopRightPointProperty = DependencyProperty.Register( |
58 |
"TopRightPoint", typeof(Point), typeof(SymControlN), new PropertyMetadata(new Point(0, 0), PointValueChanged)); |
59 |
public static readonly DependencyProperty StrokeColorProperty = DependencyProperty.Register( |
60 |
"StrokeColor", typeof(SolidColorBrush), typeof(SymControlN), new PropertyMetadata(new SolidColorBrush(Colors.Red))); |
61 |
public static readonly DependencyProperty LeftBottomPointProperty = DependencyProperty.Register( |
62 |
"LeftBottomPoint", typeof(Point), typeof(SymControlN), new PropertyMetadata(new Point(0, 0), PointValueChanged)); |
63 |
public static readonly DependencyProperty PointSetProperty = DependencyProperty.Register( |
64 |
"PointSet", typeof(List<Point>), typeof(SymControlN), new PropertyMetadata(new List<Point>())); |
65 |
public static readonly DependencyProperty PathDataProperty = DependencyProperty.Register( |
66 |
"PathData", typeof(Geometry), typeof(SymControlN), null); |
67 |
public static readonly DependencyProperty AngleProperty = DependencyProperty.Register("Angle", typeof(double), typeof(SymControlN), |
68 |
new PropertyMetadata((double)0.0, new PropertyChangedCallback(AngleValueChanged))); |
69 |
public static readonly DependencyProperty CenterXProperty = DependencyProperty.Register("CenterX", typeof(double), typeof(SymControlN), |
70 |
new PropertyMetadata((double)0, OnCenterXYChanged)); |
71 |
public static readonly DependencyProperty CenterYProperty = DependencyProperty.Register("CenterY", typeof(double), typeof(SymControlN), |
72 |
new PropertyMetadata((double)0, OnCenterXYChanged)); |
73 |
|
74 |
public static readonly DependencyProperty IsSelectedProperty = |
75 |
DependencyProperty.Register("IsSelected", typeof(bool), typeof(SymControlN), new FrameworkPropertyMetadata(false, IsSelectedChanged)); |
76 |
|
77 |
public static readonly DependencyProperty ControlTypeProperty = |
78 |
DependencyProperty.Register("ControlType", typeof(ControlType), typeof(SymControlN), new FrameworkPropertyMetadata(ControlType.Stamp)); |
79 |
#endregion |
80 |
|
81 |
#region PropertyChanged Method |
82 |
|
83 |
public static void IsSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) |
84 |
{ |
85 |
|
86 |
} |
87 |
|
88 |
public static void PointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) |
89 |
{ |
90 |
var instance = (SymControlN)sender; |
91 |
if (e.OldValue != e.NewValue && instance.Base_ViewBox != null) |
92 |
{ |
93 |
instance.SetValue(e.Property, e.NewValue); |
94 |
instance.SetViewBox(); |
95 |
} |
96 |
} |
97 |
public static void OnCenterXYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) |
98 |
{ |
99 |
var instance = (SymControlN)sender; |
100 |
if (e.OldValue != e.NewValue && instance.Base_ViewBox != null) |
101 |
{ |
102 |
instance.SetValue(e.Property, e.NewValue); |
103 |
instance.SetViewBox(); |
104 |
} |
105 |
} |
106 |
public static void AngleValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) |
107 |
{ |
108 |
var instance = (SymControlN)sender; |
109 |
if (e.OldValue != e.NewValue && instance.Base_ViewBox != null) |
110 |
{ |
111 |
instance.SetValue(e.Property, e.NewValue); |
112 |
instance.SetViewBox(); |
113 |
} |
114 |
} |
115 |
#endregion |
116 |
|
117 |
#region Properties |
118 |
public string UserID |
119 |
{ |
120 |
get { return (string)GetValue(UserIDProperty); } |
121 |
set |
122 |
{ |
123 |
if (this.UserID != value) |
124 |
{ |
125 |
SetValue(UserIDProperty, value); |
126 |
RaisePropertyChanged("UserID"); |
127 |
} |
128 |
} |
129 |
} |
130 |
public SolidColorBrush StrokeColor |
131 |
{ |
132 |
get { return (SolidColorBrush)GetValue(StrokeColorProperty); } |
133 |
set |
134 |
{ |
135 |
if (this.StrokeColor != value) |
136 |
{ |
137 |
SetValue(StrokeColorProperty, value); |
138 |
} |
139 |
} |
140 |
} |
141 |
|
142 |
public string PathXathData |
143 |
{ |
144 |
get |
145 |
{ |
146 |
return (string)GetValue(PathXathDataProperty); |
147 |
} |
148 |
set |
149 |
{ |
150 |
SetValue(PathXathDataProperty, value); |
151 |
RaisePropertyChanged("PathXathData"); |
152 |
} |
153 |
|
154 |
} |
155 |
public List<Point> PointSet |
156 |
{ |
157 |
get { return (List<Point>)GetValue(PointSetProperty); } |
158 |
set { SetValue(PointSetProperty, value); } |
159 |
} |
160 |
public Point TopRightPoint |
161 |
{ |
162 |
get { return (Point)GetValue(TopRightPointProperty); } |
163 |
set |
164 |
{ |
165 |
SetValue(TopRightPointProperty, value); |
166 |
RaisePropertyChanged("TopRightPoint"); |
167 |
} |
168 |
} |
169 |
public Point LeftBottomPoint |
170 |
{ |
171 |
get { return (Point)GetValue(LeftBottomPointProperty); } |
172 |
set |
173 |
{ |
174 |
SetValue(LeftBottomPointProperty, value); |
175 |
RaisePropertyChanged("LeftBottomPoint"); |
176 |
} |
177 |
} |
178 |
public Point EndPoint |
179 |
{ |
180 |
get { return (Point)GetValue(EndPointProperty); } |
181 |
set |
182 |
{ |
183 |
SetValue(EndPointProperty, value); |
184 |
RaisePropertyChanged("EndPoint"); |
185 |
} |
186 |
} |
187 |
public Point StartPoint |
188 |
{ |
189 |
get { return (Point)GetValue(StartPointProperty); } |
190 |
set |
191 |
{ |
192 |
SetValue(StartPointProperty, value); |
193 |
RaisePropertyChanged("StartPoint"); |
194 |
} |
195 |
} |
196 |
#endregion |
197 |
|
198 |
public override void OnApplyTemplate() |
199 |
{ |
200 |
base.OnApplyTemplate(); |
201 |
Base_ViewBox = GetTemplateChild(PART_ViewBox) as Viewbox; |
202 |
SetViewBox(); |
203 |
} |
204 |
|
205 |
public void SetViewBox() |
206 |
{ |
207 |
this.ApplyTemplate(); |
208 |
Point mid = MathSet.FindCentroid(new List<Point>() |
209 |
{ |
210 |
this.StartPoint, |
211 |
this.LeftBottomPoint, |
212 |
this.EndPoint, |
213 |
this.TopRightPoint, |
214 |
}); |
215 |
double AngleData = 0; |
216 |
|
217 |
PathFigure pathFigure = new PathFigure(); |
218 |
pathFigure.StartPoint = MathSet.RotateAbout(mid, this.StartPoint, AngleData); |
219 |
|
220 |
LineSegment lineSegment0 = new LineSegment(); |
221 |
lineSegment0.Point = MathSet.RotateAbout(mid, this.StartPoint, AngleData); |
222 |
pathFigure.Segments.Add(lineSegment0); |
223 |
|
224 |
LineSegment lineSegment1 = new LineSegment(); |
225 |
lineSegment1.Point = MathSet.RotateAbout(mid, this.LeftBottomPoint, AngleData); |
226 |
pathFigure.Segments.Add(lineSegment1); |
227 |
|
228 |
LineSegment lineSegment2 = new LineSegment(); |
229 |
lineSegment2.Point = MathSet.RotateAbout(mid, this.EndPoint, AngleData); |
230 |
pathFigure.Segments.Add(lineSegment2); |
231 |
|
232 |
LineSegment lineSegment3 = new LineSegment(); |
233 |
lineSegment3.Point = MathSet.RotateAbout(mid, this.TopRightPoint, AngleData); |
234 |
pathFigure.Segments.Add(lineSegment3); |
235 |
|
236 |
PathGeometry pathGeometry = new PathGeometry(); |
237 |
pathGeometry.Figures = new PathFigureCollection(); |
238 |
pathFigure.IsClosed = true; |
239 |
pathGeometry.Figures.Add(pathFigure); |
240 |
this.Base_ViewBox.Width = pathGeometry.Bounds.Width; |
241 |
this.Base_ViewBox.Height = pathGeometry.Bounds.Height; |
242 |
this.Tag = pathGeometry; |
243 |
|
244 |
if(Base_ViewBox.Child == null) |
245 |
{ |
246 |
SetChild(); |
247 |
} |
248 |
|
249 |
Canvas.SetLeft(this, MathSet.RotateAbout(mid, mid, AngleData).X - this.Base_ViewBox.Width / 2); |
250 |
Canvas.SetTop(this, MathSet.RotateAbout(mid, mid, AngleData).Y - this.Base_ViewBox.Height / 2); |
251 |
} |
252 |
|
253 |
public void SetChild() |
254 |
{ |
255 |
if(this.PathXathData != null) |
256 |
{ |
257 |
var xamlData = MarkupToPDF.Serialize.Core.JsonSerializerHelper.UnCompressStamp(this.PathXathData); |
258 |
System.IO.MemoryStream stream = new System.IO.MemoryStream(); |
259 |
System.IO.StreamWriter writer = new System.IO.StreamWriter(stream); |
260 |
writer.Write(xamlData); |
261 |
writer.Flush(); |
262 |
stream.Position = 0; |
263 |
|
264 |
object obj = System.Windows.Markup.XamlReader.Load(stream); |
265 |
UIElement ob = obj as UIElement; |
266 |
|
267 |
Base_ViewBox.Child = ob; |
268 |
} |
269 |
} |
270 |
|
271 |
public override bool IsSelected |
272 |
{ |
273 |
get |
274 |
{ |
275 |
return (bool)GetValue(IsSelectedProperty); |
276 |
} |
277 |
set |
278 |
{ |
279 |
SetValue(IsSelectedProperty, value); |
280 |
} |
281 |
} |
282 |
|
283 |
public override ControlType ControlType |
284 |
{ |
285 |
set |
286 |
{ |
287 |
SetValue(ControlTypeProperty, value); |
288 |
} |
289 |
get |
290 |
{ |
291 |
return (ControlType)GetValue(ControlTypeProperty); |
292 |
} |
293 |
} |
294 |
|
295 |
public override double CommentAngle |
296 |
{ |
297 |
get { return (double)GetValue(AngleProperty); } |
298 |
set |
299 |
{ |
300 |
if (this.CommentAngle != value) |
301 |
{ |
302 |
SetValue(AngleProperty, value); |
303 |
} |
304 |
} |
305 |
} |
306 |
|
307 |
|
308 |
public Double LineSize |
309 |
{ |
310 |
get { return (Double)GetValue(LineSizeProperty); } |
311 |
set |
312 |
{ |
313 |
if (this.LineSize != value) |
314 |
{ |
315 |
SetValue(LineSizeProperty, value); |
316 |
} |
317 |
} |
318 |
} |
319 |
public Geometry PathData |
320 |
{ |
321 |
get { return (Geometry)GetValue(PathDataProperty); } |
322 |
set |
323 |
{ |
324 |
SetValue(PathDataProperty, value); |
325 |
RaisePropertyChanged("PathData"); |
326 |
} |
327 |
} |
328 |
|
329 |
public override void UpdateControl() |
330 |
{ |
331 |
this.StartPoint = new Point(this.PointSet[0].X, this.PointSet[0].Y); |
332 |
this.LeftBottomPoint = new Point(this.PointSet[1].X, this.PointSet[1].Y); |
333 |
this.EndPoint = new Point(this.PointSet[2].X, this.PointSet[2].Y); |
334 |
this.TopRightPoint = new Point(this.PointSet[3].X, this.PointSet[3].Y); |
335 |
this.SetViewBox(); |
336 |
} |
337 |
|
338 |
/// <summary> |
339 |
/// call when mouse is moving while drawing control |
340 |
/// </summary> |
341 |
/// <author>humkyung</author> |
342 |
/// <param name="pt"></param> |
343 |
/// <param name="bAxisLocked"></param> |
344 |
public override void OnCreatingMouseMove(Point pt, bool bAxisLocked) |
345 |
{ |
346 |
if (this.StartPoint == this.EndPoint) |
347 |
{ |
348 |
var xamlData = MarkupToPDF.Serialize.Core.JsonSerializerHelper.UnCompressStamp(this.STAMP); |
349 |
|
350 |
if (STAMP_Contents?.Count > 0) |
351 |
{ |
352 |
foreach (var item in STAMP_Contents) |
353 |
{ |
354 |
xamlData = xamlData.Replace(item.Key, System.Security.SecurityElement.Escape(item.Value)); |
355 |
} |
356 |
} |
357 |
|
358 |
System.IO.MemoryStream stream = new System.IO.MemoryStream(); |
359 |
System.IO.StreamWriter writer = new System.IO.StreamWriter(stream); |
360 |
writer.Write(xamlData); |
361 |
writer.Flush(); |
362 |
stream.Position = 0; |
363 |
|
364 |
this.StrokeColor = new SolidColorBrush(Colors.Red); |
365 |
object obj = System.Windows.Markup.XamlReader.Load(stream); |
366 |
UIElement ob = obj as UIElement; |
367 |
|
368 |
this.SetViewBox(); |
369 |
this.PathXathData = this.STAMP; |
370 |
this.Base_ViewBox.Child = ob; |
371 |
} |
372 |
|
373 |
this.EndPoint = pt; |
374 |
this.LeftBottomPoint = new Point(this.StartPoint.X, this.EndPoint.Y); |
375 |
this.TopRightPoint = new Point(this.EndPoint.X, this.StartPoint.Y); |
376 |
|
377 |
this.PointSet = new List<Point> |
378 |
{ |
379 |
this.StartPoint, |
380 |
this.LeftBottomPoint, |
381 |
this.EndPoint, |
382 |
this.TopRightPoint, |
383 |
}; |
384 |
} |
385 |
|
386 |
/// <summary> |
387 |
/// move control point has same location of given pt along given delta |
388 |
/// </summary> |
389 |
/// <author>humkyung</author> |
390 |
/// <date>2019.06.20</date> |
391 |
/// <param name="pt"></param> |
392 |
/// <param name="dx"></param> |
393 |
/// <param name="dy"></param> |
394 |
public override void OnMoveCtrlPoint(Point pt, double dx, double dy, bool bAxisLocked = false) |
395 |
{ |
396 |
IPath path = (this as IPath); |
397 |
|
398 |
Point selected = MathSet.getNearPoint(path.PointSet, pt); |
399 |
selected.X += dx; |
400 |
selected.Y += dy; |
401 |
int i = 0; |
402 |
for (i = 0; i < (this as IPath).PointSet.Count; i++) |
403 |
{ |
404 |
if (pt.Equals((this as IPath).PointSet[i])) |
405 |
{ |
406 |
path.PointSet[i] = selected; |
407 |
break; |
408 |
} |
409 |
} |
410 |
|
411 |
var OppositeP = (i + path.PointSet.Count / 2) % path.PointSet.Count; |
412 |
var PreviousP = (i + (path.PointSet.Count - 1)) % path.PointSet.Count; |
413 |
var NextP = (i + 1) % path.PointSet.Count; |
414 |
if (bAxisLocked) |
415 |
{ |
416 |
double _dx = path.PointSet[i].X - path.PointSet[OppositeP].X; |
417 |
double _dy = path.PointSet[i].Y - path.PointSet[OppositeP].Y; |
418 |
double distance = Math.Max(Math.Abs(_dx), Math.Abs(_dy)); |
419 |
|
420 |
var PreviousV = path.PointSet[PreviousP] - path.PointSet[OppositeP]; |
421 |
PreviousV.Normalize(); |
422 |
path.PointSet[PreviousP] = path.PointSet[OppositeP] + PreviousV * distance; |
423 |
|
424 |
var NextV = path.PointSet[NextP] - path.PointSet[OppositeP]; |
425 |
NextV.Normalize(); |
426 |
path.PointSet[NextP] = path.PointSet[OppositeP] + NextV * distance; |
427 |
|
428 |
path.PointSet[i] = path.PointSet[OppositeP] + PreviousV * distance + NextV * distance; |
429 |
} |
430 |
else |
431 |
{ |
432 |
var PreviousV = MathSet.GetNormVectorBetween(path.PointSet[OppositeP], path.PointSet[PreviousP]); |
433 |
var l = MathSet.DotProduct(PreviousV.X, PreviousV.Y, path.PointSet[i].X - path.PointSet[OppositeP].X, |
434 |
path.PointSet[i].Y - path.PointSet[OppositeP].Y); |
435 |
path.PointSet[PreviousP] = new Point(path.PointSet[OppositeP].X + PreviousV.X * l, path.PointSet[OppositeP].Y + PreviousV.Y * l); |
436 |
|
437 |
var NextV = MathSet.GetNormVectorBetween(path.PointSet[OppositeP], path.PointSet[NextP]); |
438 |
l = MathSet.DotProduct(NextV.X, NextV.Y, path.PointSet[i].X - path.PointSet[OppositeP].X, path.PointSet |
439 |
[i].Y - path.PointSet[OppositeP].Y); |
440 |
path.PointSet[NextP] = new Point(path.PointSet[OppositeP].X + NextV.X * l, path.PointSet[OppositeP].Y + NextV.Y * l); |
441 |
} |
442 |
|
443 |
this.UpdateControl(); |
444 |
} |
445 |
|
446 |
/// <summary> |
447 |
/// return SymControlN's area |
448 |
/// </summary> |
449 |
/// <author>humkyung</author> |
450 |
/// <date>2019.06.13</date> |
451 |
public override Rect ItemRect |
452 |
{ |
453 |
get |
454 |
{ |
455 |
double dMinX = Math.Min(this.StartPoint.X, this.EndPoint.X); |
456 |
double dMinY = Math.Min(this.StartPoint.Y, this.EndPoint.Y); |
457 |
double dMaxX = Math.Max(this.StartPoint.X, this.EndPoint.X); |
458 |
double dMaxY = Math.Max(this.StartPoint.Y, this.EndPoint.Y); |
459 |
|
460 |
return new Rect(new Point(dMinX, dMinY), new Point(dMaxX, dMaxY)); |
461 |
} |
462 |
} |
463 |
|
464 |
/// <summary> |
465 |
/// Serialize this |
466 |
/// </summary> |
467 |
/// <param name="sUserId"></param> |
468 |
/// <returns></returns> |
469 |
public override string Serialize() |
470 |
{ |
471 |
using (S_SymControlN STemp = new S_SymControlN()) |
472 |
{ |
473 |
STemp.TransformPoint = "0|0"; |
474 |
STemp.PointSet = this.PointSet; |
475 |
//STemp.XamlData = this.PathXathData; |
476 |
STemp.UserID = this.UserID; |
477 |
STemp.DBData = this.PathXathData; |
478 |
//STemp.StrokeColor = this.StrokeColor.Color.ToString(); |
479 |
STemp.StartPoint = this.StartPoint; |
480 |
STemp.Angle = this.CommentAngle; |
481 |
STemp.EndPoint = this.EndPoint; |
482 |
STemp.LB = this.LeftBottomPoint; |
483 |
STemp.TR = this.TopRightPoint; |
484 |
STemp.Opac = this.Opacity; |
485 |
STemp.Name = this.GetType().Name.ToString(); |
486 |
|
487 |
return "|DZ|" + JsonSerializerHelper.CompressString((STemp.JsonSerialize())); |
488 |
} |
489 |
} |
490 |
|
491 |
/// <summary> |
492 |
/// create a symcontroln from given string |
493 |
/// </summary> |
494 |
/// <param name="str"></param> |
495 |
/// <returns></returns> |
496 |
public static SymControlN FromString(string str, SolidColorBrush brush, string sProjectNo) |
497 |
{ |
498 |
using (S_SymControlN s = JsonSerializerHelper.JsonDeserialize<S_SymControlN>(str)) |
499 |
{ |
500 |
return new SymControlN() |
501 |
{ |
502 |
PointSet = s.PointSet, |
503 |
StartPoint = s.StartPoint, |
504 |
EndPoint = s.EndPoint, |
505 |
CommentAngle = s.Angle, |
506 |
LeftBottomPoint = s.LB, |
507 |
TopRightPoint = s.TR, |
508 |
Opacity = s.Opac, |
509 |
PathXathData = s.DBData, |
510 |
Memo = s.Memo |
511 |
}; |
512 |
} |
513 |
} |
514 |
} |
515 |
} |
516 |
|
517 |
|