public override void DrawCustomTilePreview(SpriteBatch spriteBatch, Rectangle previewRect, TileType tileType) { if (PaintType == PaintingType.Drawing) { Utility.DrawCustomTile(spriteBatch, previewRect, tileType); } }
public void PaintArea(TileLayer layer, int tileX, int tileY, Rectangle selectorRect, TileType tileType) { int selectorTileWidth = selectorRect.Width / layer.TileSize; int selectorTileHeight = selectorRect.Height / layer.TileSize; for (int x = 0; x < selectorTileWidth; x++) { for (int y = 0; y < selectorTileHeight; y++) { Rectangle tileRect = new Rectangle(selectorRect.X + (x * layer.TileSize), selectorRect.Y + (y * layer.TileSize), layer.TileSize, layer.TileSize); Tile replacementTile = TileFactory.Get(tileType, tileRect, layer.TilesetTileWidth); bool inBounds = tileX + x >= 0 && tileY + y >= 0 && tileX + x < layer.Width && tileY + y < layer.Height; if (inBounds) { if (layer.GetTile(tileX + x, tileY + y).Value == replacementTile.Value) continue; layer.SetTile(tileX + x, tileY + y, replacementTile); } } } }
public override void Use(TileLayer layer, int tileX, int tileY, Rectangle selectorRect, TileType tileType) { int tileWidth = selectorRect.Width / layer.TileSize; int tileHeight = selectorRect.Height / layer.TileSize; for (int x = 0; x < layer.Width; x += tileWidth) for (int y = 0; y < layer.Height; y += tileHeight) PaintArea(layer, x, y, selectorRect, tileType); }
public override void Use(TileLayer layer, int tileX, int tileY, Rectangle selectorRect, TileType tileType) { for (int x = 0; x < selection.Rectangle.Width / layer.TileSize; x += selectorRect.Width / layer.TileSize) { for (int y = 0; y < selection.Rectangle.Height / layer.TileSize; y += selectorRect.Height / layer.TileSize) { Rectangle r = new Rectangle(selectorRect.X, selectorRect.Y, selection.Rectangle.Width - (x * layer.TileSize), selection.Rectangle.Height - (y * layer.TileSize)); PaintArea(layer, x + (selection.Rectangle.X / layer.TileSize), y + (selection.Rectangle.Y / layer.TileSize), r, tileType); } } }
public static Tile Get(TileType tileType, Rectangle tileRect, int tilesetTileWidth) { switch (tileType.Identifier) { case RegularTile.Identifier: return new RegularTile(tileRect, tilesetTileWidth); case EmptyTile.Identifier: return new EmptyTile(); default: return new CustomTile(tileType); } }
public static void DrawCustomTile(SpriteBatch spriteBatch, Rectangle r, TileType tileType) { Texture2D workingPixel = whitePixel ?? CreateWhitePixel(spriteBatch.GraphicsDevice); const float alphaAmount = 0.5f; int colorR = (int)(tileType.Color.R * alphaAmount); int colorG = (int)(tileType.Color.G * alphaAmount); int colorB = (int)(tileType.Color.B * alphaAmount); int colorA = (int)(tileType.Color.A * alphaAmount); Color transparentColor = new Color(colorR, colorG, colorB, colorA); spriteBatch.Draw(workingPixel, r, transparentColor); }
public void DrawMap(Map map, Tool currentTool, Rectangle toolPreviewRect, Rectangle viewingRectangle, TileType tileType, bool showOtherLayers, bool showCollisionLayer, bool showGrid, bool renderDisplay) { Vector3 previewCamera = new Vector3(-viewingRectangle.X, -viewingRectangle.Y, 0f); spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, null, Matrix.CreateTranslation(previewCamera)); spriteBatch.GraphicsDevice.Clear(displayClearColor); if (renderDisplay) { DrawLayersWithToolPreviews(map, currentTool, toolPreviewRect, viewingRectangle, tileType, showOtherLayers, showCollisionLayer); if (showGrid) DrawGrid(map.TileSize, viewingRectangle); } spriteBatch.End(); }
private void DrawLayersWithToolPreviews(Map map, Tool currentTool, Rectangle toolPreviewRect, Rectangle viewingRectangle, TileType tileType, bool showOtherLayers, bool showCollisionLayer) { Rectangle currentViewingRectangle = new Rectangle(viewingRectangle.X / map.TileSize, viewingRectangle.Y / map.TileSize, viewingRectangle.Width / map.TileSize + 3, viewingRectangle.Height / map.TileSize + 3); // Draw a couple more just in case bool isCustomTile = tileType.Identifier != 0; foreach (TileLayer layer in map.TileLayers) { bool isLayerActive = map.ActiveLayerIndex == map.TileLayers.IndexOf(layer); if (isLayerActive) { if (!isCustomTile) { currentTool.DrawRegularTilePreview(spriteBatch, map.TilesetTexture, toolPreviewRect, selectorTileset.Rectangle); } layer.Draw(spriteBatch, toolPreviewRect, !isCustomTile, currentViewingRectangle); // Draws the layer and skips tiles covered by the tool preview } else { if (showOtherLayers) { layer.Draw(spriteBatch, currentViewingRectangle); } } } if (isCustomTile) currentTool.DrawCustomTilePreview(spriteBatch, toolPreviewRect, tileType); if (showCollisionLayer) map.CustomLayer.Draw(spriteBatch, toolPreviewRect, isCustomTile, currentViewingRectangle); // Draw the collision layer and skip tiles that are covered by the tool preview if (toolPreviewRect != Rectangle.Empty) Utility.DrawRectangle(spriteBatch, 3, toolPreviewRect, Color.Orange); // Draw border around selector }
public CustomTile(TileType tileType) { this.tileType = new TileType(tileType); }
public TileType(TileType copy) { Name = copy.Name; Identifier = copy.Identifier; Color = copy.Color; }
public abstract void Use(TileLayer layer, int tileX, int tileY, Rectangle selectorRect, TileType tileType);
public abstract void DrawCustomTilePreview(SpriteBatch spriteBatch, Rectangle previewRect, TileType tileType);
public override void DrawCustomTilePreview(SpriteBatch spriteBatch, Rectangle previewRect, TileType tileType) { // The fill tool has no preview }
public override void Use(TileLayer layer, int tileX, int tileY, Rectangle selectorRect, TileType tileType) { PaintArea(layer, tileX, tileY, selectorRect, tileType); }
private void PaintWithTool() { TileType tileType = new TileType(TileType.DefaultName, 1); if (tileTypesComboBox.SelectedIndex > 0) { tileType = new TileType(tileTypes[tileTypesComboBox.SelectedIndex]); } if (Tool.PaintType == Tool.PaintingType.Erasing) { tileType.Identifier = 0; } currentTool.Use(tileTypesComboBox.SelectedIndex == 0 ? map.ActiveLayer : map.CustomLayer, Tool.Position.X / map.TileSize, Tool.Position.Y / map.TileSize, selectorTileset.Rectangle, tileType); }
private void ParseTile(int currentLayerIndex, string tileContent, int x, int y) { int tileValue; if (int.TryParse(tileContent, out tileValue)) { int tilesetTileWidth = map.TilesetTexture.Width / mapDimensions.TileSize; int ts = mapDimensions.TileSize; int tileX = (tileValue - 1) % tilesetTileWidth * ts; int tileY = (tileValue - 1) / tilesetTileWidth * ts; Rectangle tileRect = new Rectangle(tileX, tileY, ts, ts); TileType tileType = null; if (currentLayerIndex != -1 && tileValue >= 0) { tileType = new TileType(TileType.DefaultName, tileValue == 0 ? EmptyTile.Identifier : RegularTile.Identifier); } else { foreach (TileType t in tileTypes.Where(t => t.Identifier == tileValue)) { tileType = t; } } Tile newTile = TileFactory.Get(tileType, tileRect, tilesetTileWidth); if (currentLayerIndex == -1) customLayer[y, x] = newTile; else tileLayers[currentLayerIndex][y, x] = newTile; } else { throw new FormatException(string.Format("The map file could not be read because the tile at layer {0}, row {1}, column {2} is malformed.", currentLayerIndex + 1, y + 1, x + 1)); } }
public override void DrawCustomTilePreview(SpriteBatch spriteBatch, Rectangle previewRect, TileType tileType) { Utility.DrawCustomTile(spriteBatch, previewRect, tileType); }
public void ReplaceTileType(int value, TileType newTileType) { for (int x = 0; x < width; x++) for (int y = 0; y < height; y++) if (GetTile(x, y).Value == value) SetTile(x, y, new CustomTile(newTileType)); }
private void ParseKeys(XDocument xReader) { BindingList<TileType> tempTileTypes = new BindingList<TileType>(); IEnumerable<XElement> keys = xReader.Descendants("CustomLayerKeys").Elements(); foreach (XElement key in keys) { XAttribute keyNameAttribute = key.Attribute("Name"); XAttribute keyIdentifierAttribute = key.Attribute("Identifier"); XAttribute keyColorAttribute = key.Attribute("Color"); if (keyNameAttribute == null || keyIdentifierAttribute == null || keyColorAttribute == null) throw new FormatException("The map file could not be read because a key name, identifier, or color is malformed."); string keyName = keyNameAttribute.Value; int keyIdentifier; if (!int.TryParse(keyIdentifierAttribute.Value, out keyIdentifier)) throw new FormatException(string.Format("The map file could not be read because the key identifier {0} is malformed.", keyIdentifierAttribute.Value)); int keyColorARGB; if (!int.TryParse(keyColorAttribute.Value, out keyColorARGB)) throw new FormatException(string.Format("The map file could not be read because the key color {0} is malformed.", keyColorAttribute.Value)); var keyColor = System.Drawing.Color.FromArgb(keyColorARGB); TileType tileType = new TileType(keyName, keyIdentifier, keyColor); tempTileTypes.Add(tileType); } tileTypes.Clear(); foreach (TileType t in tempTileTypes) { tileTypes.Add(t); } }