HitTestResult.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // RichTextKit
  2. // Copyright © 2019-2020 Topten Software. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. // not use this product except in compliance with the License. You may obtain
  6. // a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. // License for the specific language governing permissions and limitations
  14. // under the License.
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Text;
  18. namespace Topten.RichTextKit
  19. {
  20. /// <summary>
  21. /// Used to return hit test information from the
  22. /// <see cref="TextBlock.HitTest(float, float)"/> method.
  23. /// </summary>
  24. public struct HitTestResult
  25. {
  26. /// <summary>
  27. /// The zero based index of the line number the y-coordinate is directly
  28. /// over, or -1 if the y-coordinate is before the first line, or after the
  29. /// last line.
  30. /// </summary>
  31. /// <remarks>
  32. /// The x-coordinate isn't used in calculating this value and the left/right
  33. /// limits aren't checked.
  34. /// </remarks>
  35. public int OverLine;
  36. /// <summary>
  37. /// The zero based index of the closest line to the passed y-coordinate.
  38. /// </summary>
  39. /// <remarks>
  40. /// If the point is directly over a line this value will be the same as the
  41. /// <see cref="OverLine"/> property. If the point is before the first line,
  42. /// this property will be 0. If the point is after the last line this value
  43. /// will be the index of the last line.
  44. /// </remarks>
  45. public int ClosestLine;
  46. /// <summary>
  47. /// The code point index of the first code point in the cluster that the
  48. /// point is actually over, or -1 if not over a cluster.
  49. /// </summary>
  50. public int OverCodePointIndex;
  51. /// <summary>
  52. /// The code point index of the first code point in the cluster that the
  53. /// point is closest to.
  54. /// </summary>
  55. /// <remarks>
  56. /// If the point is over a cluster, the returned code point index will vary
  57. /// depending whether the point is in the left or right half of the cluster
  58. /// and the text direction of that cluster.
  59. ///
  60. /// This value represents the code point index that the caret should be moved to
  61. /// if the user clicked the mouse at this position. To determine the co-ordinates
  62. /// and shape of the caret, see [Caret Information](/caret).
  63. /// </remarks>
  64. public int ClosestCodePointIndex;
  65. /// <summary>
  66. /// Indicates that the point is closest to the alternate caret position
  67. /// of ClosestCodePointIndex.
  68. /// </summary>
  69. /// <remarks>
  70. /// This property indicates if the tested point is beyond the end of
  71. /// a word wrapped line and not at the start of the following line.
  72. /// </remarks>
  73. public bool AltCaretPosition;
  74. /// <summary>
  75. /// Helper to get the closest position as a CaretPosition
  76. /// </summary>
  77. public CaretPosition CaretPosition => new CaretPosition(ClosestCodePointIndex, AltCaretPosition);
  78. /// <summary>
  79. /// Compares this object to another instance
  80. /// </summary>
  81. /// <param name="obj"></param>
  82. /// <returns></returns>
  83. public override bool Equals(object obj)
  84. {
  85. return obj is HitTestResult result &&
  86. OverLine == result.OverLine &&
  87. ClosestLine == result.ClosestLine &&
  88. OverCodePointIndex == result.OverCodePointIndex &&
  89. ClosestCodePointIndex == result.ClosestCodePointIndex;
  90. }
  91. /// <summary>
  92. /// Gets a hash code for this object
  93. /// </summary>
  94. /// <returns>The hash value</returns>
  95. public override int GetHashCode()
  96. {
  97. return base.GetHashCode();
  98. }
  99. /// <summary>
  100. /// Check is this is the "none" hit test result
  101. /// </summary>
  102. public bool IsNone => ClosestCodePointIndex < 0;
  103. /// <summary>
  104. /// Hit test result indicating no hit, or untested hit
  105. /// </summary>
  106. public static HitTestResult None = new HitTestResult()
  107. {
  108. OverLine = -1,
  109. OverCodePointIndex = -1,
  110. ClosestLine = -1,
  111. ClosestCodePointIndex = -1,
  112. };
  113. }
  114. }