A game framework written with osu! in mind.
at master 62 lines 2.3 kB view raw
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. 2// See the LICENCE file in the repository root for full licence text. 3 4using System; 5using osu.Framework.Graphics.OpenGL.Vertices; 6using osu.Framework.Graphics.Textures; 7using osuTK; 8using osu.Framework.Graphics.Primitives; 9using osu.Framework.Graphics.Sprites; 10using osu.Framework.Graphics.OpenGL; 11 12namespace osu.Framework.Graphics.Shapes 13{ 14 /// <summary> 15 /// Represents a sprite that is drawn in a triangle shape, instead of a rectangle shape. 16 /// </summary> 17 public class Triangle : Sprite 18 { 19 /// <summary> 20 /// Creates a new triangle with a white pixel as texture. 21 /// </summary> 22 public Triangle() 23 { 24 Texture = Texture.WhitePixel; 25 } 26 27 public override RectangleF BoundingBox => toTriangle(ToParentSpace(LayoutRectangle)).AABBFloat; 28 29 private static Primitives.Triangle toTriangle(Quad q) => new Primitives.Triangle( 30 (q.TopLeft + q.TopRight) / 2, 31 q.BottomLeft, 32 q.BottomRight); 33 34 public override bool Contains(Vector2 screenSpacePos) => toTriangle(ScreenSpaceDrawQuad).Contains(screenSpacePos); 35 36 protected override DrawNode CreateDrawNode() => new TriangleDrawNode(this); 37 38 private class TriangleDrawNode : SpriteDrawNode 39 { 40 public TriangleDrawNode(Triangle source) 41 : base(source) 42 { 43 } 44 45 protected override void Blit(Action<TexturedVertex2D> vertexAction) 46 { 47 DrawTriangle(Texture, toTriangle(ScreenSpaceDrawQuad), DrawColourInfo.Colour, null, null, 48 new Vector2(InflationAmount.X / DrawRectangle.Width, InflationAmount.Y / DrawRectangle.Height), TextureCoords); 49 } 50 51 protected override void BlitOpaqueInterior(Action<TexturedVertex2D> vertexAction) 52 { 53 var triangle = toTriangle(ConservativeScreenSpaceDrawQuad); 54 55 if (GLWrapper.IsMaskingActive) 56 DrawClipped(ref triangle, Texture, DrawColourInfo.Colour, vertexAction: vertexAction); 57 else 58 DrawTriangle(Texture, triangle, DrawColourInfo.Colour, vertexAction: vertexAction); 59 } 60 } 61 } 62}