星火微课系统客户端
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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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 _mCanvas;
  53. //private ZPenType type = ZPenType.Pen;
  54. /// <summary>
  55. /// 当前页索引
  56. /// </summary>
  57. private int _pagenum;
  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;
  70. /// <summary>
  71. /// 笔颜色
  72. /// </summary>
  73. private Color _pencolor;
  74. /// <summary>
  75. /// 所有画板线
  76. /// </summary>
  77. private readonly List<ZbbPage> _strokesPageAll = new List<ZbbPage>();
  78. // 添加这个变量是因为在用橡皮擦时 一次操作会触发多次StrokesChanged回掉 这里是把多次回掉合并在一起
  79. private ZbbPageStep _step;
  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 mpencolor)
  90. {
  91. _mCanvas = canvas;
  92. this._pencolor = mpencolor;
  93. ZbbPage page = new ZbbPage();
  94. page.lines.Add(new ZbbPageStep());
  95. _strokesPageAll.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 = this._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. private void Canvas_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e) { }
  119. private void Canvas_MouseUp(object sender, MouseButtonEventArgs e)
  120. {
  121. if (_step != null)
  122. {
  123. if (_pagenum + 1 > _strokesPageAll.Count)
  124. {
  125. ZbbPage pagetemp = new ZbbPage();
  126. pagetemp.lines.Add(new ZbbPageStep());
  127. _strokesPageAll.Add(pagetemp);
  128. }
  129. try
  130. {
  131. ZbbPage page = _strokesPageAll[_pagenum];
  132. if (page != null)
  133. {
  134. _step.lines_curr.Add(_mCanvas.Strokes);
  135. page.lines.Add(_step);
  136. _step = null;
  137. }
  138. }
  139. catch (Exception ex)
  140. {
  141. if (_strokesPageAll != null)
  142. {
  143. if (_strokesPageAll.Count == 0)
  144. {
  145. ZbbPage pagetemp = new ZbbPage();
  146. pagetemp.lines.Add(new ZbbPageStep());
  147. _strokesPageAll.Add(pagetemp);
  148. }
  149. }
  150. LogHelper.Logerror.Error("【画板】(Canvas_MouseUp)添加失败,strokes_page_all为0," + ex.Message, ex);
  151. }
  152. }
  153. }
  154. private void Strokes_StrokesChanged(object sender, StrokeCollectionChangedEventArgs e)
  155. {
  156. if (_undoOrRedo > 0)
  157. {
  158. _undoOrRedo -= 1;
  159. return;
  160. }
  161. if (_step == null)
  162. {
  163. _step = new ZbbPageStep();
  164. }
  165. // 笔模式
  166. if (e.Added.Count > 0 && e.Removed.Count == 0)
  167. {
  168. _step.lines_add.Add(e.Added);
  169. }
  170. // 橡皮模式 会多次进入回掉
  171. else if (e.Removed.Count > 0)
  172. {
  173. _step.lines_add.Add(e.Added);
  174. for (int i = 0; i < e.Removed.Count; i++)
  175. {
  176. Stroke removeItem = e.Removed[i];
  177. if (_step.lines_add.Contains(removeItem))
  178. {
  179. _step.lines_add.Remove(removeItem);
  180. }
  181. else
  182. {
  183. _step.lines_remove.Add(removeItem);
  184. }
  185. }
  186. }
  187. }
  188. // public方法 笔
  189. public void change_pen(Color color)
  190. {
  191. //this.type = ZPenType.Pen;
  192. DrawingAttributes drawingAttributes = new DrawingAttributes();
  193. _mCanvas.DefaultDrawingAttributes = drawingAttributes;
  194. drawingAttributes.Color = color;
  195. drawingAttributes.Width = _pensize;
  196. drawingAttributes.Height = _pensize;
  197. drawingAttributes.FitToCurve = true;
  198. drawingAttributes.IgnorePressure = false;
  199. _mCanvas.EditingMode = InkCanvasEditingMode.Ink;
  200. }
  201. // 橡皮
  202. public void change_erase()
  203. {
  204. //this.type = ZPenType.Erase;
  205. _mCanvas.EditingMode = InkCanvasEditingMode.EraseByPoint;
  206. _mCanvas.EraserShape = new EllipseStylusShape(_erasesize, _erasesize, 0);
  207. }
  208. // 撤销
  209. public void Undo()
  210. {
  211. ZbbPage page = _strokesPageAll[_pagenum];
  212. if (page != null && _mCanvas.Strokes.Count > 0 && page.lines.Count > 1)
  213. {
  214. ZbbPageStep last = page.lines.Last();
  215. page.lines.Remove(last);
  216. page.lines_histoty.Add(last);
  217. if (page.lines.Last().lines_curr.Count > 0)
  218. {
  219. _undoOrRedo = 2;
  220. }
  221. else
  222. {
  223. _undoOrRedo = 1;
  224. }
  225. _mCanvas.Strokes.Clear();
  226. _mCanvas.Strokes.Add(page.lines.Last().lines_curr);
  227. }
  228. }
  229. // 恢复
  230. public void Redo()
  231. {
  232. ZbbPage page = _strokesPageAll[_pagenum];
  233. if (page != null && page.lines_histoty.Count > 0)
  234. {
  235. ZbbPageStep line = page.lines_histoty[page.lines_histoty.Count - 1];
  236. page.lines.Add(line);
  237. page.lines_histoty.Remove(line);
  238. if (page.lines.Last().lines_curr.Count > 0)
  239. {
  240. _undoOrRedo = 2;
  241. }
  242. else
  243. {
  244. _undoOrRedo = 1;
  245. }
  246. _mCanvas.Strokes.Clear();
  247. _mCanvas.Strokes.Add(page.lines.Last().lines_curr);
  248. }
  249. }
  250. // 清空
  251. public void Clear()
  252. {
  253. //ZBBPage page = new ZBBPage();
  254. //page.lines.Add(new ZBBPageStep());
  255. //page.lines = new List<ZBBPageStep>();
  256. //page.lines_histoty = new List<ZBBPageStep>();
  257. //strokes_page_all.Add(page);
  258. _strokesPageAll.Clear();
  259. _mCanvas.Strokes.Clear();
  260. //for (int i=0;i< strokes_page_all.Count;i++)
  261. //{
  262. // ZBBPage page = strokes_page_all[pagenum];
  263. // if (page != null)
  264. // {
  265. // m_canvas.Strokes.Clear();
  266. // page.lines_histoty.Clear();
  267. // page.lines.Clear();
  268. // page.lines.Add(new ZBBPageStep());
  269. // }
  270. //}
  271. }
  272. /// <summary>
  273. /// 翻页
  274. /// </summary>
  275. /// <param name="mpagenum"></param>
  276. public void Changepage(int mpagenum)
  277. {
  278. if (_pagenum != mpagenum)
  279. {
  280. _pagenum = mpagenum;
  281. //新增页
  282. if (_pagenum >= _strokesPageAll.Count)
  283. {
  284. int numadd = _pagenum - _strokesPageAll.Count + 1;
  285. for (int i = 0; i < numadd; i++)
  286. {
  287. ZbbPage pagetemp = new ZbbPage();
  288. pagetemp.lines.Add(new ZbbPageStep());
  289. _strokesPageAll.Add(pagetemp);
  290. }
  291. }
  292. ZbbPage page = _strokesPageAll[_pagenum];
  293. if (page != null && page.lines.Count > 0)
  294. {
  295. if (page.lines.Last().lines_curr.Count > 0)
  296. {
  297. _undoOrRedo += 1;
  298. }
  299. if (_mCanvas.Strokes.Count > 0)
  300. {
  301. _undoOrRedo += 1;
  302. _mCanvas.Strokes.Clear();
  303. }
  304. //StrokeCollection strokes = new StrokeCollection();
  305. StylusPointCollection mstylusPoints = new StylusPointCollection();
  306. //System.Windows.Input.StylusPointDescription stylusPointDescription = new StylusPointDescription();
  307. StylusPoint mstylusPoint = new StylusPoint { X = 580, Y = 212 };
  308. mstylusPoints.Add(mstylusPoint);
  309. Stroke stroke;
  310. mstylusPoint.X = 581;
  311. mstylusPoint.Y = 213;
  312. mstylusPoints.Add(mstylusPoint);
  313. stroke = new Stroke(mstylusPoints);
  314. page.lines.Last().lines_curr.Add(stroke);
  315. _mCanvas.Strokes.Add(page.lines.Last().lines_curr);
  316. }
  317. }
  318. }
  319. private StylusPointCollection _stylusPoints = new StylusPointCollection();
  320. private StylusPoint _stylusPoint;
  321. private Stroke _stroke;
  322. private bool _isFirst = true;
  323. public void Changepages
  324. (
  325. double x,
  326. double y,
  327. bool mnew,
  328. Color color,
  329. int size,
  330. int i,
  331. float pressure
  332. )
  333. {
  334. if (mnew)
  335. {
  336. if (_stroke != null && _stroke.StylusPoints.Count > 1)
  337. {
  338. _isFirst = true;
  339. try
  340. {
  341. if (_strokesPageAll.Count <= i)
  342. {
  343. ZbbPage pagetemp = new ZbbPage();
  344. pagetemp.lines.Add(new ZbbPageStep());
  345. _strokesPageAll.Add(pagetemp);
  346. }
  347. _strokesPageAll[i].lines.Last().lines_curr.Add(_stroke);
  348. }
  349. catch (Exception ex)
  350. {
  351. if (_strokesPageAll != null)
  352. {
  353. if (_strokesPageAll.Count == 0)
  354. {
  355. ZbbPage pagetemp = new ZbbPage();
  356. pagetemp.lines.Add(new ZbbPageStep());
  357. _strokesPageAll.Add(pagetemp);
  358. }
  359. }
  360. LogHelper.Logerror.Error("【画板】(changepages)添加失败,strokes_page_all为0," + ex.Message, ex);
  361. }
  362. }
  363. _stylusPoints = new StylusPointCollection();
  364. _stylusPoint = new StylusPoint();
  365. _stroke = null;
  366. }
  367. else
  368. {
  369. if (_isFirst)
  370. {
  371. _stylusPoint.X = x;
  372. _stylusPoint.Y = y;
  373. //_color.A = (byte)(Pressure * 255f);
  374. //stylusPoint.PressureFactor = Pressure;
  375. _stylusPoints.Add(_stylusPoint);
  376. if (_stylusPoints.Count > 1)
  377. {
  378. _stroke = new Stroke(_stylusPoints);
  379. _drawingAttributes = new DrawingAttributes
  380. {
  381. Color = color,
  382. Width = size * 4.5,
  383. Height = size * 4.5,
  384. FitToCurve = true,
  385. //IsHighlighter =true,
  386. IgnorePressure = true
  387. };
  388. _stroke.DrawingAttributes = _drawingAttributes;
  389. _mCanvas.Strokes.Add(_stroke);
  390. _isFirst = false;
  391. }
  392. }
  393. else
  394. {
  395. if (_mCanvas.Strokes.Count > 0)
  396. {
  397. _stylusPoint.X = x;
  398. _stylusPoint.Y = y;
  399. _stylusPoints.Add(_stylusPoint);
  400. _stroke = new Stroke(_stylusPoints);
  401. _drawingAttributes = new DrawingAttributes
  402. {
  403. Color = color,
  404. Width = size * 4.5,
  405. Height = size * 4.5,
  406. FitToCurve = true,
  407. IgnorePressure = false
  408. };
  409. _stroke.DrawingAttributes = _drawingAttributes;
  410. _mCanvas.Strokes[_mCanvas.Strokes.Count - 1] = _stroke;
  411. }
  412. }
  413. }
  414. }
  415. //public void changepages(double _x, double _y, bool mnew, Color _color, int _size, int i)
  416. //{
  417. // try
  418. // {
  419. // if (mnew)
  420. // {
  421. // if (stroke != null && stroke.StylusPoints.Count > 1)
  422. // {
  423. // drawingAttributes = new DrawingAttributes();
  424. // //m_canvas.DefaultDrawingAttributes = drawingAttributes;
  425. // drawingAttributes.Color = _color;
  426. // drawingAttributes.Width = _size * 5;
  427. // drawingAttributes.Height = _size * 5;
  428. // drawingAttributes.FitToCurve = true;
  429. // drawingAttributes.IgnorePressure = false;
  430. // stroke.DrawingAttributes = drawingAttributes;
  431. // //m_canvas.DefaultDrawingAttributes= drawingAttributes;
  432. // m_canvas.Strokes.Add(stroke);
  433. // strokes_page_all[i].lines.Last().lines_curr.Add(stroke);
  434. // //int currCount= strokes_page_all[i].lines.Last().lines_curr.Count;
  435. // //if (currCount > 0)
  436. // //{
  437. // // strokes_page_all[i].lines.Last().lines_curr[currCount - 1] = stroke;
  438. // //}
  439. // stroke = null;
  440. // }
  441. // stylusPoints = new StylusPointCollection();
  442. // stylusPoint = new StylusPoint();
  443. // //stroke = new Stroke(stylusPoints);
  444. // //stroke = null;
  445. // }
  446. // else
  447. // {
  448. // //stylusPoints = new StylusPointCollection();
  449. // //stylusPoint = new StylusPoint();
  450. // stylusPoint.X = _x;
  451. // stylusPoint.Y = _y;
  452. // stylusPoints.Add(stylusPoint);
  453. // stroke = new Stroke(stylusPoints);
  454. // }
  455. // }
  456. // catch (Exception ex)
  457. // {
  458. // LogHelper.Logerror.Error("【XHMicroLessonSystemWindow】(changepages)点阵比书写报错:" + ex.Message, ex);
  459. // }
  460. //}
  461. }
  462. }