Skip to content

Commit

Permalink
Document Texts
Browse files Browse the repository at this point in the history
  • Loading branch information
Terria-K committed Jan 30, 2024
1 parent 2b24efd commit 9f3393a
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Riateu/Core/Canvases/Canvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public Canvas(Scene scene, GraphicsDevice device, uint width, uint height)
this.Scene = scene;
}

public virtual void BeforeDraw(ref CommandBuffer buffer, IBatch batch) {}
public virtual void BeforeDraw(CommandBuffer buffer, IBatch batch) {}
public virtual void Draw(CommandBuffer buffer, IBatch batch)
{
}
public virtual void AfterDraw(ref CommandBuffer buffer, IBatch batch) {}
public virtual void AfterDraw(CommandBuffer buffer, IBatch batch) {}

public static Canvas CreateDefault(Scene scene, GraphicsDevice device)
{
Expand Down
56 changes: 51 additions & 5 deletions Riateu/Core/Canvases/DynamicText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@

namespace Riateu;

/// <summary>
/// This type of text can be changed their property dynamically. When the property changed,
/// it will try to re-render and resubmit its texture. Becareful when using this text.
/// </summary>
public class DynamicText : Text
{
private bool dirty;
private GraphicsDevice device;
private Font font;

/// <summary>
/// A string text for this text. When changed, it will caused to re-render and resubmit
/// its texture.
/// </summary>
public string Text
{
get => text;
Expand All @@ -25,6 +33,27 @@ public string Text
}
}

/// <summary>
/// A pixel size of this font. When changed, it will caused to re-render and resubmit
/// its texture.
/// </summary>
public int Pixel
{
get => pixel;
set
{
if (pixel != value)
{
pixel = value;
dirty = true;
}
}
}

/// <summary>
/// A numeric visibled text value. When changed, it will not resubmit, instead it will
/// just change the quad of the texture.
/// </summary>
public int VisibleText
{
get => visibleText;
Expand All @@ -49,7 +78,7 @@ public int VisibleText

var span = text.AsSpan();
var newSpan = span.Slice(0, visibleText);
var f = font.TextBounds(newSpan, pixelSize, HorizontalAlignment.Left,
var f = font.TextBounds(newSpan, pixel, HorizontalAlignment.Left,
VerticalAlignment.Baseline, out WellspringCS.Wellspring.Rectangle rectVisible);

DynamicTexture = new SpriteTexture(
Expand All @@ -62,11 +91,27 @@ public int VisibleText
}

private int visibleText;
private SpriteTexture DynamicTexture;
/// <summary>
/// The texture width of the rendered text.
/// </summary>
public uint Width => (uint)rect.W;
/// <summary>
/// The texture height of the rendered text.
/// </summary>
public uint Height => (uint)rect.H;
private Rect rect;
private SpriteTexture DynamicTexture;
private string text;
private int pixel;

/// <summary>
/// An initialization for this class.
/// </summary>
/// <param name="device">An application graphics device</param>
/// <param name="font">A font to use for rendering text</param>
/// <param name="text">A text that should be rendered</param>
/// <param name="pixel">A size of the text</param>
/// <param name="textVisible">A numeric visibled text value</param>
public DynamicText(GraphicsDevice device, Font font, string text, int pixel, int textVisible = -1)
{
if (textVisible == -1)
Expand All @@ -77,9 +122,9 @@ public DynamicText(GraphicsDevice device, Font font, string text, int pixel, int
this.font = font;
Batch = new TextBatch(device);
this.text = text;
pixelSize = pixel;
this.pixel = pixel;

var f = font.TextBounds(text, pixelSize, HorizontalAlignment.Left,
var f = font.TextBounds(text, this.pixel, HorizontalAlignment.Left,
VerticalAlignment.Baseline, out WellspringCS.Wellspring.Rectangle rect);
this.rect.X = (int)rect.X;
this.rect.Y = (int)rect.Y;
Expand Down Expand Up @@ -114,7 +159,7 @@ private void Resubmit()
* Matrix4x4.CreateOrthographicOffCenter(0, Width, Height, 0, -1, 1);

Batch.Start(font);
Batch.Add(text, pixelSize, Color.White);
Batch.Add(text, pixel, Color.White);
Batch.UploadBufferData(buffer);

buffer.BeginRenderPass(new ColorAttachmentInfo(Texture, Color.Transparent));
Expand All @@ -124,6 +169,7 @@ private void Resubmit()
device.Submit(buffer);
}

/// <inheritdoc/>
public override void Draw(IBatch batch, Vector2 position)
{
if (dirty)
Expand Down
20 changes: 19 additions & 1 deletion Riateu/Core/Canvases/StaticText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,31 @@

namespace Riateu;

/// <summary>
/// This type of text cannot be changed their property dynamically. It is readonly text
/// that can be rendered.
/// </summary>
public class StaticText : Text
{
/// <summary>
/// The immutable string text of this text.
/// </summary>
public string Text => text;
private string text;

/// <summary>
/// An initialization for this class.
/// </summary>
/// <param name="device">An application graphics device</param>
/// <param name="font">A font to use for rendering text</param>
/// <param name="text">A text that should be rendered</param>
/// <param name="pixel">A size of the text</param>
/// <param name="textVisible">A numeric visibled text value</param>
public StaticText(GraphicsDevice device, Font font, string text, int pixel, int textVisible = -1)
{
textVisible = MathHelper.Clamp(textVisible, 0, text.Length);
CommandBuffer buffer = device.AcquireCommandBuffer();
this.text = text;
pixelSize = pixel;

var textSpan = text.AsSpan();
var textSpanSliced = textSpan.Slice(0, textVisible);
Expand Down Expand Up @@ -45,6 +62,7 @@ public StaticText(GraphicsDevice device, Font font, string text, int pixel, int
device.Submit(buffer);
}

/// <inheritdoc/>
public override void Draw(IBatch batch, Vector2 position)
{
batch.Add(Texture, GameContext.GlobalSampler, position, Matrix3x2.Identity);
Expand Down
8 changes: 6 additions & 2 deletions Riateu/Core/Canvases/Text.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@

namespace Riateu;

/// <summary>
/// The base class for the Text object.
/// </summary>
public abstract class Text : IDisposable
{
internal TextBatch Batch;
/// <summary>
/// The texture of a text after it rendered.
/// </summary>
public Texture Texture { get; protected set; }
protected int pixelSize;
protected string text;
private bool IsDisposed { get; set; }

public Rectangle Bounds { get; protected set; }
Expand Down
1 change: 0 additions & 1 deletion Riateu/Core/Graphics/InstanceBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using MoonWorks;
using MoonWorks.Graphics;
using MoonWorks.Math.Float;
using Riateu.Components;

namespace Riateu.Graphics;

Expand Down
4 changes: 2 additions & 2 deletions Riateu/Core/Scene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ internal void InternalUpdate(double delta)

internal void InternalBeforeDraw(ref CommandBuffer buffer, IBatch batch)
{
sceneCanvas.BeforeDraw(ref buffer, batch);
sceneCanvas.BeforeDraw(buffer, batch);
BeforeDraw(ref buffer, batch);
}

Expand All @@ -122,7 +122,7 @@ internal void InternalDraw(CommandBuffer buffer, Texture backbuffer, IBatch batc

internal void InternalAfterDraw(ref CommandBuffer buffer, IBatch batch)
{
sceneCanvas.AfterDraw(ref buffer, batch);
sceneCanvas.AfterDraw(buffer, batch);
AfterDraw(ref buffer, batch);
}

Expand Down
3 changes: 1 addition & 2 deletions Riateu/Core/Structs/InstancedVertex.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System.Runtime.InteropServices;
using MoonWorks.Graphics;
using MoonWorks.Math.Float;
using Riateu.Graphics;

namespace Riateu.Components;
namespace Riateu.Graphics;

/// <summary>
/// A vertex type to be used for the <see cref="GameContext.InstancedPipeline"/>.
Expand Down

0 comments on commit 9f3393a

Please sign in to comment.