1
0
mirror of https://github.com/ocornut/imgui.git synced 2024-11-28 17:40:56 +01:00

SetScrollPosHere() Minor hack for effective "scroll to top" to appear like user expect it (#150)

Compensate the difference between WindowPadding and ItemSpacing
This commit is contained in:
ocornut 2015-07-01 19:01:27 -06:00
parent 824cf5ae85
commit 0e5b64ecd2

View File

@ -4808,11 +4808,13 @@ float ImGui::GetScrollMaxY()
void ImGui::SetScrollPosHere(float center_y_ratio)
{
// We store a target position so centering can occur on the next frame when we are guaranteed to have a known window size
ImGuiState& g = *GImGui;
IM_ASSERT(center_y_ratio >= 0.0f && center_y_ratio <= 1.0f);
ImGuiWindow* window = GetCurrentWindow();
window->ScrollTargetRelY = (float)(int)(window->ScrollY + window->DC.CursorPosPrevLine.y - window->Pos.y + (window->DC.PrevLineHeight) * center_y_ratio);
float line_spacing = (window->DC.CursorPos.y - window->DC.CursorPosPrevLine.y) - (window->DC.PrevLineHeight);
window->ScrollTargetRelY += line_spacing * (center_y_ratio - 0.5f) * 2.0f;
window->ScrollTargetRelY += g.Style.ItemSpacing.y * (center_y_ratio - 0.5f) * 2.0f;
if (center_y_ratio <= 0.0f && window->ScrollTargetRelY <= window->WindowPadding.y) // Minor hack to make "scroll to top" take account of WindowPadding, else it would scroll to (WindowPadding.y - ItemSpacing.y)
window->ScrollTargetRelY = 0.0f;
window->ScrollTargetCenterRatioY = center_y_ratio;
}