Class Text
java.lang.Object
org.openpatch.scratch.Text
A text on the stage: plain, in a box, or in a speech or thought bubble. Every sprite has one
for
say and think; texts of your own are added to a stage with
stage.add(text).
Text score = new Text("Score: 0", -200, 160, 0);
score.setLayer(Layer.UI);
stage.add(score);
Texts are laid out from the font file at their nominal size, in stage units, so where lines break and how large boxes and bubbles are never depends on the window. They are drawn at the window's resolution, in front of the foreground layer (or in the UI layer), and stay sharp.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a font this text can switch to; the first one added is used.getAlign()intstatic Stringstatic doublegetFont()getLayer()getStage()getStyle()getText()doubledoublegetWidth()doublegetX()doublegetY()voidgoLayersBackwards(int number) voidgoLayersForwards(int number) voidDraws this text behind the other texts.voidDraws this text in front of the other texts.voidhide()Hides the text.booleanvoidnextFont()Switches to the next font added, after the last to the first.voidremove()Removes the text from its stage.voidvoidsetBackgroundColor(double red, double green, double blue) voidsetBackgroundColor(Color color) voidvoidMoves the text to another layer:Layer.UIkeeps it in place when the camera moves; any other layer puts it into the world, in front of the foreground.voidsetPosition(double x, double y) voidsetPosition(Vector2 position) voidsetStrokeColor(double red, double green, double blue) voidsetStrokeColor(Color color) voidvoidsetTextColor(double red, double green, double blue) voidsetTextColor(Color color) voidsetTextSize(double size) voidsetWidth(double width) voidsetX(double x) voidsetY(double y) voidshow()Shows the text.voidShows the text with new words until it is changed or hidden.voidShows the text for a number of seconds of game time, then hides it.voidswitchFont(String name) static voidThe font every text uses unless it sets its own, from now on.static voidA pixel font as default font, with the size at which one of its pixels is one stage pixel: for a pixel font whose outlines do not show its grid (most do: thenuseFont(String)is enough).voidCalled when the text was added to a stage.voidCalled when the text was removed from its stage.
-
Constructor Details
-
Text
public Text()An empty plain text at (0, 0). -
Text
A plain text.- Parameters:
text- what it saysx- x of its positiony- y of its position (the first line's baseline)width- where lines wrap; 0 for no wrapping
-
Text
A text in a style.- Parameters:
text- what it saysx- x of its positiony- y of its position: the first baseline for plain text, the bottom edge of a boxwidth- where lines wrap; 0 for no wrappingstyle- plain, box, speech or thought bubble
-
-
Method Details
-
useFont
The font every text uses unless it sets its own, from now on. Without it, texts use Ubuntu Mono, or Jersey 10 in pixel-art mode.- Parameters:
path- a TTF or OTF file
-
useFont
A pixel font as default font, with the size at which one of its pixels is one stage pixel: for a pixel font whose outlines do not show its grid (most do: thenuseFont(String)is enough). In pixel-art mode sizes are rounded to whole pixels of it.- Parameters:
path- a TTF or OTF filepixelSize- the size at which one font pixel is one stage pixel
-
getDefaultFont
- Returns:
- the default font file
-
getDefaultFontSize
public static double getDefaultFontSize()- Returns:
- the default font size
-
whenAddedToStage
public void whenAddedToStage()Called when the text was added to a stage. -
whenRemovedFromStage
public void whenRemovedFromStage()Called when the text was removed from its stage. -
getStage
- Returns:
- the stage the text is on (a sprite's text: the sprite's stage), or null
-
remove
public void remove()Removes the text from its stage. -
showText
Shows the text with new words until it is changed or hidden.- Parameters:
text- what it says; an empty text hides a bubble
-
showText
Shows the text for a number of seconds of game time, then hides it.- Parameters:
text- what it saysseconds- how long
-
getText
- Returns:
- what the text says
-
show
public void show()Shows the text. -
hide
public void hide()Hides the text. -
isVisible
public boolean isVisible()- Returns:
- whether the text is shown
-
setPosition
public void setPosition(double x, double y) - Parameters:
x- x of the positiony- y of the position
-
setPosition
- Parameters:
position- the position
-
getPosition
- Returns:
- the position
-
setX
public void setX(double x) - Parameters:
x- x of the position
-
getX
public double getX()- Returns:
- x of the position
-
setY
public void setY(double y) - Parameters:
y- y of the position
-
getY
public double getY()- Returns:
- y of the position
-
setWidth
public void setWidth(double width) - Parameters:
width- where lines wrap, in stage units; 0 for no wrapping
-
getWidth
public double getWidth()- Returns:
- where lines wrap; 0 for no wrapping
-
setStyle
- Parameters:
style- plain, box, speech or thought bubble
-
getStyle
- Returns:
- the style
-
setAlign
- Parameters:
align- how lines sit relative to the position
-
getAlign
- Returns:
- the alignment
-
setTextColor
- Parameters:
color- the colour of the letters
-
setTextColor
public void setTextColor(double red, double green, double blue) - Parameters:
red- red from 0 to 255green- green from 0 to 255blue- blue from 0 to 255
-
setBackgroundColor
- Parameters:
color- the fill of a box or bubble
-
setBackgroundColor
public void setBackgroundColor(double red, double green, double blue) - Parameters:
red- red from 0 to 255green- green from 0 to 255blue- blue from 0 to 255
-
setStrokeColor
- Parameters:
color- the outline of a box or bubble
-
setStrokeColor
public void setStrokeColor(double red, double green, double blue) - Parameters:
red- red from 0 to 255green- green from 0 to 255blue- blue from 0 to 255
-
addFont
-
switchFont
- Parameters:
name- the name of a font added withaddFont(String, String)
-
nextFont
public void nextFont()Switches to the next font added, after the last to the first. -
getCurrentFontName
- Returns:
- the name of the font in use, or null for the default font
-
getCurrentFontIndex
public int getCurrentFontIndex()- Returns:
- the number of the font in use, counting from 0; -1 for the default font
-
goToFrontLayer
public void goToFrontLayer()Draws this text in front of the other texts.- Scratch Block:
- go to [front v] layer
-
goToBackLayer
public void goToBackLayer()Draws this text behind the other texts.- Scratch Block:
- go to [back v] layer
-
goLayersForwards
public void goLayersForwards(int number) - Parameters:
number- how many texts to move in front of- Scratch Block:
- go [forward v] (number) layers
-
goLayersBackwards
public void goLayersBackwards(int number) - Parameters:
number- how many texts to move behind- Scratch Block:
- go [backward v] (number) layers
-
setFont
- Parameters:
path- the font file for this text, or null for the default font
-
getFont
- Returns:
- the font file, or null for the default font
-
setTextSize
public void setTextSize(double size) - Parameters:
size- the font size in stage units (in pixel-art mode rounded to whole font pixels when drawn)
-
getTextSize
public double getTextSize()- Returns:
- the font size, or the default
-
setLayer
-
getLayer
- Returns:
- the layer
-