/// <summary> /// Call to check if the mouse is over this <see cref="CanvasPoint"/> /// </summary> /// <returns><see cref="CanvasPoint"/> when mouse is over the this</returns> public override CanvasObject MouseOver(Point mousePosition, Keys modifierKeys) { var width = Image.Width; var height = Image.Height; var topLeftPoint = ToPoint.Subtract(new Point(width / 2, height / 2)); var path = new GraphicsPath(); path.AddEllipse(topLeftPoint.X, topLeftPoint.Y, width, height); return(path.IsVisible(mousePosition) ? this : null); }
/// <summary> /// Draws the <see cref="CanvasPoint"/> /// </summary> public override void Draw(Graphics graphics) { if (!Visible) { return; } var imageCenter = new Point(Image.Width / 2, Image.Height / 2); var topLeftPoint = ToPoint.Subtract(imageCenter); graphics.DrawImage(Image, topLeftPoint); }
private void loadImages(XmlNode imagesNode, string rootFolder) { // Do nothing if the given node does not exist. if (imagesNode == null) { return; } // Load the images. foreach (XmlNode imageNode in imagesNode) { // Get the URI of the image from the node. if (!imageNode.GetAttributeValue(uriAttributeName, out string imageURI)) { throw new Exception($"{imagesNodeName} node was missing {uriAttributeName} attribute."); } // Load the texture. Texture2D texture = ContentManager.Load <Texture2D>(Path.Combine(rootFolder, imageURI)); // Add the root image to the dictionary. AddImage(new Image(imageNode.Name, texture, texture.Bounds)); // If a tilemap tag was given and it parses into a point, use the image as a tilemap. bool usingGrid = imageNode.TryParseAttributeValue(tilemapTagName, ToPoint.TryParse, out Point tilemapSize); Point currentGridCell = Point.Zero; Point tileSize = (usingGrid) ? (texture.Bounds.Size.ToVector2() / tilemapSize.ToVector2()).ToPoint() : Point.Zero; // If there are child nodes, add them as images. foreach (XmlNode sourceNode in imageNode) { // Create a new source rectangle to be used. Rectangle sourceRectangle = Rectangle.Empty; // If a bounds tag was given, try to use that. if (sourceNode.GetAttributeValue(boundsTagName, out string boundsString)) { if (!ToRectangle.TryParse(boundsString, out sourceRectangle)) { throw new Exception($"Image source with name {sourceNode.Name} has an invalid {boundsTagName} attribute."); } } // Otherwise; if no bounds tag was given but a grid is being used, use that. else if (usingGrid) { // If a specific cell position was given, use that. if (sourceNode.GetAttributeValue(tilePositionTagName, out string tilePositionString)) { // Ensure the tile position is valid. if (!ToPoint.TryParse(tilePositionString, out currentGridCell)) { throw new Exception($"Image source with name {sourceNode.Name} has an invalid {tilePositionTagName} attribute."); } // Get the source rectangle from this grid position. sourceRectangle = new Rectangle(tileSize * currentGridCell, tileSize); } // Otherwise; increment the tile position. else { // Get the source rectangle from the current position. sourceRectangle = new Rectangle(tileSize * currentGridCell, tileSize); // Increment the tile position. if (currentGridCell.X + 1 >= tilemapSize.X) { currentGridCell.X = 0; currentGridCell.Y++; } else { currentGridCell.X++; } } } // Finally, if nothing at all was given and a grid isn't being used, throw an exception. else { throw new Exception($"Image source with name {sourceNode.Name} does not have enough information to be created. Must have either a {tilePositionTagName} or {boundsTagName} attribute."); } // Create a new image with the root texture and user-defined bounds. Image image = new Image(sourceNode.Name, texture, sourceRectangle); // Add the image to the dictionary. AddImage(image); } } }