MCPcopy Create free account
hub / github.com/RenderKit/embree / ScrollToRectEx

Method ScrollToRectEx

tutorials/common/imgui/imgui.cpp:9504–9571  ·  view source on GitHub ↗

Scroll to keep newly navigated item fully into view

Source from the content-addressed store, hash-verified

9502
9503// Scroll to keep newly navigated item fully into view
9504ImVec2 ImGui::ScrollToRectEx(ImGuiWindow* window, const ImRect& item_rect, ImGuiScrollFlags flags)
9505{
9506 ImGuiContext& g = *GImGui;
9507 ImRect window_rect(window->InnerRect.Min - ImVec2(1, 1), window->InnerRect.Max + ImVec2(1, 1));
9508 //GetForegroundDrawList(window)->AddRect(window_rect.Min, window_rect.Max, IM_COL32_WHITE); // [DEBUG]
9509
9510 // Check that only one behavior is selected per axis
9511 IM_ASSERT((flags & ImGuiScrollFlags_MaskX_) == 0 || ImIsPowerOfTwo(flags & ImGuiScrollFlags_MaskX_));
9512 IM_ASSERT((flags & ImGuiScrollFlags_MaskY_) == 0 || ImIsPowerOfTwo(flags & ImGuiScrollFlags_MaskY_));
9513
9514 // Defaults
9515 ImGuiScrollFlags in_flags = flags;
9516 if ((flags & ImGuiScrollFlags_MaskX_) == 0 && window->ScrollbarX)
9517 flags |= ImGuiScrollFlags_KeepVisibleEdgeX;
9518 if ((flags & ImGuiScrollFlags_MaskY_) == 0)
9519 flags |= window->Appearing ? ImGuiScrollFlags_AlwaysCenterY : ImGuiScrollFlags_KeepVisibleEdgeY;
9520
9521 const bool fully_visible_x = item_rect.Min.x >= window_rect.Min.x && item_rect.Max.x <= window_rect.Max.x;
9522 const bool fully_visible_y = item_rect.Min.y >= window_rect.Min.y && item_rect.Max.y <= window_rect.Max.y;
9523 const bool can_be_fully_visible_x = (item_rect.GetWidth() + g.Style.ItemSpacing.x * 2.0f) <= window_rect.GetWidth() || (window->AutoFitFramesX > 0) || (window->Flags & ImGuiWindowFlags_AlwaysAutoResize) != 0;
9524 const bool can_be_fully_visible_y = (item_rect.GetHeight() + g.Style.ItemSpacing.y * 2.0f) <= window_rect.GetHeight() || (window->AutoFitFramesY > 0) || (window->Flags & ImGuiWindowFlags_AlwaysAutoResize) != 0;
9525
9526 if ((flags & ImGuiScrollFlags_KeepVisibleEdgeX) && !fully_visible_x)
9527 {
9528 if (item_rect.Min.x < window_rect.Min.x || !can_be_fully_visible_x)
9529 SetScrollFromPosX(window, item_rect.Min.x - g.Style.ItemSpacing.x - window->Pos.x, 0.0f);
9530 else if (item_rect.Max.x >= window_rect.Max.x)
9531 SetScrollFromPosX(window, item_rect.Max.x + g.Style.ItemSpacing.x - window->Pos.x, 1.0f);
9532 }
9533 else if (((flags & ImGuiScrollFlags_KeepVisibleCenterX) && !fully_visible_x) || (flags & ImGuiScrollFlags_AlwaysCenterX))
9534 {
9535 if (can_be_fully_visible_x)
9536 SetScrollFromPosX(window, ImFloor((item_rect.Min.x + item_rect.Max.y) * 0.5f) - window->Pos.x, 0.5f);
9537 else
9538 SetScrollFromPosX(window, item_rect.Min.x - window->Pos.x, 0.0f);
9539 }
9540
9541 if ((flags & ImGuiScrollFlags_KeepVisibleEdgeY) && !fully_visible_y)
9542 {
9543 if (item_rect.Min.y < window_rect.Min.y || !can_be_fully_visible_y)
9544 SetScrollFromPosY(window, item_rect.Min.y - g.Style.ItemSpacing.y - window->Pos.y, 0.0f);
9545 else if (item_rect.Max.y >= window_rect.Max.y)
9546 SetScrollFromPosY(window, item_rect.Max.y + g.Style.ItemSpacing.y - window->Pos.y, 1.0f);
9547 }
9548 else if (((flags & ImGuiScrollFlags_KeepVisibleCenterY) && !fully_visible_y) || (flags & ImGuiScrollFlags_AlwaysCenterY))
9549 {
9550 if (can_be_fully_visible_y)
9551 SetScrollFromPosY(window, ImFloor((item_rect.Min.y + item_rect.Max.y) * 0.5f) - window->Pos.y, 0.5f);
9552 else
9553 SetScrollFromPosY(window, item_rect.Min.y - window->Pos.y, 0.0f);
9554 }
9555
9556 ImVec2 next_scroll = CalcNextScrollFromScrollTargetAndClamp(window);
9557 ImVec2 delta_scroll = next_scroll - window->Scroll;
9558
9559 // Also scroll parent window to keep us into view if necessary
9560 if (!(flags & ImGuiScrollFlags_NoScrollParent) && (window->Flags & ImGuiWindowFlags_ChildWindow))
9561 {

Callers

nothing calls this directly

Calls 5

ImVec2Function · 0.85
ImIsPowerOfTwoFunction · 0.85
ImFloorFunction · 0.85
ImRectFunction · 0.70

Tested by

no test coverage detected