Skip to content

Instantly share code, notes, and snippets.

@thedmd
Created October 12, 2020 16:54
Show Gist options
  • Save thedmd/c86dbbf6947cd7e71317ff677de4e874 to your computer and use it in GitHub Desktop.
Save thedmd/c86dbbf6947cd7e71317ff677de4e874 to your computer and use it in GitHub Desktop.
#pragma once
#include <imgui.h>
namespace ImEx {
struct ScopedDisableItem
{
ScopedDisableItem(bool disable, float disabledAlpha = 0.5f);
~ScopedDisableItem();
void Release();
private:
bool m_Disable = false;
float m_LastAlpha = 1.0f;
};
} // namespace ImEx {
inline ImEx::ScopedDisableItem::ScopedDisableItem(bool disable, float disabledAlpha)
: m_Disable(disable)
{
if (!m_Disable)
return;
auto wasDisabled = (ImGui::GetCurrentWindow()->DC.ItemFlags & ImGuiItemFlags_Disabled) == ImGuiItemFlags_Disabled;
ImGui::PushItemFlag(ImGuiItemFlags_Disabled, true);
auto& stale = ImGui::GetStyle();
m_LastAlpha = stale.Alpha;
// Don't override alpha if we're already in disabled context.
if (!wasDisabled)
stale.Alpha = disabledAlpha;
}
inline ImEx::ScopedDisableItem::~ScopedDisableItem()
{
Release();
}
inline void ImEx::ScopedDisableItem::Release()
{
if (!m_Disable)
return;
auto& stale = ImGui::GetStyle();
stale.Alpha = m_LastAlpha;
ImGui::PopItemFlag();
m_Disable = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment