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
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
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);
// render this node
const auto expanded = ImGui::TreeNodeEx(
......@@ -111,7 +113,6 @@ void Node::ShowTree( std::optional<int>& selectedIndex,Node*& pSelectedNode ) co
// processing for selecting node
if( ImGui::IsItemClicked() )
{
selectedIndex = GetId();
pSelectedNode = const_cast<Node*>(this);
}
// recursive rendering of open node's children
......@@ -119,7 +120,7 @@ void Node::ShowTree( std::optional<int>& selectedIndex,Node*& pSelectedNode ) co
{
for( const auto& pChild : childPtrs )
{
pChild->ShowTree( selectedIndex,pSelectedNode );
pChild->ShowTree( pSelectedNode );
}
ImGui::TreePop();
}
......@@ -149,12 +150,12 @@ public:
if( ImGui::Begin( windowName ) )
{
ImGui::Columns( 2,nullptr,true );
root.ShowTree( selectedIndex,pSelectedNode );
root.ShowTree( pSelectedNode );
ImGui::NextColumn();
if( pSelectedNode != nullptr )
{
auto& transform = transforms[*selectedIndex];
auto& transform = transforms[pSelectedNode->GetId()];
ImGui::Text( "Orientation" );
ImGui::SliderAngle( "Roll",&transform.roll,-180.0f,180.0f );
ImGui::SliderAngle( "Pitch",&transform.pitch,-180.0f,180.0f );
......@@ -169,7 +170,8 @@ public:
}
dx::XMMATRIX GetTransform() const noexcept
{
const auto& transform = transforms.at( *selectedIndex );
assert( pSelectedNode != nullptr );
const auto& transform = transforms.at( pSelectedNode->GetId() );
return
dx::XMMatrixRotationRollPitchYaw( transform.roll,transform.pitch,transform.yaw ) *
dx::XMMatrixTranslation( transform.x,transform.y,transform.z );
......@@ -179,7 +181,6 @@ public:
return pSelectedNode;
}
private:
std::optional<int> selectedIndex;
Node* pSelectedNode;
struct TransformParameters
{
......
......@@ -41,7 +41,7 @@ public:
int GetId() const noexcept;
private:
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:
std::string name;
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