123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2012 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- #include "platform/platform.h"
- #include "forest/editor/forestUndo.h"
- #include "forest/forestDataFile.h"
- #include "forest/editor/forestEditorCtrl.h"
- ForestUndoAction::ForestUndoAction( const Resource<ForestData> &data,
- ForestEditorCtrl *editor,
- const char *description )
- : UndoAction( description ),
- mEditor( editor ),
- mData( data )
- {
- }
- ForestCreateUndoAction::ForestCreateUndoAction( const Resource<ForestData> &data,
- ForestEditorCtrl *editor )
- : ForestUndoAction( data, editor, "Create Forest Items" )
- {
- }
- void ForestCreateUndoAction::addItem( ForestItemData *data,
- const Point3F &position,
- F32 rotation,
- F32 scale )
- {
- const ForestItem &item = mData->addItem( data, position, rotation, scale );
- mItems.push_back( item );
- // We store the datablock ID rather than the actual pointer
- // since the pointer could go bad.
- SimObjectId dataId = item.getData()->getId();
- mItems.last().setData( (ForestItemData*)(uintptr_t)dataId );
- }
- void ForestCreateUndoAction::redo()
- {
- for ( U32 i = 0; i < mItems.size(); i++ )
- {
- const ForestItem &item = mItems[i];
- // Not 64bit safe!
- // We store the datablock ID rather than the actual pointer
- // since the pointer could go bad.
- ForestItemData *data;
- if ( !Sim::findObject( (SimObjectId)(uintptr_t)(item.getData()), data ) )
- {
- Con::errorf( "ForestCreateUndoAction::redo() - ForestItemData for item to restore does not seem to exist. Undo stack may be hosed." );
- continue;
- }
- mData->addItem( item.getKey(),
- data,
- item.getTransform(),
- item.getScale() );
- }
- mEditor->onUndoAction();
- }
- void ForestCreateUndoAction::undo()
- {
- for ( S32 i = mItems.size()-1; i >= 0; i-- )
- {
- const ForestItem &item = mItems[i];
- mData->removeItem( item.getKey(), item.getPosition() );
- }
- mEditor->onUndoAction();
- }
- ForestDeleteUndoAction::ForestDeleteUndoAction( const Resource<ForestData> &data,
- ForestEditorCtrl *editor )
- : ForestUndoAction( data, editor, "Delete Forest Items" )
- {
- }
- void ForestDeleteUndoAction::removeItem( const ForestItem &item )
- {
- // Not 64bit safe!
- // We store the datablock ID rather than the actual pointer
- // since the pointer could go bad.
- SimObjectId dataId = item.getData()->getId();
- mItems.push_back( item );
- mItems.last().setData( (ForestItemData*)(uintptr_t)dataId );
- mData->removeItem( item.getKey(), item.getPosition() );
- }
- void ForestDeleteUndoAction::removeItem( const Vector<ForestItem> &itemList )
- {
- for ( S32 i = 0; i < itemList.size(); i++ )
- removeItem( itemList[i] );
- }
- void ForestDeleteUndoAction::redo()
- {
- for ( U32 i = 0; i < mItems.size(); i++ )
- {
- const ForestItem &item = mItems[i];
- mData->removeItem( item.getKey(), item.getPosition() );
- }
- mEditor->onUndoAction();
- }
- void ForestDeleteUndoAction::undo()
- {
- for ( S32 i = mItems.size()-1; i >= 0; i-- )
- {
- const ForestItem &item = mItems[i];
- // We store the datablock ID rather than the actual pointer
- // since the pointer could go bad.
- ForestItemData *data;
- if ( !Sim::findObject( (SimObjectId)(uintptr_t)(item.getData()), data ) )
- {
- Con::errorf( "ForestDeleteUndoAction::undo() - ForestItemData for item to restore does not seem to exist. Undo stack may be hosed." );
- continue;
- }
- mData->addItem( item.getKey(),
- data,
- item.getTransform(),
- item.getScale() );
- }
- mEditor->onUndoAction();
- }
- ForestUpdateAction::ForestUpdateAction( const Resource<ForestData> &data,
- ForestEditorCtrl *editor )
- : ForestUndoAction( data, editor, "Update Forest Items" )
- {
- }
- void ForestUpdateAction::saveItem( const ForestItem &item )
- {
- // We just store the current state... we undo it later.
- mItems.push_back( item );
- // We store the datablock ID rather than the actual pointer
- // since the pointer could go bad.
- SimObjectId dataId = item.getData()->getId();
- mItems.last().setData( (ForestItemData*)(uintptr_t)dataId );
- }
- void ForestUpdateAction::_swapState()
- {
- Vector<ForestItem> prevItems = mItems;
- mItems.clear();
-
- for ( U32 i=0; i < prevItems.size(); i++ )
- {
- const ForestItem &item = prevItems[i];
- // Save the current state so we can reverse this swap.
- //
- // Note that we do 'not' want the const ref returned by findItem
- // because when we call updateItem below we would lose our copy
- // of the items state before the call.
- //
- ForestItem newItem = mData->findItem( item.getKey() );
- if ( !newItem.isValid() )
- {
- Con::errorf( "ForestUpdateAction::_swapState() - saved item no longer exists. Undo stack may be hosed." );
- continue;
- }
- // Not 64bit safe!
- // We store the datablock ID rather than the actual pointer
- // since the pointer could go bad.
- ForestItemData *data;
- if ( !Sim::findObject( (SimObjectId)(uintptr_t)(item.getData()), data ) )
- {
- Con::errorf( "ForestUpdateAction::_swapState() - ForestItemData for item to restore does not seem to exist. Undo stack may be hosed." );
- continue;
- }
- // Now revert to the old state.
- mData->updateItem( item.getKey(),
- item.getPosition(),
- data,
- item.getTransform(),
- item.getScale() );
- // Save the state before this swap for the next swap.
- newItem.setData( (ForestItemData*)(uintptr_t)data->getId() );
- mItems.push_back( newItem );
- }
- mEditor->onUndoAction();
- }
|