@ -7368,16 +7368,16 @@ static ImVec2 CalcNextScrollFromScrollTargetAndClamp(ImGuiWindow* window)
ImVec2 scroll = window - > Scroll ;
if ( window - > ScrollTarget . x < FLT_MAX )
{
float c r_x = window - > ScrollTargetCenterRatio . x ;
float target_x = window - > ScrollTarget . x ;
scroll . x = target_x - c r_x * ( window - > SizeFull . x - window - > ScrollbarSizes . x ) ;
float c ente r_x_ratio = window - > ScrollTargetCenterRatio . x ;
float scroll_ target_x = window - > ScrollTarget . x ;
scroll . x = scroll_ target_x - c ente r_x_ratio * ( window - > SizeFull . x - window - > ScrollbarSizes . x ) ;
}
if ( window - > ScrollTarget . y < FLT_MAX )
{
float decoration_up_height = window - > TitleBarHeight ( ) + window - > MenuBarHeight ( ) ;
float c r_y = window - > ScrollTargetCenterRatio . y ;
float target_y = window - > ScrollTarget . y ;
scroll . y = target_y - c r_y * ( window - > SizeFull . y - window - > ScrollbarSizes . y - decoration_up_height ) ;
float c ente r_y_ratio = window - > ScrollTargetCenterRatio . y ;
float scroll_ target_y = window - > ScrollTarget . y ;
scroll . y = scroll_ target_y - c ente r_y_ratio * ( window - > SizeFull . y - window - > ScrollbarSizes . y - decoration_up_height ) ;
}
scroll . x = IM_FLOOR ( ImMax ( scroll . x , 0.0f ) ) ;
scroll . y = IM_FLOOR ( ImMax ( scroll . y , 0.0f ) ) ;
@ -7443,38 +7443,44 @@ float ImGui::GetScrollMaxY()
return window - > ScrollMax . y ;
}
void ImGui : : SetScrollX ( float scroll_x )
void ImGui : : SetScrollX ( ImGuiWindow * window , float scroll_x )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
window - > ScrollTarget . x = scroll_x ;
window - > ScrollTargetCenterRatio . x = 0.0f ;
}
void ImGui : : SetScrollY ( float scroll_y )
void ImGui : : SetScrollY ( ImGuiWindow * window , float scroll_y )
{
ImGuiWindow * window = GetCurrentWindow ( ) ;
window - > ScrollTarget . y = scroll_y ;
window - > ScrollTargetCenterRatio . y = 0.0f ;
}
void ImGui : : SetScrollX ( ImGuiWindow * window , float new_ scroll_x)
void ImGui : : SetScrollX ( float scroll_x)
{
window- > ScrollTarget . x = new_scroll_x ;
window- > ScrollTargetCenterRatio . x = 0.0f ;
ImGuiContext& g = * GImGui ;
SetScrollX( g . CurrentWindow , scroll_x ) ;
}
void ImGui : : SetScrollY ( ImGuiWindow * window , float new_ scroll_y)
void ImGui : : SetScrollY ( float scroll_y)
{
window- > ScrollTarget . y = new_scroll_y ;
window- > ScrollTargetCenterRatio . y = 0.0f ;
ImGuiContext& g = * GImGui ;
SetScrollY( g . CurrentWindow , scroll_y ) ;
}
// Note that a local position will vary depending on initial scroll value
// We store a target position so centering can occur on the next frame when we are guaranteed to have a known window size
// Note that a local position will vary depending on initial scroll value,
// This is a little bit confusing so bear with us:
// - local_pos = (absolution_pos - window->Pos)
// - So local_x/local_y are 0.0f for a position at the upper-left corner of a window,
// and generally local_x/local_y are >(padding+decoration) && <(size-padding-decoration) when in the visible area.
// - They mostly exists because of legacy API.
// Following the rules above, when trying to work with scrolling code, consider that:
// - SetScrollFromPosY(0.0f) == SetScrollY(0.0f + scroll.y) == has no effect!
// - SetScrollFromPosY(-scroll.y) == SetScrollY(-scroll.y + scroll.y) == SetScrollY(0.0f) == reset scroll. Of course writing SetScrollY(0.0f) directly then makes more sense
// We store a target position so centering and clamping can occur on the next frame when we are guaranteed to have a known window size
void ImGui : : SetScrollFromPosX ( ImGuiWindow * window , float local_x , float center_x_ratio )
{
IM_ASSERT ( center_x_ratio > = 0.0f & & center_x_ratio < = 1.0f ) ;
window - > ScrollTarget . x = IM_FLOOR ( local_x + window - > Scroll . x ) ;
window - > ScrollTarget . x = IM_FLOOR ( local_x + window - > Scroll . x ) ; // Convert local position to scroll offset
window - > ScrollTargetCenterRatio . x = center_x_ratio ;
}
@ -7482,7 +7488,7 @@ void ImGui::SetScrollFromPosY(ImGuiWindow* window, float local_y, float center_y
{
IM_ASSERT ( center_y_ratio > = 0.0f & & center_y_ratio < = 1.0f ) ;
local_y - = window - > TitleBarHeight ( ) + window - > MenuBarHeight ( ) ; // FIXME: Would be nice to have a more standardized access to our scrollable/client rect
window - > ScrollTarget . y = IM_FLOOR ( local_y + window - > Scroll . y ) ;
window - > ScrollTarget . y = IM_FLOOR ( local_y + window - > Scroll . y ) ; // Convert local position to scroll offset
window - > ScrollTargetCenterRatio . y = center_y_ratio ;
}
@ -7517,15 +7523,15 @@ void ImGui::SetScrollHereX(float center_x_ratio)
ImGuiContext & g = * GImGui ;
ImGuiWindow * window = g . CurrentWindow ;
float spacing_x = g . Style . ItemSpacing . x ;
float target_ x = ImLerp ( window - > DC . LastItemRect . Min . x - spacing_x , window - > DC . LastItemRect . Max . x + spacing_x , center_x_ratio ) ;
float target_ pos_ x = ImLerp ( window - > DC . LastItemRect . Min . x - spacing_x , window - > DC . LastItemRect . Max . x + spacing_x , center_x_ratio ) ;
// Tweak: snap on edges when aiming at an item very close to the edge
const float snap_x_threshold = ImMax ( 0.0f , window - > WindowPadding . x - spacing_x ) ;
const float snap_x_min = window - > DC . CursorStartPos . x - window - > WindowPadding . x ;
const float snap_x_max = window - > DC . CursorStartPos . x + window - > ContentSize . x + window - > WindowPadding . x ;
target_ x = CalcScrollSnap ( target _x, snap_x_min , snap_x_max , snap_x_threshold , center_x_ratio ) ;
target_ pos_ x = CalcScrollSnap ( target _pos _x, snap_x_min , snap_x_max , snap_x_threshold , center_x_ratio ) ;
SetScrollFromPosX ( window , target_ x - window - > Pos . x , center_x_ratio ) ;
SetScrollFromPosX ( window , target_ pos_ x - window - > Pos . x , center_x_ratio ) ; // Convert from absolute to local pos
}
// center_y_ratio: 0.0f top of last item, 0.5f vertical center of last item, 1.0f bottom of last item.
@ -7534,15 +7540,15 @@ void ImGui::SetScrollHereY(float center_y_ratio)
ImGuiContext & g = * GImGui ;
ImGuiWindow * window = g . CurrentWindow ;
float spacing_y = g . Style . ItemSpacing . y ;
float target_ y = ImLerp ( window - > DC . CursorPosPrevLine . y - spacing_y , window - > DC . CursorPosPrevLine . y + window - > DC . PrevLineSize . y + spacing_y , center_y_ratio ) ;
float target_ pos_ y = ImLerp ( window - > DC . CursorPosPrevLine . y - spacing_y , window - > DC . CursorPosPrevLine . y + window - > DC . PrevLineSize . y + spacing_y , center_y_ratio ) ;
// Tweak: snap on edges when aiming at an item very close to the edge
const float snap_y_threshold = ImMax ( 0.0f , window - > WindowPadding . y - spacing_y ) ;
const float snap_y_min = window - > DC . CursorStartPos . y - window - > WindowPadding . y ;
const float snap_y_max = window - > DC . CursorStartPos . y + window - > ContentSize . y + window - > WindowPadding . y ;
target_ y = CalcScrollSnap ( target _y, snap_y_min , snap_y_max , snap_y_threshold , center_y_ratio ) ;
target_ pos_ y = CalcScrollSnap ( target _pos _y, snap_y_min , snap_y_max , snap_y_threshold , center_y_ratio ) ;
SetScrollFromPosY ( window , target_ y - window - > Pos . y , center_y_ratio ) ;
SetScrollFromPosY ( window , target_ pos_ y - window - > Pos . y , center_y_ratio ) ; // Convert from absolute to local pos
}
//-----------------------------------------------------------------------------