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); } }
protected virtual void UpdateMatrixes() { if (!_areMatrixesDirty) { return; } Matrix2D tempMat; _transformMatrix = Matrix2D.CreateTranslation(-Entity.Transform.Position.X, -Entity.Transform.Position.Y); // position if (_zoom != 1f) { Matrix2D.CreateScale(_zoom, _zoom, out tempMat); // scale -> Matrix2D.Multiply(ref _transformMatrix, ref tempMat, out _transformMatrix); } if (Entity.Transform.Rotation != 0f) { Matrix2D.CreateRotation(Entity.Transform.Rotation, out tempMat); // rotation Matrix2D.Multiply(ref _transformMatrix, ref tempMat, out _transformMatrix); } Matrix2D.CreateTranslation((int)_origin.X, (int)_origin.Y, out tempMat); // translate -origin Matrix2D.Multiply(ref _transformMatrix, ref tempMat, out _transformMatrix); // calculate our inverse as well Matrix2D.Invert(ref _transformMatrix, out _inverseTransformMatrix); // whenever the matrix changes the bounds are then invalid _areBoundsDirty = true; _areMatrixesDirty = false; }
void UpdateTransform() { if (hierarchyDirty != DirtyType.Clean) { if (Parent != null) { Parent.UpdateTransform(); } if (_localDirty) { if (_localPositionDirty) { Matrix2D.CreateTranslation(_localPosition.X, _localPosition.Y, out _translationMatrix); _localPositionDirty = false; } if (_localRotationDirty) { Matrix2D.CreateRotation(_localRotation, out _rotationMatrix); _localRotationDirty = false; } if (_localScaleDirty) { Matrix2D.CreateScale(_localScale.X, _localScale.Y, out _scaleMatrix); _localScaleDirty = false; } Matrix2D.Multiply(ref _scaleMatrix, ref _rotationMatrix, out _localTransform); Matrix2D.Multiply(ref _localTransform, ref _translationMatrix, out _localTransform); if (Parent == null) { _worldTransform = _localTransform; _rotation = _localRotation; _scale = _localScale; _worldInverseDirty = true; } _localDirty = false; } if (Parent != null) { Matrix2D.Multiply(ref _localTransform, ref Parent._worldTransform, out _worldTransform); _rotation = _localRotation + Parent._rotation; _scale = Parent._scale * _localScale; _worldInverseDirty = true; } _worldToLocalDirty = true; _positionDirty = true; hierarchyDirty = DirtyType.Clean; } }
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; } }
/// <summary> /// compiles the text into raw verts/texture coordinates. This method must be called anytime text or any other properties are /// changed. /// </summary> public void Compile() { _charDetails = new CharDetails[_text.Length]; Character currentCharacter = null; var effects = (byte)SpriteEffects.None; var _transformationMatrix = Matrix2D.Identity; var requiresTransformation = Rotation != 0f || _scale != Vector2.One; if (requiresTransformation) { Matrix2D temp; Matrix2D.CreateTranslation(-_origin.X, -_origin.Y, out _transformationMatrix); Matrix2D.CreateScale(_scale.X, _scale.Y, out temp); Matrix2D.Multiply(ref _transformationMatrix, ref temp, 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); } var offset = requiresTransformation ? Vector2.Zero : Position - _origin; for (var i = 0; i < _text.Length; ++i) { _charDetails[i].Initialize(); _charDetails[i].Color = _color; var c = _text[i]; if (c == '\n') { offset.X = requiresTransformation ? 0f : Position.X - _origin.X; offset.Y += _font.LineHeight; currentCharacter = null; continue; } if (currentCharacter != null) { offset.X += _font.Spacing.X + currentCharacter.XAdvance; } currentCharacter = _font[c]; var p = offset; p.X += currentCharacter.Offset.X; p.Y += currentCharacter.Offset.Y; // transform our point if we need to if (requiresTransformation) { Vector2Ext.Transform(ref p, ref _transformationMatrix, out p); } var destination = new Vector4(p.X, p.Y, currentCharacter.Bounds.Width * _scale.X, currentCharacter.Bounds.Height * _scale.Y); _charDetails[i].Texture = _font.Textures[_font[currentCharacter.Char].TexturePage]; //_charDetails[i].texture = currentCharacter.subtexture.texture2D; // Batcher calculations var sourceRectangle = currentCharacter.Bounds; float sourceX, sourceY, sourceW, sourceH; var destW = destination.Z; var destH = destination.W; // calculate uvs var inverseTexW = 1.0f / (float)currentCharacter.Bounds.Width; var inverseTexH = 1.0f / (float)currentCharacter.Bounds.Height; sourceX = sourceRectangle.X * inverseTexW; sourceY = sourceRectangle.Y * inverseTexH; sourceW = Math.Max(sourceRectangle.Width, float.Epsilon) * inverseTexW; sourceH = Math.Max(sourceRectangle.Height, float.Epsilon) * inverseTexH; // Rotation Calculations float rotationMatrix1X; float rotationMatrix1Y; float rotationMatrix2X; float rotationMatrix2Y; if (!Mathf.WithinEpsilon(Rotation, 0.0f)) { var sin = Mathf.Sin(Rotation); var cos = Mathf.Cos(Rotation); rotationMatrix1X = cos; rotationMatrix1Y = sin; rotationMatrix2X = -sin; rotationMatrix2Y = cos; } else { rotationMatrix1X = 1.0f; rotationMatrix1Y = 0.0f; rotationMatrix2X = 0.0f; rotationMatrix2Y = 1.0f; } // Calculate vertices, finally. // top-left _charDetails[i].Verts[0].X = rotationMatrix2X + rotationMatrix1X + destination.X - 1; _charDetails[i].Verts[0].Y = rotationMatrix2Y + rotationMatrix1Y + destination.Y - 1; // top-right var cornerX = _cornerOffsetX[1] * destW; var cornerY = _cornerOffsetY[1] * destH; _charDetails[i].Verts[1].X = ( (rotationMatrix2X * cornerY) + (rotationMatrix1X * cornerX) + destination.X ); _charDetails[i].Verts[1].Y = ( (rotationMatrix2Y * cornerY) + (rotationMatrix1Y * cornerX) + destination.Y ); // bottom-left cornerX = _cornerOffsetX[2] * destW; cornerY = _cornerOffsetY[2] * destH; _charDetails[i].Verts[2].X = ( (rotationMatrix2X * cornerY) + (rotationMatrix1X * cornerX) + destination.X ); _charDetails[i].Verts[2].Y = ( (rotationMatrix2Y * cornerY) + (rotationMatrix1Y * cornerX) + destination.Y ); // bottom-right cornerX = _cornerOffsetX[3] * destW; cornerY = _cornerOffsetY[3] * destH; _charDetails[i].Verts[3].X = ( (rotationMatrix2X * cornerY) + (rotationMatrix1X * cornerX) + destination.X ); _charDetails[i].Verts[3].Y = ( (rotationMatrix2Y * cornerY) + (rotationMatrix1Y * cornerX) + destination.Y ); // texture coordintes _charDetails[i].TexCoords[0].X = (_cornerOffsetX[0 ^ effects] * sourceW) + sourceX; _charDetails[i].TexCoords[0].Y = (_cornerOffsetY[0 ^ effects] * sourceH) + sourceY; _charDetails[i].TexCoords[1].X = (_cornerOffsetX[1 ^ effects] * sourceW) + sourceX; _charDetails[i].TexCoords[1].Y = (_cornerOffsetY[1 ^ effects] * sourceH) + sourceY; _charDetails[i].TexCoords[2].X = (_cornerOffsetX[2 ^ effects] * sourceW) + sourceX; _charDetails[i].TexCoords[2].Y = (_cornerOffsetY[2 ^ effects] * sourceH) + sourceY; _charDetails[i].TexCoords[3].X = (_cornerOffsetX[3 ^ effects] * sourceW) + sourceX; _charDetails[i].TexCoords[3].Y = (_cornerOffsetY[3 ^ effects] * sourceH) + sourceY; } }