星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Ink;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. namespace Common.system
  12. {
  13. internal enum ZPenType : byte
  14. {
  15. Pen = 1,
  16. Erase = 2
  17. };
  18. internal class ZBBPage
  19. {
  20. public List<ZBBPageStep> lines { get; set; }
  21. public List<ZBBPageStep> lines_histoty { get; set; }
  22. public ZBBPage()
  23. {
  24. lines = new List<ZBBPageStep>();
  25. lines_histoty = new List<ZBBPageStep>();
  26. }
  27. }
  28. internal class ZBBPageStep
  29. {
  30. public StrokeCollection lines_curr { get; set; }
  31. public StrokeCollection lines_add { get; set; }
  32. public StrokeCollection lines_remove { get; set; }
  33. public ZBBPageStep()
  34. {
  35. lines_curr = new StrokeCollection();
  36. lines_add = new StrokeCollection();
  37. lines_remove = new StrokeCollection();
  38. }
  39. }
  40. public class BlackboardNew
  41. {
  42. private InkCanvas m_canvas;
  43. //private ZPenType type = ZPenType.Pen;
  44. private int pagenum = 0;
  45. private readonly int erasesize = 64;
  46. private readonly int pensize = 3;
  47. private int undoOrRedo = 0; //是否在进行撤销恢复操作
  48. private Color pencolor;
  49. private readonly List<ZBBPage> strokes_page_all = new List<ZBBPage>();
  50. // 添加这个变量是因为在用橡皮擦时 一次操作会触发多次StrokesChanged回掉 这里是把多次回掉合并在一起
  51. private ZBBPageStep step = null;
  52. public BlackboardNew(InkCanvas canvas)
  53. {
  54. init(canvas, Colors.White);
  55. }
  56. public BlackboardNew(InkCanvas canvas, Color _pencolor)
  57. {
  58. init(canvas, _pencolor);
  59. }
  60. DrawingAttributes drawingAttributes;
  61. private void init(InkCanvas canvas, Color _pencolor)
  62. {
  63. m_canvas = canvas;
  64. pencolor = _pencolor;
  65. ZBBPage page = new ZBBPage();
  66. page.lines.Add(new ZBBPageStep());
  67. strokes_page_all.Add(page);
  68. if (canvas != null)
  69. {
  70. canvas.EditingMode = InkCanvasEditingMode.Ink;
  71. drawingAttributes = new DrawingAttributes();
  72. canvas.DefaultDrawingAttributes = drawingAttributes;
  73. drawingAttributes.Width = pensize;
  74. drawingAttributes.Height = pensize;
  75. drawingAttributes.Color = pencolor;
  76. drawingAttributes.FitToCurve = true;
  77. drawingAttributes.IgnorePressure = false;
  78. canvas.Strokes.StrokesChanged += Strokes_StrokesChanged;
  79. canvas.StrokeCollected += Canvas_StrokeCollected;
  80. canvas.StrokeErasing += Canvas_StrokeErasing;
  81. canvas.StrokeErased += Canvas_StrokeErased;
  82. canvas.MouseUp += Canvas_MouseUp;
  83. }
  84. }
  85. private void Canvas_StrokeErasing(object sender, InkCanvasStrokeErasingEventArgs e)
  86. {
  87. undoOrRedo = 0;
  88. }
  89. private void Canvas_StrokeErased(object sender, RoutedEventArgs e)
  90. {
  91. }
  92. private void Canvas_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
  93. {
  94. }
  95. private void Canvas_MouseUp(object sender, MouseButtonEventArgs e)
  96. {
  97. if (step != null)
  98. {
  99. ZBBPage page = strokes_page_all[pagenum];
  100. if (page != null)
  101. {
  102. step.lines_curr.Add(m_canvas.Strokes);
  103. page.lines.Add(step);
  104. step = null;
  105. }
  106. }
  107. }
  108. private void Strokes_StrokesChanged(object sender, StrokeCollectionChangedEventArgs e)
  109. {
  110. if (undoOrRedo > 0)
  111. {
  112. undoOrRedo -= 1;
  113. return;
  114. }
  115. if (step == null)
  116. {
  117. step = new ZBBPageStep();
  118. }
  119. // 笔模式
  120. if (e.Added.Count > 0 && e.Removed.Count == 0)
  121. {
  122. step.lines_add.Add(e.Added);
  123. }
  124. // 橡皮模式 会多次进入回掉
  125. else if (e.Removed.Count > 0)
  126. {
  127. step.lines_add.Add(e.Added);
  128. for (int i = 0; i < e.Removed.Count; i++)
  129. {
  130. Stroke removeItem = e.Removed[i];
  131. if (step.lines_add.Contains(removeItem))
  132. {
  133. step.lines_add.Remove(removeItem);
  134. }
  135. else
  136. {
  137. step.lines_remove.Add(removeItem);
  138. }
  139. }
  140. }
  141. }
  142. // public方法 笔
  143. public void change_pen(Color _color)
  144. {
  145. //this.type = ZPenType.Pen;
  146. DrawingAttributes drawingAttributes = new DrawingAttributes();
  147. m_canvas.DefaultDrawingAttributes = drawingAttributes;
  148. drawingAttributes.Color = _color;
  149. drawingAttributes.Width = pensize;
  150. drawingAttributes.Height = pensize;
  151. drawingAttributes.FitToCurve = true;
  152. drawingAttributes.IgnorePressure = false;
  153. m_canvas.EditingMode = InkCanvasEditingMode.Ink;
  154. }
  155. // 橡皮
  156. public void change_erase()
  157. {
  158. //this.type = ZPenType.Erase;
  159. m_canvas.EditingMode = InkCanvasEditingMode.EraseByPoint;
  160. m_canvas.EraserShape = new EllipseStylusShape(erasesize, erasesize, 0);
  161. }
  162. // 撤销
  163. public void undo()
  164. {
  165. ZBBPage page = strokes_page_all[pagenum];
  166. if (page != null && m_canvas.Strokes.Count > 0 && page.lines.Count > 1)
  167. {
  168. ZBBPageStep last = page.lines.Last();
  169. page.lines.Remove(last);
  170. page.lines_histoty.Add(last);
  171. if (page.lines.Last().lines_curr.Count > 0)
  172. {
  173. undoOrRedo = 2;
  174. }
  175. else
  176. {
  177. undoOrRedo = 1;
  178. }
  179. m_canvas.Strokes.Clear();
  180. m_canvas.Strokes.Add(page.lines.Last().lines_curr);
  181. }
  182. }
  183. // 恢复
  184. public void redo()
  185. {
  186. ZBBPage page = strokes_page_all[pagenum];
  187. if (page != null && page.lines_histoty.Count > 0)
  188. {
  189. ZBBPageStep line = page.lines_histoty[page.lines_histoty.Count - 1];
  190. page.lines.Add(line);
  191. page.lines_histoty.Remove(line);
  192. if (page.lines.Last().lines_curr.Count > 0)
  193. {
  194. undoOrRedo = 2;
  195. }
  196. else
  197. {
  198. undoOrRedo = 1;
  199. }
  200. m_canvas.Strokes.Clear();
  201. m_canvas.Strokes.Add(page.lines.Last().lines_curr);
  202. }
  203. }
  204. // 清空
  205. public void clear()
  206. {
  207. ZBBPage page = new ZBBPage();
  208. page.lines.Add(new ZBBPageStep());
  209. page.lines = new List<ZBBPageStep>();
  210. page.lines_histoty = new List<ZBBPageStep>();
  211. strokes_page_all.Add(page);
  212. //ZBBPage page = strokes_page_all[pagenum];
  213. // if (page != null)
  214. //{
  215. // m_canvas.Strokes.Clear();
  216. // page.lines_histoty.Clear();
  217. // page.lines.Clear();
  218. // page.lines.Add(new ZBBPageStep());
  219. //}
  220. }
  221. public void changepage(int mpagenum)
  222. {
  223. if (pagenum != mpagenum)
  224. {
  225. pagenum = mpagenum;
  226. if (pagenum >= strokes_page_all.Count)
  227. {
  228. int numadd = pagenum - strokes_page_all.Count + 1;
  229. for (int i = 0; i < numadd; i++)
  230. {
  231. ZBBPage pagetemp = new ZBBPage();
  232. pagetemp.lines.Add(new ZBBPageStep());
  233. strokes_page_all.Add(pagetemp);
  234. }
  235. }
  236. ZBBPage page = strokes_page_all[pagenum];
  237. if (page != null&& page.lines.Count>0)
  238. {
  239. if (page.lines.Last().lines_curr.Count > 0)
  240. {
  241. undoOrRedo += 1;
  242. }
  243. if (m_canvas.Strokes.Count > 0)
  244. {
  245. undoOrRedo += 1;
  246. m_canvas.Strokes.Clear();
  247. }
  248. m_canvas.Strokes.Add(page.lines.Last().lines_curr);
  249. StrokeCollection strokes = new StrokeCollection();
  250. StylusPointCollection stylusPoints = new StylusPointCollection();
  251. System.Windows.Input.StylusPointDescription stylusPointDescription = new StylusPointDescription();
  252. StylusPoint stylusPoint = new StylusPoint();
  253. stylusPoint.X = 580;
  254. stylusPoint.Y = 212;
  255. stylusPoints.Add(stylusPoint);
  256. Stroke stroke = new Stroke(stylusPoints);
  257. stylusPoint.X = 581;
  258. stylusPoint.Y = 213;
  259. stylusPoints.Add(stylusPoint);
  260. stroke = new Stroke(stylusPoints);
  261. //stylusPoint.X = 586;
  262. //stylusPoint.Y = 214;
  263. //stylusPoints.Add(stylusPoint);
  264. //stroke = new Stroke(stylusPoints);
  265. //stylusPoint.X = 599;
  266. //stylusPoint.Y = 214;
  267. //stylusPoints.Add(stylusPoint);
  268. //stroke = new Stroke(stylusPoints);
  269. //stylusPoint.X = 623;
  270. //stylusPoint.Y = 214;
  271. //stylusPoints.Add(stylusPoint);
  272. //stroke = new Stroke(stylusPoints);
  273. //stylusPoint.X = 663;
  274. //stylusPoint.Y = 214;
  275. //stylusPoints.Add(stylusPoint);
  276. //stroke = new Stroke(stylusPoints);
  277. //stylusPoint.X = 963;
  278. //stylusPoint.Y = 214;
  279. //stylusPoints.Add(stylusPoint);
  280. //stroke = new Stroke(stylusPoints);
  281. //DrawingAttributes drawingAttributes = new DrawingAttributes();
  282. //m_canvas.DefaultDrawingAttributes = drawingAttributes;
  283. //drawingAttributes.Color = Colors.Red;
  284. //drawingAttributes.Width = pensize;
  285. //drawingAttributes.Height = pensize;
  286. //drawingAttributes.FitToCurve = true;
  287. //drawingAttributes.IgnorePressure = false;
  288. //stroke.DrawingAttributes = drawingAttributes;
  289. m_canvas.Strokes.Add(stroke);
  290. }
  291. }
  292. }
  293. StylusPointCollection stylusPoints = new StylusPointCollection();
  294. StylusPoint stylusPoint = new StylusPoint();
  295. Stroke stroke;
  296. bool isFirst = true;
  297. //public void changepages(double _x, double _y, bool _new, Color _color, int _size)
  298. //{
  299. // if (_new)
  300. // {
  301. // if (stroke != null && stroke.StylusPoints.Count > 1)
  302. // {
  303. // drawingAttributes = new DrawingAttributes();
  304. // //m_canvas.DefaultDrawingAttributes = drawingAttributes;
  305. // drawingAttributes.Color = _color;
  306. // drawingAttributes.Width = _size;
  307. // drawingAttributes.Height = _size;
  308. // drawingAttributes.FitToCurve = true;
  309. // drawingAttributes.IgnorePressure = false;
  310. // stroke.DrawingAttributes = drawingAttributes;
  311. // m_canvas.Strokes.Add(stroke);
  312. // isFirst = true;
  313. // }
  314. // stylusPoints = new StylusPointCollection();
  315. // stylusPoint = new StylusPoint();
  316. // //stroke = new Stroke(stylusPoints);
  317. // stroke = null;
  318. // }
  319. // else
  320. // {
  321. // if(isFirst)
  322. // {
  323. // stylusPoint.X = _x;
  324. // stylusPoint.Y = _y;
  325. // stylusPoints.Add(stylusPoint);
  326. // if (stylusPoints.Count>1)
  327. // {
  328. // stroke = new Stroke(stylusPoints);
  329. // m_canvas.Strokes.Add(stroke);
  330. // isFirst = false;
  331. // }
  332. // }
  333. // else
  334. // {
  335. // stylusPoint.X = _x;
  336. // stylusPoint.Y = _y;
  337. // stylusPoints.Add(stylusPoint);
  338. // stroke = new Stroke(stylusPoints);
  339. // m_canvas.Strokes[m_canvas.Strokes.Count - 1].StylusPoints.Add(stylusPoints);
  340. // }
  341. // }
  342. //}
  343. public void changepages(double _x, double _y, bool _new, Color _color, int _size)
  344. {
  345. if (_new)
  346. {
  347. if (stroke != null && stroke.StylusPoints.Count > 1)
  348. {
  349. drawingAttributes = new DrawingAttributes();
  350. //m_canvas.DefaultDrawingAttributes = drawingAttributes;
  351. drawingAttributes.Color = _color;
  352. drawingAttributes.Width = _size*5;
  353. drawingAttributes.Height = _size*5;
  354. drawingAttributes.FitToCurve = true;
  355. drawingAttributes.IgnorePressure = false;
  356. stroke.DrawingAttributes = drawingAttributes;
  357. //m_canvas.DefaultDrawingAttributes= drawingAttributes;
  358. m_canvas.Strokes.Add(stroke);
  359. }
  360. stylusPoints = new StylusPointCollection();
  361. stylusPoint = new StylusPoint();
  362. //stroke = new Stroke(stylusPoints);
  363. //stroke = null;
  364. }
  365. else
  366. {
  367. //stylusPoints = new StylusPointCollection();
  368. //stylusPoint = new StylusPoint();
  369. stylusPoint.X = _x;
  370. stylusPoint.Y = _y;
  371. stylusPoints.Add(stylusPoint);
  372. stroke = new Stroke(stylusPoints);
  373. }
  374. }
  375. ////声明一个 DrawingAttributes 类型的变量
  376. //DrawingAttributes drawingAttributes;
  377. //public void changepaget(double _x, double _y, bool _new,InkCanvas canvas)
  378. //{
  379. // if (_new)
  380. // {
  381. // if (stroke != null && stroke.StylusPoints.Count > 1)
  382. // {
  383. // //m_canvas.Strokes.Add(stroke);
  384. // DrawingAttributes drawingAttributes = new DrawingAttributes();
  385. // canvas.DefaultDrawingAttributes = drawingAttributes;
  386. // drawingAttributes.Width = pensize;
  387. // drawingAttributes.Height = pensize;
  388. // drawingAttributes.Color = Colors.Red;
  389. // drawingAttributes.FitToCurve = true;
  390. // drawingAttributes.IgnorePressure = false;
  391. // canvas.DefaultDrawingAttributes = drawingAttributes;
  392. // canvas.Strokes.Add(stroke);
  393. // ZBBPage page = strokes_page_all[pagenum];
  394. // if (page != null)
  395. // {
  396. // step.lines_curr.Add(m_canvas.Strokes);
  397. // page.lines.Add(step);
  398. // step = null;
  399. // }
  400. // }
  401. // stylusPoints = new StylusPointCollection();
  402. // stylusPoint = new StylusPoint();
  403. // //stroke = new Stroke(stylusPoints);
  404. // stroke = null;
  405. // }
  406. // else
  407. // {
  408. // change_pen(Colors.Red);
  409. // //stylusPoints = new StylusPointCollection();
  410. // //stylusPoint = new StylusPoint();
  411. // stylusPoint.X = _x;
  412. // stylusPoint.Y = _y;
  413. // stylusPoints.Add(stylusPoint);
  414. // stroke = new Stroke(stylusPoints);
  415. // //m_canvas.
  416. // //page.lines.Last().lines_curr
  417. // }
  418. // //page.lines.Last().lines_curr.Add();
  419. // //if (stroke.StylusPoints.Count > 20)
  420. // //{
  421. //}
  422. }
  423. }