You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
5.6 KiB

  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017-2021 Kicad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #ifndef PREVIEW_POLYGON_GEOM_MANAGER__H_
  24. #define PREVIEW_POLYGON_GEOM_MANAGER__H_
  25. #include <geometry/shape_line_chain.h>
  26. /**
  27. * Class that handles the drawing of a polygon, including management of last corner deletion
  28. * and drawing of leader lines with various constraints (eg 45 deg only).
  29. *
  30. * This class handles only the geometry of the process.
  31. */
  32. class POLYGON_GEOM_MANAGER
  33. {
  34. public:
  35. /**
  36. * "Listener" interface for a class that wants to be updated about
  37. * polygon geometry changes
  38. */
  39. class CLIENT
  40. {
  41. public:
  42. /**
  43. * Called before the first point is added - clients can do
  44. * initialization here, and can veto the start of the process
  45. * (e.g. if user cancels a dialog)
  46. *
  47. * @return false to veto start of new polygon
  48. */
  49. virtual bool OnFirstPoint( POLYGON_GEOM_MANAGER& aMgr ) = 0;
  50. ///< Sent when the polygon geometry changes
  51. virtual void OnGeometryChange( const POLYGON_GEOM_MANAGER& aMgr ) = 0;
  52. ///< Called when the polygon is complete
  53. virtual void OnComplete( const POLYGON_GEOM_MANAGER& aMgr ) = 0;
  54. virtual ~CLIENT()
  55. {
  56. }
  57. };
  58. /**
  59. * The kind of the leader line
  60. */
  61. enum class LEADER_MODE
  62. {
  63. DIRECT, ///< Unconstrained point-to-point
  64. DEG45, ///< 45 Degree only
  65. };
  66. /**
  67. * @param aClient is the client to pass the results onto
  68. */
  69. POLYGON_GEOM_MANAGER( CLIENT& aClient );
  70. /**
  71. * Lock in a polygon point.
  72. */
  73. bool AddPoint( const VECTOR2I& aPt );
  74. /**
  75. * Mark the polygon finished and update the client.
  76. */
  77. void SetFinished();
  78. /**
  79. * Clear the manager state and start again.
  80. */
  81. void Reset();
  82. /**
  83. * Set the leader mode to use when calculating the leader/returner lines.
  84. */
  85. void SetLeaderMode( LEADER_MODE aMode );
  86. LEADER_MODE GetLeaderMode() const
  87. {
  88. return m_leaderMode;
  89. }
  90. /**
  91. * Enables/disables self-intersecting polygons.
  92. *
  93. * @param aEnabled true if self-intersecting polygons are enabled.
  94. */
  95. void AllowIntersections( bool aEnabled )
  96. {
  97. m_intersectionsAllowed = true;
  98. }
  99. /**
  100. * Check whether self-intersecting polygons are enabled.
  101. *
  102. * @return true if self-intersecting polygons are enabled.
  103. */
  104. bool IntersectionsAllowed() const
  105. {
  106. return m_intersectionsAllowed;
  107. }
  108. /**
  109. * Check whether the locked points constitute a self-intersecting outline.
  110. *
  111. * @param aIncludeLeaderPts when true, also the leading points (not placed ones) will be tested.
  112. * @return True when the outline is self-intersecting.
  113. */
  114. bool IsSelfIntersecting( bool aIncludeLeaderPts ) const;
  115. /**
  116. * Set the current cursor position
  117. */
  118. void SetCursorPosition( const VECTOR2I& aPos );
  119. /**
  120. * @return true if the polygon in "in progress", i.e. it has at least
  121. * one locked-in point
  122. */
  123. bool IsPolygonInProgress() const;
  124. /**
  125. * @return true if locking in the given point would close the current polygon.
  126. */
  127. bool NewPointClosesOutline( const VECTOR2I& aPt ) const;
  128. /**
  129. * Remove the last-added point from the polygon
  130. */
  131. void DeleteLastCorner();
  132. /* =================================================================
  133. * Interfaces for users of the geometry
  134. */
  135. /**
  136. * Get the "locked-in" points that describe the polygon itself
  137. */
  138. const SHAPE_LINE_CHAIN& GetLockedInPoints() const
  139. {
  140. return m_lockedPoints;
  141. }
  142. /**
  143. * Get the points comprising the leader line (the line from the
  144. * last locked-in point to the current cursor position
  145. *
  146. * How this is drawn will depend on the LEADER_MODE
  147. */
  148. const SHAPE_LINE_CHAIN& GetLeaderLinePoints() const
  149. {
  150. return m_leaderPts;
  151. }
  152. private:
  153. /**
  154. * Update the leader line points based on a new endpoint (probably
  155. * a cursor position)
  156. */
  157. void updateLeaderPoints( const VECTOR2I& aEndPoint,
  158. LEADER_MODE aModifier = LEADER_MODE::DIRECT );
  159. ///< The "user" of the polygon data that is informed when the geometry changes
  160. CLIENT& m_client;
  161. ///< The current mode of the leader line
  162. LEADER_MODE m_leaderMode;
  163. ///< Flag enabling self-intersecting polygons
  164. bool m_intersectionsAllowed;
  165. ///< Point that have been "locked in"
  166. SHAPE_LINE_CHAIN m_lockedPoints;
  167. ///< Points in the temporary "leader" line(s)
  168. SHAPE_LINE_CHAIN m_leaderPts;
  169. };
  170. #endif // PREVIEW_POLYGON_GEOM_MANAGER__H_