diff --git a/src/Gui/SoFCColorBar.cpp b/src/Gui/SoFCColorBar.cpp index 6960ced4c7..870c33afb1 100644 --- a/src/Gui/SoFCColorBar.cpp +++ b/src/Gui/SoFCColorBar.cpp @@ -23,9 +23,11 @@ #include "PreCompiled.h" #ifndef _PreComp_ +# include # include # include # include +# include # include # include # include @@ -78,6 +80,32 @@ void SoFCColorBarBase::GLRenderBelowPath(SoGLRenderAction * action) SoSeparator::GLRenderBelowPath(action); } +float SoFCColorBarBase::getBoundingWidth(const SbVec2s& size) +{ + // These are the same camera settings for front nodes as defined in the 3d view + SoOrthographicCamera* cam = new SoOrthographicCamera; + cam->position = SbVec3f(0, 0, 5); + cam->height = 10; + cam->nearDistance = 0; + cam->farDistance = 10; + + SoGroup* group = new SoGroup(); + group->ref(); + group->addChild(cam); + group->addChild(this); + + SbViewportRegion vpr(size); + SoGetBoundingBoxAction bbact(vpr); + bbact.apply(group); + SbBox3f box = bbact.getBoundingBox(); + SbVec3f minPt, maxPt; + box.getBounds(minPt, maxPt); + group->unref(); + + float boxWidth = maxPt[0] - minPt[0]; + return boxWidth + 0.2f; +} + // -------------------------------------------------------------------------- namespace Gui { diff --git a/src/Gui/SoFCColorBar.h b/src/Gui/SoFCColorBar.h index 0238bb8702..c6a148a5f6 100644 --- a/src/Gui/SoFCColorBar.h +++ b/src/Gui/SoFCColorBar.h @@ -112,6 +112,12 @@ public: virtual const char* getColorBarName() const = 0; protected: + /** Returns the width of the color bar and labels + * + * Computes the occupied width of the color bar and its labels. + * It therefore determines the bounding box. + */ + float getBoundingWidth(const SbVec2s& size); /** * Sets the current viewer size to recalculate the new position. * diff --git a/src/Gui/SoFCColorGradient.cpp b/src/Gui/SoFCColorGradient.cpp index b68db9d5a8..8497c6d130 100644 --- a/src/Gui/SoFCColorGradient.cpp +++ b/src/Gui/SoFCColorGradient.cpp @@ -110,13 +110,12 @@ void SoFCColorGradient::setMarkerLabel(const SoMFString& label) void SoFCColorGradient::setViewportSize(const SbVec2s& size) { - // don't know why the parameter range isn't between [-1,+1] - float fRatio = ((float)size[0]) / ((float)size[1]); + float fRatio = static_cast(size[0]) / static_cast(size[1]); float fMinX = 4.0f, fMaxX = 4.5f; float fMinY = -4.0f, fMaxY = 4.0f; if (fRatio > 1.0f) { - fMinX = 4.0f * fRatio; + fMinX = 5.0f * fRatio; fMaxX = fMinX + 0.5f; } else if (fRatio < 1.0f) { @@ -124,6 +123,11 @@ void SoFCColorGradient::setViewportSize(const SbVec2s& size) fMaxY = 4.0f / fRatio; } + float boxWidth = getBoundingWidth(size); + if (fRatio < 1.0f) { + boxWidth *= fRatio; + } + // search for the labels int num = 0; for (int i = 0; i < labels->getNumChildren(); i++) { @@ -139,7 +143,7 @@ void SoFCColorGradient::setViewportSize(const SbVec2s& size) if (labels->getChild(j)->getTypeId() == SoTransform::getClassTypeId()) { if (first) { first = false; - static_cast(labels->getChild(j))->translation.setValue(fMaxX + 0.1f, fMaxY - 0.05f + fStep, 0.0f); + static_cast(labels->getChild(j))->translation.setValue(fMaxX + 0.1f - boxWidth, fMaxY - 0.05f + fStep, 0.0f); } else { static_cast(labels->getChild(j))->translation.setValue(0, -fStep, 0.0f); @@ -148,7 +152,7 @@ void SoFCColorGradient::setViewportSize(const SbVec2s& size) } } - _bbox.setBounds(fMinX, fMinY, fMaxX, fMaxY); + _bbox.setBounds(fMinX - boxWidth, fMinY, fMaxX - boxWidth, fMaxY); modifyPoints(_bbox); } diff --git a/src/Gui/SoFCColorLegend.cpp b/src/Gui/SoFCColorLegend.cpp index c197570a72..476831d781 100644 --- a/src/Gui/SoFCColorLegend.cpp +++ b/src/Gui/SoFCColorLegend.cpp @@ -190,13 +190,12 @@ void SoFCColorLegend::setMarkerValue(const SoMFString& value) void SoFCColorLegend::setViewportSize(const SbVec2s& size) { - // don't know why the parameter range isn't between [-1,+1] float fRatio = static_cast(size[0]) / static_cast(size[1]); float fMinX = 4.0f, fMaxX = 4.5f; float fMinY = -4.0f, fMaxY = 4.0f; if (fRatio > 1.0f) { - fMinX = 4.0f * fRatio; + fMinX = 5.0f * fRatio; fMaxX = fMinX + 0.5f; } else if (fRatio < 1.0f) { @@ -204,7 +203,12 @@ void SoFCColorLegend::setViewportSize(const SbVec2s& size) fMaxY = 4.0f / fRatio; } - _bbox.setBounds(fMinX, fMinY, fMaxX, fMaxY); + float boxWidth = getBoundingWidth(size); + if (fRatio < 1.0f) { + boxWidth *= fRatio; + } + + _bbox.setBounds(fMinX - boxWidth, fMinY, fMaxX - boxWidth, fMaxY); arrangeLabels(_bbox); arrangeValues(_bbox);