Commit dac6fa8a authored by chili's avatar chili
Browse files

give each node its id (fixing imgui tree id problems)

parent f67da57b
...@@ -87,7 +87,7 @@ void App::DoFrame() ...@@ -87,7 +87,7 @@ void App::DoFrame()
{ {
if( !wnd.CursorEnabled() ) if( !wnd.CursorEnabled() )
{ {
cam.Rotate( delta->x,delta->y ); cam.Rotate( (float)delta->x,(float)delta->y );
} }
} }
......
...@@ -66,8 +66,9 @@ DirectX::XMMATRIX Mesh::GetTransformXM() const noexcept ...@@ -66,8 +66,9 @@ DirectX::XMMATRIX Mesh::GetTransformXM() const noexcept
// Node // Node
Node::Node( const std::string& name,std::vector<Mesh*> meshPtrs,const DirectX::XMMATRIX& transform_in ) noxnd Node::Node( int id,const std::string& name,std::vector<Mesh*> meshPtrs,const DirectX::XMMATRIX& transform_in ) noxnd
: :
id( id ),
meshPtrs( std::move( meshPtrs ) ), meshPtrs( std::move( meshPtrs ) ),
name( name ) name( name )
{ {
...@@ -97,23 +98,20 @@ void Node::AddChild( std::unique_ptr<Node> pChild ) noxnd ...@@ -97,23 +98,20 @@ void Node::AddChild( std::unique_ptr<Node> pChild ) noxnd
childPtrs.push_back( std::move( pChild ) ); childPtrs.push_back( std::move( pChild ) );
} }
void Node::ShowTree( int& nodeIndexTracked,std::optional<int>& selectedIndex,Node*& pSelectedNode ) const noexcept void Node::ShowTree( std::optional<int>& selectedIndex,Node*& pSelectedNode ) const noexcept
{ {
// nodeIndex serves as the uid for gui tree nodes, incremented throughout recursion
const int currentNodeIndex = nodeIndexTracked;
nodeIndexTracked++;
// build up flags for current node // build up flags for current node
const auto node_flags = ImGuiTreeNodeFlags_OpenOnArrow const auto node_flags = ImGuiTreeNodeFlags_OpenOnArrow
| ((currentNodeIndex == selectedIndex.value_or( -1 )) ? ImGuiTreeNodeFlags_Selected : 0) | ((GetId() == selectedIndex.value_or( -1 )) ? ImGuiTreeNodeFlags_Selected : 0)
| ((childPtrs.size() == 0) ? ImGuiTreeNodeFlags_Leaf : 0); | ((childPtrs.size() == 0) ? ImGuiTreeNodeFlags_Leaf : 0);
// render this node // render this node
const auto expanded = ImGui::TreeNodeEx( const auto expanded = ImGui::TreeNodeEx(
(void*)(intptr_t)currentNodeIndex,node_flags,name.c_str() (void*)(intptr_t)GetId(),node_flags,name.c_str()
); );
// processing for selecting node // processing for selecting node
if( ImGui::IsItemClicked() ) if( ImGui::IsItemClicked() )
{ {
selectedIndex = currentNodeIndex; selectedIndex = GetId();
pSelectedNode = const_cast<Node*>(this); pSelectedNode = const_cast<Node*>(this);
} }
// recursive rendering of open node's children // recursive rendering of open node's children
...@@ -121,7 +119,7 @@ void Node::ShowTree( int& nodeIndexTracked,std::optional<int>& selectedIndex,Nod ...@@ -121,7 +119,7 @@ void Node::ShowTree( int& nodeIndexTracked,std::optional<int>& selectedIndex,Nod
{ {
for( const auto& pChild : childPtrs ) for( const auto& pChild : childPtrs )
{ {
pChild->ShowTree( nodeIndexTracked,selectedIndex,pSelectedNode ); pChild->ShowTree( selectedIndex,pSelectedNode );
} }
ImGui::TreePop(); ImGui::TreePop();
} }
...@@ -132,6 +130,11 @@ void Node::SetAppliedTransform( DirectX::FXMMATRIX transform ) noexcept ...@@ -132,6 +130,11 @@ void Node::SetAppliedTransform( DirectX::FXMMATRIX transform ) noexcept
dx::XMStoreFloat4x4( &appliedTransform,transform ); dx::XMStoreFloat4x4( &appliedTransform,transform );
} }
int Node::GetId() const noexcept
{
return id;
}
// Model // Model
class ModelWindow // pImpl idiom, only defined in this .cpp class ModelWindow // pImpl idiom, only defined in this .cpp
...@@ -146,7 +149,7 @@ public: ...@@ -146,7 +149,7 @@ public:
if( ImGui::Begin( windowName ) ) if( ImGui::Begin( windowName ) )
{ {
ImGui::Columns( 2,nullptr,true ); ImGui::Columns( 2,nullptr,true );
root.ShowTree( nodeIndexTracker,selectedIndex,pSelectedNode ); root.ShowTree( selectedIndex,pSelectedNode );
ImGui::NextColumn(); ImGui::NextColumn();
if( pSelectedNode != nullptr ) if( pSelectedNode != nullptr )
...@@ -212,7 +215,8 @@ Model::Model( Graphics& gfx,const std::string fileName ) ...@@ -212,7 +215,8 @@ Model::Model( Graphics& gfx,const std::string fileName )
meshPtrs.push_back( ParseMesh( gfx,*pScene->mMeshes[i] ) ); meshPtrs.push_back( ParseMesh( gfx,*pScene->mMeshes[i] ) );
} }
pRoot = ParseNode( *pScene->mRootNode ); int nextId = 0;
pRoot = ParseNode( nextId,*pScene->mRootNode );
} }
void Model::Draw( Graphics& gfx ) const noxnd void Model::Draw( Graphics& gfx ) const noxnd
...@@ -287,7 +291,7 @@ std::unique_ptr<Mesh> Model::ParseMesh( Graphics& gfx,const aiMesh& mesh ) ...@@ -287,7 +291,7 @@ std::unique_ptr<Mesh> Model::ParseMesh( Graphics& gfx,const aiMesh& mesh )
return std::make_unique<Mesh>( gfx,std::move( bindablePtrs ) ); return std::make_unique<Mesh>( gfx,std::move( bindablePtrs ) );
} }
std::unique_ptr<Node> Model::ParseNode( const aiNode& node ) noexcept std::unique_ptr<Node> Model::ParseNode( int& nextId,const aiNode& node ) noexcept
{ {
namespace dx = DirectX; namespace dx = DirectX;
const auto transform = dx::XMMatrixTranspose( dx::XMLoadFloat4x4( const auto transform = dx::XMMatrixTranspose( dx::XMLoadFloat4x4(
...@@ -302,10 +306,10 @@ std::unique_ptr<Node> Model::ParseNode( const aiNode& node ) noexcept ...@@ -302,10 +306,10 @@ std::unique_ptr<Node> Model::ParseNode( const aiNode& node ) noexcept
curMeshPtrs.push_back( meshPtrs.at( meshIdx ).get() ); curMeshPtrs.push_back( meshPtrs.at( meshIdx ).get() );
} }
auto pNode = std::make_unique<Node>( node.mName.C_Str(),std::move( curMeshPtrs ),transform ); auto pNode = std::make_unique<Node>( nextId++,node.mName.C_Str(),std::move( curMeshPtrs ),transform );
for( size_t i = 0; i < node.mNumChildren; i++ ) for( size_t i = 0; i < node.mNumChildren; i++ )
{ {
pNode->AddChild( ParseNode( *node.mChildren[i] ) ); pNode->AddChild( ParseNode( nextId,*node.mChildren[i] ) );
} }
return pNode; return pNode;
......
...@@ -35,14 +35,16 @@ class Node ...@@ -35,14 +35,16 @@ class Node
friend class Model; friend class Model;
friend class ModelWindow; friend class ModelWindow;
public: public:
Node( const std::string& name,std::vector<Mesh*> meshPtrs,const DirectX::XMMATRIX& transform ) noxnd; Node( int id,const std::string& name,std::vector<Mesh*> meshPtrs,const DirectX::XMMATRIX& transform ) noxnd;
void Draw( Graphics& gfx,DirectX::FXMMATRIX accumulatedTransform ) const noxnd; void Draw( Graphics& gfx,DirectX::FXMMATRIX accumulatedTransform ) const noxnd;
void SetAppliedTransform( DirectX::FXMMATRIX transform ) noexcept; void SetAppliedTransform( DirectX::FXMMATRIX transform ) noexcept;
int GetId() const noexcept;
private: private:
void AddChild( std::unique_ptr<Node> pChild ) noxnd; void AddChild( std::unique_ptr<Node> pChild ) noxnd;
void ShowTree( int& nodeIndex,std::optional<int>& selectedIndex,Node*& pSelectedNode ) const noexcept; void ShowTree( std::optional<int>& selectedIndex,Node*& pSelectedNode ) const noexcept;
private: private:
std::string name; std::string name;
int id;
std::vector<std::unique_ptr<Node>> childPtrs; std::vector<std::unique_ptr<Node>> childPtrs;
std::vector<Mesh*> meshPtrs; std::vector<Mesh*> meshPtrs;
DirectX::XMFLOAT4X4 transform; DirectX::XMFLOAT4X4 transform;
...@@ -58,7 +60,7 @@ public: ...@@ -58,7 +60,7 @@ public:
~Model() noexcept; ~Model() noexcept;
private: private:
static std::unique_ptr<Mesh> ParseMesh( Graphics& gfx,const aiMesh& mesh ); static std::unique_ptr<Mesh> ParseMesh( Graphics& gfx,const aiMesh& mesh );
std::unique_ptr<Node> ParseNode( const aiNode& node ) noexcept; std::unique_ptr<Node> ParseNode( int& nextId,const aiNode& node ) noexcept;
private: private:
std::unique_ptr<Node> pRoot; std::unique_ptr<Node> pRoot;
std::vector<std::unique_ptr<Mesh>> meshPtrs; std::vector<std::unique_ptr<Mesh>> meshPtrs;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment