Allowed for the text to set a text origin.

This commit is contained in:
Michel Fedde 2021-05-02 14:48:29 +02:00
parent 51f8dfd522
commit 17e76a9dd4
6 changed files with 56 additions and 38 deletions

View file

@ -27,8 +27,5 @@ namespace SM.Base.Drawing.Text
public Matrix3 TextureMatrix;
public Vector2 Offset;
public Vector2 Scale;
}
}

View file

@ -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()

View file

@ -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
}
/// <summary>
/// Defines a basis for text drawing.
/// </summary>
@ -36,6 +44,12 @@ namespace SM.Base.Drawing.Text
/// </summary>
public float Height;
/// <summary>
/// Allow to change the origin of the text.
/// <para>Default: <see cref="TextOrigin.Center"/></para>
/// </summary>
public TextOrigin Origin = TextOrigin.Center;
/// <summary>
/// The spacing between numbers.
/// <para>Default: 1</para>
@ -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;
}
}
}
}
}
}

View file

@ -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.
/// </summary>
/// <param name="item"></param>
[Obsolete("Please use Add()")]
public void addScript(IScriptable item)
{
_scriptableObjects.Add(item);

View file

@ -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);
}
/// <inheritdoc />
protected override void DrawContext(ref DrawContext context)
{

View file

@ -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);
test.Transform.Size.Set(.5f);
col.Add(test, textTex);
scene.Objects.Add(col);
scene.Objects.Add(test);
window.UpdateFrame += WindowOnUpdateFrame;
window.RenderFrame += Window_RenderFrame;