Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MG 3.8.x compatibility and FSS Scale, Effect & RichTextLayout #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,837 changes: 929 additions & 908 deletions samples/NvgSharp.Samples.Demo/Demo.cs

Large diffs are not rendered by default.

17 changes: 10 additions & 7 deletions samples/NvgSharp.Samples.Demo/NvgExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FontStashSharp;
using System.Text;
using Microsoft.Xna.Framework;

namespace NvgSharp
{
Expand Down Expand Up @@ -41,9 +42,10 @@ public enum TextVerticalAlignment

internal static class NvgExtensions
{
public static void TextAligned(this NvgContext context, SpriteFontBase font, string text, float x, float y,
public static void TextAligned(this NvgContext context, SpriteFontBase font, string text, float x, float y, Vector2 scale,
TextHorizontalAlignment horizontalAlignment = TextHorizontalAlignment.Left, TextVerticalAlignment verticalAlignment = TextVerticalAlignment.Top,
float layerDepth = 0.0f, float characterSpacing = 0.0f, float lineSpacing = 0.0f)
float layerDepth = 0.0f, float characterSpacing = 0.0f, float lineSpacing = 0.0f, FontSystemEffect effect = FontSystemEffect.Stroked,
int effectAmount = 0)
{
if (string.IsNullOrEmpty(text))
{
Expand Down Expand Up @@ -72,12 +74,13 @@ public static void TextAligned(this NvgContext context, SpriteFontBase font, str
y -= font.LineHeight;
}

context.Text(font, text, x, y, layerDepth, characterSpacing, lineSpacing);
context.Text(font, text, x, y, scale, layerDepth, characterSpacing, lineSpacing, effect, effectAmount);
}

public static void TextAligned(this NvgContext context, SpriteFontBase font, StringBuilder text, float x, float y,
TextHorizontalAlignment horizontalAlignment = TextHorizontalAlignment.Left, TextVerticalAlignment verticalAlignment = TextVerticalAlignment.Top,
float layerDepth = 0.0f, float characterSpacing = 0.0f, float lineSpacing = 0.0f)
public static void TextAligned(this NvgContext context, SpriteFontBase font, StringBuilder text, float x, float y, Vector2 scale,
TextHorizontalAlignment horizontalAlignment = TextHorizontalAlignment.Left, TextVerticalAlignment verticalAlignment = TextVerticalAlignment.Top,
float layerDepth = 0.0f, float characterSpacing = 0.0f, float lineSpacing = 0.0f, FontSystemEffect effect = FontSystemEffect.Stroked,
int effectAmount = 0)
{
if (text == null || text.Length == 0)
{
Expand Down Expand Up @@ -106,7 +109,7 @@ public static void TextAligned(this NvgContext context, SpriteFontBase font, Str
y -= font.LineHeight;
}

context.Text(font, text, x, y, layerDepth, characterSpacing, lineSpacing);
context.Text(font, text, x, y, scale, layerDepth, characterSpacing, lineSpacing, effect, effectAmount);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0</TargetFramework>
Expand Down Expand Up @@ -63,7 +63,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303" />
</ItemGroup>

<ItemGroup>
Expand Down
10 changes: 5 additions & 5 deletions samples/NvgSharp.Samples.Demo/PerfGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,34 +114,34 @@ public void Render(NvgContext vg, float x, float y)
{
var font = _fontSystem.GetFont(14);
vg.FillColor(Utility.FromRGBA(240, 240, 240, 192));
vg.TextAligned(font, _name, x + 3, y + 1, TextHorizontalAlignment.Left, TextVerticalAlignment.Top);
vg.TextAligned(font, _name, x + 3, y + 1, Vector2.One, TextHorizontalAlignment.Left, TextVerticalAlignment.Top);
}

if (_style == Style.GRAPH_RENDER_FPS)
{
var font = _fontSystem.GetFont(18);
vg.FillColor(Utility.FromRGBA(240, 240, 240, 255));
str = string.Format("{0:0.00} FPS", 1.0f / avg);
vg.TextAligned(font, str, x + w - 3, y + 1, TextHorizontalAlignment.Right, TextVerticalAlignment.Top);
vg.TextAligned(font, str, x + w - 3, y + 1, Vector2.One, TextHorizontalAlignment.Right, TextVerticalAlignment.Top);

font = _fontSystem.GetFont(15);
vg.FillColor(Utility.FromRGBA(240, 240, 240, 160));
str = string.Format("{0:0.00} ms", avg * 1000.0f);
vg.TextAligned(font, str, x + w - 3, y + h - 1, TextHorizontalAlignment.Right, TextVerticalAlignment.Bottom);
vg.TextAligned(font, str, x + w - 3, y + h - 1, Vector2.One, TextHorizontalAlignment.Right, TextVerticalAlignment.Bottom);
}
else if (_style == Style.GRAPH_RENDER_PERCENT)
{
var font = _fontSystem.GetFont(18);
vg.FillColor(Utility.FromRGBA(240, 240, 240, 255));
str = string.Format("{0:0.00} %%", avg);
vg.TextAligned(font, str, x + w - 3, y + 1, TextHorizontalAlignment.Right, TextVerticalAlignment.Top);
vg.TextAligned(font, str, x + w - 3, y + 1, Vector2.One, TextHorizontalAlignment.Right, TextVerticalAlignment.Top);
}
else
{
var font = _fontSystem.GetFont(18);
vg.FillColor(Utility.FromRGBA(240, 240, 240, 255));
str = string.Format("{0:0.00} ms", avg * 1000.0f);
vg.TextAligned(font, str, x + w - 3, y + 1, TextHorizontalAlignment.Right, TextVerticalAlignment.Top);
vg.TextAligned(font, str, x + w - 3, y + 1, Vector2.One, TextHorizontalAlignment.Right, TextVerticalAlignment.Top);
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/NvgSharp.Text/NvgSharp.Text.MonoGame.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net60</TargetFramework>
<Description>NvgSharp.Text for MonoGame</Description>
<DefineConstants>$(DefineConstants);MONOGAME</DefineConstants>
<OutputPath>bin\MonoGame\$(Configuration)</OutputPath>
<Version>0.8.0</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FontStashSharp.MonoGame" Version="$(FontStashSharpVersion)" />
<PackageReference Include="MonoGame.Framework.Portable" Version="3.7.1.189" PrivateAssets="All" />
<PackageReference Include="FontStashSharp.MonoGame" Version="1.3.7">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
69 changes: 54 additions & 15 deletions src/NvgSharp.Text/NvgText.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Text;
using FontStashSharp;
using FontStashSharp.Interfaces;
using FontStashSharp.RichText;

#if MONOGAME || FNA
using Microsoft.Xna.Framework;
Expand All @@ -27,9 +28,9 @@
internal int _lastVertexOffset;
internal Texture2D _lastTextTexture = null;

#if MONOGAME || FNA || STRIDE
#if MONOGAME || FNA || STRIDE
public GraphicsDevice GraphicsDevice => _context.GraphicsDevice;
#else
#else

public ITexture2DManager TextureManager => this;

Expand All @@ -39,7 +40,7 @@

public void SetTextureData(object texture, Rectangle bounds, byte[] data) => _context._renderer.SetTextureData(texture, bounds, data);

#endif
#endif

public TextRenderer(NvgContext context)
{
Expand Down Expand Up @@ -109,7 +110,8 @@
_lastTextTexture = null;
}

public void Text(SpriteFontBase font, TextSource text, float x, float y, float layerDepth, float characterSpacing, float lineSpacing)
public void Text(SpriteFontBase font, TextSource text, float x, float y, float layerDepth, float characterSpacing,
float lineSpacing, Vector2 scale, FontSystemEffect effect = FontSystemEffect.None, int effectAmount = 0)
{
if (text.IsNull)
{
Expand All @@ -121,38 +123,75 @@
if (text.StringText != null)
{
font.DrawText(this, text.StringText, new Vector2(x, y), Color.White,
layerDepth: layerDepth, characterSpacing: characterSpacing, lineSpacing: lineSpacing);
layerDepth: layerDepth, characterSpacing: characterSpacing, lineSpacing: lineSpacing, scale: scale,
effect: effect, effectAmount: effectAmount);
}
else
{
font.DrawText(this, text.StringBuilderText, new Vector2(x, y), Color.White,
layerDepth: layerDepth, characterSpacing: characterSpacing, lineSpacing: lineSpacing);
layerDepth: layerDepth, characterSpacing: characterSpacing, lineSpacing: lineSpacing,
scale: scale, effect: effect, effectAmount: effectAmount);
}

FlushText();
}

public void Text(RichTextLayout rtl, float x, float y, Vector2 scale, float layerDepth, TextHorizontalAlignment horzAlign)
{
if (rtl == null || rtl.Text.Length == 0)
{
return;
}

_lastVertexOffset = _context._renderCache.VertexCount;
rtl.Draw(this, new Vector2(x, y), Color.Blue, 0,
layerDepth: layerDepth, scale: scale, horizontalAlignment: horzAlign);

Check failure on line 148 in src/NvgSharp.Text/NvgText.cs

View workflow job for this annotation

GitHub Actions / BuildAndPublish

The best overload for 'Draw' does not have a parameter named 'scale'

Check failure on line 148 in src/NvgSharp.Text/NvgText.cs

View workflow job for this annotation

GitHub Actions / BuildAndPublish

The best overload for 'Draw' does not have a parameter named 'scale'
FlushText();
}
}

private static void Text(this NvgContext context, SpriteFontBase font, TextSource text, float x, float y,
float layerDepth, float characterSpacing, float lineSpacing)
float layerDepth, float characterSpacing, float lineSpacing, Vector2 scale,
FontSystemEffect effect = FontSystemEffect.None, int effectAmount = 0)
{
TextRenderer textRenderer;
if (context._textRenderer == null)
{
textRenderer = new TextRenderer(context);
context._textRenderer = textRenderer;
} else {
}
else
{
textRenderer = (TextRenderer)context._textRenderer;
}

textRenderer.Text(font, text, x, y, layerDepth, characterSpacing, lineSpacing, scale, effect, effectAmount);
}

public static void Text(this NvgContext context, RichTextLayout rtl, float x, float y, Vector2 scale, float layerDepth, TextHorizontalAlignment horzAlign = TextHorizontalAlignment.Left)
{
TextRenderer textRenderer;
if (context._textRenderer == null)
{
textRenderer = new TextRenderer(context);
context._textRenderer = textRenderer;
}
else
{
textRenderer = (TextRenderer)context._textRenderer;
}

textRenderer.Text(font, text, x, y, layerDepth, characterSpacing, lineSpacing);
textRenderer.Text(rtl, x, y, layerDepth: layerDepth, scale: scale, horzAlign: horzAlign);
}

public static void Text(this NvgContext context, SpriteFontBase font, string text, float x, float y,
float layerDepth = 0.0f, float characterSpacing = 0.0f, float lineSpacing = 0.0f) =>
Text(context, font, new TextSource(text), x, y, layerDepth, characterSpacing, lineSpacing);
public static void Text(this NvgContext context, SpriteFontBase font, string text, float x, float y, Vector2 scale,
float layerDepth = 0.0f, float characterSpacing = 0.0f, float lineSpacing = 0.0f,
FontSystemEffect effect = FontSystemEffect.None, int effectAmount = 0) =>
Text(context, font, new TextSource(text), x, y, layerDepth, characterSpacing, lineSpacing, scale, effect, effectAmount);

public static void Text(this NvgContext context, SpriteFontBase font, StringBuilder text, float x, float y,
float layerDepth = 0.0f, float characterSpacing = 0.0f, float lineSpacing = 0.0f) =>
Text(context, font, new TextSource(text), x, y, layerDepth, characterSpacing, lineSpacing);
public static void Text(this NvgContext context, SpriteFontBase font, StringBuilder text, float x, float y, Vector2 scale,
float layerDepth = 0.0f, float characterSpacing = 0.0f, float lineSpacing = 0.0f,
FontSystemEffect effect = FontSystemEffect.None, int effectAmount = 0) =>
Text(context, font, new TextSource(text), x, y, layerDepth, characterSpacing, lineSpacing, scale, effect, effectAmount);
}
}
7 changes: 5 additions & 2 deletions src/XNA/NvgSharp.MonoGame.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net60</TargetFramework>
<Description>NvgSharp for MonoGame</Description>
<DefineConstants>$(DefineConstants);MONOGAME</DefineConstants>
<OutputPath>bin\MonoGame\$(Configuration)</OutputPath>
<OutputType>Library</OutputType>
<Version>0.8.0</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
Expand All @@ -23,6 +25,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MonoGame.Framework.Portable" Version="3.7.1.189" PrivateAssets="All" />
<PackageReference Include="FontStashSharp.MonoGame" Version="1.3.7" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303" />
</ItemGroup>
</Project>
Binary file modified src/XNA/Resources/Effect.dx11.mgfxo
Binary file not shown.
Binary file modified src/XNA/Resources/Effect_AA.dx11.mgfxo
Binary file not shown.
Binary file modified src/XNA/Resources/Effect_AA.ogl.mgfxo
Binary file not shown.
4 changes: 2 additions & 2 deletions src/XNA/Resources/build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ mgfxc Effect.fx Effect_AA.ogl.mgfxo /Profile:OpenGL /defines:EDGE_AA=1
mgfxc Effect.fx Effect.dx11.mgfxo /Profile:DirectX_11
mgfxc Effect.fx Effect_AA.dx11.mgfxo /Profile:DirectX_11 /defines:EDGE_AA=1

"D:\Windows Kits\10\bin\10.0.19041.0\x64\fxc.exe" /T fx_2_0 /Fo Effect.fxb Effect.fx
"D:\Windows Kits\10\bin\10.0.19041.0\x64\fxc.exe" /T fx_2_0 /Fo Effect_AA.fxb Effect.fx /D EDGE_AA=1
"C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64\fxc.exe" /T fx_2_0 /Fo Effect.fxb Effect.fx
"C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64\fxc.exe" /T fx_2_0 /Fo Effect_AA.fxb Effect.fx /D EDGE_AA=1
Loading