// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using osu.Framework.Graphics.OpenGL.Vertices; using osu.Framework.Graphics.Textures; using osuTK; using osu.Framework.Graphics.Primitives; using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.OpenGL; namespace osu.Framework.Graphics.Shapes { /// /// Represents a sprite that is drawn in a triangle shape, instead of a rectangle shape. /// public class Triangle : Sprite { /// /// Creates a new triangle with a white pixel as texture. /// public Triangle() { Texture = Texture.WhitePixel; } public override RectangleF BoundingBox => toTriangle(ToParentSpace(LayoutRectangle)).AABBFloat; private static Primitives.Triangle toTriangle(Quad q) => new Primitives.Triangle( (q.TopLeft + q.TopRight) / 2, q.BottomLeft, q.BottomRight); public override bool Contains(Vector2 screenSpacePos) => toTriangle(ScreenSpaceDrawQuad).Contains(screenSpacePos); protected override DrawNode CreateDrawNode() => new TriangleDrawNode(this); private class TriangleDrawNode : SpriteDrawNode { public TriangleDrawNode(Triangle source) : base(source) { } protected override void Blit(Action vertexAction) { DrawTriangle(Texture, toTriangle(ScreenSpaceDrawQuad), DrawColourInfo.Colour, null, null, new Vector2(InflationAmount.X / DrawRectangle.Width, InflationAmount.Y / DrawRectangle.Height), TextureCoords); } protected override void BlitOpaqueInterior(Action vertexAction) { var triangle = toTriangle(ConservativeScreenSpaceDrawQuad); if (GLWrapper.IsMaskingActive) DrawClipped(ref triangle, Texture, DrawColourInfo.Colour, vertexAction: vertexAction); else DrawTriangle(Texture, triangle, DrawColourInfo.Colour, vertexAction: vertexAction); } } } }