Fix: Round up deltas for smooth scrolling, so target will be reached

This commit is contained in:
btzy 2019-01-12 16:28:43 +08:00 committed by Charles Pigott
parent 5ad73e4029
commit 9d75600ac0
2 changed files with 19 additions and 2 deletions

View File

@ -346,6 +346,23 @@ static inline int RoundDivSU(int a, uint b)
} }
} }
/**
* Computes (a / b) rounded away from zero.
* @param a Numerator
* @param b Denominator
* @return Quotient, rounded away from zero
*/
static inline int DivAwayFromZero(int a, uint b)
{
const int _b = static_cast<int>(b);
if (a > 0) {
return (a + _b - 1) / _b;
} else {
/* Note: Behaviour of negative numerator division is truncation toward zero. */
return (a - _b + 1) / _b;
}
}
uint32 IntSqrt(uint32 num); uint32 IntSqrt(uint32 num);
#endif /* MATH_FUNC_HPP */ #endif /* MATH_FUNC_HPP */

View File

@ -1813,8 +1813,8 @@ void UpdateViewportPosition(Window *w)
if (_settings_client.gui.smooth_scroll) { if (_settings_client.gui.smooth_scroll) {
int max_scroll = ScaleByMapSize1D(512 * ZOOM_LVL_BASE); int max_scroll = ScaleByMapSize1D(512 * ZOOM_LVL_BASE);
/* Not at our desired position yet... */ /* Not at our desired position yet... */
w->viewport->scrollpos_x += Clamp(delta_x / 4, -max_scroll, max_scroll); w->viewport->scrollpos_x += Clamp(DivAwayFromZero(delta_x, 4), -max_scroll, max_scroll);
w->viewport->scrollpos_y += Clamp(delta_y / 4, -max_scroll, max_scroll); w->viewport->scrollpos_y += Clamp(DivAwayFromZero(delta_y, 4), -max_scroll, max_scroll);
} else { } else {
w->viewport->scrollpos_x = w->viewport->dest_scrollpos_x; w->viewport->scrollpos_x = w->viewport->dest_scrollpos_x;
w->viewport->scrollpos_y = w->viewport->dest_scrollpos_y; w->viewport->scrollpos_y = w->viewport->dest_scrollpos_y;