/**
* @brief Draws the tile image on a surface.
* @param dst_surface the surface to draw
* @param dst_position position where tile pattern should be drawn on dst_surface
* @param tileset the tileset of this tile
* @param viewport coordinates of the top-left corner of dst_surface relative
* to the map (may be used for scrolling tiles)
*/
void SimpleTilePattern::draw(Surface& dst_surface, const Rectangle& dst_position,
Tileset& tileset, const Rectangle& viewport) {
Surface& tileset_image = tileset.get_tiles_image();
tileset_image.draw_region(position_in_tileset, dst_surface, dst_position);
}
void Map::ParseText(const string &text)
{
// Create a tiny xml document and use it to parse the text.
TiXmlDocument doc;
doc.Parse(text.c_str());
// Check for parsing errors.
if (doc.Error())
{
has_error = true;
error_code = TMX_PARSING_ERROR;
error_text = doc.ErrorDesc();
return;
}
TiXmlNode *mapNode = doc.FirstChild("map");
TiXmlElement* mapElem = mapNode->ToElement();
// Read the map attributes.
mapElem->Attribute("version", &version);
mapElem->Attribute("width", &width);
mapElem->Attribute("height", &height);
mapElem->Attribute("tilewidth", &tile_width);
mapElem->Attribute("tileheight", &tile_height);
// Read the orientation
std::string orientationStr = mapElem->Attribute("orientation");
if (!orientationStr.compare("orthogonal"))
{
orientation = TMX_MO_ORTHOGONAL;
}
else if (!orientationStr.compare("isometric"))
{
orientation = TMX_MO_ISOMETRIC;
}
// Read the map properties.
const TiXmlNode *propertiesNode = mapElem->FirstChild("properties");
if (propertiesNode)
{
properties.Parse(propertiesNode);
}
// Iterate through all of the tileset elements.
const TiXmlNode *tilesetNode = mapNode->FirstChild("tileset");
while (tilesetNode)
{
// Allocate a new tileset and parse it.
Tileset *tileset = new Tileset();
tileset->Parse(tilesetNode->ToElement(), file_path);
if (tileset->HasError())
{
has_error = true;
error_code = 1;
error_text = tileset->GetErrorText();
return;
}
// Add the tileset to the list.
tilesets.push_back(tileset);
tilesetNode = mapNode->IterateChildren("tileset", tilesetNode);
}
// Iterate through all of the layer elements.
TiXmlNode *layerNode = mapNode->FirstChild("layer");
while (layerNode)
{
// Allocate a new layer and parse it.
Layer *layer = new Layer(this);
layer->Parse(layerNode);
// Add the layer to the list.
layers.push_back(layer);
layerNode = mapNode->IterateChildren("layer", layerNode);
}
// Iterate through all of the objectgroup elements.
TiXmlNode *objectGroupNode = mapNode->FirstChild("objectgroup");
while (objectGroupNode)
{
// Allocate a new object group and parse it.
ObjectGroup *objectGroup = new ObjectGroup();
objectGroup->Parse(objectGroupNode);
// Add the object group to the list.
object_groups.push_back(objectGroup);
objectGroupNode = mapNode->IterateChildren("objectgroup", objectGroupNode);
}
}
void Map::ParseText(const string &text)
{
// Create a tiny xml document and use it to parse the text.
TiXmlDocument doc;
doc.Parse(text.c_str());
// Check for parsing errors.
if (doc.Error())
{
has_error = true;
error_code = TMX_PARSING_ERROR;
error_text = doc.ErrorDesc();
return;
}
TiXmlNode *mapNode = doc.FirstChild("map");
TiXmlElement* mapElem = mapNode->ToElement();
// Read the map attributes.
mapElem->Attribute("version", &version);
mapElem->Attribute("width", &width);
mapElem->Attribute("height", &height);
mapElem->Attribute("tilewidth", &tile_width);
mapElem->Attribute("tileheight", &tile_height);
// Read the orientation
std::string orientationStr = mapElem->Attribute("orientation");
if (!orientationStr.compare("orthogonal"))
{
orientation = TMX_MO_ORTHOGONAL;
}
else if (!orientationStr.compare("isometric"))
{
orientation = TMX_MO_ISOMETRIC;
}
else if (!orientationStr.compare("staggered"))
{
orientation = TMX_MO_STAGGERED;
}
const TiXmlNode *node = mapElem->FirstChild();
int zOrder = 0;
while( node )
{
// Read the map properties.
if( strcmp( node->Value(), "properties" ) == 0 )
{
properties.Parse(node);
}
// Iterate through all of the tileset elements.
if( strcmp( node->Value(), "tileset" ) == 0 )
{
// Allocate a new tileset and parse it.
Tileset *tileset = new Tileset();
tileset->Parse(node->ToElement());
// Add the tileset to the list.
tilesets.push_back(tileset);
}
// Iterate through all of the layer elements.
if( strcmp( node->Value(), "layer" ) == 0 )
{
// Allocate a new layer and parse it.
Layer *layer = new Layer(this);
layer->Parse(node);
layer->SetZOrder( zOrder );
++zOrder;
// Add the layer to the list.
layers.push_back(layer);
}
// Iterate through all of the imagen layer elements.
if( strcmp( node->Value(), "imagelayer" ) == 0 )
{
// Allocate a new layer and parse it.
ImageLayer *imageLayer = new ImageLayer(this);
imageLayer->Parse(node);
imageLayer->SetZOrder( zOrder );
++zOrder;
// Add the layer to the list.
image_layers.push_back(imageLayer);
}
// Iterate through all of the objectgroup elements.
if( strcmp( node->Value(), "objectgroup" ) == 0 )
{
// Allocate a new object group and parse it.
ObjectGroup *objectGroup = new ObjectGroup();
objectGroup->Parse(node);
objectGroup->SetZOrder( zOrder );
++zOrder;
// Add the object group to the list.
object_groups.push_back(objectGroup);
//.........这里部分代码省略.........
/**
* @brief Displays the tile on a surface.
* @param destination the destination surface
* @param dst_position position of the tile pattern on the destination surface
* @param tileset the tileset of this tile pattern
*/
void AnimatedTilePattern::display(Surface *destination, const Rectangle &dst_position, Tileset &tileset) {
Surface *tileset_image = tileset.get_tiles_image();
tileset_image->blit(position_in_tileset[current_frames[sequence]], destination, dst_position);
}
void TilesetDock::exportTileset()
{
Tileset *tileset = currentTileset();
if (!tileset)
return;
if (tileset->isExternal())
return;
int mapTilesetIndex = mMapDocument->map()->tilesets().indexOf(tileset->sharedPointer());
if (mapTilesetIndex == -1)
return;
// To export a tileset we clone it, since the tileset could now be used by
// other maps. This ensures undo can take the map back to using an embedded
// tileset, without affecting those other maps.
SharedTileset externalTileset = tileset->clone();
FormatHelper<TilesetFormat> helper(FileFormat::ReadWrite);
Preferences *prefs = Preferences::instance();
QString suggestedFileName = prefs->lastPath(Preferences::ExternalTileset);
suggestedFileName += QLatin1Char('/');
suggestedFileName += externalTileset->name();
const QLatin1String extension(".tsx");
if (!suggestedFileName.endsWith(extension))
suggestedFileName.append(extension);
// todo: remember last used filter
QString selectedFilter = TsxTilesetFormat().nameFilter();
const QString fileName =
QFileDialog::getSaveFileName(this, tr("Export Tileset"),
suggestedFileName,
helper.filter(), &selectedFilter);
if (fileName.isEmpty())
return;
prefs->setLastPath(Preferences::ExternalTileset,
QFileInfo(fileName).path());
TilesetFormat *format = helper.formatByNameFilter(selectedFilter);
if (!format)
return; // can't happen
if (!format->write(*externalTileset, fileName)) {
QString error = format->errorString();
QMessageBox::critical(window(),
tr("Export Tileset"),
tr("Error saving tileset: %1").arg(error));
return;
}
externalTileset->setFileName(fileName);
externalTileset->setFormat(format);
QUndoCommand *command = new ReplaceTileset(mMapDocument,
mapTilesetIndex,
externalTileset);
mMapDocument->undoStack()->push(command);
// Make sure the external tileset is selected
int externalTilesetIndex = mTilesets.indexOf(externalTileset);
if (externalTilesetIndex != -1)
mTabBar->setCurrentIndex(externalTilesetIndex);
}
void MapperTilesetBox::mouseClicked(gcn::MouseEvent& mouseEvent){
InfraellyWindow::mouseClicked(mouseEvent);
if( mouseEvent.isConsumed() ){ return; };
//if click was from closeTilesetButton
if( mouseEvent.getSource() == closeTilesetButton ){
//consume mouse event
mouseEvent.consume();
//get the pointer to the tileset to remove
Tileset *toRemove = tilesetList->at( tilesetDropDown->getSelected() );
//check if the user is trying to dlete teh nullTS
if( toRemove != tilesetList->at(0) ){
//store number to remove
int removeIndex = tilesetDropDown->getSelected();
//change the selected ts to the nullTS
tilesetDropDown->setSelected(0);
//delete the entry from teh list
tilesetList->removeElementAt( removeIndex );
//if there is a map activley associated
if( world != NULL ){
//stip dependency from map working on
//if there IS dependency
if( !world->empty() ){
//cycle through layers
for( size_t i = 0; i < world->size(); ++i ){
//cycle thru tiles
for( size_t j = 0; j < world->getLayer(i).getHeight() * world->getLayer(i).getWidth(); ++j){
if( world->getLayer(i).index(j).getTileset() == toRemove ){
world->getLayer(i).index(j).setTileset(NULL);
}
}
}
}
}
//dependency of tileset stripped
//delete the tileset
cache::tilesets.erase(toRemove);
//remove from selected tile
selectedTile.setSource(NULL,0,0);
}
}
//click from the tilesetImage
if( mouseEvent.getSource() == tilesetImage ){
//consume mouse event
mouseEvent.consume();
if( !tilesetList->empty() ){
Tileset *selectedTs = selectedTile.getTileset();
if( selectedTs != NULL ){
// tileset select
int tileWidth = selectedTs->getTileWidth();
int tileHeight = selectedTs->getTileHeight();
// get the x-y co-ords reletive to the tileset region
int col = mouseEvent.getX() - (tilesetImage->getX()+selectedTs->getXOffset());
int row = mouseEvent.getY() - (tilesetImage->getY()+selectedTs->getYOffset());
// if its a multiple of tilewidth/height (ie in the barrier between)
if( col % tileWidth == 0){ ++col; };
if( row % tileHeight == 0){ ++row; };
// turn co-ords into row/col
col = col/tileWidth;
row = row/tileHeight;
// calculate the amount of tiles off the screen
int trueCol = tilesetImage->getX();
int trueRow = tilesetImage->getY();
// if its a multiple of tilewidth/height (ie in the barrier between)
if( trueCol % tileWidth == 0){ ++col; };
if( trueRow % tileHeight == 0){ ++row; };
// turn co-ords into row/col
trueCol = trueCol/tileWidth;
trueRow = trueRow/tileHeight;
//add the on screen offset to the offsecreen tiles
trueCol += col-1;
trueRow += row-1;
//store the tile's new row-col
selectedTile.setSource( selectedTs, trueCol, trueRow);
//store attribute if on atrib ts
if( selectedTile.getTileset() == attribTs ){
selectedTile.setAttribute(attribGrid[trueCol][trueRow]);
}
}// end if null
}//end if empty list
}//end if event from drop down*/
}
请发表评论