星火微课系统客户端
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

BlackboardNew.cs 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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, float Pressure)
  332. {
  333. if (_new)
  334. {
  335. if (stroke != null && stroke.StylusPoints.Count > 1)
  336. {
  337. isFirst = true;
  338. try
  339. {
  340. if(strokes_page_all.Count<=i)
  341. {
  342. ZBBPage pagetemp = new ZBBPage();
  343. pagetemp.lines.Add(new ZBBPageStep());
  344. strokes_page_all.Add(pagetemp);
  345. }
  346. strokes_page_all[i].lines.Last().lines_curr.Add(stroke);
  347. }
  348. catch (Exception ex)
  349. {
  350. if (strokes_page_all != null)
  351. {
  352. if (strokes_page_all.Count == 0)
  353. {
  354. ZBBPage pagetemp = new ZBBPage();
  355. pagetemp.lines.Add(new ZBBPageStep());
  356. strokes_page_all.Add(pagetemp);
  357. }
  358. }
  359. LogHelper.WriteErrLog("【画板】(changepages)添加失败,strokes_page_all为0," + ex.Message, ex);
  360. }
  361. }
  362. stylusPoints = new StylusPointCollection();
  363. stylusPoint = new StylusPoint();
  364. stroke = null;
  365. }
  366. else
  367. {
  368. if (isFirst)
  369. {
  370. stylusPoint.X = _x;
  371. stylusPoint.Y = _y;
  372. //_color.A = (byte)(Pressure * 255f);
  373. //stylusPoint.PressureFactor = Pressure;
  374. stylusPoints.Add(stylusPoint);
  375. if (stylusPoints.Count > 1)
  376. {
  377. stroke = new Stroke(stylusPoints);
  378. drawingAttributes = new DrawingAttributes
  379. {
  380. Color = _color,
  381. Width = _size * 4.5,
  382. Height = _size * 4.5,
  383. FitToCurve = true,
  384. //IsHighlighter =true,
  385. IgnorePressure = true
  386. };
  387. stroke.DrawingAttributes = drawingAttributes;
  388. m_canvas.Strokes.Add(stroke);
  389. isFirst = false;
  390. }
  391. }
  392. else
  393. {
  394. if (m_canvas.Strokes.Count > 0)
  395. {
  396. stylusPoint.X = _x;
  397. stylusPoint.Y = _y;
  398. stylusPoints.Add(stylusPoint);
  399. stroke = new Stroke(stylusPoints);
  400. drawingAttributes = new DrawingAttributes
  401. {
  402. Color = _color,
  403. Width = _size * 4.5,
  404. Height = _size * 4.5,
  405. FitToCurve = true,
  406. IgnorePressure = false
  407. };
  408. stroke.DrawingAttributes = drawingAttributes;
  409. m_canvas.Strokes[m_canvas.Strokes.Count - 1] = stroke;
  410. }
  411. }
  412. }
  413. }
  414. //public void changepages(double _x, double _y, bool _new, Color _color, int _size, int i)
  415. //{
  416. // try
  417. // {
  418. // if (_new)
  419. // {
  420. // if (stroke != null && stroke.StylusPoints.Count > 1)
  421. // {
  422. // drawingAttributes = new DrawingAttributes();
  423. // //m_canvas.DefaultDrawingAttributes = drawingAttributes;
  424. // drawingAttributes.Color = _color;
  425. // drawingAttributes.Width = _size * 5;
  426. // drawingAttributes.Height = _size * 5;
  427. // drawingAttributes.FitToCurve = true;
  428. // drawingAttributes.IgnorePressure = false;
  429. // stroke.DrawingAttributes = drawingAttributes;
  430. // //m_canvas.DefaultDrawingAttributes= drawingAttributes;
  431. // m_canvas.Strokes.Add(stroke);
  432. // strokes_page_all[i].lines.Last().lines_curr.Add(stroke);
  433. // //int currCount= strokes_page_all[i].lines.Last().lines_curr.Count;
  434. // //if (currCount > 0)
  435. // //{
  436. // // strokes_page_all[i].lines.Last().lines_curr[currCount - 1] = stroke;
  437. // //}
  438. // stroke = null;
  439. // }
  440. // stylusPoints = new StylusPointCollection();
  441. // stylusPoint = new StylusPoint();
  442. // //stroke = new Stroke(stylusPoints);
  443. // //stroke = null;
  444. // }
  445. // else
  446. // {
  447. // //stylusPoints = new StylusPointCollection();
  448. // //stylusPoint = new StylusPoint();
  449. // stylusPoint.X = _x;
  450. // stylusPoint.Y = _y;
  451. // stylusPoints.Add(stylusPoint);
  452. // stroke = new Stroke(stylusPoints);
  453. // }
  454. // }
  455. // catch (Exception ex)
  456. // {
  457. // LogHelper.WriteErrLog("【XHMicroLessonSystemWindow】(changepages)点阵比书写报错:" + ex.Message, ex);
  458. // }
  459. //}
  460. }
  461. }