My Project
ShapePencil.cs
查看本檔案說明文件.
1 using Microsoft.Office.Tools.Ribbon;
2 using System;
3 using System.Collections;
4 using System.Collections.Generic;
5 using System.Diagnostics;
6 using System.Linq;
7 using System.Text;
8 using System.Threading.Tasks;
9 using System.Windows;
10 using System.Windows.Controls;
11 using System.Windows.Forms;
12 using System.Windows.Input;
13 using System.Windows.Media;
14 using System.Windows.Shapes;
15 
16 namespace ShapeLib.VShape
17 {
18 
19  public class ShapePencil : ShapeObj
20  {
21 
22  public override System.Collections.ArrayList getMenuItem()
23  {
24  ArrayList ret = new ArrayList();
25 
26  shapeUI ui = new shapeUI();
27  ui.uitype = shapeUIType.RibbonGroup;
28  ui.label = "Tools";
29  ret.Add(ui);
30  shapeUI pui = new shapeUI();
31  pui.label = "Pencil";
32  System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
33  System.IO.Stream myStream = myAssembly.GetManifestResourceStream("ShapeLib.icons.pencil.png");
34  pui.image = new System.Drawing.Bitmap(myStream);
35 
36 
37  pui.click = btn_Click;
38  pui.belong = "Tools";
39  ret.Add(pui);
40 
41  return ret;
42  }
43 
44 
45 
46  double B0(double u)
47  {
48  double ret = (1 - u) * (1 - u) * (1 - u) / 6.0;
49  return ret;
50  }
51 
52  double B1(double u)
53  {
54  double ret = (3 * u * u * u - 6 * u * u + 4.0) / 6.0;
55  return ret;
56  }
57  double B2(double u)
58  {
59  double ret = (-3 * u * u * u + 3 * u * u + 3 * u + 1.0) / 6.0;
60  return ret;
61  }
62  double B3(double u)
63  {
64  double ret = u * u * u / 6.0;
65  return ret;
66  }
67 
68 
69  // Point[] plist ;
70 
71  // ArrayList addlist = new ArrayList();
72 
73  //ArrayList list = new ArrayList();
74 
75  int extra ;
76  byte r, g, b;
77 
78  public override void DrawShape(gView gv, gPath data, Boolean bfirst)
79  {
80  if (bfirst)
81  {
82  shapeLib.Data.Status = "rest";
83  shapeLib.Data.bfirst = false;
84  Path p = buildShape(data);
85 
86  r = data.state.colorR;
87  g = data.state.colorG;
88  b = data.state.colorB;
89 
90  p.Stroke = new SolidColorBrush(System.Windows.Media.Color.FromRgb(r,g,b));
91  p.StrokeThickness = shapeLib.Data.strokeT;
92  // p.StrokeEndLineCap = PenLineCap.Round;
93  // p.StrokeStartLineCap = PenLineCap.Flat;
94  shapeLib.Data.mygrid.Children.Add(p);
95  gv.baseShape.Add(p);
96 
97  Debug.WriteLine("draw pencil");
98 
99  }
100  else
101  {
102  Path myLine = (Path)gv.baseShape[0];
103 
104  buildShape(data, myLine);
105 
106  }
107 
108 
109  //shapeLib.Data.mygrid.MouseDown += new System.Windows.Input.MouseButtonEventHandler(LeftButtonDown);
110  }
111  private Path buildShape(gPath data ,Path old=null)
112  {
113  Path ret = old;
114 
115  if (old == null)
116  {
117  ret = new Path();
118  }
119  int m = data.pList.Count;
120  Point[] plist = new Point[m + 3];
121  data.pList.CopyTo(plist, 1);
122  plist[0] = plist[1] ;
123  plist[m + 1] = plist[m+2]= data.controlBtn4;
124  m = m +3;
125 
126  int MAX_STEPS = 10;
127 
128  PathFigure myPathFigure = new PathFigure();
129 
130 
131  PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
132 
133  myPathFigure.Segments = myPathSegmentCollection;
134 
135  PathFigureCollection myPathFigureCollection = new PathFigureCollection();
136  myPathFigureCollection.Add(myPathFigure);
137 
138  PathGeometry myPathGeometry = new PathGeometry();
139  myPathGeometry.Figures = myPathFigureCollection;
140 
141  ret.Data = myPathGeometry;
142 
143  bool bstart = true;
144  for (int i = 0; i < m - 3; i++)
145  {
146  Point p = (Point)plist[i];
147  Point p1 = (Point)plist[i + 1];
148  Point p2 = (Point)plist[i + 2];
149  Point p3 = (Point)plist[i + 3];
150 
151 
152  if (i == m - 4)
153  {
154  extra = 1;
155  }
156 
157  // sx = p.X;
158  // sy = p.Y;
159 
160  for (int j = 0; j < MAX_STEPS + extra; j++)
161  {
162 
163  double u = j * 1.0 / MAX_STEPS;
164  double Qx = B0(u) * p.X +
165  B1(u) * p1.X +
166  B2(u) * p2.X +
167  B3(u) * p3.X;
168 
169  double Qy = B0(u) * p.Y +
170  B1(u) * p1.Y +
171  B2(u) * p2.Y +
172  B3(u) * p3.Y;
173 
174  if ( bstart)
175  {
176  bstart = false;
177  myPathFigure.StartPoint = new System.Windows.Point((int)Qx, (int)Qy);
178 
179 
180  }
181  else
182  {
183  LineSegment lineseg = new LineSegment();
184  lineseg.Point = new System.Windows.Point((int)Qx, (int)Qy);
185  myPathSegmentCollection.Add(lineseg);
186 
187  }
188 
189 
190  }
191 
192  }
193 
194  return ret;
195  }
196 
197 
198 
199  public override void MouseDownInsert(object sender, System.Windows.Input.MouseButtonEventArgs e)
200  {
201  if (e.ChangedButton == MouseButton.Right)
202  // if (e.RightButton == System.Windows.Input.MouseButtonState.Pressed)
203  {
206  shapeLib.Data.mClick = 0;
207  return;
208 
209  }
210 
211 
212  Canvas mygrid = shapeLib.Data.mygrid;
213 
214  shapeLib.Data.pStart = e.GetPosition(mygrid);
215 
216  if ( shapeLib.Data.mClick == 0)
217  {
218 
219  currPath = new gPath();
220  currPath.drawtype = shapeLib.SupportedShape(null).IndexOf(this);
222  }
224  shapeLib.Data.bCanMove = true;
225 
226  }
227 
228  public override void MouseUpInsert(object sender, System.Windows.Input.MouseButtonEventArgs e)
229  {
230  Canvas mygrid = shapeLib.Data.mygrid;
231 
232 
233 
234  if (shapeLib.Data.bCanMove && e.ChangedButton== MouseButton.Left)
235  {
236  shapeLib.Data.pEnd = e.GetPosition(mygrid);
237  double tempX, tempY;
238  double px = shapeLib.Data.pStart.X;
239  double py = shapeLib.Data.pStart.Y;
240  double ex = shapeLib.Data.pEnd.X;
241  double ey = shapeLib.Data.pEnd.Y;
242 
243  // if (px == ex && py == ey) //click
244  {
245  currPath.pList.Add(new Point(ex, ey));
246 
247  //
248  Debug.WriteLine("click");
249  remGPath(px, py, ex, ey);
250  currPath.redraw(1);
251 
252  shapeLib.Data.mClick++;
253 
254  //if (this.GetType() == typeof(ShapeCurve) && shapeLib.Data.mClick >= 3)
255  //{
256  // currPath.drawtype = shapeLib.SupportedShape(null).IndexOf(this);//line,在shaplib 中的位置
257  // shapeLib.Data.gdc.writeIn(currPath, 0);
258  // shapeLib.Data.gdc.Release();
259  // shapeLib.Data.mClick = 0;
260  //}
261 
262 
263  foreach (gPath gp in shapeLib.Data.multiSelList)
264  {
265  gp.isSel = false;
266  }
267  if (shapeLib.Data.currShape != null)
268  shapeLib.Data.currShape.isSel = false;
269 
270  shapeLib.Data.multiSelList.Clear();
271  return;
272  }
273  //remGPath(px, py, ex, ey);
274  //{
275  // currPath.drawtype = shapeLib.SupportedShape(null).IndexOf(this);//line,在shaplib 中的位置
276  // shapeLib.Data.gdc.writeIn(currPath, 0);
277  // shapeLib.Data.gdc.Release();
278  //}
279  //shapeLib.Data.gdc.bmove = false;
280  //if (shapeLib.Data.Status.Equals("rest"))
281  // currPath.redraw(1);
282 
283  //shapeLib.Data.bfirst = true;
284  //shapeLib.Data.bhave = false;
285  }
286  //throw new NotImplementedException();
287  }
288 
289  public override void MouseMoveInsert(object sender, System.Windows.Input.MouseEventArgs e)
290  {
291  Canvas mygrid = shapeLib.Data.mygrid;
292 
293 
294 
295  if ( currPath!= null && shapeLib.Data.mClick > 0)
296  {
297  // if (!shapeLib.Data.bhave) //if you can control an object
298  {
299  shapeLib.Data.pEnd =e.GetPosition(mygrid);
300  double tempX, tempY;
301  double px = shapeLib.Data.pStart.X;
302  double py = shapeLib.Data.pStart.Y;
303  double ex = shapeLib.Data.pEnd.X;
304  double ey = shapeLib.Data.pEnd.Y;
305 
306  remGPath(px, py, ex, ey);
307  currPath.redraw(0);
308 
309  }
310  }
311 
312 
313 
314  //throw new NotImplementedException();
315  }
316 
317 
318  }
319 }
static GModel Data
Definition: shapeLib.cs:42
RibbonControlEventHandler click
Definition: ShapeObj.cs:49
List< gPath > multiSelList
Definition: shapeLib.cs:148
string belong
判斷按下Button控制項
Definition: ShapeObj.cs:53
void remGPath(double px, double py, double ex, double ey)
儲存新繪製的圖形資料
Definition: ShapeObj.cs:545
shapeUIType uitype
Definition: ShapeObj.cs:45
void writeIn(gPath Data, int Action)
維護 undo stack ,把目前狀態存起來.並清空redo stack,如果之前有undo 動作,是回覆到某一狀態,在此之後的動作都可清除
Definition: GraphDoc.cs:96
System.Windows.Point controlBtn4
Definition: GraphDoc.cs:256
某一類的形狀.包含UI 的界面,繪製方式.更新方式.新增方式
Definition: ShapeObj.cs:59
override void MouseMoveInsert(object sender, System.Windows.Input.MouseEventArgs e)
Definition: ShapePencil.cs:289
shapeUIType
建構不同UI 時使用
Definition: ShapeObj.cs:31
void redraw(int removetype)
Definition: GraphDoc.cs:286
定義一個UI項目
Definition: ShapeObj.cs:42
static IList< ShapeObj > SupportedShape(getForm myview)
define supported shape
Definition: shapeLib.cs:27
List< Point > pList
Definition: GraphDoc.cs:259
override System.Collections.ArrayList getMenuItem()
覆寫System.Collections.ArrayList
Definition: ShapePencil.cs:22
List< Shape > baseShape
Definition: gView.cs:12
void btn_Click(object sender, RibbonControlEventArgs e)
UI 點選
Definition: ShapeObj.cs:107
override void MouseUpInsert(object sender, System.Windows.Input.MouseButtonEventArgs e)
Definition: ShapePencil.cs:228
System.Drawing.Image image
Definition: ShapeObj.cs:46
override void MouseDownInsert(object sender, System.Windows.Input.MouseButtonEventArgs e)
Definition: ShapePencil.cs:199
override void DrawShape(gView gv, gPath data, Boolean bfirst)
依data 繪製,如果是第一次畫要新建shape, 更新的話只要更新最後一點
Definition: ShapePencil.cs:78
Path buildShape(gPath data, Path old=null)
Definition: ShapePencil.cs:111