Commit 99e230c6 authored by chili's avatar chili
Browse files

can refactor recursive tree show to only need 1 ref now

parent dac6fa8a
...@@ -98,11 +98,13 @@ void Node::AddChild( std::unique_ptr<Node> pChild ) noxnd ...@@ -98,11 +98,13 @@ void Node::AddChild( std::unique_ptr<Node> pChild ) noxnd
childPtrs.push_back( std::move( pChild ) ); childPtrs.push_back( std::move( pChild ) );
} }
void Node::ShowTree( std::optional<int>& selectedIndex,Node*& pSelectedNode ) const noexcept void Node::ShowTree( Node*& pSelectedNode ) const noexcept
{ {
// if there is no selected node, set selectedId to an impossible value
const int selectedId = (pSelectedNode == nullptr) ? -1 : pSelectedNode->GetId();
// build up flags for current node // build up flags for current node
const auto node_flags = ImGuiTreeNodeFlags_OpenOnArrow const auto node_flags = ImGuiTreeNodeFlags_OpenOnArrow
| ((GetId() == selectedIndex.value_or( -1 )) ? ImGuiTreeNodeFlags_Selected : 0) | ((GetId() == selectedId) ? 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(
...@@ -111,7 +113,6 @@ void Node::ShowTree( std::optional<int>& selectedIndex,Node*& pSelectedNode ) co ...@@ -111,7 +113,6 @@ void Node::ShowTree( std::optional<int>& selectedIndex,Node*& pSelectedNode ) co
// processing for selecting node // processing for selecting node
if( ImGui::IsItemClicked() ) if( ImGui::IsItemClicked() )
{ {
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
...@@ -119,7 +120,7 @@ void Node::ShowTree( std::optional<int>& selectedIndex,Node*& pSelectedNode ) co ...@@ -119,7 +120,7 @@ void Node::ShowTree( std::optional<int>& selectedIndex,Node*& pSelectedNode ) co
{ {
for( const auto& pChild : childPtrs ) for( const auto& pChild : childPtrs )
{ {
pChild->ShowTree( selectedIndex,pSelectedNode ); pChild->ShowTree( pSelectedNode );
} }
ImGui::TreePop(); ImGui::TreePop();
} }
...@@ -149,12 +150,12 @@ public: ...@@ -149,12 +150,12 @@ public:
if( ImGui::Begin( windowName ) ) if( ImGui::Begin( windowName ) )
{ {
ImGui::Columns( 2,nullptr,true ); ImGui::Columns( 2,nullptr,true );
root.ShowTree( selectedIndex,pSelectedNode ); root.ShowTree( pSelectedNode );
ImGui::NextColumn(); ImGui::NextColumn();
if( pSelectedNode != nullptr ) if( pSelectedNode != nullptr )
{ {
auto& transform = transforms[*selectedIndex]; auto& transform = transforms[pSelectedNode->GetId()];
ImGui::Text( "Orientation" ); ImGui::Text( "Orientation" );
ImGui::SliderAngle( "Roll",&transform.roll,-180.0f,180.0f ); ImGui::SliderAngle( "Roll",&transform.roll,-180.0f,180.0f );
ImGui::SliderAngle( "Pitch",&transform.pitch,-180.0f,180.0f ); ImGui::SliderAngle( "Pitch",&transform.pitch,-180.0f,180.0f );
...@@ -169,7 +170,8 @@ public: ...@@ -169,7 +170,8 @@ public:
} }
dx::XMMATRIX GetTransform() const noexcept dx::XMMATRIX GetTransform() const noexcept
{ {
const auto& transform = transforms.at( *selectedIndex ); assert( pSelectedNode != nullptr );
const auto& transform = transforms.at( pSelectedNode->GetId() );
return return
dx::XMMatrixRotationRollPitchYaw( transform.roll,transform.pitch,transform.yaw ) * dx::XMMatrixRotationRollPitchYaw( transform.roll,transform.pitch,transform.yaw ) *
dx::XMMatrixTranslation( transform.x,transform.y,transform.z ); dx::XMMatrixTranslation( transform.x,transform.y,transform.z );
...@@ -179,7 +181,6 @@ public: ...@@ -179,7 +181,6 @@ public:
return pSelectedNode; return pSelectedNode;
} }
private: private:
std::optional<int> selectedIndex;
Node* pSelectedNode; Node* pSelectedNode;
struct TransformParameters struct TransformParameters
{ {
......
...@@ -41,7 +41,7 @@ public: ...@@ -41,7 +41,7 @@ public:
int GetId() const 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( std::optional<int>& selectedIndex,Node*& pSelectedNode ) const noexcept; void ShowTree( Node*& pSelectedNode ) const noexcept;
private: private:
std::string name; std::string name;
int id; int id;
......
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