| 116 | void GraphView::SetGraphToView( BaseGraph* pGraph, bool tryMaintainSelection ) |
| 117 | { |
| 118 | if ( m_pGraph == pGraph ) |
| 119 | { |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | // Record selection |
| 124 | //------------------------------------------------------------------------- |
| 125 | |
| 126 | TVector<SelectedNode> oldSelection; |
| 127 | oldSelection.swap( m_selectedNodes ); |
| 128 | |
| 129 | // Switch graph |
| 130 | //------------------------------------------------------------------------- |
| 131 | |
| 132 | ResetInternalState(); |
| 133 | |
| 134 | m_pGraph = pGraph; |
| 135 | if ( m_pGraph != nullptr ) |
| 136 | { |
| 137 | m_pViewOffset = &m_pGraph->m_viewOffset; |
| 138 | |
| 139 | DrawContext drawingContext; |
| 140 | drawingContext.SetViewScaleFactor( m_pGraph->m_viewScaleFactor ); |
| 141 | |
| 142 | // Ensure that the view overlaps some section of the nodes in the graph |
| 143 | // We dont want to show an empty area of graph |
| 144 | ImRect canvasAreaWithNodes; |
| 145 | for ( TTypeInstance<BaseNode> const& nodeInstance : m_pGraph->m_nodes ) |
| 146 | { |
| 147 | canvasAreaWithNodes.Add( nodeInstance->GetRect() ); |
| 148 | } |
| 149 | |
| 150 | ImRect visibleCanvasRect( *m_pViewOffset, ImVec2( *m_pViewOffset ) + m_canvasSize ); |
| 151 | if ( !visibleCanvasRect.Overlaps( canvasAreaWithNodes ) ) |
| 152 | { |
| 153 | *m_pViewOffset = canvasAreaWithNodes.Min; |
| 154 | } |
| 155 | |
| 156 | // Notify graph it is being viewed |
| 157 | m_pGraph->OnShowGraph(); |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | m_defaultViewOffset = Float2::Zero; |
| 162 | m_pViewOffset = &m_defaultViewOffset; |
| 163 | } |
| 164 | |
| 165 | RefreshNodeSizes(); |
| 166 | |
| 167 | // Restore old selection |
| 168 | //------------------------------------------------------------------------- |
| 169 | |
| 170 | if ( tryMaintainSelection ) |
| 171 | { |
| 172 | TVector<SelectedNode> newSelection; |
| 173 | for ( auto const& oldSelectedNode : oldSelection ) |
| 174 | { |
| 175 | auto pFoundNode = GetViewedGraph()->FindNode( oldSelectedNode.m_nodeID ); |
no test coverage detected