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.

151 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 <core/optional.h>
  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. {
  50. reset();
  51. }
  52. virtual ~PICKER_TOOL_BASE() = default;
  53. inline void SetCursor( KICURSOR aCursor ) { m_cursor = aCursor; }
  54. inline void SetSnapping( bool aSnap ) { m_snap = aSnap; }
  55. /**
  56. * Set a handler for mouse click event.
  57. *
  58. * The handler may decide to receive further click by returning true.
  59. */
  60. inline void SetClickHandler( CLICK_HANDLER aHandler )
  61. {
  62. wxASSERT( !m_clickHandler );
  63. m_clickHandler = aHandler;
  64. }
  65. /**
  66. * Set a handler for mouse motion.
  67. *
  68. * This is used for roll-over highlighting.
  69. */
  70. inline void SetMotionHandler( MOTION_HANDLER aHandler )
  71. {
  72. wxASSERT( !m_motionHandler );
  73. m_motionHandler = aHandler;
  74. }
  75. /**
  76. * Set a handler for cancel events (ESC or context-menu Cancel).
  77. */
  78. inline void SetCancelHandler( CANCEL_HANDLER aHandler )
  79. {
  80. wxASSERT( !m_cancelHandler );
  81. m_cancelHandler = aHandler;
  82. }
  83. /**
  84. * Set a handler for the finalize event.
  85. *
  86. * Takes the state of the exit from the main loop.
  87. */
  88. inline void SetFinalizeHandler( FINALIZE_HANDLER aHandler )
  89. {
  90. wxASSERT( !m_finalizeHandler );
  91. m_finalizeHandler = aHandler;
  92. }
  93. protected:
  94. ///< Reinitializes tool to its initial state.
  95. virtual void reset();
  96. EDA_DRAW_FRAME* m_frame;
  97. KICURSOR m_cursor;
  98. bool m_snap;
  99. OPT<CLICK_HANDLER> m_clickHandler;
  100. OPT<MOTION_HANDLER> m_motionHandler;
  101. OPT<CANCEL_HANDLER> m_cancelHandler;
  102. OPT<FINALIZE_HANDLER> m_finalizeHandler;
  103. OPT<VECTOR2D> m_picked;
  104. };
  105. class PICKER_TOOL : public TOOL_INTERACTIVE, public PICKER_TOOL_BASE
  106. {
  107. public:
  108. PICKER_TOOL();
  109. PICKER_TOOL( const std::string& aName );
  110. virtual ~PICKER_TOOL() = default;
  111. /// @copydoc TOOL_INTERACTIVE::Init()
  112. bool Init() override;
  113. /// @copydoc TOOL_INTERACTIVE::Reset()
  114. void Reset( RESET_REASON aReason ) override { }
  115. ///< Main event loop.
  116. int Main( const TOOL_EVENT& aEvent );
  117. protected:
  118. ///< Applies the requested VIEW_CONTROLS settings.
  119. void setControls();
  120. ///< @copydoc TOOL_INTERACTIVE::setTransitions();
  121. void setTransitions() override;
  122. };
  123. #endif /* PICKER_TOOL_H */