Improve performance of show scrolling viewport on map

pull/59/head
Jonathan G Rennison 6 years ago
parent 7f32bb34ac
commit 64815c0d8a

@ -2784,24 +2784,54 @@ void UpdateViewportPosition(Window *w)
ClampViewportToMap(vp, w->viewport->scrollpos_x, w->viewport->scrollpos_y);
if (_scrolling_viewport == w && _settings_client.gui.show_scrolling_viewport_on_map && vp->zoom < ZOOM_LVL_DRAW_MAP) {
const int gap = ScaleByZoom(1, ZOOM_LVL_MAX);
if (_scrolling_viewport == w) UpdateActiveScrollingViewport(w);
int lr_low = vp->virtual_left;
int lr_hi = w->viewport->scrollpos_x;
if (lr_low > lr_hi) Swap(lr_low, lr_hi);
int right = lr_hi + vp->virtual_width + gap;
SetViewportPosition(w, w->viewport->scrollpos_x, w->viewport->scrollpos_y);
if (update_overlay) RebuildViewportOverlay(w);
}
}
int tb_low = vp->virtual_top;
int tb_hi = w->viewport->scrollpos_y;
if (tb_low > tb_hi) Swap(tb_low, tb_hi);
int bottom = tb_hi + vp->virtual_height + gap;
void UpdateActiveScrollingViewport(Window *w)
{
if (w && (!_settings_client.gui.show_scrolling_viewport_on_map || w->viewport->zoom >= ZOOM_LVL_DRAW_MAP)) w = nullptr;
MarkAllViewportMapsDirty(lr_low, tb_low, right, bottom);
}
const bool bound_valid = (_scrolling_viewport_bound.left != _scrolling_viewport_bound.right);
SetViewportPosition(w, w->viewport->scrollpos_x, w->viewport->scrollpos_y);
if (update_overlay) RebuildViewportOverlay(w);
if (!w && !bound_valid) return;
const int gap = ScaleByZoom(1, ZOOM_LVL_MAX);
auto get_bounds = [&gap](const ViewportData *vp) -> Rect {
int lr_low = vp->virtual_left;
int lr_hi = vp->dest_scrollpos_x;
if (lr_low > lr_hi) Swap(lr_low, lr_hi);
int right = lr_hi + vp->virtual_width + gap;
int tb_low = vp->virtual_top;
int tb_hi = vp->scrollpos_y;
if (tb_low > tb_hi) Swap(tb_low, tb_hi);
int bottom = tb_hi + vp->virtual_height + gap;
return { lr_low, tb_low, right, bottom };
};
if (w && !bound_valid) {
const Rect bounds = get_bounds(w->viewport);
MarkAllViewportMapsDirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
_scrolling_viewport_bound = bounds;
} else if (!w && bound_valid) {
const Rect &bounds = _scrolling_viewport_bound;
MarkAllViewportMapsDirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
_scrolling_viewport_bound = { 0, 0, 0, 0 };
} else {
/* Calculate symmetric difference of two rectangles */
const Rect a = get_bounds(w->viewport);
const Rect &b = _scrolling_viewport_bound;
if (a.left != b.left) MarkAllViewportMapsDirty(min(a.left, b.left) - gap, min(a.top, b.top) - gap, max(a.left, b.left) + gap, max(a.bottom, b.bottom) + gap);
if (a.top != b.top) MarkAllViewportMapsDirty(min(a.left, b.left) - gap, min(a.top, b.top) - gap, max(a.right, b.right) + gap, max(a.top, b.top) + gap);
if (a.right != b.right) MarkAllViewportMapsDirty(min(a.right, b.right) - (2 * gap), min(a.top, b.top) - gap, max(a.right, b.right) + gap, max(a.bottom, b.bottom) + gap);
if (a.bottom != b.bottom) MarkAllViewportMapsDirty(min(a.left, b.left) - gap, min(a.bottom, b.bottom) - (2 * gap), max(a.right, b.right) + gap, max(a.bottom, b.bottom) + gap);
_scrolling_viewport_bound = a;
}
}

@ -76,6 +76,8 @@ void ViewportDoDraw(const ViewPort *vp, int left, int top, int right, int bottom
bool ScrollWindowToTile(TileIndex tile, Window *w, bool instant = false);
bool ScrollWindowTo(int x, int y, int z, Window *w, bool instant = false);
void UpdateActiveScrollingViewport(Window *w);
void RebuildViewportOverlay(Window *w);
bool ScrollMainWindowToTile(TileIndex tile, bool instant = false);

@ -73,6 +73,7 @@ int _scrollbar_size;
byte _scroller_click_timeout = 0;
Window *_scrolling_viewport; ///< A viewport is being scrolled with the mouse.
Rect _scrolling_viewport_bound; ///< A viewport is being scrolled with the mouse, the overlay currently covers this viewport rectangle.
bool _mouse_hovering; ///< The mouse is hovering over the same point.
SpecialMouseMode _special_mouse_mode; ///< Mode of the mouse.
@ -1878,6 +1879,7 @@ void InitWindowSystem()
_mouseover_last_w = NULL;
_last_scroll_window = NULL;
_scrolling_viewport = NULL;
_scrolling_viewport_bound = { 0, 0, 0, 0 };
_mouse_hovering = false;
NWidgetLeaf::InvalidateDimensionCache(); // Reset cached sizes of several widgets.
@ -2448,6 +2450,7 @@ static EventState HandleViewportScroll()
_cursor.fix_at = false;
_scrolling_viewport = NULL;
_last_scroll_window = NULL;
UpdateActiveScrollingViewport(nullptr);
return ES_NOT_HANDLED;
}

@ -892,6 +892,7 @@ extern int _scrollbar_size;
extern byte _scroller_click_timeout;
extern Window *_scrolling_viewport;
extern Rect _scrolling_viewport_bound;
extern bool _mouse_hovering;
/** Mouse modes. */

Loading…
Cancel
Save