From 44eacd0859ed261e1b3fcc05a0ae0d203b5c5c85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Fri, 9 Oct 2015 15:37:22 -0700 Subject: [PATCH] Added ImGui image flags. --- examples/common/imgui/imgui.h | 61 +++++++++++++++++++++---- examples/common/imgui/ocornut_imgui.cpp | 36 ++++++++++----- 2 files changed, 77 insertions(+), 20 deletions(-) diff --git a/examples/common/imgui/imgui.h b/examples/common/imgui/imgui.h index b3e3e7c4..e85b9d3a 100644 --- a/examples/common/imgui/imgui.h +++ b/examples/common/imgui/imgui.h @@ -208,20 +208,65 @@ bool imguiMouseOverArea(); namespace ImGui { +#define IMGUI_FLAGS_NONE UINT16_C(0x0000) +#define IMGUI_FLAGS_ALPHA_BLEND UINT16_C(0x0001) + // Helper function for passing bgfx::TextureHandle to ImGui::Image. - inline void Image(bgfx::TextureHandle _handle, const ImVec2& _size, const ImVec2& _uv0 = ImVec2(0, 0), const ImVec2& _uv1 = ImVec2(1, 1), const ImVec4& _tint_col = ImVec4(1, 1, 1, 1), const ImVec4& _border_col = ImVec4(0, 0, 0, 0) ) + inline void Image(bgfx::TextureHandle _handle + , uint16_t _flags + , const ImVec2& _size + , const ImVec2& _uv0 = ImVec2(0.0f, 0.0f) + , const ImVec2& _uv1 = ImVec2(1.0f, 1.0f) + , const ImVec4& _tintCol = ImVec4(1.0f, 1.0f, 1.0f, 1.0f) + , const ImVec4& _borderCol = ImVec4(0.0f, 0.0f, 0.0f, 0.0f) + ) { - union { bgfx::TextureHandle handle; ImTextureID ptr; } texture; - texture.handle = _handle; - Image(texture.ptr, _size, _uv0, _uv1, _tint_col, _border_col); + union { struct { uint16_t flags; bgfx::TextureHandle handle; } s; ImTextureID ptr; } texture; + texture.s.flags = _flags; + texture.s.handle = _handle; + Image(texture.ptr, _size, _uv0, _uv1, _tintCol, _borderCol); + } + + // Helper function for passing bgfx::TextureHandle to ImGui::Image. + inline void Image(bgfx::TextureHandle _handle + , const ImVec2& _size + , const ImVec2& _uv0 = ImVec2(0.0f, 0.0f) + , const ImVec2& _uv1 = ImVec2(1.0f, 1.0f) + , const ImVec4& _tintCol = ImVec4(1.0f, 1.0f, 1.0f, 1.0f) + , const ImVec4& _borderCol = ImVec4(0.0f, 0.0f, 0.0f, 0.0f) + ) + { + Image(_handle, IMGUI_FLAGS_ALPHA_BLEND, _size, _uv0, _uv1, _tintCol, _borderCol); } // Helper function for passing bgfx::TextureHandle to ImGui::ImageButton. - inline bool ImageButton(bgfx::TextureHandle _handle, const ImVec2& _size, const ImVec2& _uv0 = ImVec2(0,0), const ImVec2& _uv1 = ImVec2(1,1), int _frame_padding = -1, const ImVec4& _bg_col = ImVec4(0,0,0,0), const ImVec4& _tint_col = ImVec4(1,1,1,1)) + inline bool ImageButton(bgfx::TextureHandle _handle + , uint16_t _flags + , const ImVec2& _size + , const ImVec2& _uv0 = ImVec2(0.0f, 0.0f) + , const ImVec2& _uv1 = ImVec2(1.0f, 1.0f) + , int _framePadding = -1 + , const ImVec4& _bgCol = ImVec4(0.0f, 0.0f, 0.0f, 0.0f) + , const ImVec4& _tintCol = ImVec4(1.0f, 1.0f, 1.0f, 1.0f) + ) { - union { bgfx::TextureHandle handle; ImTextureID ptr; } texture; - texture.handle = _handle; - return ImageButton(texture.ptr, _size, _uv0, _uv1, _frame_padding, _bg_col, _tint_col); + union { struct { uint16_t flags; bgfx::TextureHandle handle; } s; ImTextureID ptr; } texture; + texture.s.flags = _flags; + texture.s.handle = _handle; + return ImageButton(texture.ptr, _size, _uv0, _uv1, _framePadding, _bgCol, _tintCol); + } + + // Helper function for passing bgfx::TextureHandle to ImGui::ImageButton. + inline bool ImageButton(bgfx::TextureHandle _handle + , const ImVec2& _size + , const ImVec2& _uv0 = ImVec2(0.0f, 0.0f) + , const ImVec2& _uv1 = ImVec2(1.0f, 1.0f) + , int _framePadding = -1 + , const ImVec4& _bgCol = ImVec4(0.0f, 0.0f, 0.0f, 0.0f) + , const ImVec4& _tintCol = ImVec4(1.0f, 1.0f, 1.0f, 1.0f) + ) + { + return ImageButton(_handle, IMGUI_FLAGS_ALPHA_BLEND, _size, _uv0, _uv1, _framePadding, _bgCol, _tintCol); } } // namespace ImGui diff --git a/examples/common/imgui/ocornut_imgui.cpp b/examples/common/imgui/ocornut_imgui.cpp index 3de530ab..8efddcc6 100644 --- a/examples/common/imgui/ocornut_imgui.cpp +++ b/examples/common/imgui/ocornut_imgui.cpp @@ -320,25 +320,37 @@ struct OcornutImguiContext } else if (0 != cmd->ElemCount) { - bgfx::setState(0 - | BGFX_STATE_RGB_WRITE - | BGFX_STATE_ALPHA_WRITE - | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA) - | BGFX_STATE_MSAA - ); + uint64_t state = 0 + | BGFX_STATE_RGB_WRITE + | BGFX_STATE_ALPHA_WRITE + | BGFX_STATE_MSAA + ; + + bgfx::TextureHandle th = m_texture; + + if (NULL != cmd->TextureId) + { + union { ImTextureID ptr; struct { uint16_t flags; bgfx::TextureHandle handle; } s; } texture = { cmd->TextureId }; + state |= 0 != (IMGUI_FLAGS_ALPHA_BLEND & texture.s.flags) + ? BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA) + : BGFX_STATE_NONE + ; + th = texture.s.handle; + } + else + { + state |= BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA); + } + const uint16_t xx = uint16_t(bx::fmax(cmd->ClipRect.x, 0.0f) ); const uint16_t yy = uint16_t(bx::fmax(cmd->ClipRect.y, 0.0f) ); bgfx::setScissor(xx, yy , uint16_t(bx::fmin(cmd->ClipRect.z, 65535.0f)-xx) , uint16_t(bx::fmin(cmd->ClipRect.w, 65535.0f)-yy) ); - union { void* ptr; bgfx::TextureHandle handle; } texture = { cmd->TextureId }; - - bgfx::setTexture(0, s_tex, 0 != texture.handle.idx - ? texture.handle - : m_texture - ); + bgfx::setState(state); + bgfx::setTexture(0, s_tex, th); bgfx::setVertexBuffer(&tvb, 0, numVertices); bgfx::setIndexBuffer(&tib, offset, cmd->ElemCount); bgfx::submit(m_viewId, m_program);