-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawObjFreePen.cs
242 lines (196 loc) · 7.82 KB
/
DrawObjFreePen.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
namespace PenTabletNotebook {
class DrawObjFreePen : DrawObj {
Brush mBrush;
System.Windows.Shapes.Path mPath = new System.Windows.Shapes.Path();
GeometryGroup mGeomGroup = new GeometryGroup();
PathGeometry mPathGeom = new PathGeometry();
PathFigure mPathFig = new PathFigure();
PolyLineSegment mPolyLineSegment = new PolyLineSegment();
private double LineStrokeThinkness = 2;
private int mIdx;
public override void SetBrush(Brush b) {
mBrush = b;
mPath.Stroke = mBrush;
}
/// <summary>
/// 描画物が無い時true
/// </summary>
public override bool IsEmpty() {
return 0 == mPolyLineSegment.Points.Count;
}
public override Rect GetArea() {
Point xy = new Point(double.MaxValue, double.MaxValue);
Vector wh = new Vector(0, 0);
foreach (var c in mPolyLineSegment.Points) {
// 左上座標xyを決定します。
if (c.X < xy.X) {
xy.X = c.X;
}
if (c.Y < xy.Y) {
xy.Y = c.Y;
}
}
foreach (var c in mPolyLineSegment.Points) {
// 幅高さwhを決定します。
Vector whC = c - xy;
if (wh.X < whC.X) {
wh.X = whC.X;
}
if (wh.Y < whC.Y) {
wh.Y = whC.Y;
}
}
return new Rect(xy, wh);
}
public override void Resize(Rect rAfter) {
// 現在のサイズ。
Rect rBefore = GetArea();
// 平行移動量。
Vector dXY = rAfter.TopLeft - rBefore.TopLeft;
// スケール。小さくなりすぎないようにします。
double minSz = 2.0;
double sX = (rAfter.Width < minSz ? minSz : rAfter.Width) / rBefore.Width;
double sY = (rAfter.Height < minSz ? minSz : rAfter.Height) / rBefore.Height;
var scale = new Vector(sX, sY);
// Console.WriteLine("Resize {0} {1} {2} {3}", dXY.X, dXY.Y, sX, sY);
for (int i=0; i<mPolyLineSegment.Points.Count; ++i) {
var p = mPolyLineSegment.Points[i];
var pNew = new Point(
dXY.X + rBefore.X + (p.X - rBefore.X) * scale.X,
dXY.Y + rBefore.Y + (p.Y - rBefore.Y) * scale.Y);
mPolyLineSegment.Points[i] = pNew;
if (i == 0) {
// 始点セット。
mPathFig.StartPoint = pNew;
}
}
}
public override void DeleteFromCanvas() {
if (mCanvas != null) {
int i = 0;
foreach (var c in mCanvas.Children) {
var p = c as System.Windows.Shapes.Path;
if (p != null && (int)p.Tag == mIdx) {
mCanvas.Children.RemoveAt(i);
Console.WriteLine("DrawObj {0} removed from Canvas", i);
break;
}
++i;
}
} else {
Console.WriteLine("DeleteFromCanvas() not added to canvas");
}
}
public override bool AddToCanvas(Canvas c) {
if (c != null) {
mCanvas = c;
c.Children.Add(mPath);
Console.WriteLine("DrawObj {0} added to Canvas", c.Children.Count-1);
return true;
}
return false;
}
public DrawObjFreePen(int idx, Canvas c, Brush b) {
mIdx = idx;
mCanvas = c;
mBrush = b;
mPolyLineSegment.IsSmoothJoin = true;
mPathFig.Segments.Add(mPolyLineSegment);
mPathGeom.Figures.Add(mPathFig);
mGeomGroup.Children.Add(mPathGeom);
mPath.Tag = idx;
mPath.Data = mGeomGroup;
mPath.Stroke = mBrush;
mPath.StrokeThickness = LineStrokeThinkness;
AddToCanvas(c);
}
public override void MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
var msePos = e.GetPosition(mCanvas);
Console.WriteLine("MLDn {0}, {1}", msePos.X, msePos.Y);
mPathFig.StartPoint = msePos;
}
public override void MouseMove(object sender, MouseEventArgs e) {
if (e.LeftButton != MouseButtonState.Pressed) {
return;
}
// たまに、LeftButtonDownが1度も来ないままMouseMoveが来る。
if (mPathFig.StartPoint.X == 0 &&
mPathFig.StartPoint.Y == 0) {
Console.WriteLine("Move L without Down!");
return;
}
var msePos = e.GetPosition(mCanvas);
if (1 <= mPolyLineSegment.Points.Count) {
var lastPos = mPolyLineSegment.Points[mPolyLineSegment.Points.Count - 1];
var lastToCur = msePos - lastPos;
//Console.WriteLine("MM L {0}, {1}, len={2}", msePos.X, msePos.Y, lastToCur.Length);
if (lastToCur.Length < 1.3) {
// 最後の点が近い場合追加しない。
return;
}
}
mPolyLineSegment.Points.Add(msePos);
}
public override void MouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
if (mPolyLineSegment.Points.Count == 0) {
// マウス操作でメニューからファイルをロードした場合、いきなりLButtonUpイベントが来ます。
Console.WriteLine("Mouse Lup pass");
return;
}
var msePos = e.GetPosition(mCanvas);
/*
var lastPos = mPolyLineSegment.Points[mPolyLineSegment.Points.Count - 1];
var d = lastPos - msePos;
if (d.Length < 2.0) {
// 最後の点をすこしずらします。
msePos += new Vector(2, 2);
}
*/
Console.WriteLine("MLUp {0}, {1}", msePos.X, msePos.Y);
mPolyLineSegment.Points.Add(msePos);
}
public override void Save(System.IO.BinaryWriter bw) {
var scb = mBrush as SolidColorBrush;
int doe = (int)DrawObjEnum.DOE_FreePen;
bw.Write(doe);
bw.Write(mIdx);
bw.Write(scb.Color.R);
bw.Write(scb.Color.G);
bw.Write(scb.Color.B);
int nPoints = mPolyLineSegment.Points.Count;
bw.Write(nPoints);
for (int i=0; i<nPoints; ++i) {
bw.Write(mPolyLineSegment.Points[i].X);
bw.Write(mPolyLineSegment.Points[i].Y);
}
}
public override bool Load(System.IO.BinaryReader br) {
// この関数はDrawObjNewから呼び出されます。
// DOE, mIdx, scbはDrawObjNewが読みます。
// nPoints以降を読みます。
int nPoints = br.ReadInt32();
if (0 == nPoints) {
return true;
}
double x = br.ReadDouble();
double y = br.ReadDouble();
var p0 = new Point(x, y);
mPathFig.StartPoint = p0;
mPolyLineSegment.Points.Add(p0);
// p1以降。
for (int i=1; i<nPoints; ++i) {
x = br.ReadDouble();
y = br.ReadDouble();
p0 = new Point(x, y);
mPolyLineSegment.Points.Add(p0);
}
return true;
}
}
}