Viewport: Changelog + Internals: Renamed viewport mouse fields for consistency (going to add more). (#1542)

This commit is contained in:
omar 2018-03-19 22:53:37 +01:00
parent c00523dba4
commit ccc9a22db3
3 changed files with 62 additions and 31 deletions

View File

@ -43,6 +43,37 @@ The gamepad/keyboard navigation branch (which has been in the work since July 20
Gamepad/keyboard navigation is still marked as Beta and has to be enabled explicitely. Gamepad/keyboard navigation is still marked as Beta and has to be enabled explicitely.
Various internal refactors have also been done, as part of the navigation work and as part of the upcoing viewport/docking work. Various internal refactors have also been done, as part of the navigation work and as part of the upcoing viewport/docking work.
VIEWPORT BRANCH
(IN PROGRESS, WILL MERGE INTO THE MAIN LISTS WHEN WE MERGE THE BRANCH)
- Viewport: Added support for multi-viewport [...] blah blah
- Viewport: Rendering: the ImDrawData structure now contains 'DisplayPos' and 'DisplaySize' fields. To support multi-viewport, you need to use those values when
creating your orthographic projection matrix. Use 'draw_data->DisplaySize' instead of 'io.DisplaySize', and 'draw_data->DisplayPos' instead of (0,0) as the upper-left point.
You also need to subtract 'draw_data->DisplayPos' from your scissor rectangles, as scissor rectangles are specified in the space of your target viewport.
- Examples: Back-ends have been refactored to separate the platform code (e.g. Win32, Glfw, SDL2) from the renderer code (e.g. DirectX11, OpenGL3, Vulkan).
before: imgui_impl_dx11.cpp --> after: imgui_impl_win32.cpp + imgui_impl_dx11.cpp
before: imgui_impl_dx12.cpp --> after: imgui_impl_win32.cpp + imgui_impl_dx12.cpp
before: imgui_impl_glfw_gl3.cpp --> after: imgui_impl_glfw.cpp + imgui_impl_opengl2.cpp
before: imgui_impl_glfw_vulkan.cpp --> after: imgui_impl_glfw.cpp + imgui_impl_vulkan.cpp
before: imgui_impl_sdl_gl3.cpp --> after: imgui_impl_sdl2.cpp + imgui_impl_opengl2.cpp
before: imgui_impl_sdl_gl3.cpp --> after: imgui_impl_sdl2.cpp + imgui_impl_opengl3.cpp
etc.
- The idea is what we can now easily combine and maintain back-ends and reduce code redundancy. Integration of imgui into a new/custom engine may also
be easier as there is less overlap between "windowing / inputs" and "rendering" code, so you may study or grab one half of the code and not the other.
- This change was motivated by the fact that adding support for multi-viewport requires more work from the platform and renderer back-ends, and the
amount of redundancy accross files was becoming too difficult to maintain.
- Some frameworks (such as the Allegro, Marmalade) handle both the "platform" and "rendering" part, and your custom engine may as well.
- Each example still has its own main.cpp which you may refer you to understand how to initialize and glue everything together.
- Examples: Win32: Added DPI-related helpers to access DPI features _without_ requiring the latest Windows SDK at compile time, and _without_ requiring Windows 10 at runtime.
- Examples: Platforms currently supporting multi-viewport: Win32, Glfw, SDL2.
- Examples: Renderers currently supporting multi-viewport: DirectX10, DirectX11, OpenGL2, OpenGL3, Vulkan (WIP).
- Examples: All imgui_impl_xxx files now have an individual Changelog at the top of the file, making it easier to follow how back-ends are evolving.
- Examples: Vulkan: Added various optional helpers in imgui_impl_vulkan.h (they are used for multi-viewport support) to make the examples main.cpp easier to read.
- Examples: Allegro: Renamed imgui_impl_a5.xxx files to imgui_impl_allegro5.xxx, ImGui_ImplA5_** symbols to ImGui_ImplAllegro5_xxx.
- Examples: Vulkan+SDL: Added a Vulkan+SDL example. (#1367) [@gmueckl]
- Metrics: Added a "Show window begin order" checkbox to visualize the order windows are submitted.
- Internal: Settings: Added ReadCloseFn handler to be able to patch/alter a loaded object after all the fields are known.
Breaking Changes: Breaking Changes:
(IN PROGRESS, WILL ADD TO THIS LIST AS WE WORK ON 1.60) (IN PROGRESS, WILL ADD TO THIS LIST AS WE WORK ON 1.60)

View File

@ -2122,7 +2122,7 @@ static inline bool IsWindowContentHoverable(ImGuiWindow* window, ImGuiHoveredFla
} }
// Filter by viewport // Filter by viewport
if (window->Viewport != g.MouseViewport) if (window->Viewport != g.MousePosViewport)
return false; return false;
return true; return true;
@ -3250,21 +3250,21 @@ static void ImGui::UpdateMovingWindowDropViewport(ImGuiWindow* window)
if (!(g.IO.ConfigFlags & ImGuiConfigFlags_EnableViewports)) if (!(g.IO.ConfigFlags & ImGuiConfigFlags_EnableViewports))
return; return;
ImRect mouse_viewport_rect = g.MouseViewport->GetRect(); ImRect mouse_viewport_rect = g.MousePosViewport->GetRect();
ImVec2 window_pos_in_mouse_viewport = ConvertPlatformPosToViewportPos(ConvertViewportPosToPlatformPos(window->Pos, window->Viewport), g.MouseViewport); ImVec2 window_pos_in_mouse_viewport = ConvertPlatformPosToViewportPos(ConvertViewportPosToPlatformPos(window->Pos, window->Viewport), g.MousePosViewport);
ImRect window_rect_in_mouse_viewport = ImRect(window_pos_in_mouse_viewport, window_pos_in_mouse_viewport + window->Size); ImRect window_rect_in_mouse_viewport = ImRect(window_pos_in_mouse_viewport, window_pos_in_mouse_viewport + window->Size);
if ((g.MouseViewport->Flags & ImGuiViewportFlags_CanHostOtherWindows) && mouse_viewport_rect.Contains(window_rect_in_mouse_viewport)) if ((g.MousePosViewport->Flags & ImGuiViewportFlags_CanHostOtherWindows) && mouse_viewport_rect.Contains(window_rect_in_mouse_viewport))
{ {
// Drop on an existing viewport // Drop on an existing viewport
ImGuiViewportP* old_viewport = window->Viewport; ImGuiViewportP* old_viewport = window->Viewport;
SetWindowViewportTranslateToPreservePlatformPos(window, window->Viewport, g.MouseViewport); SetWindowViewportTranslateToPreservePlatformPos(window, window->Viewport, g.MousePosViewport);
// Our current scheme allow any window to land on a viewport, so when that viewport merges, move other windows as well // Our current scheme allow any window to land on a viewport, so when that viewport merges, move other windows as well
// FIXME-OPT // FIXME-OPT
if (window->Flags & ImGuiWindowFlags_FullViewport) if (window->Flags & ImGuiWindowFlags_FullViewport)
for (int n = 0; n < g.Windows.Size; n++) for (int n = 0; n < g.Windows.Size; n++)
if (g.Windows[n]->Viewport == old_viewport) if (g.Windows[n]->Viewport == old_viewport)
SetWindowViewportTranslateToPreservePlatformPos(g.Windows[n], old_viewport, g.MouseViewport); SetWindowViewportTranslateToPreservePlatformPos(g.Windows[n], old_viewport, g.MousePosViewport);
} }
else else
{ {
@ -3363,8 +3363,8 @@ static void ImGui::UpdateViewports()
// Destroy // Destroy
if (viewport == viewport_ref) viewport_ref = NULL; if (viewport == viewport_ref) viewport_ref = NULL;
if (viewport == g.MouseViewport) g.MouseViewport = NULL; if (viewport == g.MousePosViewport) g.MousePosViewport = NULL;
if (viewport == g.MouseLastHoveredViewport) g.MouseLastHoveredViewport = NULL; if (viewport == g.MouseHoveredPrevViewport) g.MouseHoveredPrevViewport = NULL;
IM_ASSERT(viewport->RendererUserData == NULL && viewport->PlatformUserData == NULL && viewport->PlatformHandle == NULL); IM_ASSERT(viewport->RendererUserData == NULL && viewport->PlatformUserData == NULL && viewport->PlatformHandle == NULL);
IM_ASSERT(g.PlatformData.Viewports.contains(viewport) == false); IM_ASSERT(g.PlatformData.Viewports.contains(viewport) == false);
IM_DELETE(viewport); IM_DELETE(viewport);
@ -3419,7 +3419,7 @@ static void ImGui::UpdateViewports()
if (!(g.IO.ConfigFlags & ImGuiConfigFlags_EnableViewports)) if (!(g.IO.ConfigFlags & ImGuiConfigFlags_EnableViewports))
{ {
g.MouseViewport = g.MouseLastViewport = main_viewport; g.MousePosViewport = g.MousePosPrevViewport = main_viewport;
return; return;
} }
@ -3442,7 +3442,7 @@ static void ImGui::UpdateViewports()
viewport_hovered = FindViewportHoveredFromPlatformWindowStack(mouse_platform_pos); viewport_hovered = FindViewportHoveredFromPlatformWindowStack(mouse_platform_pos);
} }
if (viewport_hovered != NULL) if (viewport_hovered != NULL)
g.MouseLastHoveredViewport = viewport_hovered; g.MouseHoveredPrevViewport = viewport_hovered;
// If reference viewport just has been deleted, use a position relative to the main viewport // If reference viewport just has been deleted, use a position relative to the main viewport
if (viewport_ref == NULL) if (viewport_ref == NULL)
@ -3451,9 +3451,9 @@ static void ImGui::UpdateViewports()
g.IO.MousePos = ConvertPlatformPosToViewportPos(mouse_platform_pos, viewport_ref); g.IO.MousePos = ConvertPlatformPosToViewportPos(mouse_platform_pos, viewport_ref);
} }
g.MouseLastViewport = g.MouseViewport; g.MousePosPrevViewport = g.MousePosViewport;
g.MouseViewport = viewport_ref; g.MousePosViewport = viewport_ref;
g.MouseViewport->LastFrameAsRefViewport = g.FrameCount; g.MousePosViewport->LastFrameAsRefViewport = g.FrameCount;
// When dragging something, always refer to the last hovered viewport (so when we are between viewport, our dragged preview will tend to show in the last viewport) // When dragging something, always refer to the last hovered viewport (so when we are between viewport, our dragged preview will tend to show in the last viewport)
const bool is_mouse_dragging_with_an_expected_destination = g.DragDropActive || (g.MovingWindow != NULL); const bool is_mouse_dragging_with_an_expected_destination = g.DragDropActive || (g.MovingWindow != NULL);
@ -3461,15 +3461,15 @@ static void ImGui::UpdateViewports()
if (is_mouse_dragging_with_an_expected_destination || is_mouse_all_released) if (is_mouse_dragging_with_an_expected_destination || is_mouse_all_released)
{ {
if (is_mouse_dragging_with_an_expected_destination) if (is_mouse_dragging_with_an_expected_destination)
viewport_hovered = g.MouseLastHoveredViewport; viewport_hovered = g.MouseHoveredPrevViewport;
if (viewport_hovered != NULL && viewport_hovered != g.MouseViewport && !(viewport_hovered->Flags & ImGuiViewportFlags_NoInputs)) if (viewport_hovered != NULL && viewport_hovered != g.MousePosViewport && !(viewport_hovered->Flags & ImGuiViewportFlags_NoInputs))
{ {
g.IO.MousePos = ConvertPlatformPosToViewportPos(ConvertViewportPosToPlatformPos(g.IO.MousePos, g.MouseViewport), viewport_hovered); g.IO.MousePos = ConvertPlatformPosToViewportPos(ConvertViewportPosToPlatformPos(g.IO.MousePos, g.MousePosViewport), viewport_hovered);
g.MouseViewport = viewport_hovered; g.MousePosViewport = viewport_hovered;
} }
} }
IM_ASSERT(g.MouseViewport != NULL); IM_ASSERT(g.MousePosViewport != NULL);
} }
ImGuiPlatformData* ImGui::GetPlatformData() ImGuiPlatformData* ImGui::GetPlatformData()
@ -3698,7 +3698,7 @@ void ImGui::NewFrame()
// Update mouse input state // Update mouse input state
// If mouse just appeared or disappeared (usually denoted by -FLT_MAX component, but in reality we test for -256000.0f) we cancel out movement in MouseDelta // If mouse just appeared or disappeared (usually denoted by -FLT_MAX component, but in reality we test for -256000.0f) we cancel out movement in MouseDelta
if (IsMousePosValid(&g.IO.MousePos) && IsMousePosValid(&g.IO.MousePosPrev) && g.MouseViewport == g.MouseLastViewport) if (IsMousePosValid(&g.IO.MousePos) && IsMousePosValid(&g.IO.MousePosPrev) && g.MousePosViewport == g.MousePosPrevViewport)
g.IO.MouseDelta = g.IO.MousePos - g.IO.MousePosPrev; g.IO.MouseDelta = g.IO.MousePos - g.IO.MousePosPrev;
else else
g.IO.MouseDelta = ImVec2(0.0f, 0.0f); g.IO.MouseDelta = ImVec2(0.0f, 0.0f);
@ -3763,7 +3763,7 @@ void ImGui::NewFrame()
// - We also support the moved window toggling the NoInputs flag after moving has started in order to be able to detect windows below it, which is useful for e.g. docking mechanisms. // - We also support the moved window toggling the NoInputs flag after moving has started in order to be able to detect windows below it, which is useful for e.g. docking mechanisms.
g.HoveredWindow = (g.MovingWindow && !(g.MovingWindow->Flags & ImGuiWindowFlags_NoInputs)) ? g.MovingWindow : FindHoveredWindow(); g.HoveredWindow = (g.MovingWindow && !(g.MovingWindow->Flags & ImGuiWindowFlags_NoInputs)) ? g.MovingWindow : FindHoveredWindow();
g.HoveredRootWindow = g.HoveredWindow ? g.HoveredWindow->RootWindow : NULL; g.HoveredRootWindow = g.HoveredWindow ? g.HoveredWindow->RootWindow : NULL;
IM_ASSERT(g.HoveredWindow == NULL || g.HoveredWindow == g.MovingWindow || g.HoveredWindow->Viewport == g.MouseViewport); IM_ASSERT(g.HoveredWindow == NULL || g.HoveredWindow == g.MovingWindow || g.HoveredWindow->Viewport == g.MousePosViewport);
ImGuiWindow* modal_window = GetFrontMostModalRootWindow(); ImGuiWindow* modal_window = GetFrontMostModalRootWindow();
if (modal_window != NULL) if (modal_window != NULL)
@ -4033,7 +4033,7 @@ void ImGui::Shutdown(ImGuiContext* context)
g.FontStack.clear(); g.FontStack.clear();
g.OpenPopupStack.clear(); g.OpenPopupStack.clear();
g.CurrentPopupStack.clear(); g.CurrentPopupStack.clear();
g.CurrentViewport = g.MouseViewport = g.MouseLastViewport = g.MouseLastHoveredViewport = NULL; g.CurrentViewport = g.MousePosViewport = g.MousePosPrevViewport = g.MouseHoveredPrevViewport = NULL;
for (int i = 0; i < g.Viewports.Size; i++) for (int i = 0; i < g.Viewports.Size; i++)
IM_DELETE(g.Viewports[i]); IM_DELETE(g.Viewports[i]);
g.Viewports.clear(); g.Viewports.clear();
@ -4476,7 +4476,7 @@ void ImGui::Render()
const ImVec2 pos = g.IO.MousePos - offset; const ImVec2 pos = g.IO.MousePos - offset;
const ImTextureID tex_id = g.IO.Fonts->TexID; const ImTextureID tex_id = g.IO.Fonts->TexID;
const float sc = g.Style.MouseCursorScale; const float sc = g.Style.MouseCursorScale;
g.OverlayDrawList.PushClipRect(g.MouseViewport->Pos, g.MouseViewport->Pos + g.MouseViewport->Size); g.OverlayDrawList.PushClipRect(g.MousePosViewport->Pos, g.MousePosViewport->Pos + g.MousePosViewport->Size);
g.OverlayDrawList.PushTextureID(tex_id); g.OverlayDrawList.PushTextureID(tex_id);
g.OverlayDrawList.AddImage(tex_id, pos + ImVec2(1,0)*sc, pos+ImVec2(1,0)*sc + size*sc, uv[2], uv[3], IM_COL32(0,0,0,48)); // Shadow g.OverlayDrawList.AddImage(tex_id, pos + ImVec2(1,0)*sc, pos+ImVec2(1,0)*sc + size*sc, uv[2], uv[3], IM_COL32(0,0,0,48)); // Shadow
g.OverlayDrawList.AddImage(tex_id, pos + ImVec2(2,0)*sc, pos+ImVec2(2,0)*sc + size*sc, uv[2], uv[3], IM_COL32(0,0,0,48)); // Shadow g.OverlayDrawList.AddImage(tex_id, pos + ImVec2(2,0)*sc, pos+ImVec2(2,0)*sc + size*sc, uv[2], uv[3], IM_COL32(0,0,0,48)); // Shadow
@ -4981,7 +4981,7 @@ static ImGuiWindow* FindHoveredWindow()
if (window->Flags & ImGuiWindowFlags_NoInputs) if (window->Flags & ImGuiWindowFlags_NoInputs)
continue; continue;
IM_ASSERT(window->Viewport); IM_ASSERT(window->Viewport);
if (window->Viewport != g.MouseViewport) if (window->Viewport != g.MousePosViewport)
continue; continue;
// Using the clipped AABB, a child window will typically be clipped by its parent (not always) // Using the clipped AABB, a child window will typically be clipped by its parent (not always)
@ -5009,7 +5009,7 @@ bool ImGui::IsMouseHoveringRect(const ImVec2& r_min, const ImVec2& r_max, bool c
const ImRect rect_for_touch(rect_clipped.Min - g.Style.TouchExtraPadding, rect_clipped.Max + g.Style.TouchExtraPadding); const ImRect rect_for_touch(rect_clipped.Min - g.Style.TouchExtraPadding, rect_clipped.Max + g.Style.TouchExtraPadding);
if (!rect_for_touch.Contains(g.IO.MousePos)) if (!rect_for_touch.Contains(g.IO.MousePos))
return false; return false;
if (!g.MouseViewport->GetRect().Overlaps(rect_clipped)) if (!g.MousePosViewport->GetRect().Overlaps(rect_clipped))
return false; return false;
return true; return true;
} }
@ -6017,7 +6017,7 @@ static void ImGui::UpdateWindowViewport(ImGuiWindow* window, bool window_pos_set
if (!window_is_mouse_tooltip && !current_viewport->GetRect().Contains(window->Rect())) if (!window_is_mouse_tooltip && !current_viewport->GetRect().Contains(window->Rect()))
{ {
// Create an undecorated, temporary OS/platform window // Create an undecorated, temporary OS/platform window
ImVec2 platform_pos = ConvertViewportPosToPlatformPos(g.IO.MousePos - g.ActiveIdClickOffset, g.MouseViewport); ImVec2 platform_pos = ConvertViewportPosToPlatformPos(g.IO.MousePos - g.ActiveIdClickOffset, g.MousePosViewport);
ImGuiViewportFlags viewport_flags = ImGuiViewportFlags_NoDecoration | ImGuiViewportFlags_NoFocusOnAppearing | ImGuiViewportFlags_NoInputs; ImGuiViewportFlags viewport_flags = ImGuiViewportFlags_NoDecoration | ImGuiViewportFlags_NoFocusOnAppearing | ImGuiViewportFlags_NoInputs;
ImGuiViewportP* viewport = Viewport(window, window->ID, viewport_flags, platform_pos, window->Size); ImGuiViewportP* viewport = Viewport(window, window->ID, viewport_flags, platform_pos, window->Size);
window->Flags |= ImGuiWindowFlags_FullViewport; window->Flags |= ImGuiWindowFlags_FullViewport;
@ -6033,7 +6033,7 @@ static void ImGui::UpdateWindowViewport(ImGuiWindow* window, bool window_pos_set
// This is so we don't require of the multi-viewport windowing back-end to preserve mouse buttons after a window closure, making it easier to implement them. // This is so we don't require of the multi-viewport windowing back-end to preserve mouse buttons after a window closure, making it easier to implement them.
bool preserve_temporary_viewport = g.MovingWindow && g.MovingWindow->RootWindow == window && (window->Viewport->Flags & ImGuiWindowFlags_FullViewport); bool preserve_temporary_viewport = g.MovingWindow && g.MovingWindow->RootWindow == window && (window->Viewport->Flags & ImGuiWindowFlags_FullViewport);
if (!preserve_temporary_viewport) if (!preserve_temporary_viewport)
window->Viewport = g.MouseViewport; window->Viewport = g.MousePosViewport;
} }
} }
else if (g.NavWindow != NULL && g.NavWindow != window && (flags & ImGuiWindowFlags_Tooltip)) else if (g.NavWindow != NULL && g.NavWindow != window && (flags & ImGuiWindowFlags_Tooltip))

View File

@ -635,9 +635,9 @@ struct ImGuiContext
ImVector<ImGuiViewportP*> Viewports; ImVector<ImGuiViewportP*> Viewports;
ImGuiPlatformData PlatformData; // This is essentially the public facing version of the Viewports vector (it is updated in UpdatePlatformWindows and exclude the viewports about to be destroyed) ImGuiPlatformData PlatformData; // This is essentially the public facing version of the Viewports vector (it is updated in UpdatePlatformWindows and exclude the viewports about to be destroyed)
ImGuiViewportP* CurrentViewport; ImGuiViewportP* CurrentViewport;
ImGuiViewportP* MouseViewport; ImGuiViewportP* MousePosViewport;
ImGuiViewportP* MouseLastViewport; ImGuiViewportP* MousePosPrevViewport;
ImGuiViewportP* MouseLastHoveredViewport; ImGuiViewportP* MouseHoveredPrevViewport;
// Navigation data (for gamepad/keyboard) // Navigation data (for gamepad/keyboard)
ImGuiWindow* NavWindow; // Focused window for navigation. Could be called 'FocusWindow' ImGuiWindow* NavWindow; // Focused window for navigation. Could be called 'FocusWindow'
@ -766,8 +766,8 @@ struct ImGuiContext
NextTreeNodeOpenCond = 0; NextTreeNodeOpenCond = 0;
CurrentViewport = NULL; CurrentViewport = NULL;
MouseViewport = NULL; MousePosViewport = NULL;
MouseLastViewport = MouseLastHoveredViewport = NULL; MousePosPrevViewport = MouseHoveredPrevViewport = NULL;
NavWindow = NULL; NavWindow = NULL;
NavId = NavActivateId = NavActivateDownId = NavActivatePressedId = NavInputId = 0; NavId = NavActivateId = NavActivateDownId = NavActivatePressedId = NavInputId = 0;