NAP
Public Member Functions | Protected Member Functions | List of all members
Renderable2DTextComponentInstance Class Reference

#include <renderable2dtextcomponent.h>

Public Member Functions

 Renderable2DTextComponentInstance (EntityInstance &entity, Component &resource)
 
virtual bool init (utility::ErrorState &errorState) override
 
void draw (IRenderTarget &target)
 
utility::ETextOrientation getOrientation () const
 
void setOrientation (utility::ETextOrientation orientation)
 
const glm::ivec2 & getLocation () const
 
void setLocation (const glm::ivec2 &coordinates)
 
glm::ivec2 getTextPosition (float scale)
 
virtual RenderableGlyphgetRenderableGlyph (uint index, float scale, utility::ErrorState &error) const override
 
virtual bool isSupported (nap::CameraComponentInstance &camera) const override
 
- Public Member Functions inherited from RenderableTextComponentInstance
 RenderableTextComponentInstance (EntityInstance &entity, Component &resource)
 
const FontInstancegetFont () const
 
bool setText (const std::string &text, utility::ErrorState &error)
 
void setColor (const glm::vec3 &color)
 
bool setText (int lineIndex, const std::string &text, utility::ErrorState &error)
 
bool addLine (const std::string &text, utility::ErrorState &error)
 
void setLineIndex (int index)
 
const std::string & getText ()
 
const std::string & getText (int index)
 
void resize (int lines)
 
int getCount () const
 
void clear ()
 
const math::RectgetBoundingBox () const
 
const math::RectgetBoundingBox (int index)
 
const MaterialInstancegetMaterialInstance () const
 
MaterialInstancegetMaterialInstance ()
 
float getDPIScale () const
 
- Public Member Functions inherited from RenderableComponentInstance
 RenderableComponentInstance (EntityInstance &entity, Component &resource)
 
void draw (IRenderTarget &renderTarget, VkCommandBuffer commandBuffer, const glm::mat4 &viewMatrix, const glm::mat4 &projectionMatrix)
 
void setVisible (bool visible)
 
bool isVisible () const
 
const std::vector< ResourcePtr< RenderTag > > & getTags () const
 
RenderMask getMask () const
 
bool includesMask (RenderMask otherMask)
 
int getRank () const
 
const RenderLayergetLayer () const
 
- Public Member Functions inherited from ComponentInstance
 ComponentInstance (EntityInstance &entity, Component &resource)
 
virtual void update (double deltaTime)
 
nap::EntityInstancegetEntityInstance () const
 
nap::ComponentgetComponent () const
 
template<typename T >
T * getComponent () const
 
virtual bool init (utility::ErrorState &errorState)
 
- Public Member Functions inherited from Object
 Object ()
 
virtual ~Object ()
 
virtual void onDestroy ()
 
 Object (Object &)=delete
 
Objectoperator= (const Object &)=delete
 
 Object (Object &&)=delete
 
Objectoperator= (Object &&)=delete
 

Protected Member Functions

virtual void onDraw (IRenderTarget &renderTarget, VkCommandBuffer commandBuffer, const glm::mat4 &viewMatrix, const glm::mat4 &projectionMatrix) override
 
- Protected Member Functions inherited from RenderableTextComponentInstance
void draw (IRenderTarget &renderTarget, VkCommandBuffer commandBuffer, const glm::mat4 &viewMatrix, const glm::mat4 &projectionMatrix, const glm::mat4 &modelMatrix)
 
bool hasTransform () const
 
const nap::TransformComponentInstancegetTransform () const
 
virtual bool setup (float scale, utility::ErrorState &errorState)
 

Additional Inherited Members

- Static Public Member Functions inherited from Object
static bool isIDProperty (rtti::Instance &object, const rtti::Property &property)
 
- Public Attributes inherited from Object
std::string mID
 Property: 'mID' unique name of the object. Used as an identifier by the system. More...
 
- Protected Attributes inherited from RenderableTextComponentInstance
FontInstancemFont = nullptr
 Pointer to the font, set on initialization. More...
 
RenderServicemRenderService = nullptr
 Pointer to the Renderer. More...
 
- Protected Attributes inherited from RenderableComponentInstance
RenderServicemRenderService = nullptr
 

Description

Allows you to render text at a specific location in screen space. Use this component when you want to render text at a specific location on screen or in a render-target.

Call draw() in the render part of your application to render text to a specific location on screen or a render-target. It is also possible to render the text using RenderService::renderObjects(), this is similar to how meshes are rendered. In that case the x/y location of the camera influences the final location of the text.

When the parent entity has a transform component attached to it and 'IgnoreTransform' is set to false, the x/y Translate values are used as offset in pixel space. 2D text cannot be scaled or rotated, this ensures that every Glyph is rendered in it's native resolution. When rendering this component through the render interface of the render service it is advised to use an orthographic camera.

