Skip to content

Commit

Permalink
fixup! AnnotationsGadget : Add annotationAt()
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmehl committed Feb 5, 2025
1 parent 013ffe0 commit cf2b871
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 88 deletions.
2 changes: 1 addition & 1 deletion include/GafferUI/AnnotationsGadget.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class GAFFERUI_API AnnotationsGadget : public Gadget
// with a unique `IDRender` index per annotation and fills `selectionIds`.
// If they are not given, no modification to the selection buffer IDs are
// made (all annotations have the ID for this widget).
void renderAnnotations( const Style *style, AnnotationBufferMap *selectionIds = nullptr, IECoreGL::Selector *selector = nullptr ) const;
void renderAnnotations( const Style *style, AnnotationBufferMap *selectionIds = nullptr ) const;

struct StandardAnnotation : public Gaffer::MetadataAlgo::Annotation
{
Expand Down
118 changes: 31 additions & 87 deletions src/GafferUI/AnnotationsGadget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,53 +355,6 @@ void AnnotationsGadget::renderLayer( Layer layer, const Style *style, RenderReas
return;
}

const_cast<AnnotationsGadget *>( this )->update();

for( const auto &ga : m_annotations )
{
const Annotations &annotations = ga.second;
assert( !annotations.dirty );
if( !annotations.renderable )
{
continue;
}

const Box2f b = nodeFrame( ga.first );

V2f bookmarkIconPos( b.min.x, b.max.y );
if( ga.first->node() == ga.first->node()->ancestor<ScriptNode>()->getFocus() )
{
const StandardNodeGadget *standardNodeGadget = runTimeCast<const StandardNodeGadget>( ga.first );
if( standardNodeGadget )
{
float fbw = standardNodeGadget->focusBorderWidth();
bookmarkIconPos += V2f( -fbw, fbw );
}
}

if( annotations.bookmarked )
{
style->renderImage( Box2f( bookmarkIconPos - V2f( 1.0 ), bookmarkIconPos + V2f( 1.0 ) ), bookmarkTexture() );
}

if( annotations.numericBookmark.string().size() )
{
if( !annotations.bookmarked )
{
style->renderImage( Box2f( bookmarkIconPos - V2f( 1.0 ), bookmarkIconPos + V2f( 1.0 ) ), numericBookmarkTexture() );
}

const Box3f textBounds = style->textBound( Style::LabelText, annotations.numericBookmark.string() );

const Imath::Color4f textColor( 0.8f );
glPushMatrix();
IECoreGL::glTranslate( V2f( bookmarkIconPos.x - 0.9 - textBounds.size().x, bookmarkIconPos.y - textBounds.size().y * 0.5 - 0.2 ) );
style->renderText( Style::LabelText, annotations.numericBookmark.string(), Style::NormalState, &textColor );
glPopMatrix();
}

}

renderAnnotations( style );
}

Expand Down Expand Up @@ -769,8 +722,12 @@ void AnnotationsGadget::visibilityChanged()
}
}

