markus / MarkupToPDF / Controls / Etc / SymControl.cs @ 661b7416
이력 | 보기 | 이력해설 | 다운로드 (17.1 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 |
} |
49 |
public event PropertyChangedEventHandler PropertyChanged; |
50 |
protected void RaisePropertyChanged(string propName) |
51 |
{ |
52 |
if (PropertyChanged != null) |
53 |
PropertyChanged(this, new PropertyChangedEventArgs(propName)); |
54 |
} |
55 |
#region Dependency Properties |
56 |
public static readonly DependencyProperty UserIDProperty = DependencyProperty.Register( |
57 |
"UserID", typeof(string), typeof(SymControl), new PropertyMetadata(null)); |
58 |
public static readonly DependencyProperty LineSizeProperty = DependencyProperty.Register( |
59 |
"LineSize", typeof(double), typeof(SymControl), new PropertyMetadata((Double)1)); |
60 |
public static readonly DependencyProperty StrokeColorProperty = DependencyProperty.Register( |
61 |
"StrokeColor", typeof(SolidColorBrush), typeof(SymControl), new PropertyMetadata(new SolidColorBrush(Colors.Red))); |
62 |
public static readonly DependencyProperty PathDataProperty = DependencyProperty.Register( |
63 |
"PathData", typeof(Geometry), typeof(SymControl), null); |
64 |
public static readonly DependencyProperty EndPointProperty = DependencyProperty.Register( |
65 |
"EndPoint", typeof(Point), typeof(SymControl), new PropertyMetadata(new Point(0, 0), PointValueChanged)); |
66 |
public static readonly DependencyProperty StartPointProperty = DependencyProperty.Register( |
67 |
"StartPoint", typeof(Point), typeof(SymControl), new PropertyMetadata(new Point(0, 0), PointValueChanged)); |
68 |
public static readonly DependencyProperty TopRightPointProperty = DependencyProperty.Register( |
69 |
"TopRightPoint", typeof(Point), typeof(SymControl), new PropertyMetadata(new Point(0, 0), TRPointValueChanged)); |
70 |
public static readonly DependencyProperty LeftBottomPointProperty = DependencyProperty.Register( |
71 |
"LeftBottomPoint", typeof(Point), typeof(SymControl), new PropertyMetadata(new Point(0, 0), LBPointValueChanged)); |
72 |
public static readonly DependencyProperty PointSetProperty = DependencyProperty.Register( |
73 |
"PointSet", typeof(List<Point>), typeof(SymControl), new PropertyMetadata(new List<Point>())); |
74 |
public static readonly DependencyProperty AngleProperty = DependencyProperty.Register("Angle", typeof(double), typeof(SymControl), |
75 |
new PropertyMetadata((double)0.0, new PropertyChangedCallback(AngleValueChanged))); |
76 |
public static readonly DependencyProperty CenterXProperty = DependencyProperty.Register("CenterX", typeof(double), typeof(SymControl), |
77 |
new PropertyMetadata((double)0, OnCenterXYChanged)); |
78 |
public static readonly DependencyProperty CenterYProperty = DependencyProperty.Register("CenterY", typeof(double), typeof(SymControl), |
79 |
new PropertyMetadata((double)0, OnCenterXYChanged)); |
80 |
public static readonly DependencyProperty PaintProperty = DependencyProperty.Register( |
81 |
"Paint", typeof(PaintSet), typeof(SymControl), new PropertyMetadata(PaintSet.None)); |
82 |
|
83 |
public static readonly DependencyProperty IsSelectedProperty = |
84 |
DependencyProperty.Register("IsSelected", typeof(bool), typeof(SymControl), new FrameworkPropertyMetadata(false, IsSelectedChanged)); |
85 |
|
86 |
public static readonly DependencyProperty ControlTypeProperty = |
87 |
DependencyProperty.Register("ControlType", typeof(ControlType), typeof(SymControl), new FrameworkPropertyMetadata(ControlType.Symbol)); |
88 |
#endregion |
89 |
#region PropertyChanged Method |
90 |
public static void PointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) |
91 |
{ |
92 |
var instance = (SymControl)sender; |
93 |
if (e.OldValue != e.NewValue && instance.Base_SymPath != null) |
94 |
{ |
95 |
instance.SetValue(e.Property, e.NewValue); |
96 |
instance.SetSymPath(); |
97 |
} |
98 |
} |
99 |
|
100 |
public static void TRPointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) |
101 |
{ |
102 |
var instance = (SymControl)sender; |
103 |
if (e.OldValue != e.NewValue && instance.Base_SymPath != null) |
104 |
{ |
105 |
instance.SetValue(e.Property, e.NewValue); |
106 |
instance.SetSymPath(); |
107 |
} |
108 |
} |
109 |
public static void IsSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) |
110 |
{ |
111 |
|
112 |
} |
113 |
public static void OnCenterXYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) |
114 |
{ |
115 |
var instance = (SymControl)sender; |
116 |
if (e.OldValue != e.NewValue && instance.Base_SymPath != null) |
117 |
{ |
118 |
instance.SetValue(e.Property, e.NewValue); |
119 |
instance.SetSymPath(); |
120 |
} |
121 |
} |
122 |
|
123 |
public static void LBPointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) |
124 |
{ |
125 |
var instance = (SymControl)sender; |
126 |
if (e.OldValue != e.NewValue && instance.Base_SymPath != null) |
127 |
{ |
128 |
instance.SetValue(e.Property, e.NewValue); |
129 |
//instance.EndPoint = new Point(instance.EndPoint.X, ((Point)e.NewValue).Y); |
130 |
//instance.StartPoint = new Point(((Point)e.NewValue).X, instance.StartPoint.Y); |
131 |
////instance.PointSet[0] = instance.StartPoint; |
132 |
////instance.PointSet[2] = instance.EndPoint; |
133 |
instance.SetSymPath(); |
134 |
} |
135 |
} |
136 |
|
137 |
public static void AngleValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) |
138 |
{ |
139 |
var instance = (SymControl)sender; |
140 |
if (e.OldValue != e.NewValue && instance.Base_SymPath != null) |
141 |
{ |
142 |
instance.SetValue(e.Property, e.NewValue); |
143 |
instance.SetSymPath(); |
144 |
} |
145 |
} |
146 |
#endregion |
147 |
#region Properties |
148 |
public string UserID |
149 |
{ |
150 |
get { return (string)GetValue(UserIDProperty); } |
151 |
set |
152 |
{ |
153 |
if (this.UserID != value) |
154 |
{ |
155 |
SetValue(UserIDProperty, value); |
156 |
RaisePropertyChanged("UserID"); |
157 |
} |
158 |
} |
159 |
} |
160 |
public Double LineSize |
161 |
{ |
162 |
get { return (Double)GetValue(LineSizeProperty); } |
163 |
set |
164 |
{ |
165 |
if (this.LineSize != value) |
166 |
{ |
167 |
SetValue(LineSizeProperty, value); |
168 |
} |
169 |
} |
170 |
} |
171 |
public PaintSet Paint |
172 |
{ |
173 |
get { return (PaintSet)GetValue(PaintProperty); } |
174 |
set |
175 |
{ |
176 |
if (this.Paint != value) |
177 |
{ |
178 |
SetValue(PaintProperty, value); |
179 |
} |
180 |
} |
181 |
} |
182 |
public List<Point> PointSet |
183 |
{ |
184 |
get { return (List<Point>)GetValue(PointSetProperty); } |
185 |
set { SetValue(PointSetProperty, value); } |
186 |
} |
187 |
public double Angle |
188 |
{ |
189 |
get { return (double)GetValue(AngleProperty); } |
190 |
set |
191 |
{ |
192 |
if (this.Angle != value) |
193 |
{ |
194 |
SetValue(AngleProperty, value); |
195 |
} |
196 |
} |
197 |
} |
198 |
public SolidColorBrush StrokeColor |
199 |
{ |
200 |
get { return (SolidColorBrush)GetValue(StrokeColorProperty); } |
201 |
set |
202 |
{ |
203 |
if (this.StrokeColor != value) |
204 |
{ |
205 |
SetValue(StrokeColorProperty, value); |
206 |
} |
207 |
} |
208 |
} |
209 |
public Geometry PathData |
210 |
{ |
211 |
get { return (Geometry)GetValue(PathDataProperty); } |
212 |
set |
213 |
{ |
214 |
SetValue(PathDataProperty, value); |
215 |
RaisePropertyChanged("PathData"); |
216 |
} |
217 |
} |
218 |
public Point TopRightPoint |
219 |
{ |
220 |
get { return (Point)GetValue(TopRightPointProperty); } |
221 |
set |
222 |
{ |
223 |
SetValue(TopRightPointProperty, value); |
224 |
RaisePropertyChanged("TopRightPoint"); |
225 |
} |
226 |
} |
227 |
public Point LeftBottomPoint |
228 |
{ |
229 |
get { return (Point)GetValue(LeftBottomPointProperty); } |
230 |
set |
231 |
{ |
232 |
SetValue(LeftBottomPointProperty, value); |
233 |
RaisePropertyChanged("LeftBottomPoint"); |
234 |
} |
235 |
} |
236 |
public Point EndPoint |
237 |
{ |
238 |
get { return (Point)GetValue(EndPointProperty); } |
239 |
set |
240 |
{ |
241 |
SetValue(EndPointProperty, value); |
242 |
RaisePropertyChanged("EndPoint"); |
243 |
} |
244 |
} |
245 |
public Point StartPoint |
246 |
{ |
247 |
get { return (Point)GetValue(StartPointProperty); } |
248 |
set |
249 |
{ |
250 |
SetValue(StartPointProperty, value); |
251 |
RaisePropertyChanged("StartPoint"); |
252 |
} |
253 |
} |
254 |
public double CenterX |
255 |
{ |
256 |
get { return (double)GetValue(CenterXProperty); } |
257 |
set { SetValue(CenterXProperty, value); } |
258 |
} |
259 |
public double CenterY |
260 |
{ |
261 |
get { return (double)GetValue(CenterYProperty); } |
262 |
set { SetValue(CenterYProperty, value); } |
263 |
} |
264 |
public double AngleValue |
265 |
{ |
266 |
get { return (double)GetValue(AngleProperty); } |
267 |
set { SetValue(AngleProperty, value); } |
268 |
} |
269 |
public bool IsSelected |
270 |
{ |
271 |
get |
272 |
{ |
273 |
return (bool)GetValue(IsSelectedProperty); |
274 |
} |
275 |
set |
276 |
{ |
277 |
SetValue(IsSelectedProperty, value); |
278 |
} |
279 |
} |
280 |
|
281 |
override public ControlType ControlType |
282 |
{ |
283 |
set |
284 |
{ |
285 |
SetValue(ControlTypeProperty, value); |
286 |
} |
287 |
get |
288 |
{ |
289 |
return (ControlType)GetValue(ControlTypeProperty); |
290 |
} |
291 |
} |
292 |
|
293 |
#endregion |
294 |
public override void OnApplyTemplate() |
295 |
{ |
296 |
base.OnApplyTemplate(); |
297 |
Base_ViewBox = GetTemplateChild(PART_ViewBox) as Viewbox; |
298 |
Base_SymPath = GetTemplateChild(PART_SymPath) as Path; |
299 |
SetSymPath(); |
300 |
} |
301 |
|
302 |
private void SetSymPath() |
303 |
{ |
304 |
this.ApplyTemplate(); |
305 |
switch (this.Paint) |
306 |
{ |
307 |
case PaintSet.None: |
308 |
Base_SymPath.Fill = null; |
309 |
break; |
310 |
case PaintSet.Fill: |
311 |
Base_SymPath.Fill = this.StrokeColor; |
312 |
break; |
313 |
default: |
314 |
break; |
315 |
} |
316 |
|
317 |
Point mid = MathSet.FindCentroid(new List<Point>() |
318 |
{ |
319 |
this.StartPoint, |
320 |
this.LeftBottomPoint, |
321 |
this.EndPoint, |
322 |
this.TopRightPoint, |
323 |
}); |
324 |
|
325 |
double AngleData = this.Angle * -1; |
326 |
|
327 |
PathFigure pathFigure = new PathFigure(); |
328 |
pathFigure.StartPoint = MathSet.RotateAbout(mid, this.StartPoint, AngleData); |
329 |
|
330 |
LineSegment lineSegment0 = new LineSegment(); |
331 |
lineSegment0.Point = MathSet.RotateAbout(mid, this.StartPoint, AngleData); |
332 |
pathFigure.Segments.Add(lineSegment0); |
333 |
|
334 |
LineSegment lineSegment1 = new LineSegment(); |
335 |
lineSegment1.Point = MathSet.RotateAbout(mid, this.LeftBottomPoint, AngleData); |
336 |
pathFigure.Segments.Add(lineSegment1); |
337 |
|
338 |
LineSegment lineSegment2 = new LineSegment(); |
339 |
lineSegment2.Point = MathSet.RotateAbout(mid, this.EndPoint, AngleData); |
340 |
pathFigure.Segments.Add(lineSegment2); |
341 |
|
342 |
LineSegment lineSegment3 = new LineSegment(); |
343 |
lineSegment3.Point = MathSet.RotateAbout(mid, this.TopRightPoint, AngleData); |
344 |
pathFigure.Segments.Add(lineSegment3); |
345 |
|
346 |
PathGeometry pathGeometry = new PathGeometry(); |
347 |
pathGeometry.Figures = new PathFigureCollection(); |
348 |
pathFigure.IsClosed = true; |
349 |
pathGeometry.Figures.Add(pathFigure); |
350 |
this.Base_ViewBox.Width = pathGeometry.Bounds.Width; |
351 |
this.Base_ViewBox.Height = pathGeometry.Bounds.Height; ; |
352 |
this.Tag = pathGeometry; |
353 |
|
354 |
Canvas.SetLeft(this, MathSet.RotateAbout(mid, mid, AngleData).X - this.Base_ViewBox.Width / 2); |
355 |
Canvas.SetTop(this, MathSet.RotateAbout(mid, mid, AngleData).Y - this.Base_ViewBox.Height / 2); |
356 |
|
357 |
} |
358 |
public void updateControl() |
359 |
{ |
360 |
this.StartPoint = new Point(this.PointSet[0].X, this.PointSet[0].Y); |
361 |
this.LeftBottomPoint = new Point(this.PointSet[1].X, this.PointSet[1].Y); |
362 |
this.EndPoint = new Point(this.PointSet[2].X, this.PointSet[2].Y); |
363 |
this.TopRightPoint = new Point(this.PointSet[3].X, this.PointSet[3].Y); |
364 |
this.SetSymPath(); |
365 |
} |
366 |
|
367 |
|
368 |
public void ChangePaint(PaintSet state) |
369 |
{ |
370 |
this.Paint = state; |
371 |
this.SetSymPath(); |
372 |
} |
373 |
|
374 |
/// <summary> |
375 |
/// Serialize this |
376 |
/// </summary> |
377 |
/// <param name="sUserId"></param> |
378 |
/// <returns></returns> |
379 |
public override string Serialize() |
380 |
{ |
381 |
using (S_SymControl STemp = new S_SymControl()) |
382 |
{ |
383 |
STemp.TransformPoint = "0|0"; |
384 |
STemp.PointSet = this.PointSet; |
385 |
STemp.UserID = this.UserID; |
386 |
STemp.SizeSet = String.Format("{0}", this.LineSize.ToString()); |
387 |
STemp.PaintState = this.Paint; |
388 |
STemp.PathInfo = this.PathData.ToString(); |
389 |
STemp.StrokeColor = this.StrokeColor.Color.ToString(); |
390 |
STemp.StartPoint = this.StartPoint; |
391 |
STemp.Angle = this.Angle; |
392 |
STemp.EndPoint = this.EndPoint; |
393 |
STemp.LB = this.LeftBottomPoint; |
394 |
STemp.TR = this.TopRightPoint; |
395 |
STemp.Opac = this.Opacity; |
396 |
STemp.Name = this.GetType().Name.ToString(); |
397 |
|
398 |
return "|DZ|" + JsonSerializerHelper.CompressString((STemp.JsonSerialize())); |
399 |
} |
400 |
} |
401 |
|
402 |
/// <summary> |
403 |
/// create a symcontrol from given string |
404 |
/// </summary> |
405 |
/// <param name="str"></param> |
406 |
/// <returns></returns> |
407 |
public static SymControl FromString(string str, SolidColorBrush brush, string sProjectNo) |
408 |
{ |
409 |
SymControl instance = null; |
410 |
using (S_SymControl s = JsonSerializerHelper.JsonDeserialize<S_SymControl>(str)) |
411 |
{ |
412 |
string[] data2 = s.SizeSet.Split(delimiterChars2, StringSplitOptions.RemoveEmptyEntries); |
413 |
Common.StringToPathConverter sm = new Common.StringToPathConverter(); |
414 |
instance = new SymControl |
415 |
{ |
416 |
LineSize = Convert.ToDouble(data2.First()), |
417 |
PointSet = s.PointSet, |
418 |
Paint = s.PaintState, |
419 |
StartPoint = s.StartPoint, |
420 |
StrokeColor = brush, |
421 |
EndPoint = s.EndPoint, |
422 |
Angle = s.Angle, |
423 |
UserID = s.UserID, |
424 |
LeftBottomPoint = s.LB, |
425 |
TopRightPoint = s.TR, |
426 |
PathData = sm.Convert(s.PathInfo.ToString()), |
427 |
Opacity = s.Opac, |
428 |
Memo = s.Memo |
429 |
}; |
430 |
} |
431 |
|
432 |
return instance; |
433 |
} |
434 |
} |
435 |
} |