It is possible to cache multiple lines at once, where each line can be selected and drawn individually inside a render loop. This is useful when you want the same component to render multiple lines of text, removing the need to declare a component for each individual line. You cannot update or add a line of text when rendering a frame: inside the render loop. Only update or add new lines of text on update. You can however change the position and line of text to draw inside the render loop.

For example, on update:

// Set text for next draw operation
text_component.resize(blobs.size());
for (int i = 0; i < blobs.size(); i++)
{
text_component.setText(i, utility::stringFormat("Blob %d", i + 1), error);
}

And on render:

// Set text for next draw operation
for (int i = 0; i < blobs.size(); i++)
{
// Get blob location in screen space
glm::vec3 blob_pos = locs[i];
glm::vec2 text_pos = persp_camera.worldToScreen(blob_pos, mRenderWindow->getRectPixels());
// Set location, select line and draw
text_comp.setLocation(text_pos);
text_comp.setLineIndex(i);
text_comp.draw(*mRenderWindow)
}
Inheritance diagram for Renderable2DTextComponentInstance:
[legend]
Collaboration diagram for Renderable2DTextComponentInstance:
[legend]

Constructor & Destructor Documentation

◆ Renderable2DTextComponentInstance()

Member Function Documentation

◆ draw()

void draw ( IRenderTarget target)

Draws the current text into the active target using the stored coordinates in screen space. This is a convenience function that calls RenderableTextComponentInstance::draw using a custom view and projection matrix. These matrices are created based on on the size of your render target. Only Call this function in the render part of your application. Set text in the update part of your application. The x and y coordinates of the TransformComponent are used as an offset (in pixels) if the parent entity has a transform component. When using this function the orientation of the text is taken into account.

Parameters
targetrender target that defines the screen space bounds

◆ getLocation()

const glm::ivec2& getLocation ( ) const

Returns the current text coordinates in screen space. This is not the final position of the text! To get the actual start position of the text use getTextPosition() The final location is based on the orientation and transformation of this component.

Returns
location of the text in screen space pixel coordinates

◆ getOrientation()

utility::ETextOrientation getOrientation ( ) const
Returns
current text draw orientation

◆ getRenderableGlyph()

virtual RenderableGlyph* getRenderableGlyph ( uint  index,
float  scale,
utility::ErrorState error 
) const
overridevirtual

Creates a Renderable2DGlyph for the given index and scale in the font.

Parameters
indexthe index to create the render-able glyph for.
scalethe render-able glyph scaling factor.
errorcontains the error if the glyph representation could not be created.
Returns
the Renderable2DGlyph glyph for the given character index.

Implements RenderableTextComponentInstance.

◆ getTextPosition()

glm::ivec2 getTextPosition ( float  scale)

Returns the text position based on the stored location, orientation mode and text transformation. The return value is the actual position of the text in the world and most likely screen. Actual screen space position still depends on camera location, orientation and shader effects.

Returns
start position of the text based based on stored location, orientation mode and text transformation.

◆ init()

virtual bool init ( utility::ErrorState errorState)
overridevirtual

Initialize Renderable2DTextComponentInstance based on the Renderable2DTextComponent resource

Parameters
errorStateshould hold the error message when initialization fails
Returns
if the Renderable2DTextComponentInstance is initialized successfully

Reimplemented from RenderableTextComponentInstance.

◆ isSupported()

virtual bool isSupported ( nap::CameraComponentInstance camera) const
overridevirtual

This component can only be rendered with an orthographic camera!

Returns
if the camera is an orthographic camera or not.

Reimplemented from RenderableComponentInstance.

◆ onDraw()

virtual void onDraw ( IRenderTarget renderTarget,
VkCommandBuffer  commandBuffer,
const glm::mat4 &  viewMatrix,
const glm::mat4 &  projectionMatrix 
)
overrideprotectedvirtual

Draws the text to the currently active render target using the render service. This function is called by the render service when text is rendered with a user defined camera. In that case the viewMatrix is the world space camera location and the the projection matrix is defined by the camera type. You can only use an orthographic camera when rendering 2D text. The x/y location of the parent entity is taken into account if there is a transform component. Note that the orientation mode is also taken into account when rendering this way.

Parameters
renderTargetbound target to render to
commandBuffercurrent command buffer
viewMatrixthe camera world space location
projectionMatrixthe camera projection matrix, orthographic or perspective

Implements RenderableComponentInstance.

◆ setLocation()

void setLocation ( const glm::ivec2 &  coordinates)

Sets the new text coordinates in screen space. This is not the final position of the text! To get the actual start position of the text use getTextPosition() The final location is based on the orientation and transformation of this component.

Parameters
coordinatesthe new text location in pixel space coordinates

◆ setOrientation()

void setOrientation ( utility::ETextOrientation  orientation)

Sets the text draw orientation

Parameters
orientationnew text orientation