My Project
ShapeObj.cs
查看本檔案說明文件.
1 using Microsoft.Office.Tools.Ribbon;
2 using ShapeLib.VShape;
3 using System;
4 using System.Collections;
5 using System.Collections.Generic;
6 using System.ComponentModel;
7 using System.Diagnostics;
8 //using System.Drawing;
9 using System.Linq;
10 using System.Reflection;
11 using System.Text;
12 using System.Threading.Tasks;
13 using System.Windows;
16 using System.Windows.Controls;
17 using System.Windows.Forms;
18 using System.Windows.Input;
19 using System.Windows.Media;
20 using System.Windows.Shapes;
21 
22 
23 namespace ShapeLib.VShape
24 {
27 
31  public enum shapeUIType
32  {
35  RibbonMenu,
37  }
38 
42  public class shapeUI
43  {
44 
46  public System.Drawing.Image image;
47  public String label;
48  public ArrayList items;
49  public RibbonControlEventHandler click;
53  public string belong;
54  }
57 
58 
63  {
64 
65  protected gPath currPath;
66 
67  public virtual System.Collections.ArrayList getMenuItem()
71  {
72  ArrayList ret = new ArrayList();
73  shapeUI ui = new shapeUI();
76  ui.uitype = shapeUIType.RibbonGroup;
80  ui.label = "Shapes";
81  ret.Add(ui);
82 
83  ui = new shapeUI();
84  ui.label = "Line";
85  System.Reflection.Assembly myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
86  System.IO.Stream myStream = myAssembly.GetManifestResourceStream("ShapeLib.icons.line.png");
87  ui.image = new System.Drawing.Bitmap(myStream);
88 
89 
90 
91  ui.click = this.btn_Click;
92  ui.belong = "Shapes";
93  ret.Add(ui);
94 
95  return ret;
96  }
97 
98 
99  public virtual void finish()
100  {
101 
102  }
103 
107  public void btn_Click(object sender, RibbonControlEventArgs e)
108  {
109 
110  MouseOP(0);
111  }
112 
113  public void MouseOP(int ntype)
114  {
115  IForm f = null;
116 
117  if (shapeLib.Data.view != null)
118  {
119  f = shapeLib.Data.view();
120 
121  f.Show();
124  }
125 
126 
127 
128  if (shapeLib.Data.mygrid != null)
129  {
130  IList<ShapeObj> ret = shapeLib.SupportedShape(null);
131 
132  shapeLib.Data.UItype = ret.IndexOf(this);
133 
134 
135 
136  foreach (ShapeObj obj in ret)
137  {
138  shapeLib.Data.mygrid.MouseUp -= obj.MouseUpInsert;
139  shapeLib.Data.mygrid.MouseMove -= obj.MouseMoveInsert;
140  shapeLib.Data.mygrid.MouseDown -= obj.MouseDownInsert;
141 
142  shapeLib.Data.mygrid.MouseUp -= obj.MouseDownUpdate;
143  shapeLib.Data.mygrid.MouseMove -= obj.MouseMoveUpdate;
144  shapeLib.Data.mygrid.MouseDown -= obj.MouseUpUpdate;
145 
146  shapeLib.Data.Root.KeyDown -= obj.FormKeyDown;
147 
148  obj.finish();
149 
150  }
151 
152 
153  shapeLib.Data.Root.Focus();
154  shapeLib.Data.Root.KeyDown += this.FormKeyDown;
155 
156  if (ntype == 0)
157  {
158  shapeLib.Data.mygrid.MouseUp += this.MouseUpInsert;
159  shapeLib.Data.mygrid.MouseMove += this.MouseMoveInsert;
160  shapeLib.Data.mygrid.MouseDown += this.MouseDownInsert;
161 
162 
163  }
164  else
165  {
166 
167  shapeLib.Data.mygrid.MouseLeftButtonUp += this.MouseUpUpdate;
168  shapeLib.Data.mygrid.MouseMove += this.MouseMoveUpdate;
169  shapeLib.Data.mygrid.MouseLeftButtonDown += this.MouseDownUpdate;
170 
171  }
172 
173  }
174 
175 
176 
177 
178 
179  // System.Windows.Forms.MessageBox.Show("Clicked");
180 
181  }
182 
186  public ShapeObj Create(string svg)
187  {
188  throw new NotImplementedException();
189  }
190 
191  public void changeProperty(string prop, string value)
192  {
193  throw new NotImplementedException();
194  }
195 
199  public string SVGString()
200  {
201  throw new NotImplementedException();
202  }
203 
207  public virtual void DrawShape(gView gv, gPath data, Boolean bfirst)
208  {
209  if (bfirst)
210 
211  {
212  shapeLib.Data.Status = "rest";
213  shapeLib.Data.bfirst = false;
214 
215  Line myLine = new Line();
216  myLine.Stroke = new SolidColorBrush(System.Windows.Media.Color.FromRgb(data.state.colorR, data.state.colorG, data.state.colorB));
217 
218  myLine.X1 = data.controlBtn1.X;
219  myLine.Y1 = data.controlBtn1.Y;
220  myLine.X2 = data.controlBtn4.X;
221  myLine.Y2 = data.controlBtn4.Y;
222  myLine.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
223  myLine.VerticalAlignment = VerticalAlignment.Center;
224  myLine.StrokeThickness = shapeLib.Data.strokeT;
225  myLine.MouseLeftButtonDown += data.myLine_MouseLeftButtonDown;
226  myLine.MouseEnter += data.myLine_MouseEnter;
227  myLine.MouseLeave += data.myLine_MouseLeave;
228  shapeLib.Data.mygrid.Children.Add(myLine);
229  gv.baseShape.Add(myLine);
230  // currPath.setDrawShape( myLine);
231 
232  }
233  else
234  {
235  Line myLine = (Line)gv.baseShape[0];// =(Line) currPath.getDrawShape();
236  myLine.X2 = data.controlBtn4.X;
237  myLine.Y2 = data.controlBtn4.Y;
238  }
239 
240 
241  // throw new NotImplementedException();
242  }
243 
244 
245 
246 
247 
248  public virtual void DisplayControlPoints(gView gv, gPath data)
249  {
250  if (gv.controlShape.Count == 0)
251  {
252  Line myLine = new Line();
253  myLine.Stroke = new SolidColorBrush(System.Windows.Media.Color.FromRgb(0, 255, 0));
254  myLine.X1 = data.controlBtn1.X;
255  myLine.Y1 = data.controlBtn1.Y;
256  myLine.X2 = data.controlBtn4.X;
257  myLine.Y2 = data.controlBtn4.Y;
258  myLine.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
259  myLine.VerticalAlignment = VerticalAlignment.Center;
260  myLine.StrokeThickness = shapeLib.Data.strokeT;
261  myLine.MouseLeftButtonDown += data.myLine_MouseLeftButtonDown;
262  myLine.MouseEnter += data.myLine_MouseEnter;
263  myLine.MouseLeave += data.myLine_MouseLeave;
264  shapeLib.Data.mygrid.Children.Add(myLine);
265  gv.controlShape.Add(myLine);
266 
267 
268  }
269  else
270  {
271  Line myLine = (Line)gv.controlShape[0];// =(Line) currPath.getDrawShape();
272 
273  myLine.X1 = data.controlBtn1.X;
274  myLine.Y1 = data.controlBtn1.Y;
275  myLine.X2 = data.controlBtn4.X;
276  myLine.Y2 = data.controlBtn4.Y;
277 
278  }
279 
280 
281  // throw new NotImplementedException();
282  }
283 
284  public void MouseDownUpdate(object sender, System.Windows.Input.MouseButtonEventArgs e)
285  {
286  //int tempDraw = shapeLib.Data.gdc.sroot.PathList[shapeLib.Data.ru.Sel].drawtype;
287  //if (tempDraw == 3)
288  //{
289  // shapeLib.Data.pStart = e.GetPosition(shapeLib.Data.myControl);
290  //}
291  //else
292  //{
293  // shapeLib.Data.pStart = correctPoint(e.GetPosition(shapeLib.Data.myControl));
294  //}
295 
296  //currPath = new gPath();
297  //shapeLib.Data.tempStart = shapeLib.Data.pStart;
298 
299  //if (!shapeLib.Data.gCanMove && !shapeLib.Data.OnIt)
300  //{
301  // //hiddenCanvas();
302  // shapeLib.Data.ru.Sel = -1;
303  // shapeLib.Data.ru.Node = -1;
304  // shapeLib.Data.bConThing = false;
305  // shapeLib.Data.gdc.bmove = false;
306  // shapeLib.Data.bfirst = true;
307  // shapeLib.Data.bhave = false;
308  // shapeLib.Data.OnIt = false;
309  //}
310  //if (shapeLib.Data.ru.Sel >= 0)
311  //{
312  // shapeLib.Data.gdc.node = shapeLib.Data.ru.Node;
313  //}
314  //throw new NotImplementedException();
315  }
316 
317  public void MouseUpUpdate(object sender, System.Windows.Input.MouseButtonEventArgs e)
318  {
319  //throw new NotImplementedException();
320  }
321 
322  public void MouseMoveUpdate(object sender, System.Windows.Input.MouseEventArgs e)
323  {
324  // throw new NotImplementedException();
325  }
326 
327 
328  public virtual void MouseDownInsert(object sender, System.Windows.Input.MouseButtonEventArgs e)
329  {
330  Canvas mygrid = shapeLib.Data.mygrid;
331 
332  shapeLib.Data.pStart = correctPoint(e.GetPosition(mygrid));
333 
334  if ((this.GetType() != typeof(ShapeCurve) && this.GetType() != typeof(ShapePencil)) || shapeLib.Data.mClick == 0)
335  {
336 
337  currPath = new gPath();
338  currPath.drawtype = shapeLib.SupportedShape(null).IndexOf(this);
339  }
341  shapeLib.Data.bCanMove = true;
342 
343  }
344 
345  public virtual void MouseUpInsert(object sender, System.Windows.Input.MouseButtonEventArgs e)
346  {
347  Canvas mygrid = shapeLib.Data.mygrid;
348 
349  if (shapeLib.Data.bCanMove)
350  {
351  shapeLib.Data.pEnd = correctPoint(e.GetPosition(mygrid));
352  double tempX, tempY;
353  double px = shapeLib.Data.pStart.X;
354  double py = shapeLib.Data.pStart.Y;
355  double ex = shapeLib.Data.pEnd.X;
356  double ey = shapeLib.Data.pEnd.Y;
357 
358  if (px == ex && py == ey) //click
359  {
360 
361  //
362  Debug.WriteLine("click");
363  remGPath(px, py, ex, ey);
364  currPath.redraw(1);
365 
366  shapeLib.Data.mClick++;
367 
368  if (this.GetType() == typeof(ShapeCurve) && shapeLib.Data.mClick >= 3)
369  {
370  currPath.drawtype = shapeLib.SupportedShape(null).IndexOf(this);
376  shapeLib.Data.mClick = 0;
377  }
378 
379 
380  foreach (gPath gp in shapeLib.Data.multiSelList)
381  {
382  gp.isSel = false;
383  }
384  if (shapeLib.Data.currShape != null)
385  shapeLib.Data.currShape.isSel = false;
386 
387  shapeLib.Data.multiSelList.Clear();
388  return;
389  }
390  if (this.GetType() == typeof(ShapeRectangle))
391  {
392  if (ex < px)
393  {
394  tempX = ex;
395  ex = px;
396  px = tempX;
397  }
398  if (ey < py)
399  {
400  tempY = ey;
401  ey = py;
402  py = tempY;
403  }
404  }
405  if (this.GetType() == typeof(ShapeCircle))
406  {
407  if (ex < px)
408  {
409  tempX = ex;
410  ex = px;
411  px = tempX;
412  }
413  if (ey < py)
414  {
415  tempY = ey;
416  ey = py;
417  py = tempY;
418  }
419  }
420  if (this.GetType() == typeof(ShapeTriangle))
421  {
422  if (ex < px)
423  {
424  tempX = px;
425  px = ex;
426  ex = tempX;
427  }
428  if (ex > px)
429  {
430  tempX = ex;
431  ex = px;
432  ex = tempX;
433  }
434  }
435 
436  remGPath(px, py, ex, ey);
437 
438  if (this.GetType() == typeof(ShapeCurve))
439  {
440  shapeLib.Data.mClick++;
441  if (shapeLib.Data.mClick >= 3)
442  shapeLib.Data.mClick = 0;
443 
444  }
445  // || shapeLib.Data.mClick >=2 )
446  {
447  currPath.drawtype = shapeLib.SupportedShape(null).IndexOf(this);
453  }
454  shapeLib.Data.gdc.bmove = false;
455  if (shapeLib.Data.Status.Equals("rest"))
456  currPath.redraw(1);
457 
458  shapeLib.Data.bfirst = true;
459  shapeLib.Data.bhave = false;
460  }
461  //throw new NotImplementedException();
462  }
463 
464  public virtual void MouseMoveInsert(object sender, System.Windows.Input.MouseEventArgs e)
465  {
466  Canvas mygrid = shapeLib.Data.mygrid;
467 
468  if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
469  {
470  // if (!shapeLib.Data.bhave) //if you can control an object
471  {
472  shapeLib.Data.pEnd = correctPoint(e.GetPosition(mygrid));
473  double tempX, tempY;
474  double px = shapeLib.Data.pStart.X;
475  double py = shapeLib.Data.pStart.Y;
476  double ex = shapeLib.Data.pEnd.X;
477  double ey = shapeLib.Data.pEnd.Y;
478 
479 
480  if (this.GetType() == typeof(ShapeRectangle))
481  {
482  if (ex < px)
483  {
484  tempX = ex;
485  ex = px;
486  px = tempX;
487  }
488  if (ey < py)
489  {
490  tempY = ey;
491  ey = py;
492  py = tempY;
493  }
494  }
495  if (this.GetType() == typeof(ShapeCircle))
496  {
497  if (ex < px)
498  {
499  tempX = ex;
500  ex = px;
501  px = tempX;
502  }
503  if (ey < py)
504  {
505  tempY = ey;
506  ey = py;
507  py = tempY;
508  }
509  }
510  if (this.GetType() == typeof(ShapeTriangle))
511  {
512  if (ex < px)
513  {
514  tempX = px;
515  px = ex;
516  ex = tempX;
517  }
518  if (ex > px)
519  {
520  tempX = ex;
521  ex = px;
522  ex = tempX;
523  }
524  }
525  remGPath(px, py, ex, ey);
526  currPath.redraw(0);
527 
528  }
529  }
530  else
531  {
532  if (this.GetType() == typeof(ShapePencil))
533  {
534 
535  }
536 
537 
538  }
539 
540  //throw new NotImplementedException();
541  }
542 
543 
544  /*-------------- 其他功能 --------------*/
545  protected void remGPath(double px, double py, double ex, double ey)
549  {
554  currPath.drawtype = shapeLib.SupportedShape(null).IndexOf(this);
555 
556  if (shapeLib.Data.ru.Sel >= 0)
558  else
560 
561  if (this.GetType() == typeof(ShapeCurve))
562  {
563 
564  switch (shapeLib.Data.mClick)
565  {
566  case 0:
567  currPath.controlBtn1 = new System.Windows.Point(px, py);
568  currPath.controlBtn4 = new System.Windows.Point(ex, ey);
569 
572 
573  break;
574  case 1:
575  currPath.controlBtn2 = new System.Windows.Point(ex, ey);
576  break;
577  case 2:
578  currPath.controlBtn3 = new System.Windows.Point(ex, ey);
579 
580  break;
581 
582 
583  }
584 
585  Debug.WriteLine(currPath.controlBtn1);
586  Debug.WriteLine(currPath.controlBtn2);
587  Debug.WriteLine(currPath.controlBtn3);
588  Debug.WriteLine(currPath.controlBtn4);
589  Debug.WriteLine(px.ToString() + "," + py.ToString() + "_______________________" + shapeLib.Data.mClick.ToString());
590  }
591  else
592  {
593  currPath.controlBtn1 = new System.Windows.Point(px, py);
594  currPath.controlBtn4 = new System.Windows.Point(ex, ey);
595  currPath.controlBtn2 = new System.Windows.Point(ex, py);
596  currPath.controlBtn3 = new System.Windows.Point(px, ey);
597 
598 
599 
600  }
601 
602  }
603 
604 
605  protected System.Windows.Point correctPoint(System.Windows.Point p)
606  {
607  int lineSpace = shapeLib.Data.lineSpace;
608  System.Windows.Point temp = p;
609  double tempDX = temp.X % lineSpace;
610  double tempDY = temp.Y % lineSpace;
611  if (temp.X % lineSpace != 0)
612  {
613  temp.X = lineSpace * Math.Round((temp.X / lineSpace), 0);
614  }
615  if (temp.Y % lineSpace != 0)
616  {
617  temp.Y = lineSpace * Math.Round((temp.Y / lineSpace), 0);
618  }
619  return temp;
620  }
621 
622  public virtual void FormKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
623  {
624 
625  Debug.Write(e.Key);
626 
627 
628  if (e.Key == Key.Delete || e.Key == Key.Back)
629  {
630  foreach (gPath gp in shapeLib.Data.multiSelList)
631  {
632  gp.isSel = false;
633  gp.IsDelete = true;
634 
635  }
636  shapeLib.Data.multiSelList.Clear();
637 
638  }
639 
640 
641  if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
642  {
643  if (e.Key == Key.C)
644  {
645  shapeLib.copy();
646  }
647  else if (e.Key == Key.V)
648  {
649  shapeLib.paste();
650  }
651  }
652 
653 
654  //throw new NotImplementedException();
655  }
656 
657  }
658 }
void MouseOP(int ntype)
Definition: ShapeObj.cs:113
System.Windows.Point correctPoint(System.Windows.Point p)
Definition: ShapeObj.cs:605
static GModel Data
Definition: shapeLib.cs:42
virtual void MouseDownInsert(object sender, System.Windows.Input.MouseButtonEventArgs e)
Definition: ShapeObj.cs:328
RibbonControlEventHandler click
Definition: ShapeObj.cs:49
List< gPath > multiSelList
Definition: shapeLib.cs:148
string belong
判斷按下Button控制項
Definition: ShapeObj.cs:53
ShapeObj Create(string svg)
從xml 生成data
Definition: ShapeObj.cs:186
System.Windows.Point controlBtn3
Definition: GraphDoc.cs:255
List< gPath > PathList
Definition: GraphDoc.cs:30
Canvas drawControl
Definition: IForm.cs:19
System.Windows.Controls.UserControl getRoot
Definition: IForm.cs:20
void remGPath(double px, double py, double ex, double ey)
儲存新繪製的圖形資料
Definition: ShapeObj.cs:545
void MouseDownUpdate(object sender, System.Windows.Input.MouseButtonEventArgs e)
Definition: ShapeObj.cs:284
virtual void finish()
Definition: ShapeObj.cs:99
string SVGString()
存檔時存生xml
Definition: ShapeObj.cs:199
void MouseUpUpdate(object sender, System.Windows.Input.MouseButtonEventArgs e)
Definition: ShapeObj.cs:317
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
void myLine_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
Definition: GraphDoc.cs:392
System.Windows.Point controlBtn2
Definition: GraphDoc.cs:254
void changeProperty(string prop, string value)
Definition: ShapeObj.cs:191
void myLine_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
Definition: GraphDoc.cs:427
void MouseMoveUpdate(object sender, System.Windows.Input.MouseEventArgs e)
Definition: ShapeObj.cs:322
virtual void FormKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
Definition: ShapeObj.cs:622
virtual void DrawShape(gView gv, gPath data, Boolean bfirst)
依data 繪製,如果是第一次畫要新建shape, 更新的話只要更新最後一點
Definition: ShapeObj.cs:207
static void copy()
Definition: shapeLib.cs:55
virtual void MouseMoveInsert(object sender, System.Windows.Input.MouseEventArgs e)
Definition: ShapeObj.cs:464
shapeUIType
建構不同UI 時使用
Definition: ShapeObj.cs:31
void redraw(int removetype)
Definition: GraphDoc.cs:286
virtual void DisplayControlPoints(gView gv, gPath data)
Definition: ShapeObj.cs:248
定義一個UI項目
Definition: ShapeObj.cs:42
static IList< ShapeObj > SupportedShape(getForm myview)
define supported shape
Definition: shapeLib.cs:27
List< Shape > baseShape
Definition: gView.cs:12
System.Windows.Point controlBtn1
Definition: GraphDoc.cs:253
void myLine_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
Definition: GraphDoc.cs:435
void btn_Click(object sender, RibbonControlEventArgs e)
UI 點選
Definition: ShapeObj.cs:107
System.Windows.Controls.UserControl Root
Definition: shapeLib.cs:122
System.Drawing.Image image
Definition: ShapeObj.cs:46
virtual System.Collections.ArrayList getMenuItem()
覆寫System.Collections.ArrayList
Definition: ShapeObj.cs:67
static void paste()
Definition: shapeLib.cs:83
List< Shape > controlShape
Definition: gView.cs:13
virtual void MouseUpInsert(object sender, System.Windows.Input.MouseButtonEventArgs e)
Definition: ShapeObj.cs:345