From b45a10b6764685a4e6bb104a05b707e26873af11 Mon Sep 17 00:00:00 2001 From: Michel Fedde Date: Sat, 27 Mar 2021 18:07:52 +0100 Subject: [PATCH] Sorts objects by Transformation --- SMCode/SM2D/Scene/ItemCollection.cs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/SMCode/SM2D/Scene/ItemCollection.cs b/SMCode/SM2D/Scene/ItemCollection.cs index 51f7f0d..ff86581 100644 --- a/SMCode/SM2D/Scene/ItemCollection.cs +++ b/SMCode/SM2D/Scene/ItemCollection.cs @@ -1,7 +1,9 @@ #region usings +using System; using SM.Base.Scene; using SM.Base.Types; +using SM.Base.Window; using SM2D.Types; #endregion @@ -16,5 +18,28 @@ namespace SM2D.Scene { Transform.Size = new CVector2(1); } + + public override void Draw(DrawContext context) + { + this.Sort(Comparitor); + + base.Draw(context); + } + + private int Comparitor(IShowItem a, IShowItem b) + { + if (a is ITransformItem ta) + { + if (b is ITransformItem tb) + { + return (int)((ta.Transform.GetMatrix()[3, 2] - tb.Transform.GetMatrix()[3, 2]) * Transformation.ZIndexPercision); + } + else return 1; + } + else + { + return -1; + } + } } } \ No newline at end of file