KiCad PCB EDA Suite
Loading...
Searching...
No Matches
block_drag_displacement.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
22#include <tool/edit_points.h>
23
24
25static EDIT_POINT* resolveInCollections( const std::vector<EDIT_POINTS*>& aCollections, const PCB_HANDLE_KEY& aKey,
26 EDIT_POINTS** aOwner = nullptr )
27{
28 for( EDIT_POINTS* points : aCollections )
29 {
30 if( EDIT_POINT* point = aKey.Resolve( *points ) )
31 {
32 if( aOwner )
33 *aOwner = points;
34
35 return point;
36 }
37 }
38
39 return nullptr;
40}
41
42
43std::map<PCB_HANDLE_KEY, VECTOR2I> BlockDragMoveOrigins( const std::vector<EDIT_POINTS*>& aCollections,
44 const std::set<PCB_HANDLE_KEY>& aPicked )
45{
46 std::map<PCB_HANDLE_KEY, VECTOR2I> origins;
47
48 for( const PCB_HANDLE_KEY& key : aPicked )
49 {
50 EDIT_POINTS* owner = nullptr;
51 EDIT_POINT* point = resolveInCollections( aCollections, key, &owner );
52
53 if( !point )
54 return {};
55
56 EDIT_LINE* line = dynamic_cast<EDIT_LINE*>( point );
57
58 if( !line )
59 {
60 origins.emplace( key, point->GetPosition() );
61 continue;
62 }
63
64 // The edge, which each frame drives through its own drag rule, plus its two
65 // corners, which the frame places first so that rule starts from known points.
66 origins.emplace( key, line->GetPosition() );
67
68 for( EDIT_POINT* endpoint : { &line->GetOrigin(), &line->GetEnd() } )
69 {
70 std::optional<PCB_HANDLE_KEY> endpointKey = PCB_HANDLE_KEY::Make( *owner, endpoint );
71
72 if( !endpointKey )
73 return {};
74
75 origins.emplace( *endpointKey, endpoint->GetPosition() );
76 }
77 }
78
79 return origins;
80}
81
82
83bool BlockDragApplyDisplacement( const std::vector<EDIT_POINTS*>& aCollections,
84 const std::map<PCB_HANDLE_KEY, VECTOR2I>& aOrigins, const VECTOR2I& aDelta,
85 const GRID_HELPER& aGrid )
86{
87 // Corners first, measured from where they started, so an edge begins every frame
88 // from the same geometry and a long drag cannot drift.
89 for( const auto& [key, origin] : aOrigins )
90 {
91 if( key.IsLine() )
92 continue;
93
94 EDIT_POINT* point = resolveInCollections( aCollections, key );
95
96 if( !point )
97 return false;
98
99 point->SetPosition( origin + aDelta );
100 }
101
102 // Now let each picked edge follow its corners, which is what holds a zone edge
103 // square to its neighbours. The corners already sit exactly where they belong, and
104 // setting the edge's own position would nudge them by the rounding in a midpoint.
105 for( const auto& [key, origin] : aOrigins )
106 {
107 if( !key.IsLine() )
108 continue;
109
110 EDIT_POINT* point = resolveInCollections( aCollections, key );
111
112 if( !point )
113 return false;
114
115 point->ApplyRelation( aGrid );
116 }
117
118 return true;
119}
bool BlockDragApplyDisplacement(const std::vector< EDIT_POINTS * > &aCollections, const std::map< PCB_HANDLE_KEY, VECTOR2I > &aOrigins, const VECTOR2I &aDelta, const GRID_HELPER &aGrid)
Put every handle at its starting position plus the drag's offset.
static EDIT_POINT * resolveInCollections(const std::vector< EDIT_POINTS * > &aCollections, const PCB_HANDLE_KEY &aKey, EDIT_POINTS **aOwner=nullptr)
std::map< PCB_HANDLE_KEY, VECTOR2I > BlockDragMoveOrigins(const std::vector< EDIT_POINTS * > &aCollections, const std::set< PCB_HANDLE_KEY > &aPicked)
Note every handle the drag will move, and where each one started.
Represent a line connecting two EDIT_POINTs.
EDIT_POINT & GetEnd()
Return the end EDIT_POINT.
EDIT_POINT & GetOrigin()
Return the origin EDIT_POINT.
virtual VECTOR2I GetPosition() const override
Return coordinates of an EDIT_POINT.
EDIT_POINTS is a VIEW_ITEM that manages EDIT_POINTs and EDIT_LINEs and draws them.
Represent a single point that can be used for modifying items.
Definition edit_points.h:44
virtual void ApplyRelation(const GRID_HELPER &aGrid)
Correct coordinates of an EDIT_POINT by applying its relation.
virtual void SetPosition(const VECTOR2I &aPosition)
Set new coordinates for an EDIT_POINT.
virtual VECTOR2I GetPosition() const
Return coordinates of an EDIT_POINT.
Definition edit_points.h:72
Lasting name for one handle in an EDIT_POINTS collection.
EDIT_POINT * Resolve(EDIT_POINTS &aPoints) const
Find this key's handle in a collection, usually after it was rebuilt.
static std::optional< PCB_HANDLE_KEY > Make(const EDIT_POINTS &aPoints, const EDIT_POINT *aPoint)
Build the key for a handle in a collection.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708