public class LazyFont extends Object
loadFont(String)
. This can be kept around safely without worrying about
memory usage - the underlying texture makes up the bulk of the size, and that is only loaded once per unique
font and stored in a global cache. LazyFont
itself only contains font metadata used for rendering,
and is relatively lightweight.LazyFont.DrawableString
with
createText(String, Color, float, float, float)
. This methods arguments determine how the
text will appear and any size restrictions the block of text should be given. These can all be changed later
by calling methods directly on the returned LazyFont.DrawableString
.LazyFont.DrawableString.draw(float, float)
once per frame.LazyFont.DrawableString
, you can re-draw the text
extremely efficiently, or edit/append the text at will. Editing the LazyFont.DrawableString
in any way
will cause a full rebuild of its internal display list the next time you call
LazyFont.DrawableString.draw(float, float)
, though the performance impact should be minimal even with
extremely long text blocks.LazyFont.DrawableString
, optionally call LazyFont.DrawableString.dispose()
to ensure
its underlying OpenGL resources are cleaned up immediately. Ensure you don't call
LazyFont.DrawableString.draw(float, float)
after calling LazyFont.DrawableString.dispose()
or your game
will crash!
package data.scripts.plugins;
import com.fs.starfarer.api.Global;
import com.fs.starfarer.api.combat.BaseEveryFrameCombatPlugin;
import com.fs.starfarer.api.combat.CombatEngineAPI;
import com.fs.starfarer.api.combat.ViewportAPI;
import org.lazywizard.lazylib.ui.FontException;
import org.lazywizard.lazylib.ui.LazyFont;
import org.lwjgl.input.Mouse;
import java.awt.*;
public class LazyFontExample extends BaseEveryFrameCombatPlugin
{
private LazyFont.DrawableString toDraw;
// Set up the font and the DrawableString; only has to be done once
@Override
public void init(CombatEngineAPI engine)
{
// Load the chosen .fnt file
// Fonts are cached globally, so it's acceptable for each class using the same
// font to request their own copy of it - they will all share the underlying data
final LazyFont font;
try
{
font = LazyFont.loadFont("graphics/fonts/insignia15LTaa.fnt");
}
// FontException is thrown if the .fnt file does not exist or has malformed data
catch (FontException ex)
{
Global.getLogger(this.getClass()).error("Failed to load font", ex);
engine.removePlugin(this);
return;
}
// Create a renderable block of text (in this case, will be yellow with font size 15)
toDraw = font.createText("This is some sample text.", Color.YELLOW, 15f);
// Enable line wrapping when text reaches 400 pixels wide
toDraw.setMaxWidth(400f);
// If you need to add text to the DrawableString, do so like this:
toDraw.appendText("\nThis is a second line of sample text.");
toDraw.appendText("\nThis is a third line of sample text that shows off the automatic" +
" word wrapping when a line of text reaches the maximum width you've chosen.");
}
@Override
public void renderInUICoords(ViewportAPI view)
{
// Call draw() once per frame to render the text
// In this case, draw the text slightly below the mouse cursor
// The draw point is the top left corner of the textbox, so we adjust the X
// position to center the text horizontally below the mouse cursor
if (toDraw != null) // Needed to work around a vanilla combat plugin bug when loading the campaign
{
toDraw.draw(Mouse.getX() - (toDraw.getWidth() / 2f), Mouse.getY() - 30f);
}
}
}
Modifier and Type | Class and Description |
---|---|
class |
LazyFont.DrawableString
Represents an efficiently redrawable block of text.
|
class |
LazyFont.LazyChar
Keeps track of the metadata for each supported character in a font.
|
Modifier and Type | Method and Description |
---|---|
float |
calcWidth(String rawLine,
float fontSize)
Returns the raw width of a
String at a specific font size, without taking into account word wrapping. |
@NotNull LazyFont.DrawableString |
createText()
Create a
LazyFont.DrawableString with no text, ready for appending. |
@NotNull LazyFont.DrawableString |
createText(String text)
Create a
LazyFont.DrawableString with the specified initial text. |
@NotNull LazyFont.DrawableString |
createText(String text,
Color color)
Create a
LazyFont.DrawableString with the specified initial text and color. |
@NotNull LazyFont.DrawableString |
createText(String text,
Color color,
float size)
Create a
LazyFont.DrawableString with the specified initial text, color, and font size, with no text wrapping. |
@NotNull LazyFont.DrawableString |
createText(String text,
Color color,
float size,
float maxWidth)
Create a
LazyFont.DrawableString with the specified initial text, color, and font size, with text wrapping. |
@NotNull LazyFont.DrawableString |
createText(String text,
Color color,
float size,
float maxWidth,
float maxHeight)
Create a
LazyFont.DrawableString with the specified initial text, color, and font size, with text wrapping and
a max height - any appended text past that limit will be discarded. |
@NotNull Vector2f |
drawText(String text,
float x,
float y,
float fontSize,
float maxWidth,
float maxHeight)
Renders a block of text, used to manually create display lists - not recommended for general
usage.
|
float |
getBaseHeight()
Returns the base height of the underlying font.
|
LazyFont.LazyChar |
getChar(char character)
Returns font metadata for a specific character.
|
float |
getTextureHeight()
Returns the height of the font's underlying texture atlas.
|
int |
getTextureId()
Returns the ID of the font's underlying texture atlas.
|
float |
getTextureWidth()
Returns the width of the font's underlying texture atlas.
|
static @NotNull LazyFont |
loadFont(String fontPath)
Loads a bitmap font and returns the
LazyFont representation. |
@NotNull String |
wrapString(String toWrap,
float fontSize,
float maxWidth)
Returns the word-wrapping that would be done to a block of text to fit it within a specific area at a specific
font size.
|
@NotNull String |
wrapString(String toWrap,
float fontSize,
float maxWidth,
float maxHeight)
Returns the word-wrapping that would be done to a block of text to fit it within a specific area at a specific
font size.
|
@NotNull String |
wrapString(String toWrap,
float fontSize,
float maxWidth,
float maxHeight,
int indent)
Returns the word-wrapping that would be done to a block of text to fit it within a specific area at a specific
font size.
|
@NotNull public static @NotNull LazyFont loadFont(String fontPath) throws FontException
LazyFont
representation.
This method caches loaded fonts, so only one LazyFont
will exist for each
font file loaded, and subsequent calls will be near instantaneous.fontPath
- The relative path to the .fnt file (ex: "graphics/fonts/insignia15LTaa.fnt"
).LazyFont
representation of the bitmap font at fontPath
.FontException
- If there's no font found at fontPath
or the data in the font is malformed.@NotNull public @NotNull String wrapString(String toWrap, float fontSize, float maxWidth, float maxHeight, int indent)
calcWidth(String, float)
to determine the size of a block of
rendered text without needing the expense of building a LazyFont.DrawableString
first.toWrap
- The text to be word-wrapped.fontSize
- The font size the text would be rendered at.maxWidth
- The max width of the text area. Text will be wrapped at the last word that fit within this
width.maxHeight
- The max height of the text area. Text beyond this will be discarded.indent
- The number of empty spaces at the start of each line. Used by LazyFont.DrawableString.appendText(String, int)
.toWrap
, wrapped to fit within the area specified by maxWidth
and maxHeight
.@NotNull public @NotNull String wrapString(String toWrap, float fontSize, float maxWidth, float maxHeight)
calcWidth(String, float)
to determine the size of a block of
rendered text without needing the expense of building a LazyFont.DrawableString
first.toWrap
- The text to be word-wrapped.fontSize
- The font size the text would be rendered at.maxWidth
- The max width of the text area. Text will be wrapped at the last word that fit within this
width.maxHeight
- The max height of the text area. Text beyond this will be discarded.toWrap
, wrapped to fit within the area specified by maxWidth
and maxHeight
.@NotNull public @NotNull String wrapString(String toWrap, float fontSize, float maxWidth)
calcWidth(String, float)
to determine the size of a block of
rendered text without needing the expense of building a LazyFont.DrawableString
first.toWrap
- The text to be word-wrapped.fontSize
- The font size the text would be rendered at.maxWidth
- The max width of the text area. Text will be wrapped at the last word that fit within this
width.toWrap
, wrapped to fit within the area specified by maxWidth
.public float getBaseHeight()
public float getTextureHeight()
public float getTextureWidth()
public int getTextureId()
SpriteAPI.getTextureId()
.public float calcWidth(String rawLine, float fontSize)
String
at a specific font size, without taking into account word wrapping.@NotNull public LazyFont.LazyChar getChar(char character)
character
- The character to look up.LazyFont.LazyChar
containing metadata for how the font handles character
, or metadata for '?' if
that character is not supported by the font.@NotNull public @NotNull Vector2f drawText(String text, float x, float y, float fontSize, float maxWidth, float maxHeight)
createText(String, Color, float, float, float)
instead.text
- The text to be drawn.x
- The x coordinate of the starting point for rendering (top left corner of text).y
- The y coordinate of the starting point for rendering (top left corner of text).fontSize
- The size of the text to be drawn. For best results, this should be evenly divisible by getBaseHeight()
. Other values may cause slight blurriness or jaggedness.maxWidth
- The maximum width of the drawn text before further text will be wrapped to a new line.maxHeight
- The maximum height of the drawn text. All further text past this point will be discarded.Vector2f
containing the width and height of the drawn text area.for when you need to draw the same
block of text multiple times (99% of use cases).
@NotNull public @NotNull LazyFont.DrawableString createText(String text, Color color, float size, float maxWidth, float maxHeight)
LazyFont.DrawableString
with the specified initial text, color, and font size, with text wrapping and
a max height - any appended text past that limit will be discarded.text
- The initial text to be drawn. You can modify it later using the returned LazyFont.DrawableString
.color
- The color of the drawn text.size
- The font size of the drawn text. For best results, this should be evenly divisible by getBaseHeight()
. Other values may cause slight blurriness or jaggedness.maxWidth
- The maximum width of the drawn text before further text will be wrapped to a new line.maxHeight
- The maximum height of the drawn text. All further text past this point will be discarded.LazyFont.DrawableString
with the specified text, color, and font size, with text wrapping
at maxWidth
, and a max height of maxHeight
.@NotNull public @NotNull LazyFont.DrawableString createText(String text, Color color, float size, float maxWidth)
LazyFont.DrawableString
with the specified initial text, color, and font size, with text wrapping.text
- The initial text to be drawn. You can modify it later using the returned LazyFont.DrawableString
.color
- The color of the drawn text.size
- The font size of the drawn text. For best results, this should be evenly divisible by getBaseHeight()
. Other values may cause slight blurriness or jaggedness.maxWidth
- The maximum width of the drawn text before further text will be wrapped to a new line.LazyFont.DrawableString
with the specified text, color, and font size, with text wrapping
at maxWidth
.createText(String, Color, float, float, float)
@NotNull public @NotNull LazyFont.DrawableString createText(String text, Color color, float size)
LazyFont.DrawableString
with the specified initial text, color, and font size, with no text wrapping.text
- The initial text to be drawn. You can modify it later using the returned LazyFont.DrawableString
.color
- The color of the drawn text.size
- The font size of the drawn text. For best results, this should be evenly divisible by getBaseHeight()
. Other values may cause slight blurriness or jaggedness.LazyFont.DrawableString
with the specified text, color, and font size, with no text wrapping.createText(String, Color, float, float, float)
@NotNull public @NotNull LazyFont.DrawableString createText(String text, Color color)
LazyFont.DrawableString
with the specified initial text and color. Defaults to the base
font size, and no text wrapping.text
- The initial text to be drawn. You can modify it later using the returned LazyFont.DrawableString
.color
- The color of the drawn text.LazyFont.DrawableString
with the specified text and color, the base font size, and no text wrapping.createText(String, Color, float, float, float)
@NotNull public @NotNull LazyFont.DrawableString createText(String text)
LazyFont.DrawableString
with the specified initial text. Defaults to white text, the base
font size, and no text wrapping.text
- The initial text to be drawn. You can modify it later using the returned LazyFont.DrawableString
.LazyFont.DrawableString
with the specified text, white color, the base font size, and no text wrapping.createText(String, Color, float, float, float)
@NotNull public @NotNull LazyFont.DrawableString createText()
LazyFont.DrawableString
with no text, ready for appending. Defaults to white text, the base
font size, and no text wrapping.LazyFont.DrawableString
with white text, the base font size, and no text wrapping.createText(String, Color, float, float, float)