2014-02-27 16:29:08 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KICAD, a free EDA CAD application.
|
|
|
|
*
|
2019-08-14 08:28:07 +00:00
|
|
|
* Copyright (C) 2014-2019 CERN
|
2021-07-15 19:26:35 +00:00
|
|
|
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2014-02-27 16:29:08 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gal/graphics_abstraction_layer.h>
|
2019-05-08 18:56:03 +00:00
|
|
|
#include <gal/color4d.h>
|
2020-10-14 11:29:36 +00:00
|
|
|
#include <painter.h>
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <math/util.h> // for KiROUND
|
2019-05-08 18:56:03 +00:00
|
|
|
#include "tool/edit_points.h"
|
2014-02-27 16:29:08 +00:00
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
|
2014-03-07 09:26:33 +00:00
|
|
|
bool EDIT_POINT::WithinPoint( const VECTOR2I& aPoint, unsigned int aSize ) const
|
|
|
|
{
|
2014-03-12 09:46:11 +00:00
|
|
|
// Corners of the EDIT_POINT square
|
2014-03-07 09:26:33 +00:00
|
|
|
VECTOR2I topLeft = GetPosition() - aSize;
|
|
|
|
VECTOR2I bottomRight = GetPosition() + aSize;
|
|
|
|
|
|
|
|
return ( aPoint.x > topLeft.x && aPoint.y > topLeft.y &&
|
|
|
|
aPoint.x < bottomRight.x && aPoint.y < bottomRight.y );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-26 13:30:03 +00:00
|
|
|
EDIT_POINTS::EDIT_POINTS( EDA_ITEM* aParent )
|
|
|
|
: EDA_ITEM( NOT_USED ), m_parent( aParent ), m_allowPoints( true )
|
2014-02-27 16:29:08 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-02 17:58:12 +00:00
|
|
|
EDIT_POINT* EDIT_POINTS::FindPoint( const VECTOR2I& aLocation, KIGFX::VIEW *aView ) // fixme: ugly
|
2014-02-27 16:29:08 +00:00
|
|
|
{
|
2019-08-27 06:55:17 +00:00
|
|
|
unsigned size = std::abs( KiROUND( aView->ToWorld( EDIT_POINT::POINT_SIZE ) ) );
|
2014-02-27 16:29:08 +00:00
|
|
|
|
2019-08-26 13:30:03 +00:00
|
|
|
if( m_allowPoints )
|
2014-02-27 16:29:08 +00:00
|
|
|
{
|
2021-10-03 12:58:56 +00:00
|
|
|
for( EDIT_POINT& point : m_points )
|
2019-08-26 13:30:03 +00:00
|
|
|
{
|
|
|
|
if( point.WithinPoint( aLocation, size ) )
|
|
|
|
return &point;
|
|
|
|
}
|
2014-03-06 16:34:04 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 12:58:56 +00:00
|
|
|
for( EDIT_LINE& line : m_lines )
|
2014-03-06 16:34:04 +00:00
|
|
|
{
|
2015-01-16 15:44:42 +00:00
|
|
|
if( line.WithinPoint( aLocation, size ) )
|
|
|
|
return &line;
|
2014-02-27 16:29:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
return nullptr;
|
2014-02-27 16:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-16 15:44:42 +00:00
|
|
|
int EDIT_POINTS::GetContourStartIdx( int aPointIdx ) const
|
|
|
|
{
|
|
|
|
int lastIdx = 0;
|
|
|
|
|
2016-06-29 20:07:55 +00:00
|
|
|
for( int idx : m_contours )
|
2015-01-16 15:44:42 +00:00
|
|
|
{
|
|
|
|
if( idx >= aPointIdx )
|
|
|
|
return lastIdx;
|
|
|
|
|
|
|
|
lastIdx = idx + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastIdx;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int EDIT_POINTS::GetContourEndIdx( int aPointIdx ) const
|
|
|
|
{
|
2016-06-29 20:07:55 +00:00
|
|
|
for( int idx : m_contours )
|
2015-01-16 15:44:42 +00:00
|
|
|
{
|
|
|
|
if( idx >= aPointIdx )
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_points.size() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool EDIT_POINTS::IsContourStart( int aPointIdx ) const
|
|
|
|
{
|
2016-06-29 20:07:55 +00:00
|
|
|
for( int idx : m_contours )
|
2015-01-16 15:44:42 +00:00
|
|
|
{
|
|
|
|
if( idx + 1 == aPointIdx )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// the list is sorted, so we cannot expect it any further
|
|
|
|
if( idx > aPointIdx )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ( aPointIdx == 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool EDIT_POINTS::IsContourEnd( int aPointIdx ) const
|
|
|
|
{
|
2016-06-29 20:07:55 +00:00
|
|
|
for( int idx : m_contours )
|
2015-01-16 15:44:42 +00:00
|
|
|
{
|
|
|
|
if( idx == aPointIdx )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// the list is sorted, so we cannot expect it any further
|
|
|
|
if( idx > aPointIdx )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the end of the list surely is the end of a contour
|
|
|
|
return ( aPointIdx == (int) m_points.size() - 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EDIT_POINT* EDIT_POINTS::Previous( const EDIT_POINT& aPoint, bool aTraverseContours )
|
2014-03-07 09:26:33 +00:00
|
|
|
{
|
|
|
|
for( unsigned int i = 0; i < m_points.size(); ++i )
|
|
|
|
{
|
|
|
|
if( m_points[i] == aPoint )
|
|
|
|
{
|
2015-01-16 15:44:42 +00:00
|
|
|
if( !aTraverseContours && IsContourStart( i ) )
|
|
|
|
return &m_points[GetContourEndIdx( i )];
|
|
|
|
|
2014-03-07 09:26:33 +00:00
|
|
|
if( i == 0 )
|
|
|
|
return &m_points[m_points.size() - 1];
|
|
|
|
else
|
|
|
|
return &m_points[i - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
return nullptr;
|
2014-04-01 15:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EDIT_LINE* EDIT_POINTS::Previous( const EDIT_LINE& aLine )
|
|
|
|
{
|
2014-03-07 09:26:33 +00:00
|
|
|
for( unsigned int i = 0; i < m_lines.size(); ++i )
|
|
|
|
{
|
2014-04-01 15:18:43 +00:00
|
|
|
if( m_lines[i] == aLine )
|
2014-03-07 09:26:33 +00:00
|
|
|
{
|
|
|
|
if( i == 0 )
|
|
|
|
return &m_lines[m_lines.size() - 1];
|
|
|
|
else
|
|
|
|
return &m_lines[i - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
return nullptr;
|
2014-03-07 09:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-16 15:44:42 +00:00
|
|
|
EDIT_POINT* EDIT_POINTS::Next( const EDIT_POINT& aPoint, bool aTraverseContours )
|
2014-03-07 09:26:33 +00:00
|
|
|
{
|
|
|
|
for( unsigned int i = 0; i < m_points.size(); ++i )
|
|
|
|
{
|
|
|
|
if( m_points[i] == aPoint )
|
|
|
|
{
|
2015-01-16 15:44:42 +00:00
|
|
|
if( !aTraverseContours && IsContourEnd( i ) )
|
|
|
|
return &m_points[GetContourStartIdx( i )];
|
|
|
|
|
2014-03-07 09:26:33 +00:00
|
|
|
if( i == m_points.size() - 1 )
|
|
|
|
return &m_points[0];
|
|
|
|
else
|
|
|
|
return &m_points[i + 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
return nullptr;
|
2014-04-01 15:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EDIT_LINE* EDIT_POINTS::Next( const EDIT_LINE& aLine )
|
|
|
|
{
|
2014-03-07 09:26:33 +00:00
|
|
|
for( unsigned int i = 0; i < m_lines.size(); ++i )
|
|
|
|
{
|
2014-04-01 15:18:43 +00:00
|
|
|
if( m_lines[i] == aLine )
|
2014-03-07 09:26:33 +00:00
|
|
|
{
|
|
|
|
if( i == m_lines.size() - 1 )
|
|
|
|
return &m_lines[0];
|
|
|
|
else
|
|
|
|
return &m_lines[i + 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
return nullptr;
|
2014-03-07 09:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-08 07:45:52 +00:00
|
|
|
const BOX2I EDIT_POINTS::ViewBBox() const
|
|
|
|
{
|
|
|
|
BOX2I box;
|
|
|
|
bool empty = true;
|
|
|
|
|
|
|
|
for( const auto& point : m_points )
|
|
|
|
{
|
|
|
|
if( empty )
|
|
|
|
{
|
|
|
|
box.SetOrigin( point.GetPosition() );
|
|
|
|
empty = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
box.Merge( point.GetPosition() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for( const auto& line : m_lines )
|
|
|
|
{
|
|
|
|
if( empty )
|
|
|
|
{
|
|
|
|
box.SetOrigin( line.GetOrigin().GetPosition() );
|
|
|
|
box.SetEnd( line.GetEnd().GetPosition() );
|
|
|
|
empty = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
box.Merge( line.GetOrigin().GetPosition() );
|
|
|
|
box.Merge( line.GetEnd().GetPosition() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return box;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-02 17:58:12 +00:00
|
|
|
void EDIT_POINTS::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
|
2014-02-27 16:29:08 +00:00
|
|
|
{
|
2021-10-03 12:58:56 +00:00
|
|
|
KIGFX::GAL* gal = aView->GetGAL();
|
2020-09-24 01:27:23 +00:00
|
|
|
KIGFX::RENDER_SETTINGS* settings = aView->GetPainter()->GetSettings();
|
2021-09-16 10:14:43 +00:00
|
|
|
KIGFX::COLOR4D drawColor = settings->GetLayerColor( LAYER_AUX_ITEMS );
|
|
|
|
|
|
|
|
// Don't assume LAYER_AUX_ITEMS is always a good choice. Compare with background.
|
|
|
|
if( aView->GetGAL()->GetClearColor().Distance( drawColor ) < 0.5 )
|
|
|
|
drawColor.Invert();
|
|
|
|
|
|
|
|
// Linear darkening doesn't fit well with human color perception, and there's no guarantee
|
|
|
|
// that there's enough room for contrast either.
|
2021-11-01 16:08:16 +00:00
|
|
|
KIGFX::COLOR4D borderColor;
|
|
|
|
KIGFX::COLOR4D highlightColor;
|
2021-09-16 10:14:43 +00:00
|
|
|
double brightness = drawColor.GetBrightness();
|
|
|
|
|
|
|
|
if( brightness > 0.5 )
|
2021-11-01 16:08:16 +00:00
|
|
|
{
|
|
|
|
borderColor = drawColor.Darkened( 0.3 ).WithAlpha( 0.8 );
|
|
|
|
highlightColor = drawColor.Brightened( 0.6 ).WithAlpha( 0.8 );
|
|
|
|
}
|
2021-09-16 10:14:43 +00:00
|
|
|
else if( brightness > 0.2 )
|
2021-11-01 16:08:16 +00:00
|
|
|
{
|
|
|
|
borderColor = drawColor.Darkened( 0.6 ).WithAlpha( 0.8 );
|
|
|
|
highlightColor = drawColor.Brightened( 0.3 ).WithAlpha( 0.8 );
|
|
|
|
}
|
2021-09-16 10:14:43 +00:00
|
|
|
else
|
2021-11-01 16:08:16 +00:00
|
|
|
{
|
|
|
|
borderColor = drawColor.Brightened( 0.3 ).WithAlpha( 0.8 );
|
|
|
|
highlightColor = drawColor.Brightened( 0.6 ).WithAlpha( 0.8 );
|
|
|
|
}
|
2020-06-21 18:43:33 +00:00
|
|
|
|
|
|
|
gal->SetFillColor( drawColor );
|
2021-11-01 16:08:16 +00:00
|
|
|
gal->SetStrokeColor( borderColor );
|
2016-12-02 17:58:12 +00:00
|
|
|
gal->SetIsFill( true );
|
2020-09-24 01:27:23 +00:00
|
|
|
gal->SetIsStroke( true );
|
2016-12-02 17:58:12 +00:00
|
|
|
gal->PushDepth();
|
|
|
|
gal->SetLayerDepth( gal->GetMinDepth() );
|
|
|
|
|
2020-09-24 01:27:23 +00:00
|
|
|
double size = aView->ToWorld( EDIT_POINT::POINT_SIZE ) / 2.0;
|
|
|
|
double borderSize = aView->ToWorld( EDIT_POINT::BORDER_SIZE );
|
|
|
|
double hoverSize = aView->ToWorld( EDIT_POINT::HOVER_SIZE );
|
2014-02-27 16:29:08 +00:00
|
|
|
|
2020-10-22 00:32:56 +00:00
|
|
|
auto drawPoint =
|
|
|
|
[&]( const EDIT_POINT& aPoint, bool aDrawCircle = false )
|
|
|
|
{
|
|
|
|
if( aPoint.IsHover() || aPoint.IsActive() )
|
|
|
|
{
|
|
|
|
gal->SetStrokeColor( highlightColor );
|
|
|
|
gal->SetLineWidth( hoverSize );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-11-01 16:08:16 +00:00
|
|
|
gal->SetStrokeColor( borderColor );
|
2020-10-22 00:32:56 +00:00
|
|
|
gal->SetLineWidth( borderSize );
|
|
|
|
}
|
|
|
|
|
2021-11-01 16:08:16 +00:00
|
|
|
gal->SetFillColor( drawColor );
|
2020-10-22 00:32:56 +00:00
|
|
|
|
|
|
|
if( aDrawCircle )
|
|
|
|
gal->DrawCircle( aPoint.GetPosition(), size );
|
|
|
|
else
|
|
|
|
gal->DrawRectangle( aPoint.GetPosition() - size, aPoint.GetPosition() + size );
|
|
|
|
};
|
2020-06-21 18:43:33 +00:00
|
|
|
|
2020-10-22 00:32:56 +00:00
|
|
|
for( const EDIT_POINT& point : m_points )
|
|
|
|
drawPoint( point );
|
2014-02-27 16:29:08 +00:00
|
|
|
|
2016-06-29 20:07:55 +00:00
|
|
|
for( const EDIT_LINE& line : m_lines )
|
2020-10-22 00:32:56 +00:00
|
|
|
drawPoint( line, true );
|
2014-03-06 16:34:04 +00:00
|
|
|
|
2016-12-02 17:58:12 +00:00
|
|
|
gal->PopDepth();
|
2014-02-27 16:29:08 +00:00
|
|
|
}
|