TheCherno / Hazel

Hazel Engine
Apache License 2.0
11.73k stars 1.51k forks source link

Plus button doesn't work. #418

Open IceLuna opened 3 years ago

IceLuna commented 3 years ago

Clicking on "+" button does nothing except for Transform Component that displays "Remove component" 3 times.

To Reproduce: 1) Create empty entity; 2) Add camera and sprite components; 3) Close all of the tree nodes. image

How to fix: ------------const std::string typeName(typeid(T).name()); ------------const std::string imguiPopupID = std::string("ComponentSettings") + typeName; ------------ImGui::PushID(imguiPopupID.c_str()); Add these 3 lines of code inside SceneHierarchyPanel::DrawComponent right after the first if-branch and before calling uiFunction call ImGui::PopID(); image

OverShifted commented 3 years ago

std::string allocates memory on the heap. Doing so is not recommended every frame. Isn't it better to use sprintf or some other way of formatting instead of using + operators on std::string?

Like this piece of code below:

static char imguiPopupID[64];
// sprintf_s or snprintf but not sprintf because it is not safe
sprintf_s(imguiPopupID, 64, "ComponentSettings%s", typeid(T).name())
ImGui::PushID(imguiPopupID);

// ...

ImGui::PopID();

You can also do it with macros: (more efficient)

#define IMGUI_POPUP_ID(_T) ("ComponentSettings" #_T)

ImGui::PushID(IMGUI_POPUP_ID(T));

// ...

ImGui::PopID();
IceLuna commented 3 years ago

@OverShifted Feel free creating a pull request