KiCad PCB EDA Suite
Loading...
Searching...
No Matches
arc_mode_session.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The 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, see <https://www.gnu.org/licenses/>.
18 */
19
21
23#include <tool/actions.h>
25#include <tool/tool_event.h>
26
27
29 SHAPE_DRAW_BEHAVIOR& aBehavior )
30{
31 ARC_DRAW_MODE mode = aSettings ? aSettings->m_ArcDrawMode : ARC_DRAW_MODE::CENTER_START_END;
32
33 if( std::optional<ARC_DRAW_MODE> requested = aEvent.ParameterIf<ARC_DRAW_MODE>() )
34 mode = *requested;
35
36 if( aSettings )
37 aSettings->m_ArcDrawMode = mode;
38
39 aBehavior.SetArcDrawMode( mode );
40 return mode;
41}
42
43
45 const std::optional<ARC_TANGENT_SEED>& aChainSeed, SEED_FINDER aFindSeed,
46 std::function<void( ARC_DRAW_MODE )> aOnModeChanged ) :
47 m_behavior( aBehavior ),
48 m_settings( aSettings ),
49 m_drawingArc( aDrawingArc ),
50 m_chainSeeded( aChainSeed.has_value() ),
51 m_findSeed( std::move( aFindSeed ) ),
52 m_onModeChanged( std::move( aOnModeChanged ) )
53{
54 if( aChainSeed )
55 m_behavior.SetTangentSeed( aChainSeed->m_start, aChainSeed->m_direction, aChainSeed->m_directionIsAxis );
56}
57
58
63
64
66{
67 if( !m_drawingArc )
68 return;
69
70 if( m_settings )
71 m_settings->m_ArcDrawMode = aMode;
72
73 m_behavior.SetArcDrawMode( aMode );
74 m_chainSeeded = false;
75 m_clickSeeded = false;
76
77 if( m_onModeChanged )
78 m_onModeChanged( aMode );
79}
80
81
83{
84 if( !m_drawingArc || aEvent.Category() != TC_COMMAND )
85 return false;
86
87 if( std::optional<ARC_DRAW_MODE> mode = aEvent.ParameterIf<ARC_DRAW_MODE>() )
88 {
89 SetMode( *mode );
90 return true;
91 }
92
93 if( aEvent.IsAction( &ACTIONS::cycleArcDrawMode ) )
94 {
96 return true;
97 }
98
99 return false;
100}
101
102
103void ARC_MODE_SESSION::PlacePoint( const VECTOR2I& aMouse, const VECTOR2I& aPos )
104{
105 const bool firstPoint = !m_chainSeeded && !m_clickSeeded && m_behavior.GetStep() == 0;
106
107 if( m_drawingArc && firstPoint && m_findSeed && Mode() == ARC_DRAW_MODE::TANGENT )
108 {
109 if( std::optional<ARC_TANGENT_SEED> seed = m_findSeed( aMouse, aPos ) )
110 {
111 m_behavior.SetTangentSeed( seed->m_start, seed->m_direction, seed->m_directionIsAxis );
112 m_clickSeeded = true;
113 return;
114 }
115 }
116
117 m_behavior.AddPoint( aPos );
118 m_clickSeeded = false;
119}
120
121
123{
124 // Reset() drops a seed but keeps the mode
125 if( m_clickSeeded )
126 {
127 m_behavior.Reset();
128 m_clickSeeded = false;
129 }
130 else
131 {
132 m_behavior.RemoveLastPoint();
133 }
134}
ARC_DRAW_MODE
Construction sequence used when interactively drawing a circular arc.
@ TANGENT
End only; start and departure direction come from a tangent seed.
@ CENTER_START_END
Center, then start (sets radius), then sweep to the end.
ARC_DRAW_MODE IncrementArcDrawMode(ARC_DRAW_MODE aMode)
Return the next mode in the cycle order used by the cycle-mode action.
ARC_DRAW_MODE StartArcDrawMode(const TOOL_EVENT &aEvent, APP_SETTINGS_BASE *aSettings, SHAPE_DRAW_BEHAVIOR &aBehavior)
Pick the mode a Draw Arc action starts in and apply it to aBehavior.
static TOOL_ACTION cycleArcDrawMode
Definition actions.h:269
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
ARC_DRAW_MODE m_ArcDrawMode
Construction sequence last used by the arc drawing tool in this application.
APP_SETTINGS_BASE * m_settings
void PlacePoint(const VECTOR2I &aMouse, const VECTOR2I &aPos)
Lock in a point, or seed a tangent arc from the graphic under it when it would be the arc's first poi...
void RemoveLastPoint()
Undo the last point. A seed taken from a click is undone too, one from a chain is kept.
SHAPE_DRAW_BEHAVIOR & m_behavior
ARC_DRAW_MODE Mode() const
bool HandleModeEvent(const TOOL_EVENT &aEvent)
Handle a mode or cycle action; return true if aEvent was one.
std::function< std::optional< ARC_TANGENT_SEED >(const VECTOR2I &aMouse, const VECTOR2I &aPos)> SEED_FINDER
Tangent seed from a graphic under the cursor, given the raw and the snapped cursor positions.
SEED_FINDER m_findSeed
void SetMode(ARC_DRAW_MODE aMode)
Switch mode mid-draw, which drops any seed. Does nothing unless an arc is being drawn.
ARC_MODE_SESSION(SHAPE_DRAW_BEHAVIOR &aBehavior, APP_SETTINGS_BASE *aSettings, bool aDrawingArc, const std::optional< ARC_TANGENT_SEED > &aChainSeed, SEED_FINDER aFindSeed, std::function< void(ARC_DRAW_MODE)> aOnModeChanged)
std::function< void(ARC_DRAW_MODE)> m_onModeChanged
Abstract interface for interactive shape-drawing behaviours.
virtual void SetArcDrawMode(ARC_DRAW_MODE aMode)
Select the construction sequence (circular arcs only; no-op for others).
Generic, UI-independent tool event.
Definition tool_event.h:167
std::optional< T > ParameterIf() const
Return the parameter when it holds a T, or std::nullopt otherwise.
Definition tool_event.h:472
TOOL_EVENT_CATEGORY Category() const
Return the category (eg. mouse/keyboard/action) of an event.
Definition tool_event.h:243
bool IsAction(const TOOL_ACTION *aAction) const
Test if the event contains an action issued upon activation of the given TOOL_ACTION.
STL namespace.
@ TC_COMMAND
Definition tool_event.h:53
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708