Skip to content

Commit

Permalink
LightPositionTool : Drag to place targets
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmehl committed Dec 11, 2023
1 parent fbfe63d commit 6b0721a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 deletions.
9 changes: 9 additions & 0 deletions include/GafferSceneUI/LightPositionTool.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ class GAFFERSCENEUI_API LightPositionTool : public GafferSceneUI::TransformTool
bool handleDragMove( GafferUI::Gadget *gadget, const GafferUI::DragDropEvent &event );
bool handleDragEnd();

IECore::RunTimeTypedPtr sceneGadgetDragBegin( GafferUI::Gadget *gadget, const GafferUI::DragDropEvent &event );
bool sceneGadgetDragEnter( GafferUI::Gadget *gadget, const GafferUI::DragDropEvent &event );
bool sceneGadgetDragMove( const GafferUI::DragDropEvent &event );
bool sceneGadgetDragEnd();

bool keyPress( const GafferUI::KeyEvent &event );
bool keyRelease( const GafferUI::KeyEvent &event );
void viewportGadgetLeave( const GafferUI::ButtonEvent &event );
Expand All @@ -105,6 +110,8 @@ class GAFFERSCENEUI_API LightPositionTool : public GafferSceneUI::TransformTool

bool buttonPress( const GafferUI::ButtonEvent &event );

bool placeTarget( const IECore::LineSegment3f &eventLine );

enum class TargetMode
{
None,
Expand Down Expand Up @@ -138,6 +145,8 @@ class GAFFERSCENEUI_API LightPositionTool : public GafferSceneUI::TransformTool

IECore::MurmurHash m_key;

bool m_draggingTarget;

static ToolDescription<LightPositionTool, SceneView> g_toolDescription;
static size_t g_firstPlugIndex;

Expand Down
68 changes: 61 additions & 7 deletions src/GafferSceneUI/LightPositionTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,8 @@ size_t LightPositionTool::g_firstPlugIndex = 0;

LightPositionTool::LightPositionTool( SceneView *view, const std::string &name ) :
TransformTool( view, name ),
m_targetMode( TargetMode::None )
m_targetMode( TargetMode::None ),
m_draggingTarget( false )
{
m_shadowHandle = new ShadowHandle();
m_shadowHandle->setRasterScale( 0 );
Expand All @@ -554,6 +555,11 @@ LightPositionTool::LightPositionTool( SceneView *view, const std::string &name )
// We have to insert this before the underlying SelectionTool connections or it starts an object drag.
sg->buttonPressSignal().connectFront( boost::bind( &LightPositionTool::buttonPress, this, ::_2 ) );

sg->dragBeginSignal().connectFront( boost::bind( &LightPositionTool::sceneGadgetDragBegin, this, ::_1, ::_2 ) );
sg->dragEnterSignal().connectFront( boost::bind( &LightPositionTool::sceneGadgetDragEnter, this, ::_1, ::_2 ) );
sg->dragMoveSignal().connectFront( boost::bind( &LightPositionTool::sceneGadgetDragMove, this, ::_2 ) );
sg->dragEndSignal().connectFront( boost::bind( &LightPositionTool::sceneGadgetDragEnd, this ) );

// We need to track the tool state/view visibility so we don't leave a lingering target cursor
sg->visibilityChangedSignal().connect( boost::bind( &LightPositionTool::visibilityChanged, this, ::_1 ) );

Expand Down Expand Up @@ -730,6 +736,49 @@ bool LightPositionTool::handleDragEnd()
return false;
}

RunTimeTypedPtr LightPositionTool::sceneGadgetDragBegin( Gadget *gadget, const DragDropEvent &event )
{
if( !activePlug()->getValue() || getTargetMode() == TargetMode::None )
{
return nullptr;
}
m_draggingTarget = true;

TransformTool::dragBegin();
return gadget;
}

bool LightPositionTool::sceneGadgetDragEnter( Gadget *gadget, const DragDropEvent &event )
{
return m_draggingTarget && event.sourceGadget == gadget && event.data == gadget;
}

bool LightPositionTool::sceneGadgetDragMove( const DragDropEvent &event )
{
if( !m_draggingTarget )
{
return false;
}

// We always return true to prevent the SelectTool defaults.
if( !selectionEditable() || !m_shadowHandle->enabled() )
{
return true;
}

UndoScope undoScope( selection().back().editTarget()->ancestor<ScriptNode>(), UndoScope::Enabled, undoMergeGroup() );

placeTarget( event.line );
return true;
}

bool LightPositionTool::sceneGadgetDragEnd()
{
m_draggingTarget = false;
TransformTool::dragEnd();
return false;
}

bool LightPositionTool::keyPress( const KeyEvent &event )
{
// We track this regardless of whether we're active or not in case the tool
Expand Down Expand Up @@ -809,27 +858,32 @@ bool LightPositionTool::buttonPress( const ButtonEvent &event )
}

// We always return true to prevent the SelectTool defaults.

if( !selectionEditable() || !m_shadowHandle->enabled() )
{
return true;
}

UndoScope undoScope( selection().back().editTarget()->ancestor<ScriptNode>() );

placeTarget( event.line );
return true;
}

bool LightPositionTool::placeTarget( const LineSegment3f &eventLine )
{
ScenePlug::ScenePath scenePath;
V3f targetPos;

const SceneGadget *sceneGadget = runTimeCast<SceneGadget>( view()->viewportGadget()->getPrimaryChild() );
if( !sceneGadget->objectAt( event.line, scenePath, targetPos ) )
if( !sceneGadget->objectAt( eventLine, scenePath, targetPos ) )
{
return true;
return false;
}

const auto shadowHandle = static_cast<ShadowHandle *>( m_shadowHandle.get() );
Selection s = selection().back();
ScriptNodePtr scriptNode = s.editTarget()->ancestor<ScriptNode>();

UndoScope undoScope( scriptNode );

if( getTargetMode() == TargetMode::ShadowPivot )
{
const V3f newPivot = targetPos * sceneGadget->fullTransform();
Expand All @@ -848,7 +902,7 @@ bool LightPositionTool::buttonPress( const ButtonEvent &event )

if( !shadowHandle->getShadowPivot() || !shadowHandle->getShadowTarget() )
{
return true;
return false;
}

position( shadowHandle->getShadowPivot().value(), shadowHandle->getShadowTarget().value(), shadowHandle->getPivotDistance().value() );
Expand Down

0 comments on commit 6b0721a

Please sign in to comment.