From 17e76a9dd49725d815d073de88d32a65fbb7f5c1 Mon Sep 17 00:00:00 2001 From: Michel Fedde Date: Sun, 2 May 2021 14:48:29 +0200 Subject: [PATCH] Allowed for the text to set a text origin. --- SMCode/SM.Base/Drawing/Text/CharParameter.cs | 3 -- SMCode/SM.Base/Drawing/Text/Font.cs | 5 --- .../SM.Base/Drawing/Text/TextDrawingBasis.cs | 35 +++++++++++++++- SMCode/SM.Base/Scene/GenericItemCollection.cs | 3 +- SMCode/SM2D/Drawing/DrawText.cs | 8 ++++ SM_TEST/Program.cs | 40 +++++++------------ 6 files changed, 56 insertions(+), 38 deletions(-) diff --git a/SMCode/SM.Base/Drawing/Text/CharParameter.cs b/SMCode/SM.Base/Drawing/Text/CharParameter.cs index 6f9661a..aec2380 100644 --- a/SMCode/SM.Base/Drawing/Text/CharParameter.cs +++ b/SMCode/SM.Base/Drawing/Text/CharParameter.cs @@ -27,8 +27,5 @@ namespace SM.Base.Drawing.Text public Matrix3 TextureMatrix; - - public Vector2 Offset; - public Vector2 Scale; } } \ No newline at end of file diff --git a/SMCode/SM.Base/Drawing/Text/Font.cs b/SMCode/SM.Base/Drawing/Text/Font.cs index 9b2383c..e49fdbb 100644 --- a/SMCode/SM.Base/Drawing/Text/Font.cs +++ b/SMCode/SM.Base/Drawing/Text/Font.cs @@ -83,14 +83,9 @@ namespace SM.Base.Drawing.Text TextureMatrix = TextureTransformation.CalculateMatrix(offset, scale, 0), - - Offset = offset, - Scale = scale }); } } - - Console.WriteLine(); } public override void Compile() diff --git a/SMCode/SM.Base/Drawing/Text/TextDrawingBasis.cs b/SMCode/SM.Base/Drawing/Text/TextDrawingBasis.cs index ab6e358..d250294 100644 --- a/SMCode/SM.Base/Drawing/Text/TextDrawingBasis.cs +++ b/SMCode/SM.Base/Drawing/Text/TextDrawingBasis.cs @@ -3,12 +3,20 @@ using System; using OpenTK; using OpenTK.Graphics; +using SM.Base.Objects.Static; using SM.Base.Window; #endregion namespace SM.Base.Drawing.Text { + public enum TextOrigin + { + Left, + Center, + Right + } + /// /// Defines a basis for text drawing. /// @@ -36,6 +44,12 @@ namespace SM.Base.Drawing.Text /// public float Height; + /// + /// Allow to change the origin of the text. + /// Default: + /// + public TextOrigin Origin = TextOrigin.Center; + /// /// The spacing between numbers. /// Default: 1 @@ -85,6 +99,7 @@ namespace SM.Base.Drawing.Text { Material.Texture = font; Material.Blending = true; + Mesh = Plate.Object; } @@ -111,7 +126,6 @@ namespace SM.Base.Drawing.Text float y = 0; for (var i = 0; i < _text.Length; i++) { - if (_text[i] == ' ') { x += Font.SpaceWidth * Spacing; @@ -150,10 +164,27 @@ namespace SM.Base.Drawing.Text TextureMatrix = parameter.TextureMatrix }; - Width = Math.Max(Width, x); x += parameter.Advance; } Height = y + Font.Height; + Width = x; + + if (Origin != TextOrigin.Left) + { + foreach (Instance i in _instances) + { + if (i == null) continue; + switch (Origin) + { + case TextOrigin.Center: + i.ModelMatrix *= Matrix4.CreateTranslation(-Width / 2, 0, 0); + break; + case TextOrigin.Right: + i.ModelMatrix *= Matrix4.CreateTranslation(-Width, 0, 0); + break; + } + } + } } } } \ No newline at end of file diff --git a/SMCode/SM.Base/Scene/GenericItemCollection.cs b/SMCode/SM.Base/Scene/GenericItemCollection.cs index 3385669..a23acfa 100644 --- a/SMCode/SM.Base/Scene/GenericItemCollection.cs +++ b/SMCode/SM.Base/Scene/GenericItemCollection.cs @@ -102,7 +102,7 @@ namespace SM.Base.Scene addObject(show); if (item is IScriptable scriptable) - AddScript(scriptable); + addScript(scriptable); if (item is IFixedScriptable fixedScriptable) _fixedScriptables.Add(fixedScriptable); } @@ -145,7 +145,6 @@ namespace SM.Base.Scene /// Adds the script to the collection. /// /// - [Obsolete("Please use Add()")] public void addScript(IScriptable item) { _scriptableObjects.Add(item); diff --git a/SMCode/SM2D/Drawing/DrawText.cs b/SMCode/SM2D/Drawing/DrawText.cs index 0441603..b65465c 100644 --- a/SMCode/SM2D/Drawing/DrawText.cs +++ b/SMCode/SM2D/Drawing/DrawText.cs @@ -23,6 +23,14 @@ namespace SM2D.Drawing Transform.Size = new CVector2(1); } + public void SetHeight(float desiredHeight) + { + if (!Font.WasCompiled) Font.Compile(); + + float factor = desiredHeight / Font.Height; + Transform.Size.Set(factor); + } + /// protected override void DrawContext(ref DrawContext context) { diff --git a/SM_TEST/Program.cs b/SM_TEST/Program.cs index 95e70ae..840db06 100644 --- a/SM_TEST/Program.cs +++ b/SM_TEST/Program.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using OpenTK; using OpenTK.Graphics; using OpenTK.Graphics.OpenGL; +using SM.Base; using SM.Base.Animation; using SM.Base.Controls; using SM.Base.Drawing; @@ -33,50 +34,37 @@ namespace SM_TEST }; font.RegenerateTexture(); + SMRenderer.DefaultMesh = new Polygon(new[] { new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0) }); + window = new GLWindow(1280, 720, "0ms", WindowFlags.Window, VSyncMode.Off); window.ApplySetup(new Window2DSetup()); - window.SetScene(scene = new Scene()); + window.SetScene(scene = new Scene() + { + ShowAxisHelper = true + }); scene.Background.Color = Color4.Blue; scene.Camera = new Camera() { - RequestedWorldScale = new Vector2(0, 10) + }; - ItemCollection col = new ItemCollection(); - - DrawObject2D textTex = new DrawObject2D() - { - Texture = font, - Material = {Blending = true} - }; - float aspect = font.Height / (float) font.Width; - textTex.Transform.Size.Set(font.Width * aspect, font.Height * aspect); - textTex.Transform.Position.Set(textTex.Transform.Size.X / 2, 0); - - Vector2 fontSize = new Vector2(font.Width * aspect, font.Height * aspect); - Material uvMaterial = new Material() { Tint = new Color4(1f, 0, 0, .5f), - Blending = true + Blending = true, + Texture = font }; - col.Transform.Size.Set(1); - - DrawText test = new DrawText(font, "Level Completed") { Material = uvMaterial, - Font = font + Origin = TextOrigin.Right }; - test.Transform.Size.Set(aspect); test.Transform.Position.Set(0, 2); - - - col.Add(test, textTex); - - scene.Objects.Add(col); + test.Transform.Size.Set(.5f); + + scene.Objects.Add(test); window.UpdateFrame += WindowOnUpdateFrame; window.RenderFrame += Window_RenderFrame;