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 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
  28. * management of last corner deletion and drawing of leader lines
  29. * with various constraints (eg 45 deg only).
  30. *
  31. * This class handles only the geometry of the process.
  32. */
  33. class POLYGON_GEOM_MANAGER
  34. {
  35. public:
  36. /**
  37. * "Listener" interface for a class that wants to be updated about
  38. * polygon geometry changes
  39. */
  40. class CLIENT
  41. {
  42. public:
  43. /**
  44. * Called before the first point is added - clients can do
  45. * initialisation here, and can veto the start of the process
  46. * (e.g. if user cancels a dialog)
  47. *
  48. * @return false to veto start of new polygon
  49. */
  50. virtual bool OnFirstPoint( POLYGON_GEOM_MANAGER& aMgr ) = 0;
  51. ///> Sent when the polygon geometry changes
  52. virtual void OnGeometryChange( const POLYGON_GEOM_MANAGER& aMgr ) = 0;
  53. ///> Called when the polygon is complete
  54. virtual void OnComplete( const POLYGON_GEOM_MANAGER& aMgr ) = 0;
  55. virtual ~CLIENT()
  56. {
  57. }
  58. };
  59. /**
  60. * The kind of the leader line
  61. */
  62. enum class LEADER_MODE
  63. {
  64. DIRECT, ///> Unconstrained point-to-point
  65. DEG45, ///> 45 Degree only
  66. };
  67. /**
  68. * @param aClient is the client to pass the results onto
  69. */
  70. POLYGON_GEOM_MANAGER( CLIENT& aClient );
  71. /**
  72. * Lock in a polygon point.
  73. */
  74. bool AddPoint( const VECTOR2I& aPt );
  75. /**
  76. * Mark the polygon finished and update the client
  77. */
  78. void SetFinished();
  79. /**
  80. * Clear the manager state and start again
  81. */
  82. void Reset();
  83. /**
  84. * Set the leader mode to use when calculating the leader/returner
  85. * lines
  86. */
  87. void SetLeaderMode( LEADER_MODE aMode );
  88. LEADER_MODE GetLeaderMode() const
  89. {
  90. return m_leaderMode;
  91. }
  92. /**
  93. * Enables/disables self-intersecting polygons.
  94. * @param aEnabled true if self-intersecting polygons are enabled.
  95. */
  96. void AllowIntersections( bool aEnabled )
  97. {
  98. m_intersectionsAllowed = true;
  99. }
  100. /**
  101. * Checks whether self-intersecting polygons are enabled.
  102. * @return true if self-intersecting polygons are enabled.
  103. */
  104. bool IntersectionsAllowed() const
  105. {
  106. return m_intersectionsAllowed;
  107. }
  108. /**
  109. * Checks whether the locked points constitute a self-intersecting outline.
  110. * @param aIncludeLeaderPts when true, also the leading points (not placed ones) will be tested.
  111. * @return True when the outline is self-intersecting.
  112. */
  113. bool IsSelfIntersecting( bool aIncludeLeaderPts ) const;
  114. /**
  115. * Set the current cursor position
  116. */
  117. void SetCursorPosition( const VECTOR2I& aPos );
  118. /**
  119. * @return true if the polygon in "in progress", i.e. it has at least
  120. * one locked-in point
  121. */
  122. bool IsPolygonInProgress() const;
  123. /**
  124. * @return true if locking in the given point would close the
  125. * 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_