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.

152 lines
4.0 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019 CERN
  5. * Copyright (C) 2019-2020 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #ifndef PICKER_TOOL_H
  25. #define PICKER_TOOL_H
  26. #include <optional>
  27. #include <gal/cursors.h>
  28. #include <math/vector2d.h>
  29. #include <tool/tool_interactive.h>
  30. class EDA_DRAW_FRAME;
  31. class PICKER_TOOL_BASE
  32. {
  33. public:
  34. ///< Event handler types.
  35. typedef std::function<bool(const VECTOR2D&)> CLICK_HANDLER;
  36. typedef std::function<void(const VECTOR2D&)> MOTION_HANDLER;
  37. typedef std::function<void(void)> CANCEL_HANDLER;
  38. typedef std::function<void(const int&)> FINALIZE_HANDLER;
  39. enum pickerEndState
  40. {
  41. WAIT_CANCEL,
  42. CLICK_CANCEL,
  43. END_ACTIVATE,
  44. EVT_CANCEL,
  45. EXCEPTION_CANCEL
  46. };
  47. PICKER_TOOL_BASE() :
  48. m_frame( nullptr ),
  49. m_snap( false )
  50. {
  51. reset();
  52. }
  53. virtual ~PICKER_TOOL_BASE() = default;
  54. inline void SetCursor( KICURSOR aCursor ) { m_cursor = aCursor; }
  55. inline void SetSnapping( bool aSnap ) { m_snap = aSnap; }
  56. /**
  57. * Set a handler for mouse click event.
  58. *
  59. * The handler may decide to receive further click by returning true.
  60. */
  61. inline void SetClickHandler( CLICK_HANDLER aHandler )
  62. {
  63. wxASSERT( !m_clickHandler );
  64. m_clickHandler = aHandler;
  65. }
  66. /**
  67. * Set a handler for mouse motion.
  68. *
  69. * This is used for roll-over highlighting.
  70. */
  71. inline void SetMotionHandler( MOTION_HANDLER aHandler )
  72. {
  73. wxASSERT( !m_motionHandler );
  74. m_motionHandler = aHandler;
  75. }
  76. /**
  77. * Set a handler for cancel events (ESC or context-menu Cancel).
  78. */
  79. inline void SetCancelHandler( CANCEL_HANDLER aHandler )
  80. {
  81. wxASSERT( !m_cancelHandler );
  82. m_cancelHandler = aHandler;
  83. }
  84. /**
  85. * Set a handler for the finalize event.
  86. *
  87. * Takes the state of the exit from the main loop.
  88. */
  89. inline void SetFinalizeHandler( FINALIZE_HANDLER aHandler )
  90. {
  91. wxASSERT( !m_finalizeHandler );
  92. m_finalizeHandler = aHandler;
  93. }
  94. protected:
  95. ///< Reinitializes tool to its initial state.
  96. virtual void reset();
  97. EDA_DRAW_FRAME* m_frame;
  98. KICURSOR m_cursor;
  99. bool m_snap;
  100. std::optional<CLICK_HANDLER> m_clickHandler;
  101. std::optional<MOTION_HANDLER> m_motionHandler;
  102. std::optional<CANCEL_HANDLER> m_cancelHandler;
  103. std::optional<FINALIZE_HANDLER> m_finalizeHandler;
  104. std::optional<VECTOR2D> m_picked;
  105. };
  106. class PICKER_TOOL : public TOOL_INTERACTIVE, public PICKER_TOOL_BASE
  107. {
  108. public:
  109. PICKER_TOOL();
  110. PICKER_TOOL( const std::string& aName );
  111. virtual ~PICKER_TOOL() = default;
  112. /// @copydoc TOOL_INTERACTIVE::Init()
  113. bool Init() override;
  114. /// @copydoc TOOL_INTERACTIVE::Reset()
  115. void Reset( RESET_REASON aReason ) override { }
  116. ///< Main event loop.
  117. int Main( const TOOL_EVENT& aEvent );
  118. protected:
  119. ///< Applies the requested VIEW_CONTROLS settings.
  120. void setControls();
  121. ///< @copydoc TOOL_INTERACTIVE::setTransitions();
  122. void setTransitions() override;
  123. };
  124. #endif /* PICKER_TOOL_H */