public void Image(string imageName, Vector2? size = null, Vector2? topLeft = null, float horizontalCrop = 1.0f, float verticalCrop = 1.0f, Vector2 margin = new Vector2(), float alpha = 1f, float intensity = 1f) { if (topLeft == null) { topLeft = new Vector2(); } var texture = _graphicsContext.GetTexture(imageName).TextureArea; if (size == null) { size = texture.Size; } var dataStream = _textureRenderer.LockVertexBuffer(); var rect = new Rectangle(margin, size.Value - margin).Translate(_offset + topLeft.Value); WriteCroppedQuad(texture, rect, dataStream, horizontalCrop, verticalCrop); _textureRenderer.UnlockVertexBuffer(); _textureRenderer.Render(PrimitiveType.TriangleList, 0, 2, texture, alpha, intensity); }
public TextureArea GetTexture(string filename, Rectangle rectangle) { var newTexture = GetTextureFromCache(filename); // Determine pixel coordinates var topLeft = new Vector { X = rectangle.TopLeft.X / newTexture.Width, Y = rectangle.TopLeft.Y / newTexture.Height }; var bottomRight = new Vector { X = rectangle.BottomRight.X / newTexture.Width, Y = rectangle.BottomRight.Y / newTexture.Height }; var newTextureArea = new TextureArea(newTexture.TextureId, new Vector(topLeft.X, topLeft.Y), new Vector(bottomRight.X, bottomRight.Y)); return newTextureArea; }
private static void WriteRectangle(Rectangle extents, Color4 colour, IDataStream<TransformedColouredVertex> dataStream) { dataStream.WriteRange( new[] { new TransformedColouredVertex { Colour = colour, Position = new Vector4(extents.BottomLeft.X, extents.BottomLeft.Y, 1.0f, 1.0f) }, new TransformedColouredVertex { Colour = colour, Position = new Vector4(extents.TopLeft.X, extents.TopLeft.Y, 1.0f, 1.0f) }, new TransformedColouredVertex { Colour = colour, Position = new Vector4(extents.TopRight.X, extents.TopRight.Y, 1.0f, 1.0f) }, new TransformedColouredVertex { Colour = colour, Position = new Vector4(extents.BottomLeft.X, extents.BottomLeft.Y, 1.0f, 1.0f) }, new TransformedColouredVertex { Colour = colour, Position = new Vector4(extents.TopRight.X, extents.TopRight.Y, 1.0f, 1.0f) }, new TransformedColouredVertex { Colour = colour, Position = new Vector4(extents.BottomRight.X, extents.BottomRight.Y, 1.0f, 1.0f) } }); }
private static void WriteQuad(TextureArea texture, Rectangle rect, IDataStream<TransformedColouredTexturedVertex> dataStream, float alpha) { rect = rect.Translate(-0.5f, -0.5f); dataStream.WriteRange( new[] { MakeVertex(rect.TopLeft, texture.AtlasTopLeft, alpha), MakeVertex(rect.BottomRight, texture.AtlasBottomRight, alpha), MakeVertex(rect.BottomLeft, texture.AtlasBottomLeft, alpha), MakeVertex(rect.TopLeft, texture.AtlasTopLeft, alpha), MakeVertex(rect.TopRight, texture.AtlasTopRight, alpha), MakeVertex(rect.BottomRight, texture.AtlasBottomRight, alpha) }); }
private static void WriteCroppedQuad(TextureArea texture, Rectangle rect, IDataStream<TransformedColouredTexturedVertex> dataStream, float horizontalCrop, float verticalCrop) { rect = rect.Translate(-0.5f, -0.5f).Scale(horizontalCrop, verticalCrop); var atlasRect = new Rectangle(texture.AtlasTopLeft, texture.AtlasBottomRight); atlasRect = atlasRect.Scale(horizontalCrop, verticalCrop); dataStream.WriteRange( new[] { MakeVertex(rect.TopLeft, atlasRect.TopLeft), MakeVertex(rect.BottomRight, atlasRect.BottomRight), MakeVertex(rect.BottomLeft, atlasRect.BottomLeft), MakeVertex(rect.TopLeft, atlasRect.TopLeft), MakeVertex(rect.TopRight, atlasRect.TopRight), MakeVertex(rect.BottomRight, atlasRect.BottomRight) }); }
private void PerformUpdate(Vector2 size, TexturePatch textures, string overrideTexture = null, float alpha = 1.0f) { var rectTL = new Rectangle { TopLeft = new Vector2(0, 0), BottomRight = new Vector2(textures.TL.Width, textures.TL.Height) }; var rectT = new Rectangle { TopLeft = new Vector2(textures.TL.Width, 0), BottomRight = new Vector2(size.X - rectTL.Width, textures.T.Height) }; var rectTR = new Rectangle { TopLeft = new Vector2(rectT.Width + rectTL.Width, 0), BottomRight = new Vector2(rectT.Width + rectTL.Width + textures.TR.Width, textures.TR.Height) }; var rectL = new Rectangle { TopLeft = new Vector2(0, rectTL.Height), BottomRight = new Vector2(textures.L.Width, size.Y - rectTL.Height) }; var rectC = new Rectangle { TopLeft = new Vector2(textures.TL.Width, textures.TL.Height), BottomRight = new Vector2(size.X - rectTL.Width, size.Y - rectTL.Height) }; var rectR = new Rectangle { TopLeft = new Vector2(size.X - rectTL.Width, rectTR.Height), BottomRight = new Vector2(size.X, size.Y - rectTL.Height) }; var rectBL = new Rectangle { TopLeft = new Vector2(0, size.Y - textures.BL.Height), BottomRight = new Vector2(textures.BL.Width, size.Y) }; var rectB = new Rectangle { TopLeft = new Vector2(textures.BL.Width, size.Y - textures.B.Height), BottomRight = new Vector2(size.X - textures.BR.Width, size.Y) }; var rectBR = new Rectangle { TopLeft = new Vector2(size.X - textures.BR.Width, size.Y - textures.BR.Height), BottomRight = new Vector2(size.X, size.Y) }; rectTL = rectTL.Translate(_offset); rectT = rectT.Translate(_offset); rectTR = rectTR.Translate(_offset); rectL = rectL.Translate(_offset); rectC = rectC.Translate(_offset); rectR = rectR.Translate(_offset); rectBL = rectBL.Translate(_offset); rectB = rectB.Translate(_offset); rectBR = rectBR.Translate(_offset); var dataStream = _textureRenderer.LockVertexBuffer(); WriteQuad(textures.TL, rectTL, dataStream, alpha); WriteQuad(textures.T, rectT, dataStream, alpha); WriteQuad(textures.TR, rectTR, dataStream, alpha); WriteQuad(textures.L, rectL, dataStream, alpha); WriteQuad(textures.C, rectC, dataStream, alpha); WriteQuad(textures.R, rectR, dataStream, alpha); WriteQuad(textures.BL, rectBL, dataStream, alpha); WriteQuad(textures.B, rectB, dataStream, alpha); WriteQuad(textures.BR, rectBR, dataStream, alpha); var texture = textures.TL; if (!string.IsNullOrEmpty(overrideTexture)) { texture = _graphicsContext.GetTexture(overrideTexture).TextureArea; } _textureRenderer.UnlockVertexBuffer(); _textureRenderer.Render(PrimitiveType.TriangleList, 0, 18, texture, alpha, 1); }
public bool Intersects(Rectangle other) { return !(BottomRight.X < other.TopLeft.X || TopLeft.X > other.BottomRight.X || BottomRight.Y > other.TopLeft.Y || TopLeft.Y < other.BottomRight.Y); }
public Rectangle? IntersectingArea(Rectangle area) { if (!Intersects(area)) return null; var left = Math.Max(TopLeft.X, area.TopLeft.X); var right = Math.Min(BottomRight.X, area.BottomRight.X); var top = Math.Min(TopLeft.Y, area.TopLeft.Y); var bottom = Math.Max(BottomRight.Y, area.BottomRight.Y); return new Rectangle(new Vector2(left, top), new Vector2(right, bottom)); }