1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
/****************************************************************
**
** Attal : Lords of Doom
**
** genericDecoration.cpp
** manage decoration
**
** Version : $Id: genericDecoration.cpp,v 1.8 2004/12/18 11:37:03 audoux Exp $
**
** Author(s) : Pascal Audoux
**
** Date : 10/06/2001
**
** Licence :
** 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, 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.
**
****************************************************************/
#include "genericDecoration.h"
// generic include files
// include files for QT
// application specific include files
#include "libCommon/dataTheme.h"
extern DataTheme DataTheme;
extern QString DATA_PATH;
GenericDecoration::GenericDecoration()
{
_group = 0;
}
GenericDecoration::~GenericDecoration()
{
clear();
}
void GenericDecoration::clear()
{
GenericMapDisposition::clear();
}
void GenericDecoration::save( QTextStream * ts, int indent )
{
indentation( ts, indent );
*ts << "<decoration>" << endl;
GenericMapDisposition::save( ts, indent + 1 );
indentation( ts, indent );
*ts << "</decoration>" << endl;
}
//
// ----- DecorationGroup -----
//
DecorationGroup::DecorationGroup()
{
_name = "";
_info = "";
_effectTypeList.setAutoDelete( true );
_effectParamList.setAutoDelete( true );
}
DecorationGroup::~DecorationGroup()
{
_effectTypeList.clear();
_effectParamList.clear();
}
void DecorationGroup::save( QTextStream * ts, int indent )
{
uint nb = count();
indentation( ts, indent );
*ts << "<group>" << endl;
indentation( ts, indent+1 );
*ts << "\t<name>" << _name << "</name>" << endl;
indentation( ts, indent+1 );
*ts << "\t<info>" << _info << "</info>" << endl;
for( uint i = 0; i < _effectTypeList.count(); ++i ) {
indentation( ts, indent+1 );
*ts << "\t<effect type=\"" << (uint)(* ( _effectTypeList.at( i ) ) ) << "\">";
*ts << ( * ( _effectParamList.at( i ) ) ) << "</effect>" << endl;
}
for( uint i = 0; i < nb; i++ ) {
at( i )->save( ts, indent + 1 );
}
indentation( ts, indent );
*ts << "</group>" << endl;
}
uint DecorationGroup::getEffectNumber()
{
return _effectTypeList.count();
}
DecorationGroup::EffectType DecorationGroup::getEffectType( uint num )
{
EffectType ret = NONE;
if( num < _effectTypeList.count() ) {
ret = * ( _effectTypeList.at( num ) );
}
return ret;
}
uint DecorationGroup::getEffectParam( uint num )
{
uint ret = 0;
if( num < _effectParamList.count() ) {
ret = *( _effectParamList.at( num ) );
}
return ret;
}
void DecorationGroup::addEffect( DecorationGroup::EffectType type, uint param )
{
_effectTypeList.append( new EffectType( type ) );
_effectParamList.append( new uint( param ) );
}
void DecorationGroup::clearEffects()
{
_effectTypeList.clear();
_effectParamList.clear();
}
QString DecorationGroup::getEffectTypeString( EffectType type )
{
QString ret = "Unknown";
switch( type ) {
case NONE:
ret = "None";
break;
case NO_MOVE:
ret = "No move";
break;
case DECREASE_MOVECOST:
ret = "Decrease move cost";
break;
case INCREASE_MOVECOST:
ret = "Increase move cost";
break;
case NO_TECHNICAL:
ret = "No technical";
break;
case MAX_TECHNICAL:
ret = "Max technical";
break;
}
return ret;
}
//
// ----- DecorationList -----
//
DecorationList::DecorationList()
{
setAutoDelete( true );
}
bool DecorationList::init()
{
clear();
DecorationHandler handler( this );
QFile file( DATA_PATH + "decorations.dat" );
QXmlInputSource source( file );
QXmlSimpleReader reader;
reader.setContentHandler( &handler );
reader.setErrorHandler( &handler );
bool ok = reader.parse( source );
file.close();
if ( !ok ) {
logEE( "Parse Error (%s) : %s", QString( DATA_PATH + "decorations.dat" ).latin1(), handler.errorProtocol().latin1() );
return false;
}
return true;
}
bool DecorationList::save()
{
QString filename = DATA_PATH + "decorations.dat";
QFile file( filename );
if (! file.open( IO_WriteOnly ) ) {
logEE( "Could not open file %s for writng\n", filename.latin1() );
return false;
}
QTextStream ts( &file );
ts << "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE decorations>" << endl;
ts << "<decorations>" << endl;
for( uint i = 0; i < count(); i++ ) {
at( i )->save( &ts, 1 );
}
ts << "</decorations>" << endl;
file.close();
return true;
}
//
// ----- DecorationHandler -----
//
DecorationHandler::DecorationHandler( DecorationList * list )
{
_list = list;
}
bool DecorationHandler::startDocument()
{
_errorProt = "";
_list->clear();
_state = StateInit;
_list->append( new DecorationGroup() );
return true;
}
bool DecorationHandler::startElement( const QString &, const QString &, const QString& qName, const QXmlAttributes& atts )
{
if( qName == "decorations" && _state == StateInit ) {
_state = StateDocument;
} else if ( qName == "group" && _state == StateDocument ) {
_state = StateGroup;
_group = new DecorationGroup();
} else if ( qName == "name" && _state == StateGroup ) {
_state = StateName;
} else if ( qName == "info" && _state == StateGroup ) {
_state = StateInfo;
} else if ( qName == "effect" && _state == StateGroup ) {
_state = StateEffect;
_type = (DecorationGroup::EffectType) atts.value( "type" ).toInt();
_param = 0;
} else if ( qName == "decoration" && _state == StateGroup ) {
_state = StateDecoration;
_decor = new GenericDecoration();
} else if ( qName == "disposition" && _state == StateDecoration ) {
_state = StateDisposition;
_height = atts.value( "height" ).toInt();
_width = atts.value( "width" ).toInt();
if( ( _height != 0 ) && ( _width != 0 ) ) {
_decor->init( _height, _width );
}
} else {
return false;
}
return true;
}
bool DecorationHandler::endElement( const QString &, const QString &, const QString & )
{
switch ( _state ) {
case StateGroup:
_list->append( _group );
_state = StateDocument;
break;
case StateName:
_state = StateGroup;
break;
case StateInfo:
_state = StateGroup;
break;
case StateEffect:
_state = StateGroup;
_group->addEffect( _type, _param );
break;
case StateDecoration:
_state = StateGroup;
_group->append( _decor );
_decor->setGroup( _group );
break;
case StateDisposition:
_state = StateDecoration;
break;
default:
// do nothing
break;
}
return true;
}
bool DecorationHandler::characters( const QString& ch )
{
QString ch_simplified = ch.simplifyWhiteSpace();
if ( ch_simplified.isEmpty() ) {
return true;
}
switch( _state ) {
case StateDisposition:{
if( ( _height > 0 ) && ( _width > 0 ) ) {
QStringList li = QStringList::split( " ", ch_simplified );
GenericMapDisposition::DispositionType type;
for( int i = 0; i < _height; i++ ) {
for( int j = 0; j < _width; j++ ) {
type = GenericMapDisposition::DispositionType( li[ i*_width + j ].toInt() );
_decor->setDisposition( i, j, type );
}
}
}
} break;
case StateName:
_group->setName( ch_simplified );
break;
case StateInfo:
_group->setInfo( ch_simplified );
break;
case StateEffect:
_param = ch_simplified.toInt();
break;
default:
return false;
}
return true;
}
bool DecorationHandler::fatalError( const QXmlParseException& exception )
{
_errorProt += QString( "fatal parsing error: %1 in line %2, column %3\n" )
.arg( exception.message() )
.arg( exception.lineNumber() )
.arg( exception.columnNumber() );
return QXmlDefaultHandler::fatalError( exception );
}
|