完整代码:https://github.com/DXT00/LearnOpenGL_study/tree/1977d59c505868f63f718cb7d8d22c3f5176cd69
ImGui selectable 菜单:
const char* entityItems[] = { "Empty","PointLight","SpotLight","IronMan","Cube" };
static const char* currentItem = nullptr;
if (ImGui::BeginCombo("CreateEntity", currentItem)) {
for (int n = 0; n < IM_ARRAYSIZE(entityItems); n++)
{
bool is_selected = (currentItem == entityItems[n]); // You can store your selection however you want, outside or inside your objects
if (ImGui::Selectable(entityItems[n], is_selected)) { //ImGui::Selectable()显示可选择的选项
currentItem = entityItems[n];
if (currentItem != nullptr) {
std::string debug_currentItem(currentItem);
std::cout << debug_currentItem << std::endl;
}
}
}
ImGui::EndCombo();
}
注意:ImGui的所有用法在imgui_demo.cpp和imgui_draw.cpp中
通过ImGui控制Object 生成:
void OnImguiRender() override {
ImGui::Begin("Settings");
ImGui::ColorEdit3("Suqare Color", glm::value_ptr(m_BackgroundColor));
ImGui::End();
if (ImGui::CollapsingHeader("Create")) {
const char* const entityItems[] = { "Empty","PointLight","SpotLight","IronMan","Cube" };
static int entityIdx = -1;
if (ImGui::Combo("CreateEntity", &entityIdx, entityItems, 5))
{
switch (entityIdx)
{
case 0:
GE_CORE_INFO("Creating Empty...");
Layer::CreateEmpty();
break;
case 1:
GE_CORE_INFO("Creating PointLight...");
Layer::CreateLight(BlackPearl::LightType::PointLight);
break;
case 2:
GE_CORE_INFO("SpotLight");
Layer::CreateLight(BlackPearl::LightType::SpotLight);
break;
case 3:
GE_CORE_INFO("IronMan");
Layer::CreateModel();
break;
case 4:
GE_CORE_INFO("Cube");
Layer::CreateCube();
break;
}
}
}
if (ImGui::CollapsingHeader("Scene")) {
}
}
dynamically add items with listbox
参考:https://github.com/ocornut/imgui/issues/2259
ImGuiRender:获取现有的Object列表,并放到ListBox上
void OnImguiRender() override {
ImGui::Begin("Settings");
ImGui::ColorEdit3("Suqare Color", glm::value_ptr(m_BackgroundColor));
ImGui::End();
if (ImGui::CollapsingHeader("Create")) {
const char* const entityItems[] = { "Empty","PointLight","SpotLight","IronMan","Cube" };
static int entityIdx = -1;
if (ImGui::Combo("CreateEntity", &entityIdx, entityItems, 5))
{
switch (entityIdx)
{
case 0:
GE_CORE_INFO("Creating Empty...");
Layer::CreateEmpty();
break;
case 1:
GE_CORE_INFO("Creating PointLight...");
Layer::CreateLight(BlackPearl::LightType::PointLight);
break;
case 2:
GE_CORE_INFO("Creating SpotLight ...");
Layer::CreateLight(BlackPearl::LightType::SpotLight);
break;
case 3:
GE_CORE_INFO("Creating IronMan ...");
Layer::CreateModel();
break;
case 4:
GE_CORE_INFO("Creating Cube ...");
Layer::CreateCube();
break;
}
}
}
if (ImGui::CollapsingHeader("Scene")) {
std::vector<std::string> objsList = GetObjectsName();
ImGui::ListBoxHeader("CurrentEntities", (int)objsList.size(), 6);
for (int n = 0; n < objsList.size(); n++)
ImGui::Text("%s", objsList[n].c_str());
ImGui::ListBoxFooter();
}
}