void AnnotationsGadget::renderAnnotations( const Style *style, AnnotationBufferMap *selectionIds, IECoreGL::Selector *selector ) const
void AnnotationsGadget::renderAnnotations( const Style *style, AnnotationBufferMap *selectionIds ) const
{
const_cast<AnnotationsGadget *>( this )->update();

IECoreGL::Selector *selector = IECoreGL::Selector::currentSelector();

for( const auto &ga : m_annotations )
{
const Annotations &annotations = ga.second;
Expand All @@ -782,18 +739,41 @@ void AnnotationsGadget::renderAnnotations( const Style *style, AnnotationBufferM

const Box2f b = nodeFrame( ga.first );

V2f bookmarkIconPos( b.min.x, b.max.y );
V2f annotationOrigin( b.max.x + g_offset, b.max.y );
if( ga.first->node() == ga.first->node()->ancestor<ScriptNode>()->getFocus() )
{
const StandardNodeGadget *standardNodeGadget = runTimeCast<const StandardNodeGadget>( ga.first );
if( standardNodeGadget )
{
float fbw = standardNodeGadget->focusBorderWidth();
bookmarkIconPos += V2f( -fbw, fbw );
annotationOrigin += V2f( fbw, 0.0f );
}
}

for( const auto &a : ga.second.standardAnnotations )
if( annotations.bookmarked )
{
style->renderImage( Box2f( bookmarkIconPos - V2f( 1.0 ), bookmarkIconPos + V2f( 1.0 ) ), bookmarkTexture() );
}

if( annotations.numericBookmark.string().size() )
{
if( !annotations.bookmarked )
{
style->renderImage( Box2f( bookmarkIconPos - V2f( 1.0 ), bookmarkIconPos + V2f( 1.0 ) ), numericBookmarkTexture() );
}

const Box3f textBounds = style->textBound( Style::LabelText, annotations.numericBookmark.string() );

const Imath::Color4f textColor( 0.8f );
glPushMatrix();
IECoreGL::glTranslate( V2f( bookmarkIconPos.x - 0.9 - textBounds.size().x, bookmarkIconPos.y - textBounds.size().y * 0.5 - 0.2 ) );
style->renderText( Style::LabelText, annotations.numericBookmark.string(), Style::NormalState, &textColor );
glPopMatrix();
}

for( const auto &a : annotations.standardAnnotations )
{
if( selectionIds && selector )
{
Expand All @@ -816,51 +796,15 @@ std::optional<AnnotationsGadget::AnnotationIdentifier> AnnotationsGadget::annota
std::vector<IECoreGL::HitRecord> selection;
AnnotationBufferMap annotationBuffer;
{
auto viewportGadget = ancestor<const ViewportGadget>();
if( !viewportGadget )
{
return std::optional<AnnotationIdentifier>( std::nullopt );
}

const_cast<AnnotationsGadget *>( this )->update();

// Recreate relevant parts of `ViewportGadget::SelectionScope`, but use our
// own `IECoreGL::Selector` so we can pass it to `renderAnnotations()` for
// incrementing per-annotation.

glPushAttrib( GL_ALL_ATTRIB_BITS );
glPushClientAttrib( GL_CLIENT_ALL_ATTRIB_BITS );

const V2f rasterPosition = viewportGadget->gadgetToRasterSpace( lineInGadgetSpace.p0, this );
const V2f viewport = viewportGadget->getViewport();
Box2f ndcRegion( ( rasterPosition - V2f( 1.f ) ) / viewport, ( rasterPosition + V2f( 1.f ) ) / viewport );

IECoreGL::ToGLConverterPtr converter = new IECoreGL::ToGLCameraConverter( viewportGadget->getCamera() );
IECoreGL::CameraPtr camera = boost::static_pointer_cast<IECoreGL::Camera>( converter->convert() );
camera->setTransform( viewportGadget->getCameraTransform() );
camera->render( nullptr );

auto selector = std::make_unique<IECoreGL::Selector>( ndcRegion, IECoreGL::Selector::Mode::IDRender, selection, true );

glPushMatrix();
glMultMatrixf( fullTransform().getValue() );
ViewportGadget::SelectionScope selectionScope( lineInGadgetSpace, this, selection, IECoreGL::Selector::Mode::IDRender );

const Style *currentStyle = style();
currentStyle->bind();

// See `ViewportGadget::renderInternal()` for reasoning behind disabling blending.
glDisable( GL_BLEND );

renderAnnotations( currentStyle, &annotationBuffer, selector.get() );

glPopMatrix();

selector.reset();

glPopClientAttrib();
glPopAttrib();

std::reverse( selection.begin(), selection.end() );
renderAnnotations( currentStyle, &annotationBuffer );
}

if( selection.empty() )
Expand Down

0 comments on commit cf2b871

Please sign in to comment.