Commit 8f2f2668 authored by Vladimir Uryvaev's avatar Vladimir Uryvaev
Browse files

New features added to polyline drag in lib editor.

* VERTEX INSERT: when dragging middle of a edge a new vertex is added there;
* VERTEX DELETE: when dragging vertex onto its neighbour vertex  they're collapsed into one.
parent 37d4ed97
Loading
Loading
Loading
Loading
+29 −1
Original line number Diff line number Diff line
@@ -428,6 +428,8 @@ void LIB_POLYLINE::BeginEdit( int aEditMode, const wxPoint aPosition )
        int distanceMin = (aPosition - startPoint).x * (aPosition - startPoint).x
                          + (aPosition - startPoint).y * (aPosition - startPoint).y;

        wxPoint prevPoint = startPoint;

        // Find the right index of the point to be dragged
        BOOST_FOREACH( wxPoint point, m_PolyPoints )
        {
@@ -441,6 +443,17 @@ void LIB_POLYLINE::BeginEdit( int aEditMode, const wxPoint aPosition )
                m_ModifyIndex = index;
                distanceMin = distancePoint;
            }
            // check middle of an edge
            wxPoint offset = ( aPosition + aPosition - point - prevPoint );
            distancePoint = ( offset.x * offset.x + offset.y * offset.y ) / 4 + 1;
            if( distancePoint < distanceMin )
            {
                // Save point.
                m_initialPos = point;
                m_ModifyIndex = -index;  // negative indicates new vertex is to be inserted
                distanceMin = distancePoint;
            }
            prevPoint = point;

            index++;
        }
@@ -485,6 +498,16 @@ void LIB_POLYLINE::EndEdit( const wxPoint& aPosition, bool aAbort )
        if( m_PolyPoints[ m_PolyPoints.size() - 2 ] == m_PolyPoints.back() )
            m_PolyPoints.pop_back();
    }
    if( m_Flags == IS_RESIZED ) {
        if( m_PolyPoints.size() > 2 // do not delete last two points... keep it alive
            && ( m_ModifyIndex > 0
                 && m_PolyPoints[ m_ModifyIndex ] == m_PolyPoints[ m_ModifyIndex - 1 ]
                 || m_ModifyIndex < m_PolyPoints.size() - 1
                 && m_PolyPoints[ m_ModifyIndex ] == m_PolyPoints[ m_ModifyIndex + 1 ] ) )
        {
            m_PolyPoints.erase( m_PolyPoints.begin() + m_ModifyIndex ); // delete a point on this
        }
    }
    m_Flags = 0;
    SetEraseLastDrawItem( false );
}
@@ -499,6 +522,11 @@ void LIB_POLYLINE::calcEdit( const wxPoint& aPosition )
    }
    else if( m_Flags == IS_RESIZED )
    {
        if( m_ModifyIndex < 0 ) // negative indicates new vertex is to be inserted
        {
            m_ModifyIndex = -m_ModifyIndex;
            m_PolyPoints.insert( m_PolyPoints.begin() + m_ModifyIndex, aPosition );
        }
        m_PolyPoints[ m_ModifyIndex ] = aPosition;
    }
    else if( m_Flags == IS_MOVED )