Skip to content

Commit

Permalink
CaptureTool: tidying up.
Browse files Browse the repository at this point in the history
Was originally aimed at solving #33 before I realized even calling SetWindowPos() every frame wouldn't work.
Minor refactor however seems decent and goes in the direction of a more stateless rework.
  • Loading branch information
ocornut committed Sep 28, 2023
1 parent 6acb715 commit 27d623e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
58 changes: 37 additions & 21 deletions imgui_test_engine/imgui_capture_tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,7 @@ ImGuiCaptureStatus ImGuiCaptureContext::CaptureUpdate(ImGuiCaptureArgs* args)
_CaptureArgs = args;
_ChunkNo = 0;
_CaptureRect = _CapturedWindowRect = ImRect(FLT_MAX, FLT_MAX, -FLT_MAX, -FLT_MAX);
_BackupWindows.clear();
_BackupWindowsRect.clear();
_WindowsData.clear();
_BackupDisplayWindowPadding = g.Style.DisplayWindowPadding;
_BackupDisplaySafeAreaPadding = g.Style.DisplaySafeAreaPadding;
g.Style.DisplayWindowPadding = ImVec2(0, 0); // Allow windows to be positioned fully outside of visible viewport.
Expand Down Expand Up @@ -321,8 +320,10 @@ ImGuiCaptureStatus ImGuiCaptureContext::CaptureUpdate(ImGuiCaptureArgs* args)
for (ImGuiWindow* window : args->InCaptureWindows)
{
_CapturedWindowRect.Add(window->Rect());
_BackupWindows.push_back(window);
_BackupWindowsRect.push_back(window->Rect());
ImGuiCaptureWindowData window_data;
window_data.BackupRect = window->Rect();
window_data.Window = window;
_WindowsData.push_back(window_data);
}

if (args->InFlags & ImGuiCaptureFlags_StitchAll)
Expand Down Expand Up @@ -371,8 +372,13 @@ ImGuiCaptureStatus ImGuiCaptureContext::CaptureUpdate(ImGuiCaptureArgs* args)
if (args->InFlags & ImGuiCaptureFlags_StitchAll)
{
ImVec2 move_offset = ImVec2(args->InPadding, args->InPadding) - _CapturedWindowRect.Min + viewport_rect.Min;
for (ImGuiWindow* window : args->InCaptureWindows) // FIXME: Technically with ImGuiCaptureFlags_StitchAll we are dealing with a single window, so loop is misleading
ImGui::SetWindowPos(window, window->Pos + move_offset);
IM_ASSERT(args->InCaptureWindows.Size == _WindowsData.Size);
for (int n = 0; n < _WindowsData.Size; n++)
{
ImGuiWindow* window = _WindowsData[n].Window;
_WindowsData[n].PosDuringCapture = window->Pos + move_offset;
ImGui::SetWindowPos(window, _WindowsData[n].PosDuringCapture);
}
}

// Determine capture rectangle if not provided by user
Expand Down Expand Up @@ -425,18 +431,29 @@ ImGuiCaptureStatus ImGuiCaptureContext::CaptureUpdate(ImGuiCaptureArgs* args)
//-----------------------------------------------------------------
// Frame 4+N*4: Capture a frame
//-----------------------------------------------------------------

const ImRect clip_rect = viewport_rect;
ImRect capture_rect = _CaptureRect;
capture_rect.ClipWith(clip_rect);
const int capture_height = ImMin((int)io.DisplaySize.y, (int)_CaptureRect.GetHeight());
const int x1 = (int)(capture_rect.Min.x - clip_rect.Min.x);
const int y1 = (int)(capture_rect.Min.y - clip_rect.Min.y);
const int w = (int)capture_rect.GetWidth();
const int h = (int)ImMin(output->Height - _ChunkNo * capture_height, capture_height);

// Position windows
if ((_FrameNo > 2) && (args->InFlags & ImGuiCaptureFlags_StitchAll))
{
// Unlike SetNextWindowPos(), SetWindowPos() will still perform viewport clamping, affecting support for io.ConfigWindowsMoveFromTitleBarOnly.
IM_ASSERT(args->InCaptureWindows.Size == _WindowsData.Size);
for (int n = 0; n < _WindowsData.Size; n++)
ImGui::SetWindowPos(_WindowsData[n].Window, _WindowsData[n].PosDuringCapture - ImVec2(0, (float)capture_height * _ChunkNo));
}

if (((_FrameNo > 2) && (_FrameNo % 4) == 0) || (is_recording_video && _FrameNo > 2) || instant_capture)
{
// FIXME: Implement capture of regions wider than viewport.
// Capture a portion of image. Capturing of windows wider than viewport is not implemented yet.
const ImRect clip_rect = viewport_rect;
ImRect capture_rect = _CaptureRect;
capture_rect.ClipWith(clip_rect);
const int capture_height = ImMin((int)io.DisplaySize.y, (int)_CaptureRect.GetHeight());
const int x1 = (int)(capture_rect.Min.x - clip_rect.Min.x);
const int y1 = (int)(capture_rect.Min.y - clip_rect.Min.y);
const int w = (int)capture_rect.GetWidth();
const int h = (int)ImMin(output->Height - _ChunkNo * capture_height, capture_height);
if (h > 0)
{
IM_ASSERT(w == output->Width);
Expand All @@ -446,6 +463,7 @@ ImGuiCaptureStatus ImGuiCaptureContext::CaptureUpdate(ImGuiCaptureArgs* args)
IM_ASSERT(h == output->Height);

const ImGuiID viewport_id = 0;
//printf("ScreenCaptureFunc x1: %d, y1: %d, w: %d, h: %d\n", x1, y1, w, h);
if (!ScreenCaptureFunc(viewport_id, x1, y1, w, h, &output->Data[_ChunkNo * w * capture_height], ScreenCaptureUserData))
{
fprintf(stderr, "Screen capture function failed.\n");
Expand All @@ -455,10 +473,8 @@ ImGuiCaptureStatus ImGuiCaptureContext::CaptureUpdate(ImGuiCaptureArgs* args)
if (args->InFlags & ImGuiCaptureFlags_StitchAll)
{
// Window moves up in order to expose it's lower part.
for (ImGuiWindow* window : args->InCaptureWindows)
ImGui::SetWindowPos(window, window->Pos - ImVec2(0, (float)h));
_CaptureRect.TranslateY(-(float)h);
_ChunkNo++;
_CaptureRect.TranslateY(-(float)h);
}

if (is_recording_video && (args->InFlags & ImGuiCaptureFlags_NoSave) == 0)
Expand Down Expand Up @@ -526,13 +542,13 @@ ImGuiCaptureStatus ImGuiCaptureContext::CaptureUpdate(ImGuiCaptureArgs* args)
}

// Restore window positions unconditionally. We may have moved them ourselves during capture.
for (int i = 0; i < _BackupWindows.Size; i++)
for (int n = 0; n < _WindowsData.Size; n++)
{
ImGuiWindow* window = _BackupWindows[i];
ImGuiWindow* window = _WindowsData[n].Window;
if (window->Hidden)
continue;
ImGui::SetWindowPos(window, _BackupWindowsRect[i].Min, ImGuiCond_Always);
ImGui::SetWindowSize(window, _BackupWindowsRect[i].GetSize(), ImGuiCond_Always);
ImGui::SetWindowPos(window, _WindowsData[n].BackupRect.Min, ImGuiCond_Always);
ImGui::SetWindowSize(window, _WindowsData[n].BackupRect.GetSize(), ImGuiCond_Always);
}
g.Style.DisplayWindowPadding = _BackupDisplayWindowPadding;
g.Style.DisplaySafeAreaPadding = _BackupDisplaySafeAreaPadding;
Expand Down
12 changes: 9 additions & 3 deletions imgui_test_engine/imgui_capture_tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ enum ImGuiCaptureStatus
ImGuiCaptureStatus_Error
};

struct ImGuiCaptureWindowData
{
ImGuiWindow* Window;
ImRect BackupRect;
ImVec2 PosDuringCapture;
};

// Implements functionality for capturing images
struct IMGUI_API ImGuiCaptureContext
{
Expand All @@ -101,16 +108,15 @@ struct IMGUI_API ImGuiCaptureContext
ImGuiWindow* _HoveredWindow = NULL; // Window which was hovered at capture start.
ImGuiCaptureImageBuf _CaptureBuf; // Output image buffer.
const ImGuiCaptureArgs* _CaptureArgs = NULL; // Current capture args. Set only if capture is in progress.
ImVector<ImGuiCaptureWindowData> _WindowsData; // Backup windows that will have their rect modified and restored. args->InCaptureWindows can not be used because popups may get closed during capture and no longer appear in that list.

// [Internal] Video recording
bool _VideoRecording = false; // Flag indicating that video recording is in progress.
double _VideoLastFrameTime = 0; // Time when last video frame was recorded.
FILE* _VideoEncoderPipe = NULL; // File writing to stdin of video encoder process.
FILE* _VideoEncoderPipe = NULL; // File writing to stdin of video encoder process.

// [Internal] Backups
bool _BackupMouseDrawCursor = false; // Initial value of g.IO.MouseDrawCursor
ImVector<ImGuiWindow*> _BackupWindows; // Backup windows that will have their rect modified and restored. args->InCaptureWindows can not be used because popups may get closed during capture and no longer appear in that list.
ImVector<ImRect> _BackupWindowsRect; // Backup window state that will be restored when screen capturing is done. Size and order matches windows of ImGuiCaptureArgs::InCaptureWindows.
ImVec2 _BackupDisplayWindowPadding; // Backup padding. We set it to {0, 0} during capture.
ImVec2 _BackupDisplaySafeAreaPadding; // Backup padding. We set it to {0, 0} during capture.

Expand Down

0 comments on commit 27d623e

Please sign in to comment.