Real-time highlighting for PlEditor delete tool.

This commit is contained in:
Jeff Young 2019-06-25 15:12:40 +01:00
parent 4eaba20cd3
commit 89d1d3d299
7 changed files with 102 additions and 24 deletions

View File

@ -275,22 +275,6 @@ int LIB_EDIT_TOOL::DoDelete( const TOOL_EVENT& aEvent )
}
static bool deleteItem( SCH_BASE_FRAME* aFrame, const VECTOR2D& aPosition )
{
EE_SELECTION_TOOL* selectionTool = aFrame->GetToolManager()->GetTool<EE_SELECTION_TOOL>();
wxCHECK( selectionTool, false );
aFrame->GetToolManager()->RunAction( EE_ACTIONS::clearSelection, true );
EDA_ITEM* item = selectionTool->SelectPoint( aPosition );
if( item )
aFrame->GetToolManager()->RunAction( EE_ACTIONS::doDelete, true );
return true;
}
#define HITTEST_THRESHOLD_PIXELS 5
@ -339,7 +323,7 @@ int LIB_EDIT_TOOL::DeleteItemCursor( const TOOL_EVENT& aEvent )
}
} );
picker->SetFinalizeHandler( [&]( const int& aFinalState )
picker->SetFinalizeHandler( [this] ( const int& aFinalState )
{
if( m_pickerItem )
m_toolMgr->GetTool<EE_SELECTION_TOOL>()->UnbrightenItem( m_pickerItem );

View File

@ -349,21 +349,70 @@ static bool deleteItem( PL_EDITOR_FRAME* aFrame, const VECTOR2D& aPosition )
}
#define HITTEST_THRESHOLD_PIXELS 5
int PL_EDIT_TOOL::DeleteItemCursor( const TOOL_EVENT& aEvent )
{
m_frame->SetTool( aEvent.GetCommandStr().get() );
Activate();
PL_PICKER_TOOL* picker = m_toolMgr->GetTool<PL_PICKER_TOOL>();
wxCHECK( picker, 0 );
m_pickerItem = nullptr;
picker->SetClickHandler( std::bind( deleteItem, m_frame, std::placeholders::_1 ) );
picker->SetFinalizeHandler( [&]( const int& aFinalState )
picker->SetClickHandler( [this] ( const VECTOR2D& aPosition ) -> bool
{
if( m_pickerItem )
{
if( aFinalState == PL_PICKER_TOOL::EVT_CANCEL )
m_frame->ClearToolStack();
} );
PL_SELECTION_TOOL* selectionTool = m_toolMgr->GetTool<PL_SELECTION_TOOL>();
selectionTool->UnbrightenItem( m_pickerItem );
selectionTool->AddItemToSel( m_pickerItem, true );
m_toolMgr->RunAction( ACTIONS::doDelete, true );
m_pickerItem = nullptr;
}
return true;
} );
picker->SetMotionHandler( [this] ( const VECTOR2D& aPos )
{
int threshold = KiROUND( getView()->ToWorld( HITTEST_THRESHOLD_PIXELS ) );
EDA_ITEM* item = nullptr;
for( WS_DATA_ITEM* dataItem : WS_DATA_MODEL::GetTheInstance().GetItems() )
{
for( WS_DRAW_ITEM_BASE* drawItem : dataItem->GetDrawItems() )
{
if( drawItem->HitTest( (wxPoint) aPos, threshold ) )
{
item = drawItem;
break;
}
}
}
if( m_pickerItem != item )
{
PL_SELECTION_TOOL* selectionTool = m_toolMgr->GetTool<PL_SELECTION_TOOL>();
if( m_pickerItem )
selectionTool->UnbrightenItem( m_pickerItem );
m_pickerItem = item;
if( m_pickerItem )
selectionTool->BrightenItem( m_pickerItem );
}
} );
picker->SetFinalizeHandler( [this] ( const int& aFinalState )
{
if( m_pickerItem )
m_toolMgr->GetTool<PL_SELECTION_TOOL>()->UnbrightenItem( m_pickerItem );
if( aFinalState == PL_PICKER_TOOL::EVT_CANCEL )
m_frame->ClearToolStack();
} );
picker->Activate();
Wait();

View File

@ -89,6 +89,8 @@ private:
///> Last cursor position (needed for getModificationPoint() to avoid changes
///> of edit reference point).
VECTOR2I m_cursor;
EDA_ITEM* m_pickerItem;
};
#endif //PL_EDIT_TOOL_H

View File

@ -103,6 +103,21 @@ int PL_PICKER_TOOL::Main( const TOOL_EVENT& aEvent )
setControls();
}
else if( evt->IsMotion() )
{
if( m_motionHandler )
{
try
{
(*m_motionHandler)( cursorPos );
}
catch( std::exception& e )
{
std::cerr << "PL_PICKER_TOOL motion handler error: " << e.what() << std::endl;
}
}
}
else if( TOOL_EVT_UTILS::IsCancelInteractive( *evt ) || evt->IsActivate() )
{
if( m_cancelHandler )
@ -163,6 +178,7 @@ void PL_PICKER_TOOL::resetPicker()
m_autoPanning = false;
m_picked = NULLOPT;
m_motionHandler = NULLOPT;
m_clickHandler = NULLOPT;
m_cancelHandler = NULLOPT;
m_finalizeHandler = NULLOPT;

View File

@ -45,6 +45,7 @@ public:
///> Event handler types.
typedef std::function<bool(const VECTOR2D&)> CLICK_HANDLER;
typedef std::function<void(const VECTOR2D&)> MOTION_HANDLER;
typedef std::function<void(void)> CANCEL_HANDLER;
typedef std::function<void(const int&)> FINALIZE_HANDLER;
@ -83,6 +84,16 @@ public:
m_clickHandler = aHandler;
}
/**
* Function SetMotionHandler()
* Sets a handler for mouse motion. Used for roll-over highlighting.
*/
inline void SetMotionHandler( MOTION_HANDLER aHandler )
{
wxASSERT( !m_motionHandler );
m_motionHandler = aHandler;
}
/**
* Function SetCancelHandler()
* Sets a handler for cancel events (ESC or context-menu Cancel).
@ -120,6 +131,7 @@ private:
bool m_autoPanning;
OPT<CLICK_HANDLER> m_clickHandler;
OPT<MOTION_HANDLER> m_motionHandler;
OPT<CANCEL_HANDLER> m_cancelHandler;
OPT<FINALIZE_HANDLER> m_finalizeHandler;

View File

@ -462,6 +462,18 @@ void PL_SELECTION_TOOL::RemoveItemsFromSel( EDA_ITEMS* aList, bool aQuietMode )
}
void PL_SELECTION_TOOL::BrightenItem( EDA_ITEM* aItem )
{
highlight( aItem, BRIGHTENED );
}
void PL_SELECTION_TOOL::UnbrightenItem( EDA_ITEM* aItem )
{
unhighlight( aItem, BRIGHTENED );
}
int PL_SELECTION_TOOL::ClearSelection( const TOOL_EVENT& aEvent )
{
ClearSelection();

View File

@ -102,6 +102,9 @@ public:
int RemoveItemsFromSel( const TOOL_EVENT& aEvent );
void RemoveItemsFromSel( EDA_ITEMS* aList, bool aQuietMode = false );
void BrightenItem( EDA_ITEM* aItem );
void UnbrightenItem( EDA_ITEM* aItem );
///> Clear current selection event handler.
int ClearSelection( const TOOL_EVENT& aEvent );