Helper methods – StringDrawing Helpers - TiberiumFusion/TTPlugins GitHub Wiki

Back to the Helper methods overview article.


These helpers are located in the nested [HHelpers.StringDrawing](https://www.tiberiumfusion.com/product/ttplugins/reference/latest/html/T_com_tiberiumfusion_ttplugins_HarmonyPlugins_HHelpers_StringDrawing.htm class and assist in drawing text with ReLogic's subclassed DynamicSpriteFont type.

All helper methods are static.


Method voidDrawString(SpriteBatch spriteBatch, ReLogic.Graphics.DynamicSpriteFont relogicDynamicSpriteFont, string text, Vector2 position, Color baseColor, float rotation, Vector2 origin, Vector2 baseScale, float maxWidth = -1f)

Draws some text using a ReLogic.Graphics.DynamicSpriteFont (as opposed to a normal SpriteFont).

Parameters:

  • spriteBatch - The SpriteBatch to use.
  • relogicDynamicSpriteFont - The ReLogic.Graphics.DynamicSpriteFont to use.
  • text - The text to draw on the screen.
  • position - The position (in pixels) to draw the text at.
  • baseColor - The color to use for the text.
  • rotation - The angle to draw the text at, in radians.
  • origin - The origin position (in pixels) for registering the drawn text's position.
  • baseScale - The scale of the text. It's a Vector2, so you can scale the X and Y individually.
  • (Optional) maxWidth - Specifies how long a line of text can be until it wraps to a new line. Set to -1f to disable wrapping.

Example usage:

HHelpers.StringDrawing.DrawString(Terraria.Main.spriteBatch,
                                  Terraria.GameContent.FontAssets.MouseText.Value,
                                  "I am some text",
                                  new Vector2(200, 100),
                                  Color.White,
                                  0f,
                                  Vector2.Zero,
                                  Vector2.One);

TIP: If your code produces a "type of ReLogic.Graphics.DynamicSpriteFont is not the same as type of ReLogic.Graphics.DynamicSpriteFont" error when you use this method, you will need to create a type-fixed copy of the ReLogic.Graphics.DynamicSpriteFont you are trying to use. See TryCreateTypeFixedDynamicSpriteFontFromExisting().

Method voidDrawStringWithShadow(SpriteBatch spriteBatch, ReLogic.Graphics.DynamicSpriteFont relogicDynamicSpriteFont, string text, Vector2 position, Color baseColor, float rotation, Vector2 origin, Vector2 baseScale, float maxWidth = -1f, float spread = 2f, Color? shadowColor = null)

Draws some shadowed text using a ReLogic.Graphics.DynamicSpriteFont (as opposed to a normal SpriteFont). This method has almost the exact same parameters as DrawString(), so refer to the above section for information on those.

Additional parameters (compared to DrawString()):

  • (Optional) spread - Controls how large the text's shadow is.
  • (Optional) shadowColor - The color of the text's shadow. Set to null to use the default black color.

Method boolTryCreateTypeFixedDynamicSpriteFontFromExisting(string fontFieldName, out ReLogic.Graphics.DynamicSpriteFont result)

Creates a type-fixed copy of the ReLogic.Graphics.DynamicSpriteFont specified by field name in the Terraria.GameContent.FontAssets type. In some rare circumstances, plugin code may not execute in the same context as the rest of Terraria, which causes errors like "type xyz is not the same as type xyz". In those situations, you can use this method to create a "type-fixed" version of an existing ReLogic.Graphics.DynamicSpriteFont from Terraria.GameContent.FontAssets within the plugin's context. The type-fixed object will then work with the DrawString() helper methods.

Parameters:

  • fontFieldName - The name of the field in Terraria.GameContent.FontAssets that contains the ReLogic.Graphics.DynamicSpriteFont that you want to copy & type-fix.
  • result - A variable for receiving the new ReLogic.Graphics.DynamicSpriteFont object.

If the method succeeded, result will be set to the newly created ReLogic.Graphics.DynamicSpriteFont and true will be returned. If the method failed, result will be null and false will be returned.

NOTE: This is a very expensive operation and should be used sparingly. Call this once and store the result to use later. Do not call this method on a per-frame basis.

Example usage:

ReLogic.Graphics.DynamicSpriteFont fixedReLogicSpriteFont = null;
HHelpers.StringDrawing.TryCreateTypeFixedDynamicSpriteFontFromExisting("MouseText", out fixedReLogicSpriteFont);
// fixedReLogicSpriteFont can be used with DrawString() or DrawStringWithShadow() now

Back to the Helper methods overview article.