/// <summary> /// UI Tileset element mouse down function /// </summary> /// <param name="sender">Sender object</param> /// <param name="e">Mouse button event arguments</param> private void TilesetElementMouseDown(object sender, MouseButtonEventArgs e) { Image img = (Image)sender; double imgLeft = Canvas.GetLeft(img); double imgTop = Canvas.GetTop(img); ///Setting border to new location SetTilesetBorderLocation(imgLeft, imgTop); ///Getting and setting new TilesetElement TilesetElement element = GetTileSetElementFromImage(img); if (img != null) { m_CurrentSelectedTileSetElement = element; } }
/// <summary> /// Loads Tileset /// </summary> /// <param name="loadedPath">Path to Tileset</param> /// <param name="tileWidth">Width per tile</param> /// <param name="tileHeight">Height per tile</param> public void LoadTileSet(string loadedPath, int tileWidth, int tileHeight) { //Image from loaded path BitmapImage image = new BitmapImage(new Uri(loadedPath, UriKind.Absolute)); m_LoadedTilesetPath = loadedPath; ///Amount of tiles to fill in x and y of grid based on width of tile double xAmountOfTile = image.PixelWidth / tileWidth; double yAmountOfTile = image.PixelHeight / tileHeight; ///Width and height of image based on amount of tiles need in an axes double widthImageInCanvas = m_TileSetCanvas.Width / xAmountOfTile; double heightImageInCanvas = m_TileSetCanvas.Height / yAmountOfTile; ///X and y image place counter int xPositionIndex = -1; int yPositionIndex = 0; //When Tileset was already loaded, make sure to reset that if (m_TileSetCanvas.Children.Count > 0) { m_TileSetCanvas.Children.Clear(); m_TileSetElements.Clear(); } for (int i = 0; i < xAmountOfTile * yAmountOfTile; i++) { ///Creating the actual Tileset image Image img = new Image(); img.Width = widthImageInCanvas; img.Height = widthImageInCanvas; img.MouseDown += new MouseButtonEventHandler(TilesetElementMouseDown); xPositionIndex++; ///When x amount in image in met, reset indexes if (xPositionIndex > xAmountOfTile - 1) { xPositionIndex = 0; yPositionIndex++; } //Creating a cropped zoomed in image based on image size and position indexes CroppedBitmap cb = new CroppedBitmap(image, new Int32Rect(0 + (tileWidth * xPositionIndex), 0 + (tileHeight * yPositionIndex), tileWidth, tileHeight)); img.Source = cb; ///Setting location of image Canvas.SetLeft(img, 0 + (widthImageInCanvas * xPositionIndex)); Canvas.SetTop(img, 0 + (widthImageInCanvas * yPositionIndex)); m_TileSetCanvas.Children.Add(img); ///Creating the tilesetElement to add to the array of Tileset elememnts TilesetElement tileElement = new TilesetElement(i, img, cb); m_TileSetElements.Add(tileElement); } ///Creating and defining a border to visually display a tileset element being selected Border b = new Border(); b.BorderThickness = new Thickness(1); b.BorderBrush = Brushes.Black; b.Width = widthImageInCanvas; b.Height = widthImageInCanvas; m_TileSetCanvas.Children.Add(b); SetTilesetBorderLocation(0, 0); }