public void DrawArrow(Vector2 start, Vector2 end, float length, float width, bool drawStartIndicator, Color color) { // Draw connection segment between start- and end-point //drawLine( start, end, color ); // Precalculate halfwidth var halfWidth = width / 2; // Create directional reference Vector2 rotation = (start - end); rotation.Normalize(); // Calculate angle of directional vector float angle = (float)Math.Atan2(rotation.X, -rotation.Y); // Create matrix for rotation Matrix2D rotMatrix = Matrix2D.CreateRotation(angle); // Create translation matrix for end-point Matrix2D endMatrix = Matrix2D.CreateTranslation(end.X, end.Y); // Setup arrow end shape Vector2[] verts = new Vector2[3]; verts[0] = new Vector2(0, 0); verts[1] = new Vector2(-halfWidth, -length); verts[2] = new Vector2(halfWidth, -length); // Rotate end shape Vector2Ext.Transform(verts, ref rotMatrix, verts); // Translate end shape Vector2Ext.Transform(verts, ref endMatrix, verts); // Draw arrow end shape DrawPolygon(verts, 3, color); if (drawStartIndicator) { // Create translation matrix for start Matrix2D startMatrix = Matrix2D.CreateTranslation(start.X, start.Y); // Setup arrow start shape Vector2[] baseVerts = new Vector2[4]; baseVerts[0] = new Vector2(-halfWidth, length / 4); baseVerts[1] = new Vector2(halfWidth, length / 4); baseVerts[2] = new Vector2(halfWidth, 0); baseVerts[3] = new Vector2(-halfWidth, 0); // Rotate start shape Vector2Ext.Transform(baseVerts, ref rotMatrix, baseVerts); // Translate start shape Vector2Ext.Transform(baseVerts, ref startMatrix, baseVerts); // Draw start shape DrawPolygon(baseVerts, 4, color); } }
public static void CalculateBounds(ref Rectangle rect, Vector2 parentPosition, Vector2 position, Vector2 origin, Vector2 scale, float rotation, float width, float height) { if (rotation == 0f) { rect.X = (int)(parentPosition.X + position.X - origin.X * scale.X); rect.Y = (int)(parentPosition.Y + position.Y - origin.Y * scale.Y); rect.Width = (int)(width * scale.X); rect.Height = (int)(height * scale.Y); } else { // special care for rotated bounds. we need to find our absolute min/max values and create the bounds from that var worldPosX = parentPosition.X + position.X; var worldPosY = parentPosition.Y + position.Y; Matrix2D tempMat; // set the reference point to world reference taking origin into account var transformMatrix = Matrix2D.CreateTranslation(-worldPosX - origin.X, -worldPosY - origin.Y); Matrix2D.CreateScale(scale.X, scale.Y, out tempMat); // scale -> Matrix2D.Multiply(ref transformMatrix, ref tempMat, out transformMatrix); Matrix2D.CreateRotation(rotation, out tempMat); // rotate -> Matrix2D.Multiply(ref transformMatrix, ref tempMat, out transformMatrix); Matrix2D.CreateTranslation(worldPosX, worldPosY, out tempMat); // translate back Matrix2D.Multiply(ref transformMatrix, ref tempMat, out transformMatrix); // TODO: this is a bit silly. we can just leave the worldPos translation in the Matrix and avoid this // get all four corners in world space var topLeft = new Vector2(worldPosX, worldPosY); var topRight = new Vector2(worldPosX + width, worldPosY); var bottomLeft = new Vector2(worldPosX, worldPosY + height); var bottomRight = new Vector2(worldPosX + width, worldPosY + height); // transform the corners into our work space Vector2Ext.Transform(ref topLeft, ref transformMatrix, out topLeft); Vector2Ext.Transform(ref topRight, ref transformMatrix, out topRight); Vector2Ext.Transform(ref bottomLeft, ref transformMatrix, out bottomLeft); Vector2Ext.Transform(ref bottomRight, ref transformMatrix, out bottomRight); // find the min and max values so we can concoct our bounding box var minX = (int)Mathf.MinOf(topLeft.X, bottomRight.X, topRight.X, bottomLeft.X); var maxX = (int)Mathf.MaxOf(topLeft.X, bottomRight.X, topRight.X, bottomLeft.X); var minY = (int)Mathf.MinOf(topLeft.Y, bottomRight.Y, topRight.Y, bottomLeft.Y); var maxY = (int)Mathf.MaxOf(topLeft.Y, bottomRight.Y, topRight.Y, bottomLeft.Y); rect.Location = new Point(minX, minY); rect.Width = (int)(maxX - minX); rect.Height = (int)(maxY - minY); } }
public void DrawInto(Batcher batcher, ref FontCharacterSource text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effect, float depth) { var flipAdjustment = Vector2.Zero; var flippedVert = (effect & SpriteEffects.FlipVertically) == SpriteEffects.FlipVertically; var flippedHorz = (effect & SpriteEffects.FlipHorizontally) == SpriteEffects.FlipHorizontally; if (flippedVert || flippedHorz) { Vector2 size; MeasureString(ref text, out size); if (flippedHorz) { origin.X *= -1; flipAdjustment.X = -size.X; } if (flippedVert) { origin.Y *= -1; flipAdjustment.Y = _font.LineSpacing - size.Y; } } // TODO: This looks excessive... i suspect we could do most of this with simple vector math and avoid this much matrix work. var requiresTransformation = flippedHorz || flippedVert || rotation != 0f || scale != Vector2.One; if (requiresTransformation) { Matrix2D temp; Matrix2D.CreateTranslation(-origin.X, -origin.Y, out _transformationMatrix); Matrix2D.CreateScale((flippedHorz ? -scale.X : scale.X), (flippedVert ? -scale.Y : scale.Y), out temp); Matrix2D.Multiply(ref _transformationMatrix, ref temp, out _transformationMatrix); Matrix2D.CreateTranslation(flipAdjustment.X, flipAdjustment.Y, out temp); Matrix2D.Multiply(ref temp, ref _transformationMatrix, out _transformationMatrix); Matrix2D.CreateRotation(rotation, out temp); Matrix2D.Multiply(ref _transformationMatrix, ref temp, out _transformationMatrix); Matrix2D.CreateTranslation(position.X, position.Y, out temp); Matrix2D.Multiply(ref _transformationMatrix, ref temp, out _transformationMatrix); } // Get the default glyph here once. SpriteFont.Glyph?defaultGlyph = null; if (_font.DefaultCharacter.HasValue) { defaultGlyph = _glyphs[_font.DefaultCharacter.Value]; } var currentGlyph = SpriteFont.Glyph.Empty; var offset = requiresTransformation ? Vector2.Zero : position - origin; var firstGlyphOfLine = true; for (var i = 0; i < text.Length; ++i) { var c = text[i]; if (c == '\r') { continue; } if (c == '\n') { offset.X = requiresTransformation ? 0f : position.X - origin.X; offset.Y += _font.LineSpacing; firstGlyphOfLine = true; continue; } if (!_glyphs.TryGetValue(c, out currentGlyph)) { if (!defaultGlyph.HasValue) { throw new ArgumentException("Errors.TextContainsUnresolvableCharacters", "text"); } currentGlyph = defaultGlyph.Value; } // The first character on a line might have a negative left side bearing. // In this scenario, SpriteBatch/SpriteFont normally offset the text to the right, // so that text does not hang off the left side of its rectangle. if (firstGlyphOfLine) { offset.X += Math.Max(currentGlyph.LeftSideBearing, 0); firstGlyphOfLine = false; } else { offset.X += _font.Spacing + currentGlyph.LeftSideBearing; } var p = offset; if (flippedHorz) { p.X += currentGlyph.BoundsInTexture.Width; } p.X += currentGlyph.Cropping.X; if (flippedVert) { p.Y += currentGlyph.BoundsInTexture.Height - _font.LineSpacing; } p.Y += currentGlyph.Cropping.Y; // transform our point if we need to if (requiresTransformation) { Vector2Ext.Transform(ref p, ref _transformationMatrix, out p); } var destRect = RectangleExt.FromFloats(p.X, p.Y, currentGlyph.BoundsInTexture.Width * scale.X, currentGlyph.BoundsInTexture.Height * scale.Y); batcher.Draw(_font.Texture, destRect, currentGlyph.BoundsInTexture, color, rotation, Vector2.Zero, effect, depth); offset.X += currentGlyph.Width + currentGlyph.RightSideBearing; } }