Skip to main content
Skip to main content

HUDTextDisplay

HUDTextDisplay

Description

HUD text display. Displays a formatted single-line text with optional animations.

Parent

HUDDisplayElement

Functions

draw

Description

Draw the text.

Definition

draw()

Code

function HUDTextDisplay:draw()
if self.text = = "" then
return
end

if not self:getVisible() then
return
end

setTextBold( self.textBold)
local posX, posY = self:getPosition()
setTextAlignment( self.textAlignment)
setTextVerticalAlignment(RenderText.VERTICAL_ALIGN_BASELINE)
setTextWrapWidth( 0.9 )

if self.hasShadow then
local offset = self.screenTextSize * HUDTextDisplay.SHADOW_OFFSET_FACTOR
local r, g, b, a = unpack( self.shadowColor)
setTextColor(r, g, b, a * self.overlay.a)
renderText(posX + offset, posY - offset, self.screenTextSize, self.text)
end

local r, g, b, a = unpack( self.textColor)
setTextColor(r, g, b, a * self.overlay.a)
renderText(posX, posY, self.screenTextSize, self.text)

setTextAlignment(RenderText.ALIGN_LEFT)
setTextWrapWidth( 0 )
setTextBold( false )
setTextColor( 1 , 1 , 1 , 1 )
end

new

Description

Create a new HUDTextDisplay.

Definition

new(float posX, float posY, float textSize, integer? textAlignment, table? textColor, boolean? textBold)

Arguments

floatposXScreen space X position of the text display
floatposYScreen space Y position of the text display
floattextSizeText size in reference resolution pixels
integer?textAlignmentText alignment as one of RenderText.[ALIGN_LEFTALIGN_CENTERALIGN_RIGHT]
table?textColorText display color as an array {r, g, b, a}, default: {1, 1, 1, 1}
boolean?textBoldIf true, will render the text in bold

Return Values

boolean?HUDTextDisplayinstance

Code

function HUDTextDisplay.new(posX, posY, textSize, textAlignment, textColor, textBold)
local backgroundOverlay = Overlay.new( nil , 0 , 0 , 0 , 0 )
backgroundOverlay:setColor( 1 , 1 , 1 , 1 )
local self = HUDTextDisplay:superClass().new(backgroundOverlay, nil , HUDTextDisplay _mt)

self.initialPosX = posX
self.initialPosY = posY
self.text = "" -- must be set in a separate call which will correctly set up boundaries and position
self.textSize = textSize or 0
self.screenTextSize = self:scalePixelToScreenHeight( self.textSize)
self.textAlignment = textAlignment or RenderText.ALIGN_LEFT
self.textColor = textColor or { 1 , 1 , 1 , 1 }
self.textBold = textBold or false

self.hasShadow = false
self.shadowColor = { 0 , 0 , 0 , 1 }

return self
end

setAlpha

Description

Set the global alpha value for this text display. The alpha value will be multiplied with any text color alpha channel value.

Definition

setAlpha(float alpha)

Arguments

floatalpha

Code

function HUDTextDisplay:setAlpha(alpha)
self:setColor( nil , nil , nil , alpha)
end

setAnimation

Description

Set an animation tween (sequence) for this text display. The animation can be played when calling HUDTextDisplay:setVisible() with the "animate" paramter set to true.

Definition

setAnimation(table animationTween)

Arguments

tableanimationTween

Code

function HUDTextDisplay:setAnimation(animationTween)
self:storeOriginalPosition()
self.animation = animationTween or TweenSequence.NO_SEQUENCE
end

setScale

Description

Set the text display UI scale.

Definition

setScale(float uiScale)

Arguments

floatuiScale

Code

function HUDTextDisplay:setScale(uiScale)
HUDTextDisplay:superClass().setScale( self , uiScale)

self.screenTextSize = self:scalePixelToScreenHeight( self.textSize)
end

setText

Description

Set the text to display.

Definition

setText(string text, float textSize, integer textAlignment, table textColor, boolean textBold)

Arguments

stringtextDisplay text
floattextSizeText size in reference resolution pixels
integertextAlignmentText alignment as one of RenderText.[ALIGN_LEFTALIGN_CENTERALIGN_RIGHT]
tabletextColorText display color as an array {r, g, b, a}
booleantextBoldIf true, will render the text in bold

Code

function HUDTextDisplay:setText(text, textSize, textAlignment, textColor, textBold)
-- assign values with initial values as defaults
self.text = text or self.text
self.textSize = textSize or self.textSize
self.screenTextSize = self:scalePixelToScreenHeight( self.textSize)
self.textAlignment = textAlignment or self.textAlignment
self.textColor = textColor or self.textColor
self.textBold = textBold or self.textBold

local width, height = getTextWidth( self.screenTextSize, self.text), getTextHeight( self.screenTextSize, self.text)
self:setDimension(width, height)

local posX, posY = self.initialPosX, self.initialPosY

self:setPosition(posX, posY)
end

setTextColorChannels

Description

Set the text color by channels. Use for dynamic changes and animation.

Definition

setTextColorChannels(float r, float g, float b, float a)

Arguments

floatr
floatg
floatb
floata

Code

function HUDTextDisplay:setTextColorChannels(r, g, b, a)
self.textColor[ 1 ] = r
self.textColor[ 2 ] = g
self.textColor[ 3 ] = b
self.textColor[ 4 ] = a
end

setTextShadow

Description

Set the text shadow state.

Definition

setTextShadow(boolean isShadowEnabled, table shadowColor)

Arguments

booleanisShadowEnabledIf true, will cause a shadow to be rendered under the text
tableshadowColorShadow text color as an array {r, g, b, a}

Code

function HUDTextDisplay:setTextShadow(isShadowEnabled, shadowColor)
self.hasShadow = isShadowEnabled or self.hasShadow
self.shadowColor = shadowColor or self.shadowColor
end

setVisible

Description

Set this element's visibility.

Definition

setVisible(boolean isVisible, boolean animate)

Arguments

booleanisVisibleVisibility state
booleananimateIf true, will play the currently set animation on becoming visible or and reset it when necessary.

Code

function HUDTextDisplay:setVisible(isVisible, animate)
-- shadow parent behavior which includes repositioning
HUDElement.setVisible( self , isVisible)

if animate then
if not isVisible or not self.animation:getFinished() then
self.animation:reset()
end

if isVisible then
self.animation:start()
end
end
end

update

Description

Update this element's state.

Definition

update(float dt)

Arguments

floatdt

Code

function HUDTextDisplay:update(dt)
if self:getVisible() then
HUDTextDisplay:superClass().update( self , dt)
end
end