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

If multiple InputText's are created via loop, whenever I select one when running my app, it deselects it. #4395

Closed
Stanlyhalo opened this issue Aug 4, 2021 · 3 comments
Labels
label/id and id stack implicit identifiers, pushid(), id stack

Comments

@Stanlyhalo
Copy link

Version: 1.83
Branch: docking

My Issue/Question: If multiple InputText's are created via loop, whenever I select one when running my app, it deselects it.

I've been creating a game engine in Vulkan, and the UI with imgui, but as I started to make my entity inspector, it seems when I create input text via looping, whenever I select an input, it deselects it, and if I select the input that was the last one created, it selects all of them and i can edit the text in all of the inputs.

Here's the code of what I have:

for (int i = 0; i < storage.sel_gameEntities.size(); i++) {
	std::string s = std::to_string(storage.sel_gameEntities[i][0]->getId());
	if (ImGui::CollapsingHeader(std::string(std::string(storage.sel_gameEntities[i][0]->name) + "###" + s).c_str(), ImGuiTreeNodeFlags_None))
	{
		ImGui::InputText("Name", storage.sel_gameEntities[i][0]->buf_name, 64);
		storage.sel_gameEntities[i][0]->name = storage.sel_gameEntities[i][0]->buf_name;
	}
}
@Stanlyhalo
Copy link
Author

I seem to have fixed it lol, for those of you wondering, I did this to set I guess the "id" of the input:
image

@ocornut
Copy link
Owner

ocornut commented Aug 4, 2021 via email

@AidanSun05
Copy link

AidanSun05 commented Aug 4, 2021

You can skip the string building ID thing by using PushID(i) and PopID() in your loop and also for your CollapsingHeader.

(As a side note you can get rid of ImGuiTreeNodeFlags_None as this is the default for CollapsingHeader.
And you should put storage.sel_gameEntities[i][0] in a variable in your loop, that'll remove a lot of repetition.)

// NOTE: This code is untested but you should get my main idea here.

for (int i = 0; i < storage.sel_gameEntities.size(); i++) {
        auto* current = storage.sel_gameEntities[i][0];
	ImGui::PushID(current->getId()); // This works if getId() returns int/string/char*
	if (ImGui::CollapsingHeader(current->name)) {
                ImGui::PushID(i);
		ImGui::InputText("Name", current->buf_name, 64);
                ImGui::PopID();
		current->name = current->buf_name;
	}
        ImGui::PopID();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
label/id and id stack implicit identifiers, pushid(), id stack
Projects
None yet
Development

No branches or pull requests

3 participants