From 63a6cf493fb4c3c3f74595b8991b3589ea21bb7a Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 6 Aug 2019 14:26:51 +0200 Subject: [PATCH] added stuff for monogame.extended --- .../Extensions/BitmapFontExtensions.cs | 23 +++++++++++++++++++ .../Extensions/SpriteBatchExtensions.cs | 20 ++++++++++++++++ MLEM.Extended/MLEM.Extended.csproj | 20 ++++++++++++++++ MLEM.sln | 6 +++++ 4 files changed, 69 insertions(+) create mode 100644 MLEM.Extended/Extensions/BitmapFontExtensions.cs create mode 100644 MLEM.Extended/Extensions/SpriteBatchExtensions.cs create mode 100644 MLEM.Extended/MLEM.Extended.csproj diff --git a/MLEM.Extended/Extensions/BitmapFontExtensions.cs b/MLEM.Extended/Extensions/BitmapFontExtensions.cs new file mode 100644 index 0000000..bd4ee49 --- /dev/null +++ b/MLEM.Extended/Extensions/BitmapFontExtensions.cs @@ -0,0 +1,23 @@ +using System.Collections.Generic; +using System.Text; +using Microsoft.Xna.Framework.Graphics; +using MonoGame.Extended.BitmapFonts; + +namespace MLEM.Extended.Extensions { + public static class BitmapFontExtensions { + + public static IEnumerable SplitString(this BitmapFont font, string text, float width, float scale) { + var builder = new StringBuilder(); + foreach (var word in text.Split(' ')) { + builder.Append(word).Append(' '); + if (font.MeasureString(builder).Width * scale >= width) { + var len = builder.Length - word.Length - 1; + yield return builder.ToString(0, len); + builder.Remove(0, len); + } + } + yield return builder.ToString(); + } + + } +} \ No newline at end of file diff --git a/MLEM.Extended/Extensions/SpriteBatchExtensions.cs b/MLEM.Extended/Extensions/SpriteBatchExtensions.cs new file mode 100644 index 0000000..1965d76 --- /dev/null +++ b/MLEM.Extended/Extensions/SpriteBatchExtensions.cs @@ -0,0 +1,20 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using MonoGame.Extended.BitmapFonts; + +namespace MLEM.Extended.Extensions { + public static class SpriteBatchExtensions { + + public static void DrawCenteredString(this SpriteBatch batch, BitmapFont font, string text, Vector2 position, float scale, Color color, bool horizontal = true, bool vertical = false, float addedScale = 0) { + var size = font.MeasureString(text); + var center = new Vector2( + horizontal ? size.Width * scale / 2F : 0, + vertical ? size.Height * scale / 2F : 0); + batch.DrawString(font, text, + position + (Vector2) size * scale / 2 - center, + color, 0, size / 2, scale + addedScale, SpriteEffects.None, 0); + } + + + } +} \ No newline at end of file diff --git a/MLEM.Extended/MLEM.Extended.csproj b/MLEM.Extended/MLEM.Extended.csproj new file mode 100644 index 0000000..42b995a --- /dev/null +++ b/MLEM.Extended/MLEM.Extended.csproj @@ -0,0 +1,20 @@ + + + netstandard2.0 + + + + Ellpeck + (M)LEM (L)ibrary by (E)llpeck for (M)onoGame for MonoGame.Extended + monogame ellpeck mlem utility extensions monogame.extended extended + https://github.com/Ellpeck/MLEM + https://github.com/Ellpeck/MLEM + https://github.com/Ellpeck/MLEM/blob/master/LICENSE + 1.0.0 + + + + + + + \ No newline at end of file diff --git a/MLEM.sln b/MLEM.sln index f5a23d0..9b803e2 100644 --- a/MLEM.sln +++ b/MLEM.sln @@ -2,6 +2,8 @@ Microsoft Visual Studio Solution File, Format Version 12.00 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLEM", "MLEM\MLEM.csproj", "{1D6AB762-43C4-4775-8924-707C7EC3F142}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLEM.Extended", "MLEM.Extended\MLEM.Extended.csproj", "{232A6513-A28C-4D7F-BA5A-89281BFC1538}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -12,5 +14,9 @@ Global {1D6AB762-43C4-4775-8924-707C7EC3F142}.Debug|Any CPU.Build.0 = Debug|Any CPU {1D6AB762-43C4-4775-8924-707C7EC3F142}.Release|Any CPU.ActiveCfg = Release|Any CPU {1D6AB762-43C4-4775-8924-707C7EC3F142}.Release|Any CPU.Build.0 = Release|Any CPU + {232A6513-A28C-4D7F-BA5A-89281BFC1538}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {232A6513-A28C-4D7F-BA5A-89281BFC1538}.Debug|Any CPU.Build.0 = Debug|Any CPU + {232A6513-A28C-4D7F-BA5A-89281BFC1538}.Release|Any CPU.ActiveCfg = Release|Any CPU + {232A6513-A28C-4D7F-BA5A-89281BFC1538}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal