Commit d6c1670c authored by Maciej Suminski's avatar Maciej Suminski

Templated EDIT_POINT_CONSTRAINT.

Removed EDIT_CONSTRAINT::Update() - replaced with resetting constraints.
EDIT_CONSTRAINT is stored in EDIT_POIN using boost::shared_ptr instead of pointer.
Added EDIT_LINE::GetEnd() & GetOrigin(). Overridden ApplyConstraint() for EDIT_LINE.
Side EDIT_POINTS for zones are drawn as circles.
parent 85e8b8bd
......@@ -142,13 +142,13 @@ void EDIT_POINTS::ViewDraw( int aLayer, KIGFX::GAL* aGal ) const
aGal->DrawRectangle( point.GetPosition() - size / 2, point.GetPosition() + size / 2 );
BOOST_FOREACH( const EDIT_LINE& line, m_lines )
aGal->DrawRectangle( line.GetPosition() - size / 2, line.GetPosition() + size / 2 );
aGal->DrawCircle( line.GetPosition(), size / 2 );
aGal->PopDepth();
}
void EPC_45DEGREE::Apply()
void EC_45DEGREE::Apply()
{
// Current line vector
VECTOR2I lineVector( m_constrained.GetPosition() - m_constrainer.GetPosition() );
......@@ -162,14 +162,17 @@ void EPC_45DEGREE::Apply()
}
EPC_LINE::EPC_LINE( EDIT_POINT& aConstrained, EDIT_POINT& aConstrainer ) :
EDIT_POINT_CONSTRAINT( aConstrained ), m_constrainer( aConstrainer )
EC_LINE::EC_LINE( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ), m_constrainer( aConstrainer )
{
Update();
// Compute line coefficients
VECTOR2D delta = m_constrainer.GetPosition() - m_constrained.GetPosition();
m_coefA = delta.y / delta.x;
m_coefB = m_constrainer.GetY() - m_coefA * m_constrainer.GetX();
}
void EPC_LINE::Apply()
void EC_LINE::Apply()
{
VECTOR2I position = m_constrained.GetPosition();
......@@ -189,16 +192,7 @@ void EPC_LINE::Apply()
}
void EPC_LINE::Update()
{
// Compute line coefficients
VECTOR2D delta = m_constrainer.GetPosition() - m_constrained.GetPosition();
m_coefA = delta.y / delta.x;
m_coefB = m_constrainer.GetY() - m_coefA * m_constrainer.GetX();
}
void EPC_CIRCLE::Apply()
void EC_CIRCLE::Apply()
{
VECTOR2I centerToEnd = m_end.GetPosition() - m_center.GetPosition();
VECTOR2I centerToPoint = m_constrained.GetPosition() - m_center.GetPosition();
......
......@@ -27,6 +27,8 @@
#include <vector>
#include <list>
#include <boost/shared_ptr.hpp>
#include <math/box2.h>
#include <base_struct.h>
......@@ -35,12 +37,13 @@
class EDIT_POINT;
/**
* Class EDIT_POINT_CONSTRAINT
* Class EDIT_CONSTRAINT
*
* Allows to describe constraints between two points. After the constrained point is changed,
* Allows to describe constraints between two edit handles. After the constrained handle is changed,
* Apply() has to be called to fix its coordinates according to the implemented constraint.
*/
class EDIT_POINT_CONSTRAINT
template<class EDIT_TYPE>
class EDIT_CONSTRAINT
{
public:
/**
......@@ -48,9 +51,9 @@ public:
*
* @param aConstrained is EDIT_POINT to which the constraint is applied.
*/
EDIT_POINT_CONSTRAINT( EDIT_POINT& aConstrained ) : m_constrained( aConstrained ) {};
EDIT_CONSTRAINT( EDIT_TYPE& aConstrained ) : m_constrained( aConstrained ) {};
virtual ~EDIT_POINT_CONSTRAINT() {};
virtual ~EDIT_CONSTRAINT() {};
/**
* Function Apply()
......@@ -59,17 +62,8 @@ public:
*/
virtual void Apply() = 0;
/**
* Function Update()
*
* Updates contraint's traits.
*/
virtual void Update()
{
}
protected:
EDIT_POINT& m_constrained; ///< Point that is constrained by rules implemented by Apply()
EDIT_TYPE& m_constrained; ///< Point that is constrained by rules implemented by Apply()
};
......@@ -88,12 +82,9 @@ public:
* @param aPoint stores coordinates for EDIT_POINT.
*/
EDIT_POINT( const VECTOR2I& aPoint ) :
m_position( aPoint ), m_constraint( NULL ) {};
m_position( aPoint ) {};
virtual ~EDIT_POINT()
{
delete m_constraint;
}
virtual ~EDIT_POINT() {}
/**
* Function GetPosition()
......@@ -153,12 +144,9 @@ public:
* Sets a constraint for and EDIT_POINT.
* @param aConstraint is the constraint to be set.
*/
void SetConstraint( EDIT_POINT_CONSTRAINT* aConstraint )
void SetConstraint( EDIT_CONSTRAINT<EDIT_POINT>* aConstraint )
{
if( m_constraint )
delete m_constraint;
m_constraint = aConstraint;
m_constraint.reset( aConstraint );
}
/**
......@@ -167,9 +155,9 @@ public:
* Returns the constraint imposed on an EDIT_POINT. If there are no constraints, NULL is
* returned.
*/
EDIT_POINT_CONSTRAINT* GetConstraint() const
EDIT_CONSTRAINT<EDIT_POINT>* GetConstraint() const
{
return m_constraint;
return m_constraint.get();
}
/**
......@@ -179,8 +167,7 @@ public:
*/
void ClearConstraint()
{
delete m_constraint;
m_constraint = NULL;
m_constraint.reset();
}
/**
......@@ -199,7 +186,7 @@ public:
*
* Corrects coordinates of an EDIT_POINT by applying previously set constraint.
*/
void ApplyConstraint()
virtual void ApplyConstraint()
{
if( m_constraint )
m_constraint->Apply();
......@@ -214,8 +201,11 @@ public:
static const int POINT_SIZE = 10;
protected:
VECTOR2I m_position; ///< Position of EDIT_POINT
EDIT_POINT_CONSTRAINT* m_constraint; ///< Constraint for the point, NULL if none
///> Position of EDIT_POINT
VECTOR2I m_position;
///> Constraint for the point, NULL if none
boost::shared_ptr<EDIT_CONSTRAINT<EDIT_POINT> > m_constraint;
};
......@@ -256,6 +246,36 @@ public:
m_end.SetPosition( m_end.GetPosition() + difference );
}
///> @copydoc EDIT_POINT::ApplyConstraint()
virtual void ApplyConstraint()
{
m_origin.ApplyConstraint();
m_end.ApplyConstraint();
if( m_constraint )
m_constraint->Apply();
}
/**
* Function GetOrigin()
*
* Returns the origin EDIT_POINT.
*/
EDIT_POINT& GetOrigin()
{
return m_origin;
}
/**
* Function GetEnd()
*
* Returns the end EDIT_POINT.
*/
EDIT_POINT& GetEnd()
{
return m_end;
}
bool operator==( const EDIT_POINT& aOther ) const
{
return GetPosition() == aOther.GetPosition();
......@@ -420,11 +440,11 @@ private:
/**
* Class EPC_VERTICAL.
* Class EC_VERTICAL.
*
* EDIT_POINT_CONSTRAINT that imposes a constraint that two points have to have the same X coordinate.
* EDIT_CONSTRAINT that imposes a constraint that two points have to have the same X coordinate.
*/
class EPC_VERTICAL : public EDIT_POINT_CONSTRAINT
class EC_VERTICAL : public EDIT_CONSTRAINT<EDIT_POINT>
{
public:
/**
......@@ -433,11 +453,11 @@ public:
* @param aConstrained is the point that is put under constrain.
* @param aConstrainer is the point that is the source of the constrain.
*/
EPC_VERTICAL( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
EDIT_POINT_CONSTRAINT( aConstrained ), m_constrainer( aConstrainer )
EC_VERTICAL( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ), m_constrainer( aConstrainer )
{}
///> @copydoc EDIT_POINT_CONSTRAINT::Apply()
///> @copydoc EDIT_CONSTRAINT::Apply()
virtual void Apply()
{
VECTOR2I point = m_constrained.GetPosition();
......@@ -451,11 +471,11 @@ private:
/**
* Class EPC_HORIZONTAL.
* Class EC_HORIZONTAL.
*
* EDIT_POINT_CONSTRAINT that imposes a constraint that two points have to have the same Y coordinate.
* EDIT_CONSTRAINT that imposes a constraint that two points have to have the same Y coordinate.
*/
class EPC_HORIZONTAL : public EDIT_POINT_CONSTRAINT
class EC_HORIZONTAL : public EDIT_CONSTRAINT<EDIT_POINT>
{
public:
/**
......@@ -464,11 +484,11 @@ public:
* @param aConstrained is the point that is put under constrain.
* @param aConstrainer is the point that is the source of the constrain.
*/
EPC_HORIZONTAL( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
EDIT_POINT_CONSTRAINT( aConstrained ), m_constrainer( aConstrainer )
EC_HORIZONTAL( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ), m_constrainer( aConstrainer )
{}
///> @copydoc EDIT_POINT_CONSTRAINT::Apply()
///> @copydoc EDIT_CONSTRAINT::Apply()
virtual void Apply()
{
VECTOR2I point = m_constrained.GetPosition();
......@@ -482,12 +502,12 @@ private:
/**
* Class EPC_45DEGREE
* Class EC_45DEGREE
*
* EDIT_POINT_CONSTRAINT that imposes a constraint that two to be located at angle of 45 degree
* EDIT_CONSTRAINT that imposes a constraint that two to be located at angle of 45 degree
* multiplicity.
*/
class EPC_45DEGREE : public EDIT_POINT_CONSTRAINT
class EC_45DEGREE : public EDIT_CONSTRAINT<EDIT_POINT>
{
public:
/**
......@@ -496,11 +516,11 @@ public:
* @param aConstrained is the point that is put under constrain.
* @param aConstrainer is the point that is the source of the constrain.
*/
EPC_45DEGREE( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
EDIT_POINT_CONSTRAINT( aConstrained ), m_constrainer( aConstrainer )
EC_45DEGREE( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer ) :
EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ), m_constrainer( aConstrainer )
{}
///> @copydoc EDIT_POINT_CONSTRAINT::Apply()
///> @copydoc EDIT_CONSTRAINT::Apply()
virtual void Apply();
private:
......@@ -509,37 +529,32 @@ private:
/**
* Class EPC_LINE
* Class EC_LINE
*
* EDIT_POINT_CONSTRAINT that imposes a constraint that a point has to lie on a line (determined
* EDIT_CONSTRAINT that imposes a constraint that a point has to lie on a line (determined
* by 2 points).
*/
class EPC_LINE : public EDIT_POINT_CONSTRAINT
class EC_LINE : public EDIT_CONSTRAINT<EDIT_POINT>
{
public:
EPC_LINE( EDIT_POINT& aConstrained, EDIT_POINT& aConstrainer );
EC_LINE( EDIT_POINT& aConstrained, const EDIT_POINT& aConstrainer );
///> @copydoc EDIT_POINT_CONSTRAINT::Apply()
///> @copydoc EDIT_CONSTRAINT::Apply()
virtual void Apply();
/**
* Function Update()
* Updates line coefficients that make the constraining line.
*/
void Update();
private:
EDIT_POINT& m_constrainer; ///< Point that imposes the constraint.
double m_coefA, m_coefB;
EDIT_POINT m_constrainer; ///< Point that imposes the constraint.
double m_coefA; ///< Line A coefficient (y = Ax + B)
double m_coefB; ///< Line B coefficient (y = Ax + B)
};
/**
* Class EPC_CIRCLE.
* Class EC_CIRCLE.
*
* EDIT_POINT_CONSTRAINT that imposes a constraint that a point has to lie on a circle.
* EDIT_CONSTRAINT that imposes a constraint that a point has to lie on a circle.
*/
class EPC_CIRCLE : public EDIT_POINT_CONSTRAINT
class EC_CIRCLE : public EDIT_CONSTRAINT<EDIT_POINT>
{
public:
/**
......@@ -549,11 +564,11 @@ public:
* @parama aCenter is the point that is the center of the circle.
* @parama aEnd is the point that decides on the radius of the circle.
*/
EPC_CIRCLE( EDIT_POINT& aConstrained, const EDIT_POINT& aCenter, const EDIT_POINT& aEnd ) :
EDIT_POINT_CONSTRAINT( aConstrained ), m_center( aCenter ), m_end( aEnd )
EC_CIRCLE( EDIT_POINT& aConstrained, const EDIT_POINT& aCenter, const EDIT_POINT& aEnd ) :
EDIT_CONSTRAINT<EDIT_POINT>( aConstrained ), m_center( aCenter ), m_end( aEnd )
{}
///> @copydoc EDIT_POINT_CONSTRAINT::Apply()
///> @copydoc EDIT_CONSTRAINT::Apply()
virtual void Apply();
private:
......
......@@ -72,7 +72,7 @@ public:
// Set constraints
// Arc end has to stay at the same radius as the start
(*points)[2].SetConstraint( new EPC_CIRCLE( (*points)[2], (*points)[0], (*points)[1] ) );
(*points)[2].SetConstraint( new EC_CIRCLE( (*points)[2], (*points)[0], (*points)[1] ) );
break;
case S_CIRCLE:
......@@ -114,8 +114,8 @@ public:
points->AddPoint( dimension->m_featureLineDO );
// Dimension height setting - edit points should move only along the feature lines
(*points)[0].SetConstraint( new EPC_LINE( (*points)[0], (*points)[2] ) );
(*points)[1].SetConstraint( new EPC_LINE( (*points)[1], (*points)[3] ) );
(*points)[0].SetConstraint( new EC_LINE( (*points)[0], (*points)[2] ) );
(*points)[1].SetConstraint( new EC_LINE( (*points)[1], (*points)[3] ) );
break;
}
......@@ -176,7 +176,7 @@ int POINT_EDITOR::OnSelectionChange( TOOL_EVENT& aEvent )
PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
EDA_ITEM* item = selection.items.GetPickedItem( 0 );
EDIT_POINT constrainer( VECTOR2I( 0, 0 ) );
boost::shared_ptr<EDIT_POINT_CONSTRAINT> degree45Constraint;
boost::shared_ptr<EDIT_CONSTRAINT<EDIT_POINT> > degree45Constraint;
m_editPoints = EDIT_POINTS_FACTORY::Make( item );
if( !m_editPoints )
......@@ -243,7 +243,7 @@ int POINT_EDITOR::OnSelectionChange( TOOL_EVENT& aEvent )
{
// Find a proper constraining point for 45 degrees mode
constrainer = get45DegConstrainer();
degree45Constraint.reset( new EPC_45DEGREE( *m_dragPoint, constrainer ) );
degree45Constraint.reset( new EC_45DEGREE( *m_dragPoint, constrainer ) );
}
else
{
......@@ -271,6 +271,7 @@ int POINT_EDITOR::OnSelectionChange( TOOL_EVENT& aEvent )
else if( evt->IsMouseUp( BUT_LEFT ) )
{
degree45Constraint.reset();
modified = false;
}
......@@ -441,15 +442,15 @@ void POINT_EDITOR::updateItem() const
else if( isModified( (*m_editPoints)[2] ) )
{
dimension->SetOrigin( wxPoint( m_dragPoint->GetPosition().x, m_dragPoint->GetPosition().y ) );
(*m_editPoints)[0].GetConstraint()->Update();
(*m_editPoints)[1].GetConstraint()->Update();
(*m_editPoints)[0].SetConstraint( new EC_LINE( (*m_editPoints)[0], (*m_editPoints)[2] ) );
(*m_editPoints)[1].SetConstraint( new EC_LINE( (*m_editPoints)[1], (*m_editPoints)[3] ) );
}
else if( isModified( (*m_editPoints)[3] ) )
{
dimension->SetEnd( wxPoint( m_dragPoint->GetPosition().x, m_dragPoint->GetPosition().y ) );
(*m_editPoints)[0].GetConstraint()->Update();
(*m_editPoints)[1].GetConstraint()->Update();
(*m_editPoints)[0].SetConstraint( new EC_LINE( (*m_editPoints)[0], (*m_editPoints)[2] ) );
(*m_editPoints)[1].SetConstraint( new EC_LINE( (*m_editPoints)[1], (*m_editPoints)[3] ) );
}
break;
......@@ -585,6 +586,6 @@ EDIT_POINT POINT_EDITOR::get45DegConstrainer() const
break;
}
// In any other case we may align item to the current cursor position. TODO wrong desc
// In any other case we may align item to its original position
return m_original;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment