Drawing.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using Terminal.Gui;
  3. using Terminal.Gui.Graphs;
  4. namespace UICatalog.Scenarios {
  5. [ScenarioMetadata (Name: "Drawing", Description: "Demonstrates StraightLineCanvas.")]
  6. [ScenarioCategory ("Controls")]
  7. [ScenarioCategory ("Layout")]
  8. public class Drawing : Scenario {
  9. public override void Setup ()
  10. {
  11. var canvas = new DrawingPanel {
  12. Width = Dim.Fill (),
  13. Height = Dim.Fill ()
  14. };
  15. Win.Add(canvas);
  16. }
  17. class DrawingPanel : View
  18. {
  19. StraightLineCanvas canvas;
  20. Point? currentLineStart = null;
  21. public DrawingPanel ()
  22. {
  23. canvas = new StraightLineCanvas (Application.Driver);
  24. }
  25. public override void Redraw (Rect bounds)
  26. {
  27. base.Redraw (bounds);
  28. var runes = canvas.GenerateImage (bounds);
  29. for(int y=0;y<bounds.Height;y++) {
  30. for (int x = 0; x < bounds.Width; x++) {
  31. var rune = runes [y, x];
  32. if(rune.HasValue) {
  33. AddRune (x, y, rune.Value);
  34. }
  35. }
  36. }
  37. }
  38. public override bool OnMouseEvent (MouseEvent mouseEvent)
  39. {
  40. if(mouseEvent.Flags.HasFlag(MouseFlags.Button1Pressed)) {
  41. if(currentLineStart == null) {
  42. currentLineStart = new Point(mouseEvent.X,mouseEvent.Y);
  43. }
  44. }
  45. else {
  46. if (currentLineStart != null) {
  47. var start = currentLineStart.Value;
  48. var end = new Point(mouseEvent.X, mouseEvent.Y);
  49. var orientation = Orientation.Vertical;
  50. var length = end.Y - start.Y;
  51. // if line is wider than it is tall switch to horizontal
  52. if(Math.Abs(start.X - end.X) > Math.Abs(start.Y - end.Y))
  53. {
  54. orientation = Orientation.Horizontal;
  55. length = end.X - start.X;
  56. }
  57. canvas.AddLine (start, length, orientation, BorderStyle.Single);
  58. currentLineStart = null;
  59. SetNeedsDisplay ();
  60. }
  61. }
  62. return base.OnMouseEvent (mouseEvent);
  63. }
  64. }
  65. }
  66. }