My Project
GraphDoc.cs
查看本檔案說明文件.
1 using ShapeLib.VShape;
2 //using Microsoft.Office.Interop.Word;
3 //using Microsoft.Office.Tools.Word;
4 using System;
5 using System.Collections;
6 using System.Collections.Generic;
7 using System.Diagnostics;
8 using System.Linq;
9 using System.Text;
10 using System.Threading.Tasks;
11 using System.Windows;
14 using System.Windows.Input;
15 using System.Windows.Media;
16 using System.Windows.Shapes;
17 using System.Xml.Serialization;
18 
21 namespace ShapeLib.VShape
22 {
25 
26  [XmlRoot(ElementName = "SVGRoot", Namespace = "")]
27  public class SVGRoot
28  {
29  [XmlElement("PathList")]
30  public List<gPath> PathList = new List<gPath>();
31  }
37  public class GraphDoc
38  {
39  public SVGRoot sroot = new SVGRoot();
40  public List<gView> shapeList = new List<gView>();
41  public List<gPath> FullList = new List<gPath>(); //remember all action from grid
42  public Stack UndoStack = new Stack();
43  public Stack RedoStack = new Stack();
44  public int selIndex = -1; // The last seat in PathList array
45  public int node = 0;
46  public int mx;
47  public int my;
48  public bool bmove;
49 
50  public int checkWhich(gPath gp)
51  {
52  int whichOne = -1;
53  for (int i = sroot.PathList.Count - 1; i >= 0; i--)
54  {
55  if (gp.drawtype < 3 || gp.drawtype == 4)
56  {
57  if (sroot.PathList[i].controlBtn1 != gp.controlBtn1)
58  continue;
59  if (sroot.PathList[i].controlBtn2 != gp.controlBtn2)
60  continue;
61  if (sroot.PathList[i].controlBtn3 != gp.controlBtn3)
62  continue;
63  if (sroot.PathList[i].controlBtn4 != gp.controlBtn4)
64  continue;
65  }
66  if (gp.drawtype == 3)
67  {
68  if (sroot.PathList[i].controlBtn1 != gp.controlBtn1)
69  continue;
70  if (sroot.PathList[i].controlBtn4 != gp.controlBtn4)
71  continue;
72  }
73  whichOne = i;
74  }
75  return whichOne;
76  }
77 
78  public void reDrawAll()
79  {
80  foreach (gPath gp in sroot.PathList)
81  {
82  if (!gp.IsDelete)
83  {
84  gp.redraw(1);
85 
86  }
87  }
88 
89  }
90 
96  public void writeIn(gPath Data, int Action)
97  {
98 
99 
100  saveState pa;
101 
102  int lens = RedoStack.Count;
103  RedoStack.Clear();
104  FullList.RemoveRange(FullList.Count - lens, lens);
105 
106 
107 
108  if (Action == 0)
109  {
112  gPath g = new gPath();
116  pa = new saveState(Action, sroot.PathList.Count, (FullList.Count));
117 
118  UndoStack.Push(pa);
119  g.copyVal(Data);
120  FullList.Add(Data);
121  sroot.PathList.Add(g);
122  }
123  else
124  {
127  pa = new saveState(Action, Data.ListPlace, FullList.Count);
128  FullList.Add(Data);
129  UndoStack.Push(pa);
130  }
131  }
132 
133 
138  public void reDo()
139  {
140  if (RedoStack.Count > 0)
141  {
142  gPath tempPath = new gPath();
143  saveState tempPA;
144 
145  tempPA = (saveState)RedoStack.Pop();
146 
147  if (tempPA.currSate >= 0 && tempPA.currSate < FullList.Count)
148  {
149  tempPath.copyVal(FullList[tempPA.currSate]);
150 
151 
152  if (tempPA.Action == 0)
153  sroot.PathList[tempPA.GraphIndex].IsDelete = false;
154 
155  sroot.PathList[tempPA.GraphIndex] = tempPath;
156  sroot.PathList[tempPA.GraphIndex].redraw(1);
157 
158  }
159  else
160  {
161  // sroot.PathList.RemoveAt(tempPA.GraphIndex);
162  }
163  UndoStack.Push(tempPA);
164 
165  }
166  }
167 
171  public void unDo()
172  {
176  if (UndoStack.Count > 0)
177  {
178  gPath tempPath = new gPath();
179  saveState tempPA;
180  shapeLib.Data.mClick = 0;
181 
182  tempPA = (saveState)UndoStack.Pop();
183 
184  if (tempPA.currSate >= 0 && tempPA.currSate < FullList.Count)
185  {
186  if (tempPA.Action == 0)
187  {
188 
189  sroot.PathList[tempPA.GraphIndex].IsDelete = true;
190 
191  }
192  else
193  {
197  int i;
198  for (i = tempPA.currSate - 1; i >= 0; i--)
199  {
200  if (FullList[i].ListPlace == tempPA.GraphIndex)
201  {
202  tempPath.copyVal(FullList[i]);
203  sroot.PathList[tempPA.GraphIndex] = tempPath;
204  sroot.PathList[tempPA.GraphIndex].redraw(1);
205  break;
206 
207  }
208  }
209  if (i < 0) //something wrong
210  {
211  Debug.WriteLine("something wrong");
212  }
213 
214  }
215 
216  }
217  else
218  {
219 
220  //something wrong
221  Debug.WriteLine("something wrong");
222 
223  //sroot.PathList.RemoveAt(tempPA.GraphIndex);
224  }
228  RedoStack.Push(tempPA);
229 
230  }
231  }
232 
233  public void Release()
234  {
235  this.RedoStack.Clear();
236  }
237  }
238 
239  [Serializable]
240  public struct gPro
241  {
242  public byte colorR;
243  public byte colorG;
244  public byte colorB;
245  public int strokeT;
246  }
247  [Serializable]
248  public class gPath
249  {
250  public int drawtype;
251  public gPro state;
252  public int ListPlace;
253  public System.Windows.Point controlBtn1;
254  public System.Windows.Point controlBtn2;
255  public System.Windows.Point controlBtn3;
256  public System.Windows.Point controlBtn4;
257  public String Text = "";
258  bool _isSel;
259  public List<Point> pList = new List<Point>();
260  public bool isSel
261  {
262  get
263  {
264  return _isSel;
265  }
266  set
267  {
268 
269  if (value != _isSel)
270  {
271  _isSel = value;
272  if (!value)
273  redraw(2);
274  else
275  redraw(1);
276  }
277 
278  }
279  }
280  private int shapeIndex = -1;
281 
286  public void redraw(int removetype)
287  {
288  gView gv = null;
289  Boolean bfirst = false;
290 
291  if (shapeIndex < 0)
292  {
293  gv = new gView();
295  shapeLib.Data.gdc.shapeList.Add(gv);
296  bfirst = true;
297  }
298  else
300 
301 
302  if (isSel)
303  {
304  foreach (Shape sp in gv.baseShape)
305  shapeLib.Data.mygrid.Children.Remove(sp);
306  shapeLib.SupportedShape(null)[drawtype].DisplayControlPoints(gv, this);
307 
308  }
309  else
310  {
311  foreach (Shape sp in gv.controlShape)
312  shapeLib.Data.mygrid.Children.Remove(sp);
313  gv.controlShape.Clear();
314 
315  switch (removetype)
316  {
317  case -1:
318  foreach (Shape sp in gv.baseShape)
319  shapeLib.Data.mygrid.Children.Remove(sp);
320  break;
321 
322 
323  case 2:
324  foreach (Shape sp in gv.baseShape)
325  shapeLib.Data.mygrid.Children.Add(sp);
326  shapeLib.SupportedShape(null)[drawtype].DrawShape(gv, this, bfirst);
327  break;
328  case 0:
329  case 1:
330  shapeLib.SupportedShape(null)[drawtype].DrawShape(gv, this, bfirst);
331 
332  break;
333 
334  }
335  }
336  }
337 
338  // public Shape getDrawShape()
339  // {
340  // if (shapeIndex >= 0 && shapeIndex < shapeLib.Data.gdc.shapeList.Count)
341  // {
342  // Shape ishape = shapeLib.Data.gdc.shapeList[shapeIndex];
343  // return ishape;
344  // }
345  // return null;
346 
347  // }
348 
349  //public void setDrawShape(Shape value)
350  // {
351  // shapeLib.Data.gdc.shapeList.Add(value);
352  // shapeIndex = shapeLib.Data.gdc.shapeList.Count - 1;
353  // }
354 
355 
356  private bool isdel = false;
357  public bool IsDelete
358  {
359  get
360  {
361 
362 
363  return isdel;
364  //throw new NotImplementedException();
365  }
366  set
367  {
368  if (value == true)
369  {
370  this.redraw(-1);
371 
372 
373 
374  }
375  else
376  {
377  if (isdel)
378  {
379  this.redraw(2);
380 
381 
382  }
383 
384 
385  }
386  isdel = true;
387 
388 
389  // throw new NotImplementedException();
390  }
391  }
392  public void myLine_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
393  {
394  if (shapeLib.Data.UItype < 0)
395  {
396  if (isSel)
397  {
398  double gapX, gapY, x, y;
399  shapeLib.Data.mygrid.Cursor = Cursors.SizeAll;
400  gapX = Math.Abs(shapeLib.Data.currShape.controlBtn4.X + shapeLib.Data.currShape.controlBtn1.X) / 2.0;
401  gapY = Math.Abs(shapeLib.Data.currShape.controlBtn4.Y + shapeLib.Data.currShape.controlBtn1.Y) / 2.0;
402  x = e.GetPosition(shapeLib.Data.mygrid).X;
403  y = e.GetPosition(shapeLib.Data.mygrid).Y;
412  //if (this.Equals(typeof(ShapeRectangle)))
413  //{
414  // this.controlBtn1 = e.GetPosition(shapeLib.Data.mygrid);
415  //}
416  }
417  else
418  {
419  shapeLib.Data.mygrid.Cursor = Cursors.Hand;
420 
421  }
422 
423  }
424  // throw new NotImplementedException();
425  }
426 
427  public void myLine_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
428  {
429  shapeLib.Data.mygrid.Cursor = Cursors.Arrow;
430 
431  // throw new NotImplementedException();
432  }
433 
434 
435  public void myLine_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
436  {
437  if (shapeLib.Data.UItype < 0)
438  {
442  if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
443  {
444  shapeLib.Data.multiSelList.Add(this);
445 
446  }
447  else
448  {
449  foreach (gPath gp in shapeLib.Data.multiSelList)
450  {
451  gp.isSel = false;
452  }
453  if (shapeLib.Data.currShape != null && shapeLib.Data.currShape != this)
454  shapeLib.Data.currShape.isSel = false;
455  shapeLib.Data.multiSelList.Clear();
456  shapeLib.Data.multiSelList.Add(this);
457  }
458 
459  shapeLib.Data.currShape = this;
460  this.isSel = true;
461 
462  IInsertOP sh = shapeLib.SupportedShape(null)[this.drawtype];
463  sh.MouseOP(1);
464 
465  e.Handled = true;
466  }
467  //throw new NotImplementedException();
468  }
469 
470 
471  public void copyVal(gPath obj)
472  {
473 
474  drawtype = obj.drawtype;
475  state = obj.state;
476  ListPlace = obj.ListPlace;
477  shapeIndex = obj.shapeIndex;
478 
479  controlBtn1 = obj.controlBtn1;
480  controlBtn2 = obj.controlBtn2;
481  controlBtn3 = obj.controlBtn3;
482  controlBtn4 = obj.controlBtn4;
483 
484  foreach (Point p in obj.pList)
485  {
486  pList.Add(p);
487  }
488  }
489 
490  }
491 
492  public class RUse
493  {
494  public int Sel = -1;
495  public int Node = -1;
496  public System.Windows.Point Point;
497  }
498 
499  public class gPoint
500  {
501  public System.Windows.Point mouseXY;
502 
503  public System.Windows.Point point0;
504  public System.Windows.Point point1;
505  public System.Windows.Point point2;
506  public System.Windows.Point point3;
507  }
511  public class saveState
512  {
516  public int Action;
520  public int GraphIndex;
524 
525  //int preSate;
526 
530  public int currSate;
531 
532 
533 
534  public saveState()
535  {
536  Action = -1;
537  GraphIndex = -1;
538  currSate = -1;
539  //changeP = 0;
540  //lastP = -1;
541  //leastP = 0;
542 
543  }
544  public saveState(int a, int b, int c)
545  {
546  Action = a;
547  GraphIndex = b;
548  currSate = c;
549  }
550 
551  }
552 
553  public class checkHitDraw
554  {
555  public int checkHitWhich(List<gPath> l, gPoint gp, int drawType)
556  {
557  int whichOne = -1;
558  for (int i = l.Count - 1; i >= 0; i--)
559  {
560  if (drawType < 3 || drawType == 4)
561  {
562  if (l[i].controlBtn1 != gp.point0)
563  continue;
564  if (l[i].controlBtn2 != gp.point1)
565  continue;
566  if (l[i].controlBtn3 != gp.point2)
567  continue;
568  if (l[i].controlBtn4 != gp.point3)
569  continue;
570  }
571  if (drawType == 3)
572  {
573  if (l[i].drawtype != drawType)
574  continue;
575  if (l[i].controlBtn1 != gp.point0)
576  continue;
577  if (l[i].controlBtn4 != gp.point3)
578  continue;
579  }
580  whichOne = i;
581  }
582  return whichOne;
583  }
584 
585  public bool checkHitEllipse(gPoint p)
586  {
587  bool tf = false;
588  double c_x = (p.point1.X - p.point0.X) / 2;
589  double c_y = (p.point2.Y - p.point0.Y) / 2;
590  System.Windows.Point center = new System.Windows.Point(p.point0.X + c_x, p.point0.Y + c_y);
591 
592  double simpleX = Math.Sqrt((1 - Math.Pow((p.mouseXY.Y - center.Y), 2) / Math.Pow(c_y, 2)) * Math.Pow(c_x, 2));
593  double simpleY = Math.Sqrt((1 - Math.Pow((p.mouseXY.X - center.X), 2) / Math.Pow(c_x, 2)) * Math.Pow(c_y, 2));
594  double higherPlaceX = center.X + simpleX;
595  double lowerPlaceX = center.X - simpleX;
596  double higherPlaceY = center.Y + simpleY;
597  double lowerPlaceY = center.Y - simpleY;
598 
599  if (p.mouseXY.X <= higherPlaceX && p.mouseXY.X >= higherPlaceX - 3)
600  tf = true;
601  if (p.mouseXY.X >= lowerPlaceX && p.mouseXY.X <= lowerPlaceX + 3)
602  tf = true;
603  if (p.mouseXY.Y <= higherPlaceY && p.mouseXY.Y >= higherPlaceY - 3)
604  tf = true;
605  if (p.mouseXY.Y >= lowerPlaceY && p.mouseXY.Y <= lowerPlaceY + 3)
606  tf = true;
607 
608  return tf;
609  }
610 
611  public bool checkHitRect(gPoint p)
612  {
613  bool tf = false;
614  if ((p.mouseXY.X <= p.point1.X + 3 && p.mouseXY.X >= p.point1.X - 3) || (p.mouseXY.X >= p.point0.X - 3 && p.mouseXY.X <= p.point0.X + 3))
615  {
616  if (p.mouseXY.Y <= p.point2.Y && p.mouseXY.Y >= p.point0.Y)
617  {
618  tf = true;
619  }
620  }
621  if ((p.mouseXY.Y <= p.point2.Y + 3 && p.mouseXY.Y >= p.point2.Y - 3) || (p.mouseXY.Y >= p.point0.Y - 3 && p.mouseXY.Y <= p.point0.Y + 3))
622  {
623  if (p.mouseXY.X <= p.point1.X && p.mouseXY.X >= p.point0.X)
624  {
625  tf = true;
626  }
627  }
628  return tf;
629  }
630 
631  public bool checkHitLine(System.Windows.Point downPlace, gPath p)
632  {
633  bool tf = false;
634  double m = (p.controlBtn4.Y - p.controlBtn1.Y) / (p.controlBtn4.X - p.controlBtn1.X);
635  double xm = (downPlace.Y - p.controlBtn1.Y) / m + p.controlBtn1.X;
636  double ym = (downPlace.X - p.controlBtn1.X) * m + p.controlBtn1.Y;
637  if (downPlace.X >= xm - 3 && downPlace.X <= xm + 3)
638  tf = true;
639  if (downPlace.Y >= ym - 3 && downPlace.Y <= ym + 3)
640  tf = true;
641  return tf;
642  }
643 
644  public bool checkHitCurve(String Data, gPath p)
645  {
646  bool tf = true;
647  String[] tmpStr = Data.Split(',');
648  double[] tmpDouStr = new double[tmpStr.Length];
649  for (int i = 0; i < tmpStr.Length; i++)
650  {
651  tmpDouStr[i] = Convert.ToDouble(tmpStr[i]);
652  }
653  System.Windows.Point tmpPoint0 = new System.Windows.Point(tmpDouStr[0], tmpDouStr[1]);
654  System.Windows.Point tmpPoint1 = new System.Windows.Point(tmpDouStr[2], tmpDouStr[3]);
655  System.Windows.Point tmpPoint2 = new System.Windows.Point(tmpDouStr[4], tmpDouStr[5]);
656  System.Windows.Point tmpPoint3 = new System.Windows.Point(tmpDouStr[6], tmpDouStr[7]);
657 
658  if (!tmpPoint0.Equals(p.controlBtn1))
659  tf = false;
660  if (!tmpPoint1.Equals(p.controlBtn2))
661  tf = false;
662  if (!tmpPoint2.Equals(p.controlBtn3))
663  tf = false;
664  if (!tmpPoint3.Equals(p.controlBtn4))
665  tf = false;
666  return tf;
667  }
668 
669  public int checkHitCorner(System.Windows.Point downPlace, gPath p)
670  {
671  int Node = -1;
672  if ((downPlace.X >= p.controlBtn1.X - 4) && (downPlace.X <= p.controlBtn1.X + 4) && (downPlace.Y >= p.controlBtn1.Y - 4) && (downPlace.Y <= p.controlBtn1.Y + 4))
673  {
674  Node = 0;
675  }
676  if ((downPlace.X >= p.controlBtn2.X - 4) && (downPlace.X <= p.controlBtn2.X + 4) && (downPlace.Y >= p.controlBtn2.Y - 4) && (downPlace.Y <= p.controlBtn2.Y + 4))
677  {
678  Node = 1;
679  }
680  if ((downPlace.X >= p.controlBtn3.X - 4) && (downPlace.X <= p.controlBtn3.X + 4) && (downPlace.Y >= p.controlBtn3.Y - 4) && (downPlace.Y <= p.controlBtn3.Y + 4))
681  {
682  Node = 2;
683  }
684  if ((downPlace.X >= p.controlBtn4.X - 4) && (downPlace.X <= p.controlBtn4.X + 4) && (downPlace.Y >= p.controlBtn4.Y - 4) && (downPlace.Y <= p.controlBtn4.Y + 4))
685  {
686  Node = 3;
687  }
688  return Node;
689  }
690 
691  public bool checkHitCenter(System.Windows.Point downPlace, gPath p)
692  {
693  bool tf = true;
694  if (downPlace.X > p.controlBtn2.X || downPlace.X < p.controlBtn1.X)
695  tf = false;
696  if (downPlace.Y > p.controlBtn4.Y || downPlace.Y < p.controlBtn1.Y)
697  tf = false;
698  return tf;
699  }
700  }
701 
702 }
static GModel Data
Definition: shapeLib.cs:42
System.Windows.Point point3
Definition: GraphDoc.cs:506
List< gPath > FullList
Definition: GraphDoc.cs:41
List< gPath > multiSelList
Definition: shapeLib.cs:148
System.Windows.Point controlBtn3
Definition: GraphDoc.cs:255
List< gPath > PathList
Definition: GraphDoc.cs:30
bool checkHitCenter(System.Windows.Point downPlace, gPath p)
Definition: GraphDoc.cs:691
int checkHitWhich(List< gPath > l, gPoint gp, int drawType)
Definition: GraphDoc.cs:555
System.Windows.Point Point
Definition: GraphDoc.cs:496
List< gView > shapeList
Definition: GraphDoc.cs:40
System.Windows.Point point1
Definition: GraphDoc.cs:504
System.Windows.Point point0
Definition: GraphDoc.cs:503
System.Windows.Point point2
Definition: GraphDoc.cs:505
void writeIn(gPath Data, int Action)
維護 undo stack ,把目前狀態存起來.並清空redo stack,如果之前有undo 動作,是回覆到某一狀態,在此之後的動作都可清除
Definition: GraphDoc.cs:96
System.Windows.Point controlBtn4
Definition: GraphDoc.cs:256
bool checkHitLine(System.Windows.Point downPlace, gPath p)
Definition: GraphDoc.cs:631
void myLine_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
Definition: GraphDoc.cs:392
System.Windows.Point controlBtn2
Definition: GraphDoc.cs:254
int Action
0: insert, 1:update, 2:delete
Definition: GraphDoc.cs:516
void myLine_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
Definition: GraphDoc.cs:427
為了維護undo redo, 系統任何操作必需把狀態存起來,
Definition: GraphDoc.cs:511
void redraw(int removetype)
Definition: GraphDoc.cs:286
int GraphIndex
目前圖形串列中的第幾個,為必免順序改變,凡加入的就一直存在(data list)
Definition: GraphDoc.cs:520
記錄shape list,action data stack 記錄動作,每個動作(pointAry)包含,該圖是圖形的第幾個(Listplace),之前記錄是否己有相同圖是第幾個,...
Definition: GraphDoc.cs:37
System.Windows.Point mouseXY
Definition: GraphDoc.cs:501
static IList< ShapeObj > SupportedShape(getForm myview)
define supported shape
Definition: shapeLib.cs:27
bool checkHitCurve(String Data, gPath p)
Definition: GraphDoc.cs:644
void MouseOP(int ntype)
List< Point > pList
Definition: GraphDoc.cs:259
List< Shape > baseShape
Definition: gView.cs:12
void copyVal(gPath obj)
Definition: GraphDoc.cs:471
System.Windows.Point controlBtn1
Definition: GraphDoc.cs:253
bool checkHitEllipse(gPoint p)
Definition: GraphDoc.cs:585
void unDo()
undo 回到前一狀態
Definition: GraphDoc.cs:171
bool checkHitRect(gPoint p)
Definition: GraphDoc.cs:611
void myLine_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
Definition: GraphDoc.cs:435
saveState(int a, int b, int c)
Definition: GraphDoc.cs:544
int checkWhich(gPath gp)
Definition: GraphDoc.cs:50
int checkHitCorner(System.Windows.Point downPlace, gPath p)
Definition: GraphDoc.cs:669
void reDo()
重作到目前狀態
Definition: GraphDoc.cs:138
List< Shape > controlShape
Definition: gView.cs:13
int currSate
操作前的狀態 fullList 中,操作前該物件所在位置,即最後一個狀態.至少會有新增的狀態.以GraphIndex 去找,其實可以不用記錄
Definition: GraphDoc.cs:530