Commit c59d104d authored by Robin Eschbach's avatar Robin Eschbach
Browse files

[ADD #41] undo and redo for adding and removing of vertices

parent 890dcea0
......@@ -26,38 +26,62 @@
#include "WConnectorData.h"
#include "action/WFiberActionAddVertex.h"
#include "action/WFiberActionRemoveVertex.h"
#include "WFiberHandler.h"
WFiberHandler::WFiberHandler( WMPointConnector* pointConnector )
{
m_pointConnector = pointConnector;
m_actionHandler = WActionHandler::SPtr( new WActionHandler() );
m_fibers = PCFiberListSPtr( new PCFiberList() );
m_fibers->push_back( PCFiber() );
m_selectedFiber = 0;
m_fiberCount = 1;
}
void WFiberHandler::addVertexToFiber( osg::Vec3 vertex )
void WFiberHandler::addVertexToFiber( osg::Vec3 vertex, bool silent )
{
m_fibers->at( m_selectedFiber ).push_back( vertex );
if( !silent )
{
m_actionHandler->pushAction( WFiberActionAddVertex::SPtr( new WFiberActionAddVertex( vertex, this ) ) );
}
}
void WFiberHandler::removeVertexFromFiber( osg::Vec3 vertex )
void WFiberHandler::addVertexToFiberAt( osg::Vec3 vertex, size_t position, bool silent )
{
auto fiber = m_fibers->begin() + m_selectedFiber;
fiber->emplace( fiber->begin() + position, vertex );
for( auto it = fiber->begin(); it != fiber->end(); )
if( !silent )
{
m_actionHandler->pushAction( WFiberActionAddVertex::SPtr( new WFiberActionAddVertex( vertex, this ) ) );
}
}
void WFiberHandler::removeVertexFromFiber( osg::Vec3 vertex, bool silent )
{
auto fiber = m_fibers->begin() + m_selectedFiber;
size_t position = 0;
for( auto it = fiber->begin(); it != fiber->end(); it++)
{
if( *it == vertex )
{
fiber->erase( it );
break;
}
else
{
it++;
}
position++;
}
if( !silent )
{
m_actionHandler->pushAction( WFiberActionRemoveVertex::SPtr( new WFiberActionRemoveVertex( vertex, position, this ) ) );
}
}
......@@ -84,6 +108,9 @@ void WFiberHandler::createProperties( WPropertyGroup::SPtr properties )
WPropertyHelper::PC_NOTEMPTY::addTo( m_fiberSelection );
m_addFiber = properties->addProperty( "Add Line", "Add Line", WPVBaseTypes::PV_TRIGGER_READY, notifier );
m_undoTrigger = properties->addProperty( "Undo", "Undo", WPVBaseTypes::PV_TRIGGER_READY, notifier );
m_redoTrigger = properties->addProperty( "Redo", "Redo", WPVBaseTypes::PV_TRIGGER_READY, notifier );
}
void WFiberHandler::updateProperty( WPropertyBase::SPtr property )
......@@ -99,7 +126,7 @@ void WFiberHandler::updateProperty( WPropertyBase::SPtr property )
m_possibleFiberSelections->addItem( ItemType::create( name, name, "", NULL ) );
m_fiberSelection->set( m_possibleFiberSelections->getSelectorLast() );
}
if( property == m_fiberSelection )
else if( property == m_fiberSelection )
{
m_selectedFiber = m_fiberSelection->get().getItemIndexOfSelected( 0 );
m_pointConnector->getConnectorData()->deselectPoint();
......@@ -108,9 +135,25 @@ void WFiberHandler::updateProperty( WPropertyBase::SPtr property )
m_pointConnector->updatePoints();
}
else if( property == m_undoTrigger && m_undoTrigger->get(true ) == WPVBaseTypes::PV_TRIGGER_TRIGGERED )
{
m_undoTrigger->set( WPVBaseTypes::PV_TRIGGER_READY, false );
m_actionHandler->undo();
}
else if( property == m_redoTrigger && m_redoTrigger->get(true ) == WPVBaseTypes::PV_TRIGGER_TRIGGERED )
{
m_redoTrigger->set( WPVBaseTypes::PV_TRIGGER_READY, false );
m_actionHandler->redo();
}
}
WFiberHandler::PCFiberListSPtr WFiberHandler::getFibers()
{
return m_fibers;
}
WMPointConnector* WFiberHandler::getPointConnector()
{
return m_pointConnector;
}
......@@ -38,6 +38,7 @@
#include "core/kernel/WModule.h"
#include "WMPointConnector.h"
#include "action/WActionHandler.h"
/**
* Handles the fibers of the WMPointsConnector.
......@@ -85,14 +86,25 @@ public:
/**
* Adds a vertex to the currently selected fiber.
* \param vertex The vertex to add.
* \param silent Whether or not this should add to the undo stack.
*/
void addVertexToFiber( osg::Vec3 vertex );
void addVertexToFiber( osg::Vec3 vertex, bool silent = false );
/**
* Adds a vertex to the currently selected fiber.
* \param vertex The vertex to add.
* \param position The position where to add the vertex.
* \param silent Whether or not this should add to the undo stack.
*/
void addVertexToFiberAt( osg::Vec3 vertex, size_t position, bool silent );
/**
* Removes a vertex from the currently selected fiber.
* \param vertex The vertex to remove.
* \param silent Whether or not this should add to the undo stack.
*/
void removeVertexFromFiber( osg::Vec3 vertex );
void removeVertexFromFiber( osg::Vec3 vertex, bool silent = false );
/**
* Selects the last point of the currently selected fiber.
......@@ -104,6 +116,8 @@ public:
*/
PCFiberListSPtr getFibers();
WMPointConnector* getPointConnector();
private:
/**
* Update handler for the properties
......@@ -116,6 +130,11 @@ private:
*/
WMPointConnector* m_pointConnector;
/**
* Handles the undo and redo action.
*/
WActionHandler::SPtr m_actionHandler;
/**
* Stores the amount of new created fibers.
*/
......@@ -141,6 +160,9 @@ private:
*/
WPropTrigger m_addFiber;
WPropTrigger m_undoTrigger;
WPropTrigger m_redoTrigger;
/**
* A pointer to the list of fibers.
*/
......
......@@ -112,6 +112,11 @@ public:
*/
void updatePoints();
/**
* Updates the fiber output
*/
void updateOutput();
/**
* \return boost::shared_ptr< WConnectorData > The WConnectorData of this module.
*/
......@@ -144,11 +149,6 @@ private:
*/
float hitVertex( osg::Vec3 rayStart, osg::Vec3 rayDir, osg::Vec3 vertex, float radius );
/**
* Updates the fiber output
*/
void updateOutput();
/**
* Creates the WMPointRenderer and runs it.
*/
......
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <vector>
#include "WActionHandler.h"
static const size_t MAX_STACK_SIZE = 30;
WActionHandler::WActionHandler()
{
m_undo = ActionStack( new std::vector< WFiberAction::SPtr >() );
m_redo = ActionStack( new std::vector< WFiberAction::SPtr >() );
}
WActionHandler::~WActionHandler()
{
m_undo->clear();
m_redo->clear();
}
void WActionHandler::pushAction( WFiberAction::SPtr action )
{
m_undo->push_back( action );
while( m_undo->size() > MAX_STACK_SIZE )
{
m_undo->erase( m_undo->begin() );
}
m_redo->clear();
}
void WActionHandler::undo()
{
if( m_undo->empty() )
{
return;
}
WFiberAction::SPtr action = m_undo->back();
action->undo();
m_undo->pop_back();
m_redo->push_back( action );
}
void WActionHandler::redo()
{
if( m_redo->empty() )
{
return;
}
WFiberAction::SPtr action = m_redo->back();
action->redo();
m_redo->pop_back();
m_undo->push_back( action );
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WACTIONHANDLER_H
#define WACTIONHANDLER_H
#include <vector>
#include "WFiberAction.h"
class WActionHandler
{
public:
typedef boost::shared_ptr< WActionHandler > SPtr;
WActionHandler();
~WActionHandler();
void pushAction( WFiberAction::SPtr action );
void undo();
void redo();
private:
typedef boost::shared_ptr< std::vector< WFiberAction::SPtr > > ActionStack;
ActionStack m_undo;
ActionStack m_redo;
};
#endif // WACTIONHANDLER_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include "WFiberAction.h"
WFiberAction::~WFiberAction()
{
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WFIBERACTION_H
#define WFIBERACTION_H
#include <boost/shared_ptr.hpp>
class WFiberAction
{
public:
typedef boost::shared_ptr< WFiberAction > SPtr;
virtual ~WFiberAction();
virtual void undo() = 0;
virtual void redo() = 0;
};
#endif // WFIBERACTION_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include "WFiberActionAddVertex.h"
#include "../WConnectorData.h"
WFiberActionAddVertex::WFiberActionAddVertex( osg::Vec3 vertex, WFiberHandler* fiberHandler ):
m_vertex( vertex ),
m_fiberHandler( fiberHandler )
{
}
WFiberActionAddVertex::~WFiberActionAddVertex()
{
}
void WFiberActionAddVertex::undo()
{
m_fiberHandler->removeVertexFromFiber( m_vertex, true );
m_fiberHandler->getPointConnector()->getConnectorData()->deselectPoint();
m_fiberHandler->selectLastPoint();
m_fiberHandler->getPointConnector()->updatePoints();
m_fiberHandler->getPointConnector()->updateOutput();
}
void WFiberActionAddVertex::redo()
{
m_fiberHandler->addVertexToFiber( m_vertex, true );
m_fiberHandler->getPointConnector()->getConnectorData()->deselectPoint();
m_fiberHandler->selectLastPoint();
m_fiberHandler->getPointConnector()->updatePoints();
m_fiberHandler->getPointConnector()->updateOutput();
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WFIBERACTIONADDVERTEX_H
#define WFIBERACTIONADDVERTEX_H
#include <boost/shared_ptr.hpp>
#include <osg/Geode>
#include "WFiberAction.h"
#include "../WFiberHandler.h"
class WFiberActionAddVertex : public WFiberAction
{
public:
typedef boost::shared_ptr< WFiberActionAddVertex > SPtr;
WFiberActionAddVertex( osg::Vec3 vertex, WFiberHandler* fiberHandler );
~WFiberActionAddVertex();
virtual void undo();
virtual void redo();
private:
osg::Vec3 m_vertex;
WFiberHandler* m_fiberHandler;
};
#endif // WFIBERACTIONADDVERTEX_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include "WFiberActionRemoveVertex.h"
#include "../WConnectorData.h"
WFiberActionRemoveVertex::WFiberActionRemoveVertex( osg::Vec3 vertex, size_t position, WFiberHandler* fiberHandler ):
m_vertex( vertex ),
m_position( position ),
m_fiberHandler( fiberHandler )
{
}
WFiberActionRemoveVertex::~WFiberActionRemoveVertex()
{
}
void WFiberActionRemoveVertex::undo()
{
m_fiberHandler->addVertexToFiberAt( m_vertex, m_position, true );
m_fiberHandler->getPointConnector()->getConnectorData()->deselectPoint();
m_fiberHandler->selectLastPoint();
m_fiberHandler->getPointConnector()->updatePoints();
m_fiberHandler->getPointConnector()->updateOutput();
}
void WFiberActionRemoveVertex::redo()
{
m_fiberHandler->removeVertexFromFiber( m_vertex, true );
m_fiberHandler->getPointConnector()->getConnectorData()->deselectPoint();
m_fiberHandler->selectLastPoint();
m_fiberHandler->getPointConnector()->updatePoints();
m_fiberHandler->getPointConnector()->updateOutput();
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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