233 lines
7.4 KiB
C++
Executable File
233 lines
7.4 KiB
C++
Executable File
#include "textcomponent.h"
|
|
|
|
|
|
#include <dtEntity/nodemasks.h>
|
|
#include <osg/BlendFunc>
|
|
#include <osg/Geode>
|
|
#include <osg/Geometry>
|
|
#include <osg/LineWidth>
|
|
#include <osg/Node>
|
|
#include <osg/PolygonMode>
|
|
#include <osgText/Text>
|
|
#include <sstream>
|
|
|
|
namespace VCtrl
|
|
{
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
const dtEntity::StringId TextComponent::TYPE(dtEntity::SID("Text"));
|
|
const dtEntity::StringId TextComponent::PathId(dtEntity::SID("Path"));
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
TextComponent::TextComponent()
|
|
: dtEntityOSG::TransformComponent(new osg::AutoTransform())
|
|
, mPath(
|
|
dtEntity::DynamicStringProperty::SetValueCB(this, &TextComponent::SetText),
|
|
dtEntity:: DynamicStringProperty::GetValueCB(this, &TextComponent::GetText)
|
|
)
|
|
{
|
|
Register(PathId, &mPath);
|
|
|
|
osg::AutoTransform* tr = static_cast<osg::AutoTransform*>(GetNode());
|
|
tr->setName("text");
|
|
tr->setAutoRotateMode(osg::AutoTransform::ROTATE_TO_SCREEN);
|
|
tr->setAutoScaleToScreen(true);
|
|
tr->setNodeMask(dtEntity::NodeMasks::PICKABLE | dtEntity::NodeMasks::VISIBLE);
|
|
tr->setNormal(osg::Vec3(0,1,0));
|
|
osg::StateSet* ss = tr->getOrCreateStateSet();
|
|
ss->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
|
|
ss->setRenderBinDetails(90, "DepthSortedBin");
|
|
ss->setMode(GL_BLEND,osg::StateAttribute::ON);
|
|
ss->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
|
|
ss->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
|
|
osg::ref_ptr<osg::LineWidth> lw = new osg::LineWidth();
|
|
lw->setWidth(2);
|
|
ss->setAttributeAndModes(lw, osg::StateAttribute::ON);
|
|
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
void TextComponent::Rebuild()
|
|
{
|
|
osg::AutoTransform* tr = static_cast<osg::AutoTransform*>(GetNode());
|
|
tr->dirtyBound();
|
|
tr->setInitialBound(osg::BoundingSphere(osg::Vec3(), 10));
|
|
tr->removeChildren(0, tr->getNumChildren());
|
|
|
|
if(!mVisible) return;
|
|
|
|
osg::ref_ptr<osg::Geode> g = new osg::Geode();
|
|
tr->addChild(g);
|
|
tr->setInitialBound(osg::BoundingSphere(osg::Vec3(), 1));
|
|
|
|
osgText::Text* text = new osgText::Text();
|
|
text->setBackdropType(osgText::Text::NONE);
|
|
text->setCharacterSize(20);
|
|
text->setCharacterSizeMode(osgText::TextBase::SCREEN_COORDS);
|
|
//text->setColor(color);
|
|
if(mDisText.empty())
|
|
{
|
|
text->setText(mText);
|
|
}
|
|
else
|
|
{
|
|
text->setText(mDisText);
|
|
}
|
|
text->setAlignment(osgText::TextBase::CENTER_CENTER);
|
|
text->setAxisAlignment(osgText::TextBase::SCREEN);
|
|
//text->setPosition(position);
|
|
osg::StateSet* ss = text->getOrCreateStateSet();
|
|
osg::PolygonMode* polyModeObj = new osg::PolygonMode;
|
|
polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::FILL);
|
|
ss->setAttribute(polyModeObj);
|
|
|
|
g->addDrawable(text);
|
|
|
|
if(mSelected)
|
|
{
|
|
int x, y, w, h;
|
|
int b = 6;
|
|
x = -25 / 2 - b;
|
|
y = 0 - b;
|
|
w = 25 + b * 2;
|
|
h = 4 + b * 2;
|
|
|
|
osg::ref_ptr<osg::Geometry> sel = new osg::Geometry();
|
|
g->addDrawable(sel);
|
|
osg::Vec4Array* colours = new osg::Vec4Array(1);
|
|
(*colours)[0] = osg::Vec4(1,0,0,1);
|
|
sel->setColorArray(colours);
|
|
sel->setColorBinding(osg::Geometry::BIND_OVERALL);
|
|
osg::Vec3Array* coords = new osg::Vec3Array(4);
|
|
(*coords)[0] = osg::Vec3(x , y , 0);
|
|
(*coords)[1] = osg::Vec3(x , y + h, 0);
|
|
(*coords)[2] = osg::Vec3(x + w, y + h, 0);
|
|
(*coords)[3] = osg::Vec3(x + w, y , 0);
|
|
sel->setVertexArray(coords);
|
|
sel->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, 0, 4));
|
|
osg::ref_ptr<osg::PolygonMode> p = new osg::PolygonMode;
|
|
p->setMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE);
|
|
sel->getOrCreateStateSet()->setAttribute(p);
|
|
}
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
std::string TextComponent::GetText() const
|
|
{
|
|
return mText;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
void TextComponent::SetText(const std::string& text)
|
|
{
|
|
if(mText != text)
|
|
{
|
|
mText = text;
|
|
Rebuild();
|
|
}
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
std::string TextComponent::GetDisText() const
|
|
{
|
|
return mDisText;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
void TextComponent::SetDisText(const std::string& text)
|
|
{
|
|
if(mDisText != text)
|
|
{
|
|
mDisText = text;
|
|
Rebuild();
|
|
}
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
dtEntity::Vec3d TextComponent::GetTranslation() const
|
|
{
|
|
osg::AutoTransform* tr = static_cast<osg::AutoTransform*>(GetNode());
|
|
return tr->getPosition();
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
void TextComponent::SetTranslation(const dtEntity::Vec3d& v)
|
|
{
|
|
osg::AutoTransform* tr = static_cast<osg::AutoTransform*>(GetNode());
|
|
tr->setPosition(v);
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
dtEntity::Vec3d TextComponent::GetScale() const
|
|
{
|
|
osg::AutoTransform* tr = static_cast<osg::AutoTransform*>(GetNode());
|
|
return tr->getScale();
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
void TextComponent::SetScale(const dtEntity::Vec3d& v)
|
|
{
|
|
osg::AutoTransform* tr = static_cast<osg::AutoTransform*>(GetNode());
|
|
tr->setScale(v);
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
dtEntity::Quat TextComponent::GetRotation() const
|
|
{
|
|
osg::AutoTransform* tr = static_cast<osg::AutoTransform*>(GetNode());
|
|
return tr->getRotation();
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
void TextComponent::SetRotation(const dtEntity::Quat& v)
|
|
{
|
|
osg::AutoTransform* tr = static_cast<osg::AutoTransform*>(GetNode());
|
|
tr->setRotation(v);
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
void TextComponent::SetColor(const osg::Vec4& v)
|
|
{
|
|
if(mColor != v)
|
|
{
|
|
mColor = v;
|
|
Rebuild();
|
|
}
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
bool TextComponent::GetShowSelection() const
|
|
{
|
|
return mSelected;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
void TextComponent::SetShowSelection(bool v)
|
|
{
|
|
//if(mSelected != v)
|
|
{
|
|
mSelected = v;
|
|
Rebuild();
|
|
}
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
void TextComponent::SetVisible(bool v)
|
|
{
|
|
//if(mVisible != v)
|
|
{
|
|
mVisible = v;
|
|
Rebuild();
|
|
}
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
TextSystem::TextSystem(dtEntity::EntityManager& em)
|
|
: dtEntity::DefaultEntitySystem<TextComponent>(em, dtEntityOSG::TransformComponent::TYPE)
|
|
{
|
|
}
|
|
|
|
}
|