From 1a3ca7d8ac7bc163940127ef93602efa8ccfba4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Wed, 11 Nov 2015 16:43:32 -0800 Subject: [PATCH] ImGui docking fixes. --- 3rdparty/ocornut-imgui/imgui.cpp | 1 + 3rdparty/ocornut-imgui/imgui.h | 7 +- 3rdparty/ocornut-imgui/imgui_draw.cpp | 2 + 3rdparty/ocornut-imgui/imgui_wm.cpp | 12 +-- examples/common/imgui/ocornut_imgui.cpp | 137 +++++++++++++----------- examples/common/imgui/scintilla.cpp | 2 +- 6 files changed, 87 insertions(+), 74 deletions(-) diff --git a/3rdparty/ocornut-imgui/imgui.cpp b/3rdparty/ocornut-imgui/imgui.cpp index 284cae644..d4ba56716 100644 --- a/3rdparty/ocornut-imgui/imgui.cpp +++ b/3rdparty/ocornut-imgui/imgui.cpp @@ -4352,6 +4352,7 @@ static float* GetStyleVarFloatAddr(ImGuiStyleVar idx) case ImGuiStyleVar_FrameRounding: return &g.Style.FrameRounding; case ImGuiStyleVar_IndentSpacing: return &g.Style.IndentSpacing; case ImGuiStyleVar_GrabMinSize: return &g.Style.GrabMinSize; + case ImGuiStyleVar_ViewId: return &g.Style.ViewId; } return NULL; } diff --git a/3rdparty/ocornut-imgui/imgui.h b/3rdparty/ocornut-imgui/imgui.h index d2c4155d9..032e0cf79 100644 --- a/3rdparty/ocornut-imgui/imgui.h +++ b/3rdparty/ocornut-imgui/imgui.h @@ -591,7 +591,8 @@ enum ImGuiStyleVar_ ImGuiStyleVar_ItemSpacing, // ImVec2 ImGuiStyleVar_ItemInnerSpacing, // ImVec2 ImGuiStyleVar_IndentSpacing, // float - ImGuiStyleVar_GrabMinSize // float + ImGuiStyleVar_GrabMinSize, // float + ImGuiStyleVar_ViewId // uint8_t }; enum ImGuiAlign_ @@ -656,6 +657,7 @@ struct ImGuiStyle float ScrollbarSize; // Width of the vertical scrollbar, Height of the horizontal scrollbar float ScrollbarRounding; // Radius of grab corners for scrollbar float GrabMinSize; // Minimum width/height of a grab box for slider/scrollbar + float ViewId; float GrabRounding; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs. ImVec2 DisplayWindowPadding; // Window positions are clamped to be visible within the display area by at least this amount. Only covers regular windows. ImVec2 DisplaySafeAreaPadding; // If you cannot see the edge of your screen (e.g. on a TV) increase the safe area padding. Covers popups/tooltips as well regular windows. @@ -1037,8 +1039,9 @@ struct ImDrawCmd ImTextureID TextureId; // User-provided texture ID. Set by user in ImfontAtlas::SetTexID() for fonts or passed to Image*() functions. Ignore if never using images or multiple fonts atlas. ImDrawCallback UserCallback; // If != NULL, call the function instead of rendering the vertices. clip_rect and texture_id will be set normally. void* UserCallbackData; // The draw callback code can access this. + unsigned char ViewId; - ImDrawCmd() { ElemCount = 0; ClipRect.x = ClipRect.y = -8192.0f; ClipRect.z = ClipRect.w = +8192.0f; TextureId = NULL; UserCallback = NULL; UserCallbackData = NULL; } + ImDrawCmd() { ElemCount = 0; ClipRect.x = ClipRect.y = -8192.0f; ClipRect.z = ClipRect.w = +8192.0f; TextureId = NULL; UserCallback = NULL; UserCallbackData = NULL; ViewId = 0; } }; // Vertex index (override with, e.g. '#define ImDrawIdx unsigned int' in ImConfig) diff --git a/3rdparty/ocornut-imgui/imgui_draw.cpp b/3rdparty/ocornut-imgui/imgui_draw.cpp index 5baae3117..db9c7fb65 100644 --- a/3rdparty/ocornut-imgui/imgui_draw.cpp +++ b/3rdparty/ocornut-imgui/imgui_draw.cpp @@ -139,6 +139,7 @@ void ImDrawList::AddDrawCmd() ImDrawCmd draw_cmd; draw_cmd.ClipRect = _ClipRectStack.Size ? _ClipRectStack.back() : GNullClipRect; draw_cmd.TextureId = _TextureIdStack.Size ? _TextureIdStack.back() : NULL; + draw_cmd.ViewId = (unsigned char)(GImGui->Style.ViewId); IM_ASSERT(draw_cmd.ClipRect.x <= draw_cmd.ClipRect.z && draw_cmd.ClipRect.y <= draw_cmd.ClipRect.w); CmdBuffer.push_back(draw_cmd); @@ -250,6 +251,7 @@ void ImDrawList::ChannelsSplit(int channels_count) ImDrawCmd draw_cmd; draw_cmd.ClipRect = _ClipRectStack.back(); draw_cmd.TextureId = _TextureIdStack.back(); + draw_cmd.ViewId = (unsigned char)(GImGui->Style.ViewId); _Channels[i].CmdBuffer.push_back(draw_cmd); } } diff --git a/3rdparty/ocornut-imgui/imgui_wm.cpp b/3rdparty/ocornut-imgui/imgui_wm.cpp index abb51c183..0d05485d3 100644 --- a/3rdparty/ocornut-imgui/imgui_wm.cpp +++ b/3rdparty/ocornut-imgui/imgui_wm.cpp @@ -16,7 +16,7 @@ #include #define IMGUI_NEW(type) new (ImGui::MemAlloc(sizeof(type) ) ) type -#define IMGUI_DELETE(type, obj) reinterpret_cast(obj)->~type(), ImGui::MemFree(obj) +#define IMGUI_DELETE(type, obj) static_cast(obj)->~type(), ImGui::MemFree(obj) #define IMGUI_DELETE_NULL(type, obj) for (;;) { if (NULL != obj) { IMGUI_DELETE(type, obj); obj = NULL; } break; } namespace ImGuiWM @@ -768,7 +768,6 @@ namespace ImGuiWM //ImGui::PushStyleColor(ImGuiCol_ChildWindowBg, ImColor(59, 59, 59, 255)); ImGui::BeginChild((*itActiveWindow)->GetId(), ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar); - ImVec2 oWinPos = ImGui::GetWindowPos(); ImVec2 oWinSize = ImGui::GetWindowSize(); @@ -935,7 +934,7 @@ namespace ImGuiWM { ImGui::GetIO().Fonts = NULL; } - ImGui::Shutdown(); + RestoreState(); ImGui::MemFree(m_pState); } @@ -977,7 +976,9 @@ namespace ImGuiWM void PlatformWindow::Paint() { + PaintBegin(); WindowManager::GetInstance()->Paint(this); + PaintEnd(); } bool PlatformWindow::IsMain() @@ -1343,7 +1344,7 @@ namespace ImGuiWM m_lOrphanWindows.remove(pAction->m_pWindow); - IMGUI_DELETE(PlatformWindowAction, pAction); + IMGUI_DELETE(DockAction, pAction); m_lDockActions.erase(m_lDockActions.begin()); } } @@ -1385,13 +1386,10 @@ namespace ImGuiWM } ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(5.0f, 5.0f)); - char name[64]; ImFormatString(name, sizeof(name), "Window %p", pWindow); ImGui::Begin(name, NULL, iFlags); - pWindow->PaintBegin(); pWindow->PaintContainer(); - pWindow->PaintEnd(); ImGui::End(); ImGui::PopStyleVar(1); diff --git a/examples/common/imgui/ocornut_imgui.cpp b/examples/common/imgui/ocornut_imgui.cpp index 1aee45039..f685ac59b 100644 --- a/examples/common/imgui/ocornut_imgui.cpp +++ b/examples/common/imgui/ocornut_imgui.cpp @@ -14,7 +14,11 @@ #include #ifndef USE_ENTRY -# define USE_ENTRY defined(SCI_NAMESPACE) +# if defined(SCI_NAMESPACE) +# define USE_ENTRY 1 +# else +# define USE_ENTRY 0 +# endif // defined(SCI_NAMESPACE) #endif // USE_ENTRY #if USE_ENTRY @@ -29,8 +33,6 @@ #include "vs_ocornut_imgui.bin.h" #include "fs_ocornut_imgui.bin.h" -void viewCallback(const ImDrawList* _parentList, const ImDrawCmd* _cmd); - class PlatformWindow : public ImGuiWM::PlatformWindow { typedef ImGuiWM::PlatformWindow Super; @@ -47,7 +49,7 @@ public: && !_isDragWindow) { m_window = entry::createWindow(0, 0, 640, 380); - extern void pwToWindow(entry::WindowHandle _handle, PlatformWindow* _pw); + extern void pwToWindow(entry::WindowHandle _handle, class PlatformWindow* _pw); pwToWindow(m_window, this); } else @@ -59,6 +61,12 @@ public: virtual ~PlatformWindow() { +#if USE_ENTRY + if (0 != m_window.idx) + { + entry::destroyWindow(m_window); + } +#endif // USE_ENTRY } virtual bool Init(ImGuiWM::PlatformWindow* /*_parent*/) BX_OVERRIDE @@ -100,7 +108,6 @@ public: virtual void SetPosition(const ImVec2& _pos) BX_OVERRIDE { - #if USE_ENTRY if (0 != m_window.idx && m_pos.x != _pos.x @@ -126,38 +133,9 @@ public: { } - virtual void PaintBegin() - { -#if USE_ENTRY - if (!m_bIsDragWindow) - { - ImDrawList* drawList = ImGui::GetWindowDrawList(); - union { entry::WindowHandle handle; void* ptr; } cast = { m_window }; - drawList->AddCallback(viewCallback, cast.ptr); - drawList->PushClipRect(ImVec4(0.0f, 0.0f, m_size.x, m_size.y) ); - } -#endif // USE_ENTRY - } - - virtual void Paint() BX_OVERRIDE - { - if (!m_bIsDragWindow) - { - Super::Paint(); - } - } - - virtual void PaintEnd() - { -#if USE_ENTRY - if (!m_bIsDragWindow) - { - ImDrawList* drawList = ImGui::GetWindowDrawList(); - drawList->PopClipRect(); - drawList->AddCallback(viewCallback, NULL); - } -#endif // USE_ENTRY - } + virtual void PaintBegin() BX_OVERRIDE; + virtual void Paint() BX_OVERRIDE; + virtual void PaintEnd() BX_OVERRIDE; virtual void Destroy() BX_OVERRIDE { @@ -257,7 +235,7 @@ struct OcornutImguiContext const uint8_t viewId = window.m_viewId; bgfx::setViewClear(viewId , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH - , 0x303030ff + rand() + , 0x303030ff , 1.0f , 0 ); @@ -353,7 +331,7 @@ struct OcornutImguiContext bgfx::setTexture(0, s_tex, th); bgfx::setVertexBuffer(&tvb, 0, numVertices); bgfx::setIndexBuffer(&tib, offset, cmd->ElemCount); - bgfx::submit(m_viewId, m_program); + bgfx::submit(cmd->ViewId, m_program); } offset += cmd->ElemCount; @@ -443,9 +421,11 @@ struct OcornutImguiContext uint8_t* data; int32_t width; int32_t height; - void* font = ImGui::MemAlloc(_size); - memcpy(font, _data, _size); - io.Fonts->AddFontFromMemoryTTF(font, _size, _fontSize); + { + void* font = ImGui::MemAlloc(_size); + memcpy(font, _data, _size); + io.Fonts->AddFontFromMemoryTTF(font, _size, _fontSize); + } io.Fonts->GetTexDataAsRGBA32(&data, &width, &height); @@ -498,18 +478,16 @@ struct OcornutImguiContext }; Window* w0 = new Window("test"); - WindowX* w1 = new WindowX("abcd"); + WindowX* w1 = new WindowX("Scintilla"); Window* w2 = new Window("xyzw"); Window* w3 = new Window("0123"); - m_wm->Dock(w0); - m_wm->DockWith(w1, w0, ImGuiWM::E_DOCK_ORIENTATION_RIGHT); - m_wm->DockWith(w2, w1, ImGuiWM::E_DOCK_ORIENTATION_BOTTOM); - m_wm->DockWith(w3, w0, ImGuiWM::E_DOCK_ORIENTATION_BOTTOM); + m_wm->Dock(w0); + m_wm->DockWith(w1, w0, ImGuiWM::E_DOCK_ORIENTATION_RIGHT); + m_wm->DockWith(w2, w1, ImGuiWM::E_DOCK_ORIENTATION_BOTTOM); + m_wm->DockWith(w3, w0, ImGuiWM::E_DOCK_ORIENTATION_BOTTOM); } #endif // 0 - - ImGui::NewFrame(); } void destroy() @@ -527,8 +505,7 @@ struct OcornutImguiContext void beginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, int _width, int _height, char _inputChar, uint8_t _viewId) { - m_viewId = _viewId; - m_defaultViewId = _viewId; + m_viewId = _viewId; ImGuiIO& io = ImGui::GetIO(); if (_inputChar < 0x7f) @@ -563,6 +540,7 @@ struct OcornutImguiContext #endif // defined(SCI_NAMESPACE) ImGui::NewFrame(); + ImGui::PushStyleVar(ImGuiStyleVar_ViewId, (float)_viewId); #if 0 ImGui::ShowTestWindow(); //Debug only. @@ -578,6 +556,7 @@ struct OcornutImguiContext void endFrame() { m_wm->Run(); + ImGui::PopStyleVar(1); ImGui::Render(); } @@ -590,7 +569,6 @@ struct OcornutImguiContext int64_t m_last; int32_t m_lastScroll; uint8_t m_viewId; - uint8_t m_defaultViewId; #if USE_ENTRY struct Window @@ -612,22 +590,53 @@ struct OcornutImguiContext static OcornutImguiContext s_ctx; -#if USE_ENTRY - -void viewCallback(const ImDrawList* /*_parentList*/, const ImDrawCmd* _cmd) +void PlatformWindow::PaintBegin() { - union { void* ptr; entry::WindowHandle handle; } cast = { _cmd->UserCallbackData }; +#if USE_ENTRY + if (!m_bIsDragWindow) + { + OcornutImguiContext::Window& win = s_ctx.m_window[m_window.idx]; + entry::WindowState& state = win.m_state; + ImGuiIO& io = ImGui::GetIO(); + io.MousePos = ImVec2((float)state.m_mouse.m_mx, (float)state.m_mouse.m_my); + io.MouseDown[0] = !!state.m_mouse.m_buttons[entry::MouseButton::Left]; + io.MouseDown[1] = !!state.m_mouse.m_buttons[entry::MouseButton::Right]; + io.MouseDown[2] = !!state.m_mouse.m_buttons[entry::MouseButton::Middle]; + io.MouseWheel = float(state.m_mouse.m_mz); - if (0 != cast.handle.idx) - { - s_ctx.m_viewId = s_ctx.m_window[cast.handle.idx].m_viewId; + ImGui::PushStyleVar(ImGuiStyleVar_ViewId, (float)win.m_viewId); } - else +#endif // USE_ENTRY +} + +void PlatformWindow::Paint() +{ + if (!m_bIsDragWindow) { - s_ctx.m_viewId = s_ctx.m_defaultViewId; + Super::Paint(); } } +void PlatformWindow::PaintEnd() +{ +#if USE_ENTRY + if (!m_bIsDragWindow) + { + ImGui::PopStyleVar(1); + + entry::WindowState& state = s_ctx.m_window[0].m_state; + ImGuiIO& io = ImGui::GetIO(); + io.MousePos = ImVec2((float)state.m_mouse.m_mx, (float)state.m_mouse.m_my); + io.MouseDown[0] = !!state.m_mouse.m_buttons[entry::MouseButton::Left]; + io.MouseDown[1] = !!state.m_mouse.m_buttons[entry::MouseButton::Right]; + io.MouseDown[2] = !!state.m_mouse.m_buttons[entry::MouseButton::Middle]; + io.MouseWheel = float(state.m_mouse.m_mz); + } +#endif // USE_ENTRY +} + +#if USE_ENTRY + void pwToWindow(entry::WindowHandle _handle, PlatformWindow* _pw) { s_ctx.m_window[_handle.idx].m_pw = _pw; @@ -656,7 +665,7 @@ void imguiUpdateWindow(const entry::WindowState& _state) } else { - window.m_viewId = s_ctx.m_defaultViewId; + window.m_viewId = s_ctx.m_viewId; } } @@ -674,9 +683,9 @@ void OcornutImguiContext::memFree(void* _ptr) BX_FREE(s_ctx.m_allocator, _ptr); } -void OcornutImguiContext::renderDrawLists(ImDrawData* draw_data) +void OcornutImguiContext::renderDrawLists(ImDrawData* _drawData) { - s_ctx.render(draw_data); + s_ctx.render(_drawData); } void IMGUI_create(const void* _data, uint32_t _size, float _fontSize, bx::AllocatorI* _allocator) diff --git a/examples/common/imgui/scintilla.cpp b/examples/common/imgui/scintilla.cpp index f9f0d06ea..1e5e5f89b 100644 --- a/examples/common/imgui/scintilla.cpp +++ b/examples/common/imgui/scintilla.cpp @@ -1101,7 +1101,7 @@ namespace Scintilla } // namespace Scintilla -ScintillaEditor* ImGuiScintilla(const char* _name, bool* _opened, const ImVec2& _size) +ScintillaEditor* ImGuiScintilla(const char* _name, bool* /*_opened*/, const ImVec2& /*_size*/) { ScintillaEditor* sci = NULL;