Skip to content

Commit

Permalink
Fix qtcoinrave textSize implementation to target message nodes, add b…
Browse files Browse the repository at this point in the history
…reakpoints for textSize and message translations
  • Loading branch information
Jonathan committed Mar 13, 2024
1 parent e1ff951 commit 7d0efe1
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 23 deletions.
5 changes: 2 additions & 3 deletions plugins/qtcoinrave/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,6 @@ KinBody::LinkPtr KinBodyItem::GetLinkFromIv(SoNode* plinknode) const

RobotItem::RobotItem(QtCoinViewerPtr viewer, RobotBasePtr robot, ViewGeometry viewgeom) : KinBodyItem(viewer, robot, viewgeom), _probot(robot)
{
_textSize = 18;
}

void RobotItem::Load()
Expand Down Expand Up @@ -695,7 +694,7 @@ void RobotItem::CreateAxis(RobotItem::EE& ee, const string& name, const Vector*

SoFont* pfont = new SoFont();
pfont->name = "Courier:Bold";
pfont->size = _textSize;
pfont->size = 18;
ptextsep->addChild(pfont);

SoText2 * ptext = new SoText2();
Expand Down Expand Up @@ -769,4 +768,4 @@ bool RobotItem::UpdateFromModel(const vector<dReal>& vjointvalues, const vector<
}

return true;
}
}
6 changes: 1 addition & 5 deletions plugins/qtcoinrave/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,10 @@ class RobotItem : public KinBodyItem
}
virtual void SetGrab(bool bGrab, bool bUpdate=true);
virtual void Load();
virtual void SetTextSize(double size) {
_textSize = size;
}
private:
void CreateAxis(EE& ee, const string& name, const Vector* pdirection=NULL);
std::vector< EE > _vEndEffectors, _vAttachedSensors;
RobotBasePtr _probot;
double _textSize;
};
typedef boost::shared_ptr<RobotItem> RobotItemPtr;
typedef boost::shared_ptr<RobotItem const> RobotItemConstPtr;
Expand All @@ -252,4 +248,4 @@ BOOST_TYPEOF_REGISTER_TYPE(RobotItem)
BOOST_TYPEOF_REGISTER_TYPE(RobotItem::EE)
#endif

#endif // ITEM_H
#endif // ITEM_H
108 changes: 95 additions & 13 deletions plugins/qtcoinrave/qtcoinviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,17 +257,22 @@ void QtCoinViewer::_InitConstructor(std::istream& sinput)

// add the message texts
SoSeparator* pmsgsep = new SoSeparator();
SoTranslation* pmsgtrans0 = new SoTranslation();
pmsgtrans0->translation.setValue(SbVec3f(-0.978f,0.93f,0));
pmsgsep->addChild(pmsgtrans0);

_messagefont = new SoFont();
_messagefont->size = 18;
pmsgsep->addChild(_messagefont);

_messageBaseTranslation = new SoTranslation();
_messageBaseTranslation->translation.setValue(SbVec3f(-0.978f,0.874f,0));
pmsgsep->addChild(_messageBaseTranslation);
SoBaseColor* pcolor0 = new SoBaseColor();
pcolor0->rgb.setValue(0.0f,0.0f,0.0f);
pmsgsep->addChild(pcolor0);
_messageNodes[0] = new SoText2();
pmsgsep->addChild(_messageNodes[0]);

_messageShadowTranslation = new SoTranslation();
_messageShadowTranslation->translation.setValue(SbVec3f(-0.002f,0.032f,0));
_messageShadowTranslation->translation.setValue(SbVec3f(-0.002f,0.0540f,0));
pmsgsep->addChild(_messageShadowTranslation);
SoBaseColor* pcolor1 = new SoBaseColor();
pcolor1->rgb.setValue(0.99f,0.99f,0.99f);
Expand Down Expand Up @@ -640,12 +645,93 @@ void QtCoinViewer::SetUserText(const string& userText)
_userText = userText;
}

class SetTextSizeMessage : public QtCoinViewer::EnvMessage
{
public:
SetTextSizeMessage(QtCoinViewerPtr pviewer, void** ppreturn, double size)
: EnvMessage(pviewer, ppreturn, false), _textSize(size) {
}

virtual void viewerexecute() {
QtCoinViewerPtr pviewer = _pviewer.lock();
if( !pviewer ) {
return;
}
pviewer->_SetTextSize(_textSize);
EnvMessage::viewerexecute();
}

private:
double _textSize;
};

void QtCoinViewer::SetTextSize(double size)
{
if ( size >= 0 ) {
_textSize = size;
UpdateFromModel();
if (_timerSensor->isScheduled() && _bUpdateEnvironment) {
EnvMessagePtr pmsg(new SetTextSizeMessage(shared_viewer(), (void**)NULL, size));
pmsg->callerexecute(false);
}
}

void QtCoinViewer::_SetTextSize(double size)
{
if ( size > 0 ) {
// TODO: use a font that does not rely on hardcoded breakpoints
// move down to next text size breakpoint so that message shadow aligns nicely
_messagefont->size = _GetTextBaseSize(size);
// adjust the text offsets
_messageBaseTranslation->translation.setValue(_GetMessageBaseTranslation());
_messageShadowTranslation->translation.setValue(_GetMessageShadowTranslation());
}
}

double QtCoinViewer::_GetTextBaseSize(double size)
{
if (size < 14.0) {
return 10.0;
}
if (size < 18.0) {
return 14.0;
}
if (size < 26.0) {
return 18.0;
}
return 26.0;
}

SbVec3f QtCoinViewer::_GetMessageBaseTranslation()
{
SbViewportRegion v = _pviewer->getViewportRegion();
float fwratio = 964.0f/v.getWindowSize()[0], fhratio = 688.0f/v.getWindowSize()[1];
float size = _messagefont->size.getValue();
if (size < 14.0f) {
return SbVec3f(-1.0f+(0.022f*fwratio),1.0f-(0.07f*fhratio),0);
}
if (size < 18.0f) {
return SbVec3f(-1.0f+(0.022f*fwratio),1.0f-(0.098f*fhratio),0);
}
if (size < 26.0f) {
return SbVec3f(-1.0f+(0.022f*fwratio),1.0f-(0.126f*fhratio),0);
}
return SbVec3f(-1.0f+(0.022f*fwratio),1.0f-(0.182f*fhratio),0);
}

SbVec3f QtCoinViewer::_GetMessageShadowTranslation()
{
SbViewportRegion v = _pviewer->getViewportRegion();
float fwratio = 964.0f/v.getWindowSize()[0], fhratio = 688.0f/v.getWindowSize()[1];
float size = _messagefont->size.getValue();

if (size < 14.0f) {
return SbVec3f(-0.002f*fwratio,(0.032f*fhratio)*(size/10.0f),0);
}
if (size < 18.0f) {
return SbVec3f(-0.002f*fwratio,(0.0448f*fhratio)*(size/14.0f),0);
}
if (size < 26.0f) {
return SbVec3f(-0.002f*fwratio,(0.0540f*fhratio)*(size/18.0f),0);
}
return SbVec3f(-0.002f*fwratio,(0.0777f*fhratio)*(size/26.0f),0);
}

bool QtCoinViewer::LoadModel(const string& pfilename)
Expand Down Expand Up @@ -2809,9 +2895,7 @@ void QtCoinViewer::AdvanceFrame(bool bForward)
}

// adjust the shadow text
SbViewportRegion v = _pviewer->getViewportRegion();
float fwratio = 964.0f/v.getWindowSize()[0], fhratio = 688.0f/v.getWindowSize()[1];
_messageShadowTranslation->translation.setValue(SbVec3f(-0.002f*fwratio,0.032f*fhratio,0));
_messageShadowTranslation->translation.setValue(_GetMessageShadowTranslation());

// search for all new lines
string msg = ss.str();
Expand Down Expand Up @@ -3023,9 +3107,7 @@ void QtCoinViewer::UpdateFromModel()
}

if( pbody->IsRobot() ) {
boost::shared_ptr<RobotItem> probot = boost::shared_ptr<RobotItem>(new RobotItem(shared_viewer(), RaveInterfaceCast<RobotBase>(pbody), _viewGeometryMode),ITEM_DELETER);
probot->SetTextSize(_textSize);
pitem = probot;
pitem = boost::shared_ptr<RobotItem>(new RobotItem(shared_viewer(), RaveInterfaceCast<RobotBase>(pbody), _viewGeometryMode),ITEM_DELETER);
}
else {
pitem = boost::shared_ptr<KinBodyItem>(new KinBodyItem(shared_viewer(), pbody, _viewGeometryMode),ITEM_DELETER);
Expand Down
8 changes: 7 additions & 1 deletion plugins/qtcoinrave/qtcoinviewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ public slots:
virtual void _SetTriangleMesh(SoSeparator* pparent, const float* ppoints, int stride, const int* pIndices, int numTriangles);
virtual void _Reset();
virtual void _SetBkgndColor(const RaveVector<float>& color);
virtual void _SetTextSize(double size);
virtual double _GetTextBaseSize(double size);
virtual SbVec3f _GetMessageBaseTranslation();
virtual SbVec3f _GetMessageShadowTranslation();
virtual void _closegraph(SoSwitch* handle);
virtual void _SetGraphTransform(SoSwitch* handle, const RaveTransform<float>& t);
virtual void _SetGraphShow(SoSwitch* handle, bool bshow);
Expand Down Expand Up @@ -413,7 +417,9 @@ public slots:
std::list< boost::shared_ptr<IvDragger> > _plistdraggers; /// draggers drawn
SoEventCallback* _eventKeyboardCB;

SoFont* _messagefont;
boost::array<SoText2*,2> _messageNodes;
SoTranslation* _messageBaseTranslation;
SoTranslation* _messageShadowTranslation;

bool _altDown[2];
Expand All @@ -422,7 +428,6 @@ public slots:

std::string _name;
std::string _userText;
double _textSize;
std::map<KinBodyPtr, KinBodyItemPtr> _mapbodies; ///< all the bodies created

ItemPtr _pSelectedItem; ///< the currently selected item
Expand Down Expand Up @@ -528,6 +533,7 @@ public slots:
friend class DeselectMessage;
friend class ResetMessage;
friend class SetBkgndColorMessage;
friend class SetTextSizeMessage;
friend class StartPlaybackTimerMessage;
friend class StopPlaybackTimerMessage;
friend class SetGraphTransformMessage;
Expand Down
2 changes: 1 addition & 1 deletion plugins/qtosgrave/osgviewerwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,7 @@ void QOSGViewerWidget::SetViewport(int width, int height)

void QOSGViewerWidget::SetHUDTextSize(double size)
{
if ( size >= 0 ) {
if ( size > 0 ) {
_hudTextSize = size;

float scale = this->devicePixelRatio();
Expand Down

0 comments on commit 7d0efe1

Please sign in to comment.