Skip to content

Commit

Permalink
add imgui enum property util (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
T-rvw authored Sep 23, 2023
1 parent 9566c07 commit aa30b0e
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 59 deletions.
10 changes: 5 additions & 5 deletions Engine/Source/Editor/UILayers/AssetBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ void AssetBrowser::UpdateAssetFolderTree()
//m_pImportFileBrowser->SetTypeFilters({ ".dds", "*.exr", "*.hdr", "*.ktx", ".tga" });
m_pImportFileBrowser->Open();

CD_INFO("Import asset type: {}", GetIOAssetTypeName(m_importOptions.AssetType));
CD_INFO("Import asset type: {}", nameof::nameof_enum(m_importOptions.AssetType));
}

else if (ImGui::Selectable("Shader"))
Expand All @@ -358,7 +358,7 @@ void AssetBrowser::UpdateAssetFolderTree()
//m_pImportFileBrowser->SetTypeFilters({ ".sc" }); // ".hlsl"
m_pImportFileBrowser->Open();

CD_INFO("Import asset type: {}", GetIOAssetTypeName(m_importOptions.AssetType));
CD_INFO("Import asset type: {}", nameof::nameof_enum(m_importOptions.AssetType));
}
else if (ImGui::Selectable("Model"))
{
Expand All @@ -367,7 +367,7 @@ void AssetBrowser::UpdateAssetFolderTree()
//m_pImportFileBrowser->SetTypeFilters({ ".fbx", ".gltf" }); // ".obj", ".dae", ".ogex"
m_pImportFileBrowser->Open();

CD_INFO("Import asset type: {}", GetIOAssetTypeName(m_importOptions.AssetType));
CD_INFO("Import asset type: {}", nameof::nameof_enum(m_importOptions.AssetType));
}

#ifdef ENABLE_DDGI
Expand Down Expand Up @@ -402,7 +402,7 @@ void AssetBrowser::UpdateAssetFolderTree()
m_pExportFileBrowser->SetTitle("ExportAssets - SceneDatabase");
m_pExportFileBrowser->Open();

CD_INFO("Export asset type: {}", GetIOAssetTypeName(m_exportOptions.AssetType));
CD_INFO("Export asset type: {}", nameof::nameof_enum(m_exportOptions.AssetType));
}

ImGui::EndPopup();
Expand Down Expand Up @@ -875,7 +875,7 @@ void AssetBrowser::ProcessSceneDatabase(cd::SceneDatabase* pSceneDatabase, bool
pSceneDatabase->GetTextures().clear();
for (auto& material : pSceneDatabase->GetMaterials())
{
for (int textureTypeIndex = 0; textureTypeIndex < static_cast<int>(cd::MaterialTextureType::Count); ++textureTypeIndex)
for (int textureTypeIndex = 0; textureTypeIndex < nameof::enum_count<cd::MaterialTextureType>(); ++textureTypeIndex)
{
material.RemoveTexture(static_cast<cd::MaterialTextureType>(textureTypeIndex));
}
Expand Down
22 changes: 0 additions & 22 deletions Engine/Source/Editor/UILayers/AssetBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,8 @@ enum class IOAssetType
Terrain,
Light,
Unknown,

Count,
};

constexpr const char *IOAssetTypeName[] =
{
"CubeMap",
"DDGIModel",
"Model",
"Shader",
"SceneDatabase",
"Terrain",
"Light",
"Unknown",
};

static_assert(static_cast<int>(IOAssetType::Count) == sizeof(IOAssetTypeName) / sizeof(char*),
"IO asset type and names mismatch.");

CD_FORCEINLINE const char* GetIOAssetTypeName(IOAssetType type)
{
return IOAssetTypeName[static_cast<size_t>(type)];
}

struct AssetImportOptions
{
IOAssetType AssetType = IOAssetType::Unknown;
Expand Down
26 changes: 6 additions & 20 deletions Engine/Source/Editor/UILayers/Inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ void UpdateComponentWidget<engine::MaterialComponent>(engine::SceneWorld* pScene
{
// TODO : generic cull mode.
ImGuiUtils::ImGuiBoolProperty("TwoSided", pMaterialComponent->GetTwoSided());
ImGuiUtils::ImGuiStringProperty("BlendMode", nameof::nameof_enum(pMaterialComponent->GetBlendMode()).data());
ImGuiUtils::ImGuiEnumProperty("BlendMode", pMaterialComponent->GetBlendMode());
}

ImGui::Separator();
Expand All @@ -200,7 +200,7 @@ void UpdateComponentWidget<engine::MaterialComponent>(engine::SceneWorld* pScene
}

// Textures
for (int textureTypeValue = 0; textureTypeValue < static_cast<int>(cd::MaterialTextureType::Count); ++textureTypeValue)
for (int textureTypeValue = 0; textureTypeValue < nameof::enum_count<cd::MaterialTextureType>(); ++textureTypeValue)
{
auto textureType = static_cast<cd::MaterialTextureType>(textureTypeValue);
bool allowNoTextures = textureType == cd::MaterialTextureType::BaseColor ||
Expand Down Expand Up @@ -386,7 +386,7 @@ void UpdateComponentWidget<engine::LightComponent>(engine::SceneWorld* pSceneWor
if (isOpen)
{
cd::LightType lightType = pLightComponent->GetType();
std::string lightTypeName = cd::GetLightTypeName(lightType);
std::string lightTypeName(nameof::nameof_enum(lightType));

ImGuiUtils::ImGuiStringProperty("Type", lightTypeName);
ImGuiUtils::ColorPickerProperty("Color", pLightComponent->GetColor());
Expand Down Expand Up @@ -559,25 +559,11 @@ void UpdateComponentWidget<engine::SkyComponent>(engine::SceneWorld* pSceneWorld

if (!skyTypes.empty())
{
static const char* crtItem = nameof::nameof_enum(engine::SkyType::SkyBox).data();
if (ImGui::BeginCombo("##combo", crtItem))
auto currentSkyType = pSkyComponent->GetSkyType();
if (ImGuiUtils::ImGuiEnumProperty("SkyType", currentSkyType))
{
for (size_t index = 0; index < skyTypes.size(); ++index)
{
bool isSelected = (crtItem == skyTypes[index]);
if (ImGui::Selectable(skyTypes[index], isSelected))
{
crtItem = skyTypes[index];
pSkyComponent->SetSkyType(static_cast<engine::SkyType>(index));
}
if (isSelected)
{
ImGui::SetItemDefaultFocus();
}
}
ImGui::EndCombo();
pSkyComponent->SetSkyType(currentSkyType);
}

}

if (pSkyComponent->GetAtmophericScatteringEnable())
Expand Down
10 changes: 4 additions & 6 deletions Engine/Source/Editor/UILayers/MainMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,9 @@ void MainMenu::EditMenu()

if (ImGui::BeginMenu(CD_TEXT("TEXT_STYLE")))
{
// It is not convenient in C++ to loop enum except define an extra array to wrap them.
// C++ 20/23 ranges may look better but still needs std::iota inside its implementation.
for (engine::ThemeColor theme = engine::ThemeColor::Black; theme < engine::ThemeColor::Count;
theme = static_cast<engine::ThemeColor>(static_cast<int>(theme) + 1))
for (uint32_t index = 0U; index < nameof::enum_count<engine::ThemeColor>(); ++index)
{
engine::ThemeColor theme = static_cast<engine::ThemeColor>(index);
engine::ImGuiContextInstance* pImGuiContextInstance = GetImGuiContextInstance();
if (ImGui::MenuItem(nameof::nameof_enum(theme).data(), "", pImGuiContextInstance->GetImGuiThemeColor() == theme))
{
Expand All @@ -101,9 +99,9 @@ void MainMenu::EditMenu()

if (ImGui::BeginMenu(CD_TEXT("TEXT_LANGUAGE")))
{
for (engine::Language language = engine::Language::ChineseSimplied; language < engine::Language::Count;
language = static_cast<engine::Language>(static_cast<int>(language) + 1))
for (uint32_t index = 0U; index < nameof::enum_count<engine::Language>(); ++index)
{
engine::Language language = static_cast<engine::Language>(index);
engine::ImGuiContextInstance* pImGuiContextInstance = GetImGuiContextInstance();
if (ImGui::MenuItem(nameof::nameof_enum(language).data(), "", pImGuiContextInstance->GetImGuiLanguage() == language))
{
Expand Down
2 changes: 1 addition & 1 deletion Engine/Source/Runtime/ECWorld/SceneWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ void SceneWorld::AddMaterialToSceneDatabase(engine::Entity entity)
pMaterialData->SetFloatProperty(cd::MaterialPropertyGroup::Roughness, cd::MaterialProperty::Factor, pMaterialComponent->GetRoughnessFactor());
pMaterialData->SetBoolProperty(cd::MaterialPropertyGroup::General, cd::MaterialProperty::TwoSided, pMaterialComponent->GetTwoSided());

for (int textureTypeValue = 0; textureTypeValue <static_cast<int>(cd::MaterialTextureType::Count); ++textureTypeValue)
for (int textureTypeValue = 0; textureTypeValue < nameof::enum_count<cd::MaterialTextureType>(); ++textureTypeValue)
{
if (MaterialComponent::TextureInfo* textureInfo = pMaterialComponent->GetTextureInfo(static_cast<cd::MaterialPropertyGroup>(textureTypeValue)))
{
Expand Down
39 changes: 39 additions & 0 deletions Engine/Source/Runtime/ImGui/ImGuiUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "Math/UnitSystem.hpp"

#include <imgui/imgui.h>

namespace ImGuiUtils
{

Expand All @@ -15,6 +16,44 @@ static bool ImGuiBoolProperty(const char* pName, bool& value)
return ImGui::Checkbox(pName, &value);
}

template<typename EnumType>
static bool ImGuiEnumProperty(const char* pName, EnumType& value)
{
bool dirty = false;

ImGui::Columns(2);
ImGui::TextUnformatted(pName);
ImGui::NextColumn();
ImGui::PushItemWidth(-1);

if (ImGui::BeginCombo("##combo", nameof::nameof_enum(value).data()))
{
auto enumCount = nameof::enum_count<EnumType>();
for (uint32_t enumIndex = 0U; enumIndex < enumCount; ++enumIndex)
{
EnumType enumValue = static_cast<EnumType>(enumIndex);
bool isSelected = enumValue == value;
if (ImGui::Selectable(nameof::nameof_enum(enumValue).data(), isSelected))
{
value = enumValue;
dirty = true;
}

if (isSelected)
{
ImGui::SetItemDefaultFocus();
}
}
ImGui::EndCombo();
}

ImGui::PopItemWidth();
ImGui::NextColumn();
ImGui::Columns(1);

return dirty;
}

static bool ImGuiStringProperty(const char* pName, const char* pValue)
{
ImGui::Columns(2);
Expand Down
3 changes: 1 addition & 2 deletions Engine/Source/Runtime/ImGui/Language.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ enum class Language
Japanese,
Korean,
Thai,
Vitnam,
Count,
Vitnam
};

}
3 changes: 1 addition & 2 deletions Engine/Source/Runtime/ImGui/ThemeColor.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ enum class ThemeColor
Classic,
Dark,
Grey,
Light,
Count
Light
};

}

0 comments on commit aa30b0e

Please sign in to comment.