Class TextCanvas
TextCanvas extends TexturedRectangle and renders a 2D grid of
characters (rows and columns) onto a texture-mapped rectangle. Each character cell
supports independent foreground and background colors through CanvasCharacter.
Characters are rendered using a monospace font at a fixed size (16 x 32 texture pixels per character). The canvas automatically switches between two render modes based on the viewer's distance and viewing angle:
RenderMode.TEXTURE-- renders all characters to a shared texture bitmap. This is efficient for distant or obliquely viewed text.RenderMode.CHARACTERS-- renders each character as an individual textured polygon with higher quality anti-aliased tiles. Used when the viewer is close and looking at the text nearly head-on.
Usage example
Transform location = new Transform(new Point3D(0, 0, 500));
TextCanvas canvas = new TextCanvas(location, "Hello, World!",
Color.WHITE, Color.BLACK);
shapeCollection.addShape(canvas);
// Or create a blank canvas and write to it
TextCanvas blank = new TextCanvas(location, new TextPointer(10, 40),
Color.GREEN, Color.BLACK);
blank.locate(0, 0);
blank.print("Line 1");
blank.locate(1, 0);
blank.print("Line 2");
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Fontstatic final intFont character height in world coordinates.static final intFont character height in texture pixels.static final intFont character width in world coordinates.static final intFont character width in texture pixels.Fields inherited from class eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.TexturedRectangle
bottomLeft, bottomRight, textureBottomLeft, textureBottomRight, textureTopLeft, textureTopRight, topLeft, topRightFields inherited from class eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractShape
mouseInteractionController -
Constructor Summary
ConstructorsConstructorDescriptionTextCanvas(Transform location, TextPointer dimensions, Color foregroundColor, Color backgroundColor) Creates a blank text canvas with the specified dimensions.TextCanvas(Transform location, String text, Color foregroundColor, Color backgroundColor) Creates a text canvas initialized with the given text string. -
Method Summary
Modifier and TypeMethodDescriptionvoidbeforeTransformHook(TransformStack transformPipe, RenderingContext context) This method should be overridden by anyone wanting to customize shape before it is rendered.voidclear()Clears the entire canvas, resetting all characters to spaces with the default colors.getCharLocation(int row, int column) Computes the 3D world coordinate for the center of the character cell at the given row and column.getSize()Returns the dimensions of this text canvas.static TextPointergetTextDimensions(String text) Computes the row and column dimensions needed to fit the given text.voidlocate(int row, int column) Moves the internal cursor to the specified row and column.voidPrints a string starting at the current cursor location, advancing the cursor after each character.voidputChar(char character) Writes a character at the current cursor location and advances the cursor.voidputChar(int row, int column, char character) Writes a character at the specified row and column using the current foreground and background colors.voidputChar(TextPointer location, char character) Writes a character at the position specified by aTextPointer.voidsetBackgroundColor(Color backgroundColor) Sets the default background color for subsequent character writes.voidsetForegroundColor(Color foregroundColor) Sets the default foreground (text) color for subsequent character writes.voidReplaces the entire canvas content with the given multi-line text string.voidsetTextColor(Color color) Sets the foreground color of all existing characters on the canvas.Methods inherited from class eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.TexturedRectangle
getTexture, initializeMethods inherited from class eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.base.AbstractCompositeShape
addShape, addShape, getGroup, getLocation, getOriginalSubShapes, getViewSpaceTracker, hideGroup, removeGroup, setBackfaceCulling, setColor, setGroupForUngrouped, setLightingManager, setMouseInteractionController, setShadingEnabled, setTransform, showGroup, transform
-
Field Details
-
FONT_CHAR_WIDTH
public static final int FONT_CHAR_WIDTHFont character width in world coordinates.- See Also:
-
FONT_CHAR_HEIGHT
public static final int FONT_CHAR_HEIGHTFont character height in world coordinates.- See Also:
-
FONT_CHAR_WIDTH_TEXTURE_PIXELS
public static final int FONT_CHAR_WIDTH_TEXTURE_PIXELSFont character width in texture pixels.- See Also:
-
FONT_CHAR_HEIGHT_TEXTURE_PIXELS
public static final int FONT_CHAR_HEIGHT_TEXTURE_PIXELSFont character height in texture pixels.- See Also:
-
FONT
-
-
Constructor Details
-
TextCanvas
Creates a text canvas initialized with the given text string.The canvas dimensions are automatically computed from the text content (number of lines determines rows, longest line determines columns).
- Parameters:
location- the 3D transform positioning this canvas in the scenetext- the initial text content (may contain newlines for multiple rows)foregroundColor- the default text colorbackgroundColor- the default background color
-
TextCanvas
public TextCanvas(Transform location, TextPointer dimensions, Color foregroundColor, Color backgroundColor) Creates a blank text canvas with the specified dimensions.The canvas is initialized with spaces in every cell, filled with the specified background color. Characters can be written using
putChar(char),print(String), orsetText(String).- Parameters:
location- the 3D transform positioning this canvas in the scenedimensions- the grid size as aTextPointerwhererowis the number of rows andcolumnis the number of columnsforegroundColor- the default text colorbackgroundColor- the default background color
-
-
Method Details
-
getTextDimensions
Computes the row and column dimensions needed to fit the given text.- Parameters:
text- the text content (may contain newlines)- Returns:
- a
TextPointerwhererowis the number of lines andcolumnis the length of the longest line
-
beforeTransformHook
Description copied from class:AbstractCompositeShapeThis method should be overridden by anyone wanting to customize shape before it is rendered.- Overrides:
beforeTransformHookin classAbstractCompositeShape
-
clear
public void clear()Clears the entire canvas, resetting all characters to spaces with the default colors.Both the character grid and the backing texture bitmap are reset.
-
getCharLocation
Computes the 3D world coordinate for the center of the character cell at the given row and column.- Parameters:
row- the row index (0-based, from the top)column- the column index (0-based, from the left)- Returns:
- the 3D coordinate of the character cell center, relative to the canvas origin
-
getSize
Returns the dimensions of this text canvas.- Returns:
- a
TextPointerwhererowis the number of rows andcolumnis the number of columns
-
locate
public void locate(int row, int column) Moves the internal cursor to the specified row and column.Subsequent calls to
putChar(char)andprint(String)will begin writing at this position.- Parameters:
row- the target row (0-based)column- the target column (0-based)
-
print
Prints a string starting at the current cursor location, advancing the cursor after each character.When the cursor reaches the end of a row, it wraps to the beginning of the next row.
- Parameters:
text- the text to print- See Also:
-
putChar
public void putChar(char character) Writes a character at the current cursor location and advances the cursor.The cursor moves one column to the right. If it exceeds the row width, it wraps to column 0 of the next row.
- Parameters:
character- the character to write
-
putChar
public void putChar(int row, int column, char character) Writes a character at the specified row and column using the current foreground and background colors.If the row or column is out of bounds, the call is silently ignored.
- Parameters:
row- the row index (0-based)column- the column index (0-based)character- the character to write
-
putChar
Writes a character at the position specified by aTextPointer.- Parameters:
location- the row and column positioncharacter- the character to write
-
setBackgroundColor
Sets the default background color for subsequent character writes.- Parameters:
backgroundColor- the new background color
-
setForegroundColor
Sets the default foreground (text) color for subsequent character writes.- Parameters:
foregroundColor- the new foreground color
-
setText
Replaces the entire canvas content with the given multi-line text string.Each line of text (separated by newlines) is written to consecutive rows, starting from row 0. Characters beyond the canvas width are ignored.
- Parameters:
text- the text to display (may contain newline characters)
-
setTextColor
Sets the foreground color of all existing characters on the canvas.This updates the color of every
CanvasCharacterin the grid, but does not affect the backing texture. It is primarily useful inRenderMode.CHARACTERSmode.- Parameters:
color- the new foreground color for all characters
-