1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-24 05:38:33 +01:00

Compare commits

...

4 commits

10 changed files with 39 additions and 33 deletions

View file

@ -14,7 +14,7 @@ Improvements
- Improved NinePatch memory performance
- Moved sound-related classes into Sound namespace
- Added customizable overloads for Keybind, Combination and GenericInput ToString methods
- Added ColorExtensions.Invert and made ColorHelper.Invert obsolete
- Moved ColorHelper.Invert to ColorExtensions.Invert
- Removed LINQ Any and All usage in various methods to improve memory usage
Fixes
@ -32,7 +32,7 @@ Fixes
### MLEM.Extended
Improvements
- Adjusted GenericStashFont line height calculations to result in the same values as GenericSpriteFont
- Adjusted GenericStashFont line height calculations to result in values closer to GenericSpriteFont and added a constructor parameter to set a custom line height
### MLEM.Data
Additions

View file

@ -1,3 +1,4 @@
using System.Linq;
using System.Text;
using FontStashSharp;
using Microsoft.Xna.Framework;
@ -24,14 +25,21 @@ namespace MLEM.Extended.Font {
/// Creates a new generic font using <see cref="SpriteFontBase"/>.
/// Optionally, a bold and italic version of the font can be supplied.
/// </summary>
/// <remarks>
/// <see cref="DynamicSpriteFont"/> doesn't expose a text-independent line height (https://github.com/rds1983/FontStashSharp/blob/main/src/FontStashSharp/DynamicSpriteFont.cs#L130).
/// Since <see cref="GenericFont"/> exposes <see cref="LineHeight"/>, there is somewhat of an incompatibility between the two.
/// Because of this, <see cref="GenericStashFont"/> uses a heuristic to determine a text-independent line height based on the tallest character out of a set of predetermined characters (spaces, numbers and uppercase and lowercase A through Z).
/// Because this heuristic is just that, and because it excludes non-latin characters, the desired line height can be specified using <paramref name="lineHeight"/>, overriding the default heuristic.
/// </remarks>
/// <param name="font">The font to wrap</param>
/// <param name="bold">A bold version of the font</param>
/// <param name="italic">An italic version of the font</param>
public GenericStashFont(SpriteFontBase font, SpriteFontBase bold = null, SpriteFontBase italic = null) {
/// <param name="lineHeight">The line height that should be used for <see cref="LineHeight"/> instead of the heuristic described in the remarks</param>
public GenericStashFont(SpriteFontBase font, SpriteFontBase bold = null, SpriteFontBase italic = null, float? lineHeight = null) {
this.Font = font;
this.LineHeight = CalculateLineHeight(font);
this.Bold = bold != null ? new GenericStashFont(bold) : this;
this.Italic = italic != null ? new GenericStashFont(italic) : this;
this.LineHeight = lineHeight ?? CalculateLineHeight(font);
this.Bold = bold != null ? new GenericStashFont(bold, null, null, lineHeight) : this;
this.Italic = italic != null ? new GenericStashFont(italic, null, null, lineHeight) : this;
}
/// <inheritdoc />
@ -54,11 +62,11 @@ namespace MLEM.Extended.Font {
// this is the same calculation used internally by StaticSpriteFont
return s.FontSize + s.LineSpacing;
} else {
// Y (min y) just stores the glyph's Y offset, whereas Y2 (max y) stores the glyph's height
// since we technically want line spacing rather than line height, we calculate it like this
var bounds = new Bounds();
font.TextBounds(" ", Vector2.Zero, ref bounds);
return bounds.Y2 + (bounds.Y2 - bounds.Y);
// use a heuristic to determine the text-independent line heights as described in the constructor remarks
return new[] {' ', '\n', OneEmSpace, Zwsp, Nbsp}
.Concat(Enumerable.Range('a', 'z' - 'a' + 1).SelectMany(c => new[] {(char) c, char.ToUpper((char) c)}))
.Concat(Enumerable.Range('0', '9' - '0' + 1).Select(c => (char) c))
.Select(c => font.MeasureString(c.ToString()).Y).Max();
}
}

View file

@ -24,7 +24,7 @@
<PackageReference Include="MonoGame.Extended.Tiled" Version="3.8.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="FontStashSharp.MonoGame" Version="0.9.2">
<PackageReference Include="FontStashSharp.MonoGame" Version="0.9.5">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641">
@ -33,6 +33,6 @@
</ItemGroup>
<ItemGroup>
<None Include="../Media/Logo.png" Pack="true" PackagePath=""/>
<None Include="../Media/Logo.png" Pack="true" PackagePath="" />
</ItemGroup>
</Project>

View file

@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
@ -184,12 +183,8 @@ namespace MLEM.Ui.Elements {
this.SetText(text, true);
MlemPlatform.EnsureExists();
this.OnPressed += async e => {
var title = this.MobileTitle ?? this.PlaceholderText;
var result = await MlemPlatform.Current.OpenOnScreenKeyboard(title, this.MobileDescription, this.Text, false);
if (result != null)
this.SetText(result.Replace('\n', ' '), true);
};
this.OnPressed += OnPressed;
this.OnTextInput += (element, key, character) => {
if (!this.IsSelected || this.IsHidden)
return;
@ -206,6 +201,13 @@ namespace MLEM.Ui.Elements {
};
this.OnDeselected += e => this.CaretPos = 0;
this.OnSelected += e => this.CaretPos = this.text.Length;
async void OnPressed(Element e) {
var title = this.MobileTitle ?? this.PlaceholderText;
var result = await MlemPlatform.Current.OpenOnScreenKeyboard(title, this.MobileDescription, this.Text, false);
if (result != null)
this.SetText(result.Replace('\n', ' '), true);
}
}
private void HandleTextChange(bool textChanged = true) {

View file

@ -1,5 +1,4 @@
using System;
using System.Linq;
using Microsoft.Xna.Framework;
using MLEM.Ui.Style;

View file

@ -16,7 +16,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="TextCopy" Version="4.3.0" />
<PackageReference Include="TextCopy" Version="4.3.1" />
<ProjectReference Include="..\MLEM\MLEM.csproj" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641">
@ -25,6 +25,6 @@
</ItemGroup>
<ItemGroup>
<None Include="../Media/Logo.png" Pack="true" PackagePath=""/>
<None Include="../Media/Logo.png" Pack="true" PackagePath="" />
</ItemGroup>
</Project>

View file

@ -34,12 +34,6 @@ namespace MLEM.Extensions {
/// </summary>
public static class ColorHelper {
/// <inheritdoc cref="ColorExtensions.Invert"/>
[Obsolete("This method has been moved to ColorExtensions.Invert in 5.1.0")]
public static Color Invert(this Color color) {
return ColorExtensions.Invert(color);
}
/// <summary>
/// Parses a hexadecimal number into an rgba color.
/// The number should be in the format <c>0xaarrggbb</c>.

View file

@ -72,7 +72,7 @@ namespace Sandbox {
textureData[textureData.FromIndex(textureData.ToIndex(25, 9))] = Color.Yellow;
}
var system = new FontSystem(this.GraphicsDevice, 1024, 1024);
var system = new FontSystem();
system.AddFont(File.ReadAllBytes("Content/Fonts/Cadman_Roman.otf"));
//var font = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont"));
//var font = new GenericBitmapFont(LoadContent<BitmapFont>("Fonts/Regular"));

View file

@ -18,7 +18,7 @@
<PackageReference Include="MonoGame.Extended.Content.Pipeline" Version="3.8.0" />
<PackageReference Include="MonoGame.Extended.Tiled" Version="3.8.0" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641" />
<PackageReference Include="FontStashSharp.MonoGame" Version="0.9.2" />
<PackageReference Include="FontStashSharp.MonoGame" Version="0.9.5" />
</ItemGroup>
<ItemGroup>

View file

@ -13,10 +13,13 @@
<ItemGroup>
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641" />
<PackageReference Include="coverlet.collector" Version="3.0.3" />
<PackageReference Include="coverlet.collector" Version="3.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="NunitXml.TestLogger" Version="3.0.107" />
</ItemGroup>