01.10.2020

+ Time controls (Stopwatch, Timers, Intervals)
+ Added smmeries to everything in SM.Base

~ Renamed Vectors to CVectors.
This commit is contained in:
Michel Fedde 2020-10-01 15:39:03 +02:00
parent 7acdba92f8
commit 97e638d9d9
44 changed files with 1092 additions and 289 deletions

View file

@ -1,16 +1,29 @@
using System;
using System.Configuration.Assemblies;
using OpenTK;
namespace SM.Data.Fonts
namespace SM.Base.Text
{
/// <summary>
/// Contains information for a font character.
/// </summary>
[Serializable]
public class CharParameter
public struct CharParameter
{
/// <summary>
/// The position on the X-axis.
/// </summary>
public int X;
/// <summary>
/// The width of the character.
/// </summary>
public float Width;
public float RelativeX;
public float RelativeWidth;
/// <summary>
/// The normalized position inside the texture.
/// </summary>
public float NormalizedX;
/// <summary>
/// The normalized width inside the texture.
/// </summary>
public float NormalizedWidth;
}
}

View file

@ -5,28 +5,50 @@ using System.Security.Policy;
using OpenTK;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Input;
using SM.Base.Textures;
using SM.Data.Fonts;
using SM.OGL.Texture;
namespace SM.Base.Text
{
public class Font : TextureBase
/// <summary>
/// Represents a font.
/// </summary>
public class Font : Texture
{
public Dictionary<char, CharParameter> Positions = new Dictionary<char, CharParameter>();
public override TextureMinFilter Filter { get; set; } = TextureMinFilter.Linear;
/// <inheritdoc />
public override TextureWrapMode WrapMode { get; set; } = TextureWrapMode.ClampToEdge;
/// <summary>
/// The font family, that is used to find the right font.
/// </summary>
public FontFamily FontFamily;
/// <summary>
/// The font style.
/// <para>Default: <see cref="System.Drawing.FontStyle.Regular"/></para>
/// </summary>
public FontStyle FontStyle = FontStyle.Regular;
/// <summary>
/// The font size.
/// <para>Default: 12</para>
/// </summary>
public float FontSize = 12;
/// <summary>
/// The char set for the font.
/// <para>Default: <see cref="FontCharStorage.SimpleUTF8"/></para>
/// </summary>
public ICollection<char> CharSet = FontCharStorage.SimpleUTF8;
public int Width { get; private set; }
public int Height { get; private set; }
public Bitmap Texture { get; private set; }
/// <summary>
/// This contains all information for the different font character.
/// </summary>
public Dictionary<char, CharParameter> Positions = new Dictionary<char, CharParameter>();
/// <summary>
/// Generates a font from a font family from the specified path.
/// </summary>
/// <param name="path">The specified path</param>
public Font(string path)
{
PrivateFontCollection pfc = new PrivateFontCollection();
@ -34,16 +56,26 @@ namespace SM.Base.Text
FontFamily = pfc.Families[0];
}
/// <summary>
/// Generates a font from a specified font family.
/// </summary>
/// <param name="font">Font-Family</param>
public Font(FontFamily font)
{
FontFamily = font;
}
/// <summary>
/// Regenerates the texture.
/// </summary>
public void RegenerateTexture()
{
Bitmap map = new Bitmap(1000,20);
Width = 0;
Height = 0;
Positions = new Dictionary<char, CharParameter>();
Bitmap map = new Bitmap(1000, 20);
Dictionary<char, float[]> charParams = new Dictionary<char, float[]>();
using (System.Drawing.Font f = new System.Drawing.Font(FontFamily, FontSize, FontStyle))
{
using (Graphics g = Graphics.FromImage(map))
@ -56,7 +88,7 @@ namespace SM.Base.Text
SizeF size = g.MeasureString(s, f);
try
{
Positions.Add(c, new CharParameter() { Width = size.Width, X = Width });
charParams.Add(c, new[] {size.Width, Width });
}
catch
{
@ -71,25 +103,34 @@ namespace SM.Base.Text
map = new Bitmap(Width, Height);
using (Graphics g = Graphics.FromImage(map))
{
foreach (KeyValuePair<char, CharParameter> keyValuePair in Positions)
foreach (KeyValuePair<char, float[]> keyValuePair in charParams)
{
keyValuePair.Value.RelativeX = (keyValuePair.Value.X + 0.00001f) / Width;
keyValuePair.Value.RelativeWidth = (keyValuePair.Value.Width) / Width;
float normalizedX = (keyValuePair.Value[1] + 0.00001f) / Width;
float normalizedWidth = (keyValuePair.Value[0]) / Width;
g.DrawString(keyValuePair.Key.ToString(), f, Brushes.White, keyValuePair.Value.X, 0);
CharParameter parameter;
Positions.Add(keyValuePair.Key, parameter = new CharParameter()
{
NormalizedWidth = normalizedWidth,
NormalizedX = normalizedX,
Width = keyValuePair.Value[0],
X = (int)keyValuePair.Value[1]
});
g.DrawString(keyValuePair.Key.ToString(), f, Brushes.White, parameter.X, 0);
}
}
}
Texture = map;
_id = SM.Base.Textures.Texture.GenerateTexture(map, Filter, WrapMode);
Map = map;
Recompile();
}
/// <inheritdoc />
protected override void Compile()
{
base.Compile();
RegenerateTexture();
base.Compile();
}
}
}

View file

@ -1,7 +1,13 @@
namespace SM.Data.Fonts
{
/// <summary>
/// Contains default char sets.
/// </summary>
public class FontCharStorage
{
/// <summary>
/// Contains the english alphabet and the common special character.
/// </summary>
public static readonly char[] SimpleUTF8 = new char[]
{
'!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6',
@ -10,5 +16,13 @@
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~'
};
/// <summary>
/// Contains only numbers.
/// </summary>
public static readonly char[] Numbers = new[]
{
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
};
}
}

View file

@ -7,12 +7,25 @@ using SM.Data.Fonts;
namespace SM.Base.Text
{
/// <summary>
/// Defines a basis for text drawing.
/// </summary>
/// <typeparam name="TTransform">Transformation type</typeparam>
public abstract class TextDrawingBasis<TTransform> : DrawingBasis<TTransform>
where TTransform : GenericTransformation, new()
{
protected Instance[] _modelMatrixs;
/// <summary>
/// The different instances for drawing.
/// </summary>
protected Instance[] _instances;
/// <summary>
/// The text, that is drawn.
/// </summary>
protected string _text;
/// <summary>
/// The font.
/// </summary>
public Font Font
{
get => (Font) _material.Texture;
@ -23,6 +36,9 @@ namespace SM.Base.Text
}
}
/// <summary>
/// The text, that is drawn.
/// </summary>
public string Text
{
get => _text;
@ -33,30 +49,47 @@ namespace SM.Base.Text
}
}
/// <summary>
/// The font color.
/// </summary>
public Color4 Color
{
get => _material.Tint;
set => _material.Tint = value;
}
/// <summary>
/// The spacing between numbers.
/// <para>Default: 1</para>
/// </summary>
public float Spacing = 1;
/// <summary>
/// Creates a text object with a font.
/// </summary>
/// <param name="font">The font.</param>
protected TextDrawingBasis(Font font)
{
_material.Texture = font;
}
/// <inheritdoc />
public override void Draw(DrawContext context)
{
base.Draw(context);
if (_modelMatrixs == null) GenerateMatrixes();
if (_instances == null) GenerateMatrixes();
}
/// <summary>
/// This generates the instances.
/// </summary>
/// <exception cref="Exception">The font doesn't contain a character that is needed for the text.</exception>
public void GenerateMatrixes()
{
if (!Font.WasCompiled) Font.RegenerateTexture();
_modelMatrixs = new Instance[_text.Length];
_instances = new Instance[_text.Length];
float x = 0;
CharParameter _last = new CharParameter();
@ -80,11 +113,11 @@ namespace SM.Base.Text
Matrix4 matrix = Matrix4.CreateScale(parameter.Width, Font.Height, 1) *
Matrix4.CreateTranslation(x, 0, 0);
_modelMatrixs[i] = new Instance
_instances[i] = new Instance
{
ModelMatrix = matrix,
TexturePosition = new Vector2(parameter.RelativeX, 0),
TextureScale = new Vector2(parameter.RelativeWidth, 1)
TexturePosition = new Vector2(parameter.NormalizedX, 0),
TextureScale = new Vector2(parameter.NormalizedWidth, 1)
};
x += parameter.Width * Spacing;