class MARKER rework: fix a few minor issues, remove duplicate code and add comments.

This commit is contained in:
jean-pierre charras 2018-12-19 15:07:07 +01:00
parent 64f1fb9e79
commit 15843ae01a
9 changed files with 148 additions and 122 deletions

View File

@ -8,40 +8,34 @@
#include "dialog_display_info_HTML_base.h"
static const wxPoint MarkerShapeCorners[] =
{
wxPoint( 0, 0 ),
wxPoint( 8, 1 ),
wxPoint( 4, 3 ),
wxPoint( 13, 8 ),
wxPoint( 9, 9 ),
wxPoint( 8, 13 ),
wxPoint( 3, 4 ),
wxPoint( 1, 8 )
};
const unsigned CORNERS_COUNT = DIM( MarkerShapeCorners );
void MARKER_BASE::DrawMarker( EDA_DRAW_PANEL* aPanel, wxDC* aDC, GR_DRAWMODE aDrawMode,
const wxPoint& aOffset )
{
wxPoint corners[CORNERS_COUNT];
// Build the marker shape polygon in internal units:
const int ccount = GetShapePolygonCornerCount();
std::vector<wxPoint> shape;
shape.reserve( ccount );
for( int ii = 0; ii < ccount; ii++ )
shape.push_back( wxPoint( GetShapePolygonCorner( ii ).x * MarkerScale(),
GetShapePolygonCorner( ii ).y * MarkerScale() ) );
GRSetDrawMode( aDC, aDrawMode );
for( unsigned ii = 0; ii < CORNERS_COUNT; ii++ )
for( int ii = 0; ii < ccount; ii++ )
{
corners[ii] = MarkerShapeCorners[ii];
corners[ii].x *= m_ScalingFactor;
corners[ii].y *= m_ScalingFactor;
corners[ii] += m_Pos + aOffset;
shape[ii] += m_Pos + aOffset;
}
GRClosedPoly( aPanel->GetClipBox(), aDC, CORNERS_COUNT, corners,
GRClosedPoly( aPanel->GetClipBox(), aDC, ccount, &shape[0],
true, // = Filled
0, // outline width
m_Color, // outline color
m_Color // fill collor
);
#if 0 // For testing purpose only:
EDA_RECT bbox = GetBoundingBoxMarker();
GRRect( aPanel->GetClipBox(), aDC, bbox, 10, m_Color );
#endif
}

View File

@ -31,7 +31,7 @@
#include "fctsys.h"
#include "gr_basic.h"
//#include "gr_basic.h"
#include "base_screen.h"
#include "common.h"
#include "macros.h"
@ -39,22 +39,22 @@
#include "dialog_display_info_HTML_base.h"
// Default marquer shape:
const int M_SHAPE_SCALE = 6; // default scaling factor for MarkerShapeCorners coordinates
/* The graphic shape of markers is a polygon.
* MarkerShapeCorners contains the coordinates of corners of the polygonal default shape
* actual coordinates are these values * .m_ScalingFactor
*/
static const wxPoint MarkerShapeCorners[] =
* they are arbitrary units to make coding shape easy.
* internal units coordinates are these values scaled by .m_ScalingFactor
*/
static const VECTOR2I MarkerShapeCorners[] =
{
wxPoint( 0, 0 ),
wxPoint( 8, 1 ),
wxPoint( 4, 3 ),
wxPoint( 13, 8 ),
wxPoint( 9, 9 ),
wxPoint( 8, 13 ),
wxPoint( 3, 4 ),
wxPoint( 1, 8 )
VECTOR2I( 0, 0 ),
VECTOR2I( 8, 1 ),
VECTOR2I( 4, 3 ),
VECTOR2I( 13, 8 ),
VECTOR2I( 9, 9 ),
VECTOR2I( 8, 13 ),
VECTOR2I( 3, 4 ),
VECTOR2I( 1, 8 ),
VECTOR2I( 0, 0 )
};
const unsigned CORNERS_COUNT = DIM( MarkerShapeCorners );
@ -67,16 +67,17 @@ void MARKER_BASE::init()
m_MarkerType = MARKER_UNSPEC;
m_ErrorLevel = MARKER_SEVERITY_UNSPEC;
m_Color = RED;
wxPoint start = MarkerShapeCorners[0];
wxPoint end = MarkerShapeCorners[0];
const VECTOR2I* point_shape = GetShapePolygon();
wxPoint start( point_shape->x, point_shape->y );
wxPoint end = start;
for( unsigned ii = 0; ii < CORNERS_COUNT; ii++ )
for( int ii = 1; ii < GetShapePolygonCornerCount(); ii++ )
{
wxPoint corner = MarkerShapeCorners[ii];
start.x = std::min( start.x, corner.x);
start.y = std::min( start.y, corner.y);
end.x = std::max( end.x, corner.x);
end.y = std::max( end.y, corner.y);
++point_shape;
start.x = std::min( start.x, point_shape->x);
start.y = std::min( start.y, point_shape->y);
end.x = std::max( end.x, point_shape->x);
end.y = std::max( end.y, point_shape->y);
}
m_ShapeBoundingBox.SetOrigin(start);
@ -97,7 +98,7 @@ MARKER_BASE::MARKER_BASE( const MARKER_BASE& aMarker )
MARKER_BASE::MARKER_BASE()
{
m_ScalingFactor = M_SHAPE_SCALE;
m_ScalingFactor = 1;
init();
}
@ -106,7 +107,7 @@ MARKER_BASE::MARKER_BASE( EDA_UNITS_T aUnits, int aErrorCode, const wxPoint& aMa
EDA_ITEM* aItem, const wxPoint& aPos,
EDA_ITEM* bItem, const wxPoint& bPos )
{
m_ScalingFactor = M_SHAPE_SCALE;
m_ScalingFactor = 1;
init();
SetData( aUnits, aErrorCode, aMarkerPos, aItem, aPos, bItem, bPos );
@ -117,7 +118,7 @@ MARKER_BASE::MARKER_BASE( int aErrorCode, const wxPoint& aMarkerPos,
const wxString& aText, const wxPoint& aPos,
const wxString& bText, const wxPoint& bPos )
{
m_ScalingFactor = M_SHAPE_SCALE;
m_ScalingFactor = 1;
init();
SetData( aErrorCode, aMarkerPos, aText, aPos, bText, bPos );
@ -127,7 +128,7 @@ MARKER_BASE::MARKER_BASE( int aErrorCode, const wxPoint& aMarkerPos,
MARKER_BASE::MARKER_BASE( int aErrorCode, const wxPoint& aMarkerPos,
const wxString& aText, const wxPoint& aPos )
{
m_ScalingFactor = M_SHAPE_SCALE;
m_ScalingFactor = 1;
init();
SetData( aErrorCode, aMarkerPos, aText, aPos );
@ -159,26 +160,43 @@ void MARKER_BASE::SetData( int aErrorCode, const wxPoint& aMarkerPos,
}
bool MARKER_BASE::HitTestMarker( const wxPoint& refPos ) const
bool MARKER_BASE::HitTestMarker( const wxPoint& aHitPosition ) const
{
wxPoint rel_pos = refPos - m_Pos;
rel_pos.x /= m_ScalingFactor;
rel_pos.y /= m_ScalingFactor;
EDA_RECT bbox = GetBoundingBoxMarker();
return m_ShapeBoundingBox.Contains( rel_pos );
return bbox.Contains( aHitPosition );
}
const VECTOR2I* MARKER_BASE::GetShapePolygon() const
{
return MarkerShapeCorners;
}
const VECTOR2I& MARKER_BASE::GetShapePolygonCorner( int aIdx ) const
{
return MarkerShapeCorners[aIdx];
}
int MARKER_BASE::GetShapePolygonCornerCount() const
{
return CORNERS_COUNT;
}
EDA_RECT MARKER_BASE::GetBoundingBoxMarker() const
{
wxSize realsize = m_ShapeBoundingBox.GetSize();
wxPoint realposition = m_ShapeBoundingBox.GetPosition();
realsize.x *= m_ScalingFactor;
realsize.y *= m_ScalingFactor;
realposition.x *= m_ScalingFactor;
realposition.y *= m_ScalingFactor;
realposition += m_Pos;
return EDA_RECT( m_Pos, realsize );
wxSize size_iu = m_ShapeBoundingBox.GetSize();
wxPoint position_iu = m_ShapeBoundingBox.GetPosition();
size_iu.x *= m_ScalingFactor;
size_iu.y *= m_ScalingFactor;
position_iu.x *= m_ScalingFactor;
position_iu.y *= m_ScalingFactor;
position_iu += m_Pos;
return EDA_RECT( position_iu, size_iu );
}

View File

@ -32,14 +32,18 @@
#include <trigo.h>
#include <msgpanel.h>
#include <bitmaps.h>
#include <base_units.h>
#include <sch_marker.h>
#include <erc.h>
/// Factor to convert the maker unit shape to internal units:
#define SCALING_FACTOR Millimeter2iu( 0.1 )
SCH_MARKER::SCH_MARKER() : SCH_ITEM( NULL, SCH_MARKER_T ), MARKER_BASE()
{
m_ScalingFactor = 8;
m_ScalingFactor = SCALING_FACTOR;
}
@ -47,7 +51,7 @@ SCH_MARKER::SCH_MARKER( const wxPoint& pos, const wxString& text ) :
SCH_ITEM( NULL, SCH_MARKER_T ),
MARKER_BASE( 0, pos, text, pos )
{
m_ScalingFactor = 8;
m_ScalingFactor = SCALING_FACTOR;
}

View File

@ -1,8 +1,8 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2004-2017 KiCad Developers, see change_log.txt for contributors.
* Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2004-2018 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License

View File

@ -1364,21 +1364,16 @@ void SCH_PAINTER::draw( SCH_BITMAP *aBitmap, int aLayer )
void SCH_PAINTER::draw( SCH_MARKER *aMarker, int aLayer )
{
const int scale = aMarker->m_ScalingFactor;
const int scale = aMarker->MarkerScale();
// If you are changing this, update the bounding box as well
const VECTOR2D arrow[] =
{
VECTOR2D( 0 * scale, 0 * scale ),
VECTOR2D( 8 * scale, 1 * scale ),
VECTOR2D( 4 * scale, 3 * scale ),
VECTOR2D( 13 * scale, 8 * scale ),
VECTOR2D( 9 * scale, 9 * scale ),
VECTOR2D( 8 * scale, 13 * scale ),
VECTOR2D( 3 * scale, 4 * scale ),
VECTOR2D( 1 * scale, 8 * scale ),
VECTOR2D( 0 * scale, 0 * scale )
};
// Build the marker shape polygon in internal units:
const int ccount = aMarker->GetShapePolygonCornerCount();
std::vector<VECTOR2D> arrow;
arrow.reserve( ccount );
for( int ii = 0; ii < ccount; ii++ )
arrow.push_back( VECTOR2D( aMarker->GetShapePolygonCorner( ii ).x * scale,
aMarker->GetShapePolygonCorner( ii ).y * scale ) );
COLOR4D color = m_schSettings.GetLayerColor( LAYER_ERC_WARN );
@ -1390,7 +1385,7 @@ void SCH_PAINTER::draw( SCH_MARKER *aMarker, int aLayer )
m_gal->SetFillColor( color );
m_gal->SetIsFill( true );
m_gal->SetIsStroke( false );
m_gal->DrawPolygon( arrow, sizeof( arrow ) / sizeof( VECTOR2D ) );
m_gal->DrawPolygon( &arrow[0], arrow.size() );
m_gal->Restore();
}

View File

@ -52,16 +52,15 @@ public:
};
wxPoint m_Pos; ///< position of the marker
int m_ScalingFactor; ///< Scaling factor for m_Size and m_Corners (can
///< set the physical size)
protected:
int m_ScalingFactor; ///< Scaling factor to convert corners coordinates
///< to internat units coordinates
TYPEMARKER m_MarkerType; ///< The type of marker (useful to filter markers)
MARKER_SEVERITY m_ErrorLevel; ///< Specify the severity of the error
COLOR4D m_Color; ///< color
EDA_RECT m_ShapeBoundingBox; ///< Bounding box of the graphic symbol, relative
///< to the position of the shape, used for Hit
///< Tests
///< to the position of the shape, in marker shape units
DRC_ITEM m_drc;
void init();
@ -116,6 +115,22 @@ public:
~MARKER_BASE();
/** The scaling factor to convert polygonal shape coordinates to internal units
*/
int MarkerScale() const { return m_ScalingFactor; }
/** @return the shape polygon corners list
*/
const VECTOR2I* GetShapePolygon() const;
/** @return the shape polygon corner aIdx
*/
const VECTOR2I& GetShapePolygonCorner( int aIdx ) const;
/** @return the default shape polygon corner count
*/
int GetShapePolygonCornerCount() const;
/**
* Function DrawMarker
* draws the shape is the polygon defined in m_Corners (array of wxPoints).
@ -125,7 +140,7 @@ public:
/**
* Function GetPos
* returns the position of this MARKER, const.
* @return the position of this MARKER in internal units.
*/
const wxPoint& GetPos() const
{
@ -229,12 +244,11 @@ public:
void DisplayMarkerInfo( EDA_DRAW_FRAME* aFrame );
/**
* Function HitTestMarker
* tests if the given wxPoint is within the bounds of this object.
* @param ref_pos A wxPoint to test
* Tests if the given wxPoint is within the bounds of this object.
* @param aHitPosition is the wxPoint to test (in internal units)
* @return bool - true if a hit, else false
*/
bool HitTestMarker( const wxPoint& ref_pos ) const;
bool HitTestMarker( const wxPoint& aHitPosition ) const;
/**
* Function GetBoundingBoxMarker

View File

@ -6,9 +6,9 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
* Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors.
* Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -41,9 +41,8 @@
#include <layers_id_colors_and_visibility.h>
/// Adjust the actual size of markers, when using default shape
#define SCALING_FACTOR Mils2iu( 3 )
/// Factor to convert the maker unit shape to internal units:
#define SCALING_FACTOR Millimeter2iu( 0.1 )
MARKER_PCB::MARKER_PCB( BOARD_ITEM* aParent ) :
BOARD_ITEM( aParent, PCB_MARKER_T ),
@ -146,5 +145,19 @@ void MARKER_PCB::ViewGetLayers( int aLayers[], int& aCount ) const
const EDA_RECT MARKER_PCB::GetBoundingBox() const
{
return EDA_RECT( m_Pos, wxSize( 1300000, 1300000 ) );
EDA_RECT bbox = m_ShapeBoundingBox;
wxPoint pos = m_Pos;
pos.x += int( bbox.GetOrigin().x * MarkerScale() );
pos.y += int( bbox.GetOrigin().y * MarkerScale() );
return EDA_RECT( pos, wxSize( int( bbox.GetWidth() * MarkerScale() ),
int( bbox.GetHeight() * MarkerScale() ) ) );
}
const BOX2I MARKER_PCB::ViewBBox() const
{
EDA_RECT bbox = GetBoundingBox();
return BOX2I( bbox.GetOrigin(), VECTOR2I( bbox.GetWidth(), bbox.GetHeight() ) );
}

View File

@ -1,8 +1,8 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2009-2014 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
* Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors.
* Copyright (C) 2009-2018 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
* Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -34,13 +34,14 @@
#include <class_board_item.h>
#include <marker_base.h>
// Coordinates count for the basic shape marker
#define MARKER_SHAPE_POINT_COUNT 9
class MSG_PANEL_ITEM;
class MARKER_PCB : public BOARD_ITEM, public MARKER_BASE
{
public:
MARKER_PCB( BOARD_ITEM* aParent );
@ -78,10 +79,6 @@ public:
return aItem && PCB_MARKER_T == aItem->Type();
}
/** A scaling factor to create the marker symbol
*/
static int MarkerScale() {return Millimeter2iu( 0.1 ); }
void Move(const wxPoint& aMoveVector) override
{
m_Pos += aMoveVector;
@ -113,12 +110,7 @@ public:
BITMAP_DEF GetMenuImage() const override;
const BOX2I ViewBBox() const override
{
// The following is based on the PCB_PAINTER::draw( const MARKER_PCB* )
// the value 13 comes from the max relative coordinate of the shape)
return BOX2I( m_Pos, VECTOR2I( 13*MarkerScale(), 13*MarkerScale() ) );
}
const BOX2I ViewBBox() const override;
const EDA_RECT GetBoundingBox() const override;

View File

@ -1243,20 +1243,16 @@ void PCB_PAINTER::draw( const PCB_TARGET* aTarget )
void PCB_PAINTER::draw( const MARKER_PCB* aMarker )
{
const int scale = MARKER_PCB::MarkerScale();
const int scale = aMarker->MarkerScale();
// If you are changing this, update MARKER_PCB::ViewBBox()
const VECTOR2D arrow[] = {
VECTOR2D( 0 * scale, 0 * scale ),
VECTOR2D( 8 * scale, 1 * scale ),
VECTOR2D( 4 * scale, 3 * scale ),
VECTOR2D( 13 * scale, 8 * scale ),
VECTOR2D( 9 * scale, 9 * scale ),
VECTOR2D( 8 * scale, 13 * scale ),
VECTOR2D( 3 * scale, 4 * scale ),
VECTOR2D( 1 * scale, 8 * scale ),
VECTOR2D( 0 * scale, 0 * scale )
};
// Build the marker shape polygon in internal units:
const int ccount = aMarker->GetShapePolygonCornerCount();
std::vector<VECTOR2D> arrow;
arrow.reserve( ccount );
for( int ii = 0; ii < ccount; ii++ )
arrow.push_back( VECTOR2D( aMarker->GetShapePolygonCorner( ii ).x * scale,
aMarker->GetShapePolygonCorner( ii ).y * scale ) );
auto strokeColor = m_pcbSettings.GetColor( aMarker, LAYER_DRC );
@ -1265,7 +1261,7 @@ void PCB_PAINTER::draw( const MARKER_PCB* aMarker )
m_gal->SetFillColor( strokeColor );
m_gal->SetIsFill( true );
m_gal->SetIsStroke( false );
m_gal->DrawPolygon( arrow, sizeof( arrow ) / sizeof( VECTOR2D ) );
m_gal->DrawPolygon( &arrow[0], arrow.size() );
m_gal->Restore();
}