Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename tree nodes #3730

Open
dgregorius opened this issue Jan 17, 2021 · 14 comments
Open

Rename tree nodes #3730

dgregorius opened this issue Jan 17, 2021 · 14 comments
Labels

Comments

@dgregorius
Copy link

dgregorius commented Jan 17, 2021

Version/Branch of Dear ImGui:

Version: 1.79
Branch: Viewport

Back-end/Renderer/Compiler/OS

Back-ends: imgui_impl_opengl3.cpp + imgui_impl_glfw.cpp
Compiler: VS 2019
Operating System: Windows 10

My Issue/Question:

I want to be able to rename the nodes in a tree view. What is the recommend way to do this? Do I need to re-implement the tree using selectables and editable text widgets?

Thanks!

@twaritwaikar
Copy link

You provide the name for the node in Dear ImGui, so just change the string which you pass in?

@dgregorius
Copy link
Author

I mean renaming like in press F2 and type a new name into the widget.

@ocornut
Copy link
Owner

ocornut commented Jan 17, 2021 via email

@dgregorius
Copy link
Author

Great, this makes a lot of sense and sounds easy enough!

@ocornut ocornut added the tree tree nodes label Jan 18, 2021
@dgregorius
Copy link
Author

Ok, this is nearly working. The only thing I am missing is how to correctly align the input text item. How do I get the current text offset of the tree node?

@dgregorius dgregorius reopened this Jan 18, 2021
@ocornut
Copy link
Owner

ocornut commented Jan 18, 2021 via email

@dgregorius
Copy link
Author

dgregorius commented Jan 18, 2021

Found it, thanks! Works great now.

One final question regarding the constant tree node id. Can I just push the transform pointer onto the Id stack and then have the label be ignored for Id hashing. I looked for this in the FAQ, but couldn't find a syntax for this. So it seems I would need to do something like label = name###transform_address for example. What is the preferred way to do this?

@ocornut
Copy link
Owner

ocornut commented Jan 18, 2021 via email

@caxapexac
Copy link

caxapexac commented Dec 26, 2021

@ocornut
I hope you can give an advice, I'm making unity-like hierarchy rename behaviour where name is replaced to input field and vice versa

What I need:

  1. m_nodeRename became not null (any way)
  2. keyboard is automatically focused into appeared inputfield
  3. enter press or lose focus finish editing (make m_nodeRename = nullptr)

What I have:
Strange focus behaviour, input field isn't selectable under treenode

        bool opened = ImGui::TreeNodeEx(node, flags, "");

        // TreeNode logic
        if (ImGui::IsItemClicked())
        {
            m_selectedNode = node;
        }

        // TreeNode render
        ImGui::SameLine();
        if (m_nodeToRename == node)
        {
            std::string buffer = std::string(node->getName());
            if (ImGui::InputText("###rename", buffer.data(), buffer.size(), ImGuiInputTextFlags_AutoSelectAll))
            {
                node->setName(buffer);
            }

            if (ImGui::IsItemFocused())
            {
                // What to do here?
                // ImGui::SetKeyboardFocusHere(); 
            }
            else
            {
                // What to do here?
                // m_nodeToRename = nullptr;
            }
        }
        else
        {
            ImGui::Text("%s", node->getName().c_str());
        }

        // TreeNode children
        if (opened)
        {
            int index = 0;
            node->foreachChildren([=, &index] (Node& child)
            {
                ImGui::PushID(index);
                // etc
                index++;
            });
            ImGui::TreePop();
        }

@caxapexac
Copy link

hierarchy

This is about selection under tree node, as you can see it almost fully covers input field click zone

@caxapexac
Copy link

UPD: solved by added

if (node != m_nodeToRename) flags |= ImGuiTreeNodeFlags_SpanAvailWidth | ImGuiTreeNodeFlags_SpanFullWidth;

So only not renaming nodes are clickable at full width

@Shadowblitz16
Copy link

I get getting different sizes for InputText and Text
image

@ocornut
Copy link
Owner

ocornut commented Dec 28, 2023

InputText and all framed widget use FramePadding.y. You can use PushStyleVar() to set FramePadding.y to 0.

@Shadowblitz16
Copy link

Shadowblitz16 commented Dec 28, 2023

InputText and all framed widget use FramePadding.y. You can use PushStyleVar() to set FramePadding.y to 0.

Thankyou
Is there a way to keep the same framePadding x?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants