123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Ink;
- using System.Windows.Input;
- using System.Windows.Media;
-
- namespace Common.system
- {
- internal enum ZPenType : byte
- {
- Pen = 1,
- Erase = 2
- };
-
- internal class ZBBPage
- {
- public List<ZBBPageStep> lines { get; set; }
- public List<ZBBPageStep> lines_histoty { get; set; }
-
- public ZBBPage()
- {
- lines = new List<ZBBPageStep>();
- lines_histoty = new List<ZBBPageStep>();
- }
- }
-
- internal class ZBBPageStep
- {
- public StrokeCollection lines_curr { get; set; }
- public StrokeCollection lines_add { get; set; }
- public StrokeCollection lines_remove { get; set; }
-
- public ZBBPageStep()
- {
- lines_curr = new StrokeCollection();
- lines_add = new StrokeCollection();
- lines_remove = new StrokeCollection();
- }
- }
- public class BlackboardNew
- {
- private InkCanvas m_canvas;
-
- //private ZPenType type = ZPenType.Pen;
- private int pagenum = 0;
-
- private readonly int erasesize = 64;
- private readonly int pensize = 3;
- private int undoOrRedo = 0; //是否在进行撤销恢复操作
- private Color pencolor;
-
- private readonly List<ZBBPage> strokes_page_all = new List<ZBBPage>();
-
- // 添加这个变量是因为在用橡皮擦时 一次操作会触发多次StrokesChanged回掉 这里是把多次回掉合并在一起
- private ZBBPageStep step = null;
-
- public BlackboardNew(InkCanvas canvas)
- {
- init(canvas, Colors.White);
- }
-
- public BlackboardNew(InkCanvas canvas, Color _pencolor)
- {
- init(canvas, _pencolor);
- }
- DrawingAttributes drawingAttributes;
- private void init(InkCanvas canvas, Color _pencolor)
- {
- m_canvas = canvas;
- pencolor = _pencolor;
- ZBBPage page = new ZBBPage();
- page.lines.Add(new ZBBPageStep());
- strokes_page_all.Add(page);
- if (canvas != null)
- {
- canvas.EditingMode = InkCanvasEditingMode.Ink;
- drawingAttributes = new DrawingAttributes();
- canvas.DefaultDrawingAttributes = drawingAttributes;
- drawingAttributes.Width = pensize;
- drawingAttributes.Height = pensize;
- drawingAttributes.Color = pencolor;
- drawingAttributes.FitToCurve = true;
- drawingAttributes.IgnorePressure = false;
- canvas.Strokes.StrokesChanged += Strokes_StrokesChanged;
- canvas.StrokeCollected += Canvas_StrokeCollected;
- canvas.StrokeErasing += Canvas_StrokeErasing;
- canvas.StrokeErased += Canvas_StrokeErased;
- canvas.MouseUp += Canvas_MouseUp;
- }
- }
-
- private void Canvas_StrokeErasing(object sender, InkCanvasStrokeErasingEventArgs e)
- {
- undoOrRedo = 0;
- }
-
- private void Canvas_StrokeErased(object sender, RoutedEventArgs e)
- {
- }
-
- private void Canvas_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
- {
- }
-
- private void Canvas_MouseUp(object sender, MouseButtonEventArgs e)
- {
- if (step != null)
- {
- ZBBPage page = strokes_page_all[pagenum];
- if (page != null)
- {
- step.lines_curr.Add(m_canvas.Strokes);
- page.lines.Add(step);
- step = null;
- }
- }
- }
-
- private void Strokes_StrokesChanged(object sender, StrokeCollectionChangedEventArgs e)
- {
- if (undoOrRedo > 0)
- {
- undoOrRedo -= 1;
- return;
- }
-
- if (step == null)
- {
- step = new ZBBPageStep();
- }
-
- // 笔模式
- if (e.Added.Count > 0 && e.Removed.Count == 0)
- {
- step.lines_add.Add(e.Added);
- }
- // 橡皮模式 会多次进入回掉
- else if (e.Removed.Count > 0)
- {
- step.lines_add.Add(e.Added);
- for (int i = 0; i < e.Removed.Count; i++)
- {
- Stroke removeItem = e.Removed[i];
- if (step.lines_add.Contains(removeItem))
- {
- step.lines_add.Remove(removeItem);
- }
- else
- {
- step.lines_remove.Add(removeItem);
- }
- }
- }
- }
-
- // public方法 笔
- public void change_pen(Color _color)
- {
- //this.type = ZPenType.Pen;
- DrawingAttributes drawingAttributes = new DrawingAttributes();
- m_canvas.DefaultDrawingAttributes = drawingAttributes;
- drawingAttributes.Color = _color;
- drawingAttributes.Width = pensize;
- drawingAttributes.Height = pensize;
- drawingAttributes.FitToCurve = true;
- drawingAttributes.IgnorePressure = false;
- m_canvas.EditingMode = InkCanvasEditingMode.Ink;
- }
-
- // 橡皮
- public void change_erase()
- {
- //this.type = ZPenType.Erase;
- m_canvas.EditingMode = InkCanvasEditingMode.EraseByPoint;
- m_canvas.EraserShape = new EllipseStylusShape(erasesize, erasesize, 0);
- }
-
- // 撤销
- public void undo()
- {
- ZBBPage page = strokes_page_all[pagenum];
-
- if (page != null && m_canvas.Strokes.Count > 0 && page.lines.Count > 1)
- {
- ZBBPageStep last = page.lines.Last();
- page.lines.Remove(last);
- page.lines_histoty.Add(last);
- if (page.lines.Last().lines_curr.Count > 0)
- {
- undoOrRedo = 2;
- }
- else
- {
- undoOrRedo = 1;
- }
-
- m_canvas.Strokes.Clear();
- m_canvas.Strokes.Add(page.lines.Last().lines_curr);
- }
- }
-
- // 恢复
- public void redo()
- {
- ZBBPage page = strokes_page_all[pagenum];
- if (page != null && page.lines_histoty.Count > 0)
- {
- ZBBPageStep line = page.lines_histoty[page.lines_histoty.Count - 1];
-
- page.lines.Add(line);
- page.lines_histoty.Remove(line);
- if (page.lines.Last().lines_curr.Count > 0)
- {
- undoOrRedo = 2;
- }
- else
- {
- undoOrRedo = 1;
- }
- m_canvas.Strokes.Clear();
- m_canvas.Strokes.Add(page.lines.Last().lines_curr);
- }
- }
-
- // 清空
- public void clear()
- {
-
- ZBBPage page = new ZBBPage();
- page.lines.Add(new ZBBPageStep());
- page.lines = new List<ZBBPageStep>();
- page.lines_histoty = new List<ZBBPageStep>();
- strokes_page_all.Add(page);
-
-
- //ZBBPage page = strokes_page_all[pagenum];
- // if (page != null)
- //{
- // m_canvas.Strokes.Clear();
- // page.lines_histoty.Clear();
- // page.lines.Clear();
- // page.lines.Add(new ZBBPageStep());
- //}
- }
-
- public void changepage(int mpagenum)
- {
- if (pagenum != mpagenum)
- {
- pagenum = mpagenum;
- if (pagenum >= strokes_page_all.Count)
- {
- int numadd = pagenum - strokes_page_all.Count + 1;
- for (int i = 0; i < numadd; i++)
- {
- ZBBPage pagetemp = new ZBBPage();
- pagetemp.lines.Add(new ZBBPageStep());
- strokes_page_all.Add(pagetemp);
- }
- }
-
- ZBBPage page = strokes_page_all[pagenum];
- if (page != null&& page.lines.Count>0)
- {
- if (page.lines.Last().lines_curr.Count > 0)
- {
- undoOrRedo += 1;
- }
- if (m_canvas.Strokes.Count > 0)
- {
- undoOrRedo += 1;
- m_canvas.Strokes.Clear();
- }
-
-
-
-
- StrokeCollection strokes = new StrokeCollection();
- StylusPointCollection stylusPoints = new StylusPointCollection();
- System.Windows.Input.StylusPointDescription stylusPointDescription = new StylusPointDescription();
- StylusPoint stylusPoint = new StylusPoint();
- stylusPoint.X = 580;
- stylusPoint.Y = 212;
- stylusPoints.Add(stylusPoint);
- Stroke stroke = new Stroke(stylusPoints);
-
- stylusPoint.X = 581;
- stylusPoint.Y = 213;
- stylusPoints.Add(stylusPoint);
- stroke = new Stroke(stylusPoints);
- page.lines.Last().lines_curr.Add(stroke);
- m_canvas.Strokes.Add(page.lines.Last().lines_curr);
- //stylusPoint.X = 586;
- //stylusPoint.Y = 214;
- //stylusPoints.Add(stylusPoint);
- //stroke = new Stroke(stylusPoints);
-
- //stylusPoint.X = 599;
- //stylusPoint.Y = 214;
- //stylusPoints.Add(stylusPoint);
- //stroke = new Stroke(stylusPoints);
- //stylusPoint.X = 623;
- //stylusPoint.Y = 214;
- //stylusPoints.Add(stylusPoint);
- //stroke = new Stroke(stylusPoints);
-
- //stylusPoint.X = 663;
- //stylusPoint.Y = 214;
- //stylusPoints.Add(stylusPoint);
- //stroke = new Stroke(stylusPoints);
- //stylusPoint.X = 963;
- //stylusPoint.Y = 214;
- //stylusPoints.Add(stylusPoint);
- //stroke = new Stroke(stylusPoints);
- //DrawingAttributes drawingAttributes = new DrawingAttributes();
- //m_canvas.DefaultDrawingAttributes = drawingAttributes;
- //drawingAttributes.Color = Colors.Red;
- //drawingAttributes.Width = pensize;
- //drawingAttributes.Height = pensize;
- //drawingAttributes.FitToCurve = true;
- //drawingAttributes.IgnorePressure = false;
- //stroke.DrawingAttributes = drawingAttributes;
- //m_canvas.Strokes.Add(stroke);
- }
- }
- }
- StylusPointCollection stylusPoints = new StylusPointCollection();
- StylusPoint stylusPoint = new StylusPoint();
- Stroke stroke;
- //bool isFirst = true;
- //public void changepages(double _x, double _y, bool _new, Color _color, int _size)
- //{
- // if (_new)
- // {
- // if (stroke != null && stroke.StylusPoints.Count > 1)
- // {
- // drawingAttributes = new DrawingAttributes();
- // //m_canvas.DefaultDrawingAttributes = drawingAttributes;
- // drawingAttributes.Color = _color;
- // drawingAttributes.Width = _size;
- // drawingAttributes.Height = _size;
- // drawingAttributes.FitToCurve = true;
- // drawingAttributes.IgnorePressure = false;
- // stroke.DrawingAttributes = drawingAttributes;
- // m_canvas.Strokes.Add(stroke);
- // isFirst = true;
- // }
-
- // stylusPoints = new StylusPointCollection();
- // stylusPoint = new StylusPoint();
- // //stroke = new Stroke(stylusPoints);
- // stroke = null;
- // }
- // else
- // {
- // if(isFirst)
- // {
- // stylusPoint.X = _x;
- // stylusPoint.Y = _y;
- // stylusPoints.Add(stylusPoint);
- // if (stylusPoints.Count>1)
- // {
- // stroke = new Stroke(stylusPoints);
- // m_canvas.Strokes.Add(stroke);
- // isFirst = false;
- // }
- // }
- // else
- // {
- // stylusPoint.X = _x;
- // stylusPoint.Y = _y;
- // stylusPoints.Add(stylusPoint);
- // stroke = new Stroke(stylusPoints);
- // m_canvas.Strokes[m_canvas.Strokes.Count - 1].StylusPoints.Add(stylusPoints);
- // }
- // }
- //}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void changepages(double _x, double _y, bool _new, Color _color, int _size)
- {
- if (_new)
- {
- if (stroke != null && stroke.StylusPoints.Count > 1)
- {
- drawingAttributes = new DrawingAttributes();
- //m_canvas.DefaultDrawingAttributes = drawingAttributes;
- drawingAttributes.Color = _color;
- drawingAttributes.Width = _size*5;
- drawingAttributes.Height = _size*5;
- drawingAttributes.FitToCurve = true;
- drawingAttributes.IgnorePressure = false;
-
- stroke.DrawingAttributes = drawingAttributes;
- //m_canvas.DefaultDrawingAttributes= drawingAttributes;
- m_canvas.Strokes.Add(stroke);
- }
-
- stylusPoints = new StylusPointCollection();
- stylusPoint = new StylusPoint();
- //stroke = new Stroke(stylusPoints);
- //stroke = null;
- }
- else
- {
- //stylusPoints = new StylusPointCollection();
- //stylusPoint = new StylusPoint();
- stylusPoint.X = _x;
- stylusPoint.Y = _y;
- stylusPoints.Add(stylusPoint);
- stroke = new Stroke(stylusPoints);
- }
- }
- ////声明一个 DrawingAttributes 类型的变量
- //DrawingAttributes drawingAttributes;
- //public void changepaget(double _x, double _y, bool _new,InkCanvas canvas)
- //{
- // if (_new)
- // {
- // if (stroke != null && stroke.StylusPoints.Count > 1)
- // {
- // //m_canvas.Strokes.Add(stroke);
- // DrawingAttributes drawingAttributes = new DrawingAttributes();
- // canvas.DefaultDrawingAttributes = drawingAttributes;
- // drawingAttributes.Width = pensize;
- // drawingAttributes.Height = pensize;
- // drawingAttributes.Color = Colors.Red;
- // drawingAttributes.FitToCurve = true;
- // drawingAttributes.IgnorePressure = false;
- // canvas.DefaultDrawingAttributes = drawingAttributes;
- // canvas.Strokes.Add(stroke);
-
- // ZBBPage page = strokes_page_all[pagenum];
- // if (page != null)
- // {
- // step.lines_curr.Add(m_canvas.Strokes);
- // page.lines.Add(step);
- // step = null;
- // }
- // }
-
- // stylusPoints = new StylusPointCollection();
- // stylusPoint = new StylusPoint();
- // //stroke = new Stroke(stylusPoints);
- // stroke = null;
- // }
- // else
- // {
- // change_pen(Colors.Red);
- // //stylusPoints = new StylusPointCollection();
- // //stylusPoint = new StylusPoint();
- // stylusPoint.X = _x;
- // stylusPoint.Y = _y;
- // stylusPoints.Add(stylusPoint);
- // stroke = new Stroke(stylusPoints);
- // //m_canvas.
-
- // //page.lines.Last().lines_curr
-
- // }
-
-
- // //page.lines.Last().lines_curr.Add();
- // //if (stroke.StylusPoints.Count > 20)
- // //{
-
-
-
-
-
- //}
- }
- }
|