ADTF_DISPLAY_TOOLBOX  3.8.0 (ADTF 3.14.3)
Source Code for Demo 2D Text Drawer
Location
./src/examples/src/textdrawer
This example shows:
  • how to create a custom drawer.
  • how to use the IFont interface.
  • how to use the cColor class.
  • how to use the canvas inside the Draw method.
Header
#pragma once
class cTextDrawer :
public adtf::ucom::object<adtf::disptb::drawerlib::cDrawer>
{
public:
ADTF_CLASS_ID_NAME(
cTextDrawer,
"demo_text.2d_drawer.disptb.cid",
"Demo 2D Text Drawer"
);
cTextDrawer();
~cTextDrawer() = default;
tResult Init(tInitStage eStage) override;
tResult Shutdown(tInitStage eStage) override;
tResult Draw(const adtf::ucom::iobject_ptr<adtf::disptb::graphicslib::ICanvas>& pCanvas) override;
tResult ContextCreated(const adtf::ucom::iobject_ptr<adtf::disptb::graphicslib::ICanvas>& pCanvas) override;
tResult ContextDestroyed() override;
private:
adtf::base::property_variable<adtf_util::cString> m_strText;
adtf::base::property_variable<adtf_util::cString> m_strColor;
adtf::base::property_variable<tBool> m_bProjection;
adtf::base::property_variable<tBool> m_bFlip;
adtf::base::property_variable<tInt> m_nFontSize;
adtf::base::property_variable<tFloat64> m_nRotation;
};
Default Font handling interface for basic font support.
Copyright © Audi Electronics Venture GmbH.
Implementation
#include "text_drawer.h"
#ifdef WIN32
#include <windows.h>
#undef CreateFont
#endif
ADTF_PLUGIN_VERSION("Demo 2D Text Drawer Plugin",
disptb,
DISPTB_VERSION_MAJOR,
DISPTB_VERSION_MINOR,
DISPTB_VERSION_PATCH,
cTextDrawer)
cTextDrawer::cTextDrawer() :
m_strText("Hello World!"),
m_strColor("#000000ff"),
m_bProjection(tTrue),
m_nFontSize(11),
m_nRotation(0)
{
m_strText.SetDescription("Text to display. Linebreaks with LF character are used as such.");
RegisterPropertyVariable("Text", m_strText);
m_strColor.SetDescription("Text color key in format #RRGGBBAA.");
RegisterPropertyVariable("Color", m_strColor);
m_bProjection.SetDescription("If enabled, scale and rotate the text according to the projection. Otherwise render projection-indepent.");
RegisterPropertyVariable("Projection", m_bProjection);
m_bFlip.SetDescription("If enabled, flip the text upside down");
RegisterPropertyVariable("Flip", m_bFlip);
m_nFontSize.SetDescription("Font size in pt.");
m_nFontSize.SetValidRange(1, 80);
RegisterPropertyVariable("Fontsize", m_nFontSize);
m_nRotation.SetDescription("Text rotation in degree.");
m_nRotation.SetValidRange(0, 360);
RegisterPropertyVariable("Rotation", m_nRotation);
SetDescription("Use this Drawer to paint a given string as pixels onto canvas.");
SetHelpLink("$(ADTF_DISPLAY_TOOLBOX_DIR)/doc/displaytoolbox_html/page_2d_text_drawer_readme.html");
}
tResult cTextDrawer::Init(tInitStage eStage)
{
RETURN_IF_FAILED(cDrawer::Init(eStage));
adtf_util::cString color = m_strColor;
RETURN_NOERROR;
}
tResult cTextDrawer::Shutdown(tInitStage eStage)
{
RETURN_IF_FAILED(cDrawer::Shutdown(eStage));
RETURN_NOERROR;
}
tResult cTextDrawer::Draw(const adtf::ucom::iobject_ptr<adtf::disptb::graphicslib::ICanvas>& pCanvas)
{
adtf_util::cString color = m_strColor;
adtf_util::cString text = m_strText;
pCanvas->Font(m_pFont);
pCanvas->SetFontAngle(*m_nRotation);
pCanvas->SetFlag(adtf::disptb::graphicslib::CANVAS_FLAG_TEXT_FLIP_HORIZONTALLY, *m_bFlip);
pCanvas->SetFlag(adtf::disptb::graphicslib::CANVAS_FLAG_TEXT_BASELINE_ON_TOP, tFalse);
pCanvas->SetFlag(adtf::disptb::graphicslib::CANVAS_FLAG_TEXT_SCALE_WITH_COORDINATE_SYSTEM, *m_bProjection);
pCanvas->SetFlag(adtf::disptb::graphicslib::CANVAS_FLAG_TEXT_ROTATE_WITH_COORDINATE_SYSTEM, *m_bProjection);
pCanvas->Color(&m_oColor);
pCanvas->OutputText(0, 0, "%s", text.GetPtr());
RETURN_NOERROR;
}
tResult cTextDrawer::ContextCreated(const adtf::ucom::iobject_ptr<adtf::disptb::graphicslib::ICanvas>& pCanvas)
{
m_pFont = pCanvas->CreateFont("arial", *m_nFontSize, adtf::disptb::graphicslib::IFont::STYLE_Default);
RETURN_NOERROR;
}
tResult cTextDrawer::ContextDestroyed()
{
if(m_pFont)
{
delete m_pFont;
m_pFont = nullptr;
}
RETURN_NOERROR;
}
static tResult ColorFromString(const tChar *strColor, cColor &oColor)
Converts a hexadecimal color value into a cColor object The input format is like the HTML color defin...
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.