ADTF_DISPLAY_TOOLBOX  3.8.0 (ADTF 3.14.3)
Source Code for Demo 2D Coordinate Space Drawer
Location
./src/examples/src/coordspacedrawer
This example shows:
  • how to create a custom drawer
  • how to add new properties to the derived drawer implementation.
  • how to use the canvas inside the Draw method.
Header
#pragma once
class cCoordSpaceDrawer :
public adtf::ucom::object<adtf::disptb::drawerlib::cDrawer>
{
public:
ADTF_CLASS_ID_NAME(
cCoordSpaceDrawer,
"demo_coord_space.2d_drawer.disptb.cid",
"Demo 2D Coordinate Space Drawer"
);
cCoordSpaceDrawer();
~cCoordSpaceDrawer() = default;
tResult Init(tInitStage eStage) override;
tResult Shutdown(tInitStage eStage) override;
tResult Draw(const adtf::ucom::iobject_ptr<adtf::disptb::graphicslib::ICanvas>& o_pCanvas) override;
private:
adtf::base::property_variable<tFloat32> m_fRange = 100.0f;
adtf::base::property_variable<tFloat32> m_fStep = 10.0f;
adtf::base::property_variable<tBool> m_bGrid = tTrue;
};
Copyright © Audi Electronics Venture GmbH.
Implementation
#include "coord_space_drawer.h"
ADTF_PLUGIN_VERSION("Demo 2D Coordinate Space Drawer Plugin",
disptb,
DISPTB_VERSION_MAJOR,
DISPTB_VERSION_MINOR,
DISPTB_VERSION_PATCH,
cCoordSpaceDrawer)
cCoordSpaceDrawer::cCoordSpaceDrawer()
{
m_fRange.SetDescription("Range for drawing.");
RegisterPropertyVariable("range", m_fRange);
m_fStep.SetDescription("Step for drawing.");
RegisterPropertyVariable("step", m_fStep);
m_bGrid.SetDescription("If enabled, the grid is visible");
RegisterPropertyVariable("grid", m_bGrid);
SetDescription("Use this Drawer to paint a grid based coordinate system onto the canvas.");
SetHelpLink("$(ADTF_DISPLAY_TOOLBOX_DIR)/doc/displaytoolbox_html/page_2d_coordinate_space_drawer_readme.html");
}
tResult cCoordSpaceDrawer::Init(tInitStage eStage)
{
RETURN_IF_FAILED(cDrawer::Init(eStage));
RETURN_NOERROR;
}
tResult cCoordSpaceDrawer::Shutdown(tInitStage eStage)
{
RETURN_IF_FAILED(cDrawer::Shutdown(eStage));
RETURN_NOERROR;
}
tResult cCoordSpaceDrawer::Draw(const adtf::ucom::iobject_ptr<adtf::disptb::graphicslib::ICanvas>& o_pCanvas)
{
// If the Visible property is set to false, the method isn't called,
// so there is no need to check this here.
tInt nSteps = (tInt)(m_fRange / m_fStep);
o_pCanvas->Color(192, 192, 192);
// If the grid is visible draw it
if (m_bGrid)
{
tFloat32 iLen = m_fRange;
for (tInt i = 0; i <= nSteps; i++)
{
tFloat32 iStep = i * m_fStep;
o_pCanvas->DrawLine(iStep, -iLen, iStep, iLen);
o_pCanvas->DrawLine(-iLen, iStep, iLen, iStep);
o_pCanvas->DrawLine(-iStep, -iLen, -iStep, iLen);
o_pCanvas->DrawLine(-iLen, -iStep, iLen, -iStep);
}
}
// draw the axes
o_pCanvas->Color(0, 0, 0);
o_pCanvas->DrawLine(tInt(-m_fRange), 0, tInt(m_fRange), 0);
o_pCanvas->DrawLine(0, tInt(-m_fRange), 0, tInt(m_fRange));
// draw the intervals which points always in positive direction
for (tInt i = 0; i <= nSteps; i++)
{
tFloat32 iLen = (i % 5 == 0) ? (tFloat32) 4.0 : (tFloat32) 2.0;
iLen = (i % 10 == 0) ? 6 : iLen;
tFloat32 iStep = i * m_fStep;
o_pCanvas->DrawLine(iStep, 0.0, iStep, iLen);
o_pCanvas->DrawLine(0.0, iStep, iLen, iStep);
o_pCanvas->DrawLine(-iStep, 0.0, -iStep, iLen);
o_pCanvas->DrawLine(0.0, -iStep, iLen, -iStep);
}
RETURN_NOERROR;
}