67 std::unique_lock<std::mutex> lock(
m_mutex );
77 std::unique_lock<std::mutex> lock(
m_mutex );
95 if( aAcceptImmediately )
109 std::lock_guard<std::mutex> lock(
m_mutex );
125 std::unique_lock<std::mutex> lock(
m_mutex );
177 const std::chrono::milliseconds acceptanceTimeout(
180 m_activationHelper = std::make_unique<ACTIVATION_HELPER<std::unique_ptr<PENDING_BATCH>>>(
182 [
this]( std::unique_ptr<PENDING_BATCH>&& aAccepted )
186 wxCHECK_MSG( aAccepted !=
nullptr,
void(),
"Null proposal accepted" );
205 std::size_t hash =
hash_val( aIsPersistent );
216 std::unique_ptr<CONSTRUCTION_ITEM_BATCH> aBatch,
bool aIsPersistent )
218 if( aBatch->empty() )
225 bool acceptImmediately =
false;
232 acceptImmediately =
true;
242 std::make_unique<PENDING_BATCH>(
PENDING_BATCH{ std::move( *aBatch ), aIsPersistent } );
246 m_activationHelper->ProposeActivation( std::move( pendingBatch ), hash, acceptImmediately );
280 std::vector<CONSTRUCTION_ITEM_BATCH> persistentBatches, temporaryBatches;
284 if( aAcceptedBatch->IsPersistent )
291 bool anyNewItems =
false;
326 getInvolved( batch );
327 temporaryBatches.push_back( batch );
334 const auto addDrawables =
335 [&](
const std::vector<CONSTRUCTION_ITEM_BATCH>& aBatches,
bool aIsPersistent )
349 addDrawables( persistentBatches,
true );
350 addDrawables( temporaryBatches,
false );
372 std::vector<CONSTRUCTION_ITEM_BATCH>& aToExtend )
const
382 aToExtend.push_back( batch );
404 if( aDir.
x == 0 && aDir.
y == 0 )
418 if( dx < 0 || ( dx == 0 && dy < 0 ) )
433 if( normalized.
x == 0 && normalized.
y == 0 )
436 for(
size_t i = 0; i < aDirections.size(); ++i )
438 if( aDirections[i] == normalized )
439 return static_cast<int>( i );
448 std::vector<VECTOR2I> uniqueDirections;
449 uniqueDirections.reserve( aDirections.size() );
451 for(
const VECTOR2I& direction : aDirections )
455 if( normalized.
x == 0 && normalized.
y == 0 )
458 if( std::find( uniqueDirections.begin(), uniqueDirections.end(), normalized )
459 == uniqueDirections.end() )
461 uniqueDirections.push_back( normalized );
559 std::optional<int> aDistToNearest,
564 wxLogTrace(
traceSnap,
"GetNearestSnapLinePoint: cursor=(%d, %d), nearestGrid=(%d, %d), distToNearest=%s, snapRange=%d",
565 aCursor.
x, aCursor.
y, aNearestGrid.
x, aNearestGrid.
y,
566 aDistToNearest ? wxString::Format(
"%d", *aDistToNearest ) : wxString(
"none" ), aSnapRange );
570 wxLogTrace(
traceSnap,
" No snap line origin or no directions, returning nullopt" );
574 const bool gridBetterThanNearest = !aDistToNearest || *aDistToNearest > aSnapRange;
575 const bool gridActive = aGridSize.
x > 0 && aGridSize.
y > 0;
577 wxLogTrace(
traceSnap,
" snapLineOrigin=(%d, %d), directions count=%zu, gridBetterThanNearest=%d, gridActive=%d",
580 if( !gridBetterThanNearest )
582 wxLogTrace(
traceSnap,
" Grid not better than nearest, returning nullopt" );
586 const int escapeRange = 2 * aSnapRange;
589 wxLogTrace(
traceSnap,
" escapeRange=%d, longRangeEscapeAngle=%.1f deg",
590 escapeRange, longRangeEscapeAngle.
AsDegrees() );
596 double bestPerpDistance = std::numeric_limits<double>::max();
597 std::optional<VECTOR2I> bestSnapPoint;
605 if( dirLength == 0.0 )
607 wxLogTrace(
traceSnap,
" Direction %zu: zero length, skipping", ii );
611 VECTOR2D dirUnit = dirVector / dirLength;
613 double distanceAlong =
delta.Dot( dirUnit );
614 VECTOR2D projection = origin + dirUnit * distanceAlong;
618 wxLogTrace(
traceSnap,
" Direction %zu: dir=(%d, %d), perpDist=%.1f, distAlong=%.1f",
619 ii, direction.
x, direction.
y, perpDistance, distanceAlong );
621 if( perpDistance > aSnapRange )
623 wxLogTrace(
traceSnap,
" perpDistance > snapRange, skipping" );
627 bool escaped =
false;
629 if( perpDistance >= escapeRange )
633 double angleDiff = ( deltaAngle - directionAngle ).Normalize180().AsDegrees();
635 wxLogTrace(
traceSnap,
" In escape range: deltaAngle=%.1f, dirAngle=%.1f, angleDiff=%.1f",
641 wxLogTrace(
traceSnap,
" ESCAPED (angle diff too large)" );
647 wxLogTrace(
traceSnap,
" Not updating (escaped)" );
657 if( direction.
x == 0 && direction.
y != 0 )
660 snapPoint.
x = origin.
x;
661 snapPoint.
y = aNearestGrid.
y;
662 wxLogTrace(
traceSnap,
" Vertical line: snapping to grid Y, snapPoint=(%.1f, %.1f)",
663 snapPoint.
x, snapPoint.
y );
665 else if( direction.
y == 0 && direction.
x != 0 )
668 snapPoint.
x = aNearestGrid.
x;
669 snapPoint.
y = origin.
y;
670 wxLogTrace(
traceSnap,
" Horizontal line: snapping to grid X, snapPoint=(%.1f, %.1f)",
671 snapPoint.
x, snapPoint.
y );
676 VECTOR2D gridOriginD( aGridOrigin );
677 VECTOR2D relProjection = projection - gridOriginD;
680 double bestGridScore = std::numeric_limits<double>::max();
681 VECTOR2D bestGridPoint = projection;
683 for(
int dx = -1; dx <= 1; ++dx )
685 for(
int dy = -1; dy <= 1; ++dy )
687 double gridX = std::round( relProjection.
x / aGridSize.
x ) * aGridSize.
x + dx * aGridSize.
x;
688 double gridY = std::round( relProjection.
y / aGridSize.
y ) * aGridSize.
y + dy * aGridSize.
y;
689 VECTOR2D gridPt( gridX + gridOriginD.
x, gridY + gridOriginD.
y );
692 VECTOR2D gridDelta = gridPt - origin;
693 double gridDistAlong = gridDelta.
Dot( dirUnit );
694 VECTOR2D gridProjection = origin + dirUnit * gridDistAlong;
695 double gridPerpDist = ( gridPt - gridProjection ).EuclideanNorm();
698 double distFromCursor = ( gridPt -
cursor ).EuclideanNorm();
701 double score = gridPerpDist + distFromCursor * 0.1;
703 if( score < bestGridScore )
705 bestGridScore = score;
706 bestGridPoint = gridPt;
711 snapPoint = bestGridPoint;
712 wxLogTrace(
traceSnap,
" Diagonal line: snapping to grid intersection, snapPoint=(%.1f, %.1f)",
713 snapPoint.
x, snapPoint.
y );
718 wxLogTrace(
traceSnap,
" Grid not active, using projection" );
721 if( perpDistance < bestPerpDistance )
723 bestPerpDistance = perpDistance;
724 bestSnapPoint =
KiROUND( snapPoint );
725 wxLogTrace(
traceSnap,
" NEW BEST: perpDist=%.1f, snapPoint=(%d, %d)",
726 bestPerpDistance, bestSnapPoint->x, bestSnapPoint->y );
730 wxLogTrace(
traceSnap,
" Not updating (perpDist=%.1f >= bestPerp=%.1f)",
731 perpDistance, bestPerpDistance );
737 wxLogTrace(
traceSnap,
" RETURNING bestSnapPoint=(%d, %d)", bestSnapPoint->x, bestSnapPoint->y );
738 return *bestSnapPoint;
741 wxLogTrace(
traceSnap,
" RETURNING nullopt (no valid snap found)" );
778 std::vector<KIGFX::CONSTRUCTION_GEOM::SNAP_GUIDE> guides;
783 if( origin && !directions.empty() )
785 const std::optional<int> activeDirection =
m_snapLineManager.GetActiveDirection();
786 const int guideLength = 500000;
788 for(
size_t ii = 0; ii < directions.size(); ++ii )
790 const VECTOR2I& direction = directions[ii];
792 if( direction.
x == 0 && direction.
y == 0 )
795 VECTOR2I scaled = direction * guideLength;
798 guide.
Segment =
SEG( *origin - scaled, *origin + scaled );
800 if( activeDirection && *activeDirection ==
static_cast<int>( ii ) )
811 guides.push_back( guide );
827std::vector<CONSTRUCTION_MANAGER::CONSTRUCTION_ITEM_BATCH>
830 std::vector<CONSTRUCTION_MANAGER::CONSTRUCTION_ITEM_BATCH> batches;
835 snapLineOrigin.has_value() )
847 const std::optional<int> activeDirection =
m_snapLineManager.GetActiveDirection();
849 for(
size_t ii = 0; ii < directions.size(); ++ii )
851 const VECTOR2I& direction = directions[ii];
853 VECTOR2I scaledDirection = direction * 100000;
856 entry.
Drawable =
LINE{ *snapLineOrigin, *snapLineOrigin + scaledDirection };
857 entry.
LineWidth = ( activeDirection && *activeDirection ==
static_cast<int>( ii ) ) ? 2 : 1;
863 batches.push_back( std::move( batch ) );
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
void onTimerExpiry(wxTimerEvent &aEvent)
Timer expiry callback in the UI thread.
void acceptPendingProposal()
ACTIVATION_HELPER(std::chrono::milliseconds aTimeout, ACTIVATION_CALLBACK aCallback)
std::optional< std::size_t > m_lastAcceptedProposalTag
The last proposal that was accepted.
ACTIVATION_CALLBACK m_callback
Callback to call when the proposal is accepted.
std::chrono::milliseconds m_timeout
Activation timeout in milliseconds.
void ProposeActivation(T &&aProposal, std::size_t aProposalTag, bool aAcceptImmediately)
std::optional< std::size_t > m_pendingProposalTag
The last proposal tag that was made.
T m_lastProposal
The most recently-proposed item.
std::function< void(T &&)> ACTIVATION_CALLBACK
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
A color representation with 4 components: red, green, blue, alpha.
void GetConstructionItems(std::vector< CONSTRUCTION_ITEM_BATCH > &aToExtend) const
Get the list of additional geometry items that should be considered.
void ProposeConstructionItems(std::unique_ptr< CONSTRUCTION_ITEM_BATCH > aBatch, bool aIsPersistent)
Add a batch of construction items to the helper.
CONSTRUCTION_VIEW_HANDLER & m_viewHandler
bool HasActiveConstruction() const
CONSTRUCTION_MANAGER(CONSTRUCTION_VIEW_HANDLER &aViewHandler)
void CancelProposal()
Cancel outstanding proposals for new geometry.
std::deque< CONSTRUCTION_ITEM_BATCH > m_temporaryConstructionBatches
Temporary construction items are added and removed as needed.
void Clear()
Clear all construction items.
std::vector< CONSTRUCTION_ITEM > CONSTRUCTION_ITEM_BATCH
std::optional< CONSTRUCTION_ITEM_BATCH > m_persistentConstructionBatch
Within one "operation", there is one set of construction items that are "persistent",...
std::unique_ptr< ACTIVATION_HELPER< std::unique_ptr< PENDING_BATCH > > > m_activationHelper
unsigned getMaxTemporaryBatches() const
How many batches of temporary construction items can be active at once.
std::mutex m_batchesMutex
Protects the persistent and temporary construction batches.
bool InvolvesAllGivenRealItems(const std::vector< EDA_ITEM * > &aItems) const
Check if all 'real' (non-null = constructed) the items in the batch are in the list of items currentl...
void acceptConstructionItems(std::unique_ptr< PENDING_BATCH > aAcceptedBatchHash)
std::set< EDA_ITEM * > m_involvedItems
Set of all items for which construction geometry has been added.
Interface wrapper for the construction geometry preview with a callback to signal the view owner that...
CONSTRUCTION_VIEW_HANDLER(KIGFX::CONSTRUCTION_GEOM &aHelper)
KIGFX::CONSTRUCTION_GEOM & GetViewItem()
A base class for most all the KiCad significant classes used in schematics and boards.
A color representation with 4 components: red, green, blue, alpha.
Shows construction geometry for things like line extensions, arc centers, etc.
void AddDrawable(const DRAWABLE &aItem, bool aIsPersistent, int aLineWidth=1)
void SetSnapGuides(std::vector< SNAP_GUIDE > aGuides)
OPT_VECTOR2I GetNearestSnapLinePoint(const VECTOR2I &aCursor, const VECTOR2I &aNearestGrid, std::optional< int > aDistToNearest, int snapRange, const VECTOR2D &aGridSize=VECTOR2D(0, 0), const VECTOR2I &aGridOrigin=VECTOR2I(0, 0)) const
If the snap line is active, return the best snap point that is closest to the cursor.
OPT_VECTOR2I m_snapLineEnd
void SetDirections(const std::vector< VECTOR2I > &aDirections)
void SetSnappedAnchor(const VECTOR2I &aAnchorPos)
Inform this manager that an anchor snap has been made.
CONSTRUCTION_VIEW_HANDLER & m_viewHandler
void ClearSnapLine()
Clear the snap line origin and end points.
std::vector< VECTOR2I > m_directions
SNAP_MANAGER * m_snapManager
SNAP_LINE_MANAGER(CONSTRUCTION_VIEW_HANDLER &aViewHandler)
void SetSnapLineOrigin(const VECTOR2I &aOrigin)
The snap point is a special point that is located at the last point the cursor snapped to.
std::optional< int > m_activeDirection
void SetSnapLineEnd(const OPT_VECTOR2I &aSnapPoint)
Set the end point of the snap line.
OPT_VECTOR2I m_snapLineOrigin
A SNAP_MANAGER glues together the snap line manager and construction manager., along with some other ...
void SetSnapGuideColors(const KIGFX::COLOR4D &aBase, const KIGFX::COLOR4D &aHighlight)
KIGFX::COLOR4D m_snapGuideColor
KIGFX::COLOR4D m_snapGuideHighlightColor
GFX_UPDATE_CALLBACK m_updateCallback
CONSTRUCTION_MANAGER m_constructionManager
std::vector< CONSTRUCTION_MANAGER::CONSTRUCTION_ITEM_BATCH > GetConstructionItems() const
Get a list of all the active construction geometry, computed from the combined state of the snap line...
void updateView() override
SNAP_LINE_MANAGER m_snapLineManager
SNAP_MANAGER(KIGFX::CONSTRUCTION_GEOM &aHelper)
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
constexpr extended_type Dot(const VECTOR2< T > &aVector) const
Compute dot product of self with aVector.
static std::size_t HashConstructionBatchSources(const CONSTRUCTION_MANAGER::CONSTRUCTION_ITEM_BATCH &aBatch, bool aIsPersistent)
Construct a hash based on the sources of the items in the batch.
static VECTOR2I normalizeDirection(const VECTOR2I &aDir)
static std::optional< int > findDirectionIndex(const std::vector< VECTOR2I > &aDirections, const VECTOR2I &aDelta)
const wxChar *const traceSnap
Flag to enable snap/grid helper debug tracing.
static constexpr void hash_combine(std::size_t &seed)
This is a dummy function to take the final case of hash_combine below.
static constexpr std::size_t hash_val(const Types &... args)
The Cairo implementation of the graphics abstraction layer.
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
std::optional< VECTOR2I > OPT_VECTOR2I
KIGFX::CONSTRUCTION_GEOM::DRAWABLE Drawable
Items to be used for the construction of "virtual" anchors, for example, when snapping to a point inv...
std::vector< DRAWABLE_ENTRY > Constructions
CONSTRUCTION_ITEM_BATCH Batch
wxLogTrace helper definitions.
VECTOR2< int32_t > VECTOR2I
VECTOR2< double > VECTOR2D