Skip to content

Commit f42ff8d

Browse files
committed
Add C# version for examples of custom BBCode tags and text effects
Include C# implementations for custom BBCode tags and text effects examples in "BBCode in RichTextLabel" tutorial.
1 parent 1c485f9 commit f42ff8d

File tree

1 file changed

+71
-3
lines changed

1 file changed

+71
-3
lines changed

tutorials/ui/bbcode_in_richtextlabel.rst

+71-3
Original file line numberDiff line numberDiff line change
@@ -911,8 +911,9 @@ Here are some examples of custom effects:
911911
Ghost
912912
~~~~~
913913

914-
::
915-
914+
.. tabs::
915+
.. code-tab:: gdscript GDScript
916+
916917
@tool
917918
extends RichTextEffect
918919
class_name RichTextGhost
@@ -931,10 +932,35 @@ Ghost
931932
char_fx.color.a = alpha
932933
return true
933934

935+
.. code-tab:: csharp
936+
937+
using Godot;
938+
939+
[Tool]
940+
public class RichTextGhost : RichTextEffect
941+
{
942+
// Syntax: [ghost freq=5.0 span=10.0][/ghost]
943+
944+
// Define the tag name.
945+
private const string Bbcode = "ghost";
946+
947+
public override bool _ProcessCustomFx(CharFXTransform charFX)
948+
{
949+
// Get parameters, or use the provided default value if missing.
950+
var speed = charFx.Env.Get("freq", 5.0f);
951+
var span = charFx.Env.Get("span", 10.0f);
952+
953+
var alpha = Mathf.Sin(charFx.ElapsedTime * speed + (charFx.Range.X / span)) * 0.5f + 0.5f;
954+
charFx.Color.A = alpha;
955+
return true;
956+
}
957+
}
958+
934959
Matrix
935960
~~~~~~
936961

937-
::
962+
.. tabs::
963+
.. code-tab:: gdscript GDScript
938964

939965
@tool
940966
extends RichTextEffect
@@ -970,6 +996,48 @@ Matrix
970996
char_fx.glyph_index = get_text_server().font_get_glyph_index(char_fx.font, 1, value, 0)
971997
return true
972998

999+
.. code-tab:: csharp
1000+
1001+
using Godot;
1002+
1003+
[Tool]
1004+
public class RichTextMatrix : RichTextEffect
1005+
{
1006+
// Syntax: [matrix clean=2.0 dirty=1.0 span=50][/matrix]
1007+
1008+
// Define the tag name.
1009+
private const string Bbcode = "matrix";
1010+
1011+
// Gets TextServer for retrieving font information.
1012+
private TextServer GetTextServer()
1013+
{
1014+
return TextServerManager.GetPrimaryInterface();
1015+
}
1016+
1017+
public override bool _ProcessCustomFx(CharFXTransform charFX)
1018+
{
1019+
// Get parameters, or use the provided default value if missing.
1020+
var clearTime = charFx.Env.Get("clean", 2.0f);
1021+
var dirtyTime = charFx.Env.Get("dirty", 1.0f);
1022+
var textSpan = charFx.Env.Get("span", 50);
1023+
1024+
var value = charFx.GlyphIndex;
1025+
1026+
var matrixTime = Mathf.Fmod(charFx.ElapsedTime + (charFx.Range.X / textSpan), clearTime + dirtyTime);
1027+
1028+
matrixTime = matrixTime < clearTime ? 0.0f : (matrixTime - clearTime) / dirtyTime;
1029+
1030+
if (matrixTime > 0.0f)
1031+
{
1032+
value = (int)(1 * matrixTime * (126 - 65));
1033+
value %= (126 - 65);
1034+
value += 65;
1035+
}
1036+
charFx.GlyphIndex = GetTextServer().FontGetGlyphIndex(charFx.Font, 1, value, 0);
1037+
return true;
1038+
}
1039+
}
1040+
9731041
This will add a few new BBCode commands, which can be used like so:
9741042

9751043
.. code-block:: none

0 commit comments

Comments
 (0)