Ruler.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Text;
  5. using System.Text.Json.Serialization;
  6. namespace Terminal.Gui {
  7. /// <summary>
  8. /// Draws a ruler on the screen.
  9. /// </summary>
  10. /// <remarks>
  11. /// <para>
  12. /// </para>
  13. /// </remarks>
  14. public class Ruler {
  15. /// <summary>
  16. /// Gets or sets whether the ruler is drawn horizontally or vertically. The default is horizontally.
  17. /// </summary>
  18. public Orientation Orientation { get; set; }
  19. /// <summary>
  20. /// Gets or sets the length of the ruler. The default is 0.
  21. /// </summary>
  22. public int Length { get; set; }
  23. /// <summary>
  24. /// Gets or sets the foreground and background color to use.
  25. /// </summary>
  26. public Attribute Attribute { get; set; } = new Attribute ();
  27. string _hTemplate { get; } = "|123456789";
  28. string _vTemplate { get; } = "-123456789";
  29. /// <summary>
  30. /// Draws the <see cref="Ruler"/>.
  31. /// </summary>
  32. /// <param name="location">The location to start drawing the ruler, in screen-relative coordinates.</param>
  33. /// <param name="start">The start value of the ruler.</param>
  34. public void Draw (Point location, int start = 0)
  35. {
  36. if (start < 0) {
  37. throw new ArgumentException ("start must be greater than or equal to 0");
  38. }
  39. if (Length < 1) {
  40. return;
  41. }
  42. if (Orientation == Orientation.Horizontal) {
  43. var hrule = _hTemplate.Repeat ((int)Math.Ceiling ((double)Length + 2 / (double)_hTemplate.Length)) [start..(Length + start)];
  44. // Top
  45. Application.Driver.Move (location.X, location.Y);
  46. Application.Driver.AddStr (hrule);
  47. } else {
  48. var vrule = _vTemplate.Repeat ((int)Math.Ceiling ((double)(Length + 2) / (double)_vTemplate.Length)) [start..(Length + start)];
  49. for (var r = location.Y; r < location.Y + Length; r++) {
  50. Application.Driver.Move (location.X, r);
  51. Application.Driver.AddRune ((Rune)vrule [r - location.Y]);
  52. }
  53. }
  54. }
  55. }
  56. }