// tileact.cpp -- tile actions // // Written by Frederic Bouvier, started May 2002. // // Copyright (C) 2002 Frederic Bouvier - fredb@users.sourceforge.net // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; either version 2 of the // License, or (at your option) any later version. // // This program 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 // General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. // // $Id: tileact.cpp,v 1.6 2005/05/09 07:02:15 fredb Exp $ #ifdef _MSC_VER #pragma warning( disable : 4786 4503 ) #endif #include "tileact.hpp" #include "mainwin.hpp" FGSD_ActionID FGSD_AddRemoveTileAction::_id; FGSD_AddRemoveTileAction::FGSD_AddRemoveTileAction( bool __add ) : _add( __add ) { } FGSD_AddRemoveTileAction::FGSD_AddRemoveTileAction( bool __add, const char *__name ) : _add( __add ) { _nameList.push_back( __name ); } bool FGSD_AddRemoveTileAction::undo() { bool ret = false; if ( _add ) { ret = _mainWindow->removeTiles( _nameList ); } else { ret = _mainWindow->loadTiles( _nameList ); } return ret; } bool FGSD_AddRemoveTileAction::redo( bool __step ) { bool ret = false; if ( _add ) { ret = _mainWindow->loadTiles( _nameList, __step ? FGSD_Action::_step : 0 ); } else { ret = _mainWindow->removeTiles( _nameList ); } return ret; } std::string FGSD_AddRemoveTileAction::name() const { return _add ? "add" : "remove"; } std::string FGSD_AddRemoveTileAction::description() const { return _add ? "add tiles" : "remove tiles"; } bool FGSD_AddRemoveTileAction::combine( FGSD_Action *__action ) { bool ret = false; FGSD_AddRemoveTileAction *act = (FGSD_AddRemoveTileAction *)__action->convertTo( FGSD_AddRemoveTileAction::_id ); if ( act && act->_add == _add ) { for ( NameList::iterator ni = _nameList.begin(); ni != _nameList.end(); ++ni ) act->_nameList.push_back( *ni ); ret = true; } return ret; } FGSD_ActionID FGSD_AddRemoveTileAction::type() const { return _id; } void *FGSD_AddRemoveTileAction::convertTo( FGSD_ActionID __id ) { return __id == _id ? (void *)this : 0; } FGSD_Action *FGSD_AddRemoveTileAction::clone() const { return new FGSD_AddRemoveTileAction( *this ); } bool FGSD_AddRemoveTileAction::save( const char *__project, SGPropertyNode *__node ) { bool ret = false; size_t nb = _nameList.size(); if ( nb ) { SGPropertyNode *node = __node->getChild( "family", 0, true ); node->setAttributes( SGPropertyNode::READ | SGPropertyNode::WRITE | SGPropertyNode::ARCHIVE ); node->setStringValue( "scenery-tiles" ); node = __node->getChild( "name", 0, true ); node->setAttributes( SGPropertyNode::READ | SGPropertyNode::WRITE | SGPropertyNode::ARCHIVE ); node->setStringValue( name().c_str() ); } for ( size_t i = 0; i < nb; i++ ) { SGPropertyNode *node = __node->getChild( "index", i, true ); if ( node ) { node->setAttributes( SGPropertyNode::READ | SGPropertyNode::WRITE | SGPropertyNode::ARCHIVE ); node->setStringValue( _nameList[ i ].c_str() ); ret = true; } } return ret; } bool FGSD_AddRemoveTileAction::load( const char *__project, SGPropertyNode *__node ) { bool ret = false; _nameList.clear(); std::vector cName = __node->getChildren( "index" ); size_t nb = cName.size(); for ( size_t i = 0; i < nb; i++ ) { std::string name = cName[ i ]->getStringValue(); _nameList.push_back( name ); } return ret; } size_t FGSD_AddRemoveTileAction::weight( SGPropertyNode *__node ) { std::vector cName = __node->getChildren( "index" ); return cName.size(); }