星火微课系统客户端
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

BlackboardNew.cs 17KB

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