/// <summary> /// Rotate the sprite by the offset. /// </summary> /// <param name="dir">Direction to rotate.</param> public bool Rotate(RotateDirection dir) { if (PixelWidth == PixelHeight) return RotateSquareInPlace(dir); if (dir == RotateDirection.Clockwise180) return RotateRect180InPlace(); // Rotate rect sprite: int newX = TileHeight; int newY = TileWidth; int num_tiles = newX * newY; // Allocate the new tiles. Tile[] newTiles = new Tile[num_tiles]; for (int i = 0; i < num_tiles; i++) newTiles[i] = new Tile(this, m_ss.NextTileId++); // Rotate/copy data into new tiles. int pxOldWidth = PixelWidth; int pxOldHeight = PixelHeight; for (int ix = 0; ix < pxOldWidth; ix++) { for (int iy = 0; iy < pxOldHeight; iy++) { int y = iy; if (dir == RotateDirection.Clockwise90) y = pxOldHeight-1 - iy; // Calc tile/pixel index for rotated sprite. // Note that x,y are reversed for the rotated sprite. int tileNewX = y / Tile.TileSize; int pxNewX = y % Tile.TileSize; int tileNewY = ix / Tile.TileSize; int pxNewY = ix % Tile.TileSize; int nTileIndex = (tileNewY * TileHeight) + tileNewX; newTiles[nTileIndex].SetPixel(pxNewX, pxNewY, GetPixel(ix,iy)); } } // Switch over to the newly created tile data. m_tileWidth = newX; m_tileHeight = newY; m_Tiles = newTiles; return true; }
public bool Resize(int tileWidth, int tileHeight) { // If the new size == the old size if (tileWidth == m_tileWidth && tileHeight == m_tileHeight) return false; int oldX = m_tileWidth; // AKA: TileWidth int oldY = m_tileHeight; // AKA: TileHeight int old_tiles = m_tileWidth * m_tileHeight; // AKA: NumTiles int newX = tileWidth; int newY = tileHeight; int new_tiles = tileWidth * tileHeight; // If we are clipping any tiles with the new size if (oldX > newX || oldY > newY) { // Check to see if any of the clipped tiles contain data bool fHasData = false; // Handle case where the old sprite is wider than the new sprite: // e.g. old=4,2 xxOO old=4,2 xxOO old=4,4 xxOO // new=2,4 xxOO new=2,2 xxOO new=2,2 xxOO // nn ooOO // nn ooOO // (O = tile only in old sprite - checked by this loop, // o = tile only in old sprite - NOT checked by this loop, // n = tile only in new sprite - not checked, // x = tile shared in both sprites - not checked) for (int ix = newX; ix < oldX; ix++) { for (int iy = 0; iy < oldY; iy++) { if (!m_Tiles[(iy * oldX) + ix].IsEmpty()) fHasData = true; } } // Handle the case where the old sprite is taller than the new sprite: // e.g. old=2,4 xxnn old=2,4 xx old=4,4 xxoo // new=4,2 xxnn new=2,2 xx new=2,2 xxoo // OO OO OOoo // OO OO OOoo // Note that we can cut the x-loop short when oldX > newX since we've covered that case // in the above loop. int minX = (oldX < newX ? oldX : newX); for (int iy = newY; iy < oldY; iy++) { for (int ix = 0; ix < minX; ix++) { if (!m_Tiles[(iy * oldX) + ix].IsEmpty()) fHasData = true; } } // Make sure it's OK with the user before discarding clipped data //if (fHasData && !m_doc.AskYesNo("Resizing the sprite will cause some tiles to be clipped and the associated data will be lost.\nAre you sure you want to resize the sprite?")) if (fHasData && !m_doc.AskYesNo(ResourceMgr.GetString("WarnSpriteResizeClip"))) return false; } // Allocate the new tiles (copying over from the current tiles as needed) Tile[] newTiles = new Tile[new_tiles]; for (int ix = 0; ix < newX; ix++) { for (int iy = 0; iy < newY; iy++) { newTiles[(iy * newX) + ix] = new Tile(this, m_ss.NextTileId++); if (ix < oldX && iy < oldY) newTiles[(iy * newX) + ix].CopyData(m_Tiles[(iy * oldX) + ix]); } } // Switch over to the newly resized tile data m_tileWidth = newX; m_tileHeight = newY; m_Tiles = newTiles; return true; }
public void Init(Document doc, Spriteset ss, int nWidth, int nHeight, string strName, int id, string strDesc, int nSubpalette) { m_doc = doc; m_ss = ss; if (strName == "" || ss.HasNamedSprite(strName) ) strName = ss.AutoGenerateSpriteName(); m_strName = strName; m_strDesc = strDesc; SubpaletteID = nSubpalette; m_tileWidth = nWidth; m_tileHeight = nHeight; int nTiles = NumTiles; m_Tiles = new Tile[nTiles]; for (int i=0; i < nTiles; i++) m_Tiles[i] = new Tile(this, ss.NextTileId++); // Make an initial snapshot of the (empty) sprite. m_snapshot = GetUndoData(); }
public void ApplyUndoData(UndoData undo) { // If the sprite isn't the same size (in tiles) as the undo data, resize it. if (undo.width * undo.height != m_tileWidth * m_tileHeight) { int nTiles = undo.width * undo.height; m_Tiles = new Tile[nTiles]; for (int i = 0; i < nTiles; i++) m_Tiles[i] = new Tile(this, m_ss.NextTileId++); } m_strName = undo.name; m_strDesc = undo.desc; SubpaletteID = undo.subpalette; m_tileWidth = undo.width; m_tileHeight = undo.height; for (int i = 0; i < NumTiles; i++) m_Tiles[i].ApplyUndoData(undo.tiles[i]); RecordSnapshot(); }
/// <summary> /// Copy the data from the specified tile into this one /// </summary> /// <param name="t"></param> public void CopyData(Tile t) { for (int iRow = 0; iRow < TileSize; iRow++) { for (int iColumn = 0; iColumn < TileSize; iColumn++) SetPixel(iColumn, iRow, t.GetPixel(iColumn, iRow)); } FlushBitmaps(); }
public void Duplicate(Tile tileToCopy) { CopyData(tileToCopy); m_sprite = tileToCopy.m_sprite; }