20.09.2020

+ Instance Drawing
+ Text and Font

~ Made "DrawBackground" forced Background for 2D

- DrawEmpty
This commit is contained in:
Michel Fedde 2020-09-20 18:29:09 +02:00
parent acccf5f0e7
commit c4a0847567
29 changed files with 365 additions and 85 deletions

View file

@ -0,0 +1,16 @@
using System;
using System.Configuration.Assemblies;
using OpenTK;
namespace SM.Data.Fonts
{
[Serializable]
public class CharParameter
{
public int X;
public float Width;
public float RelativeX;
public float RelativeWidth;
}
}

View file

@ -0,0 +1,95 @@
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.Security.Policy;
using OpenTK;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Input;
using SM.Data.Fonts;
using SM.OGL.Texture;
namespace SM.Base.Text
{
public class Font : TextureBase
{
public Dictionary<char, CharParameter> Positions = new Dictionary<char, CharParameter>();
public override TextureMinFilter Filter { get; set; } = TextureMinFilter.Linear;
public override TextureWrapMode WrapMode { get; set; } = TextureWrapMode.ClampToEdge;
public FontFamily FontFamily;
public FontStyle FontStyle = FontStyle.Regular;
public float FontSize = 12;
public ICollection<char> CharSet = FontCharStorage.SimpleUTF8;
public int Width { get; private set; }
public int Height { get; private set; }
public Bitmap Texture { get; private set; }
public Font(string path)
{
PrivateFontCollection pfc = new PrivateFontCollection();
pfc.AddFontFile(path);
FontFamily = pfc.Families[0];
}
public Font(FontFamily font)
{
FontFamily = font;
}
public void RegenerateTexture()
{
Bitmap map = new Bitmap(1000,20);
Width = 0;
Height = 0;
using (System.Drawing.Font f = new System.Drawing.Font(FontFamily, FontSize, FontStyle))
{
using (Graphics g = Graphics.FromImage(map))
{
g.Clear(Color.Transparent);
foreach (char c in CharSet)
{
string s = c.ToString();
SizeF size = g.MeasureString(s, f);
try
{
Positions.Add(c, new CharParameter() { Width = size.Width, X = Width });
}
catch
{
// ignored
}
if (Height < size.Height) Height = (int)size.Height;
Width += (int)size.Width;
}
}
map = new Bitmap(Width, Height);
using (Graphics g = Graphics.FromImage(map))
{
foreach (KeyValuePair<char, CharParameter> keyValuePair in Positions)
{
keyValuePair.Value.RelativeX = (keyValuePair.Value.X + 0.00001f) / Width;
keyValuePair.Value.RelativeWidth = (keyValuePair.Value.Width) / Width;
g.DrawString(keyValuePair.Key.ToString(), f, Brushes.White, keyValuePair.Value.X, 0);
}
}
}
Texture = map;
_id = SM.Base.Textures.Texture.GenerateTexture(map, Filter, WrapMode);
}
protected override void Compile()
{
base.Compile();
RegenerateTexture();
}
}
}

View file

@ -0,0 +1,14 @@
namespace SM.Data.Fonts
{
public class FontCharStorage
{
public static readonly char[] SimpleUTF8 = new char[]
{
'!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', ':', ';', '<', '=', '>', '?', '@', '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', '[', '\\', ']', '^', '_', '`',
'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', '{', '|', '}', '~'
};
}
}

View file

@ -0,0 +1,83 @@
using System;
using OpenTK;
using OpenTK.Graphics;
using SM.Base.Contexts;
using SM.Base.Scene;
using SM.Data.Fonts;
namespace SM.Base.Text
{
public abstract class TextDrawingBasis<TTransform> : DrawingBasis<TTransform>
where TTransform : GenericTransformation, new()
{
protected Instance[] _modelMatrixs;
protected string _text;
public Font Font
{
get => (Font) _material.Texture;
set
{
_material.Texture = value;
GenerateMatrixes();
}
}
public string Text
{
get => _text;
set
{
_text = value;
GenerateMatrixes();
}
}
public Color4 Color
{
get => _material.Tint;
set => _material.Tint = value;
}
protected TextDrawingBasis(Font font)
{
_material.Texture = font;
}
public override void Draw(DrawContext context)
{
base.Draw(context);
if (_modelMatrixs == null) GenerateMatrixes();
}
public void GenerateMatrixes()
{
if (!Font.WasCompiled) Font.RegenerateTexture();
_modelMatrixs = new Instance[_text.Length];
float x = 0;
for (var i = 0; i < _text.Length; i++)
{
CharParameter parameter;
try
{
parameter = Font.Positions[_text[i]];
}
catch
{
throw new Exception("Font doesn't contain '"+_text[i]+"'");
}
Matrix4 matrix = Matrix4.CreateScale(parameter.Width, Font.Height, 1) * Matrix4.CreateTranslation(x, 0, 0);
_modelMatrixs[i] = new Instance
{
ModelMatrix = matrix,
TexturePosition = new Vector2(parameter.RelativeX, 0),
TextureScale = new Vector2(parameter.RelativeWidth, 1)
};
x += parameter.Width;
}
}
}
}