Improve caching of game-related variables

- Add caching for chatbox bounds
- Use cache variables when calculating OverlayBounds
- Cache everything at once when needed in the caching method without
early returns

Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
This commit is contained in:
Tomas Slusny
2018-03-23 13:32:09 +01:00
parent 4a8af3d049
commit c3eddf02a8

View File

@@ -104,6 +104,7 @@ public class OverlayRenderer extends MouseListener implements KeyListener
// Overlay state validation // Overlay state validation
private Rectangle viewportBounds; private Rectangle viewportBounds;
private Rectangle chatboxBounds;
private boolean chatboxHidden; private boolean chatboxHidden;
private boolean isResizeable; private boolean isResizeable;
private OverlayBounds snapCorners; private OverlayBounds snapCorners;
@@ -270,7 +271,7 @@ public class OverlayRenderer extends MouseListener implements KeyListener
return; return;
} }
if (shouldInvalidateOverlays()) if (shouldInvalidateBounds())
{ {
snapCorners = buildSnapCorners(); snapCorners = buildSnapCorners();
} }
@@ -307,7 +308,7 @@ public class OverlayRenderer extends MouseListener implements KeyListener
overlayPosition = overlay.getPreferredPosition(); overlayPosition = overlay.getPreferredPosition();
} }
if (overlayPosition == OverlayPosition.ABOVE_CHATBOX_RIGHT && !client.isResized()) if (overlayPosition == OverlayPosition.ABOVE_CHATBOX_RIGHT && !isResizeable)
{ {
// On fixed mode, ABOVE_CHATBOX_RIGHT is in the same location as // On fixed mode, ABOVE_CHATBOX_RIGHT is in the same location as
// BOTTOM_RIGHT. Just use BOTTOM_RIGHT to prevent overlays from // BOTTOM_RIGHT. Just use BOTTOM_RIGHT to prevent overlays from
@@ -495,24 +496,33 @@ public class OverlayRenderer extends MouseListener implements KeyListener
overlay.setBounds(new Rectangle(point, dimension)); overlay.setBounds(new Rectangle(point, dimension));
} }
private boolean shouldInvalidateOverlays() private boolean shouldInvalidateBounds()
{ {
final Client client = clientProvider.get(); final Client client = clientProvider.get();
final Widget widget = client.getWidget(WidgetInfo.CHATBOX_MESSAGES); final Widget chatbox = client.getWidget(WidgetInfo.CHATBOX_MESSAGES);
final boolean resizeableChanged = isResizeable != client.isResized(); final boolean resizeableChanged = isResizeable != client.isResized();
boolean changed = false;
if (resizeableChanged) if (resizeableChanged)
{ {
isResizeable = client.isResized(); isResizeable = client.isResized();
return true; changed = true;
} }
final boolean chatboxHiddenChanged = chatboxHidden != (widget != null && widget.isHidden()); final boolean chatboxBoundsChanged = chatbox == null || !chatbox.getBounds().equals(chatboxBounds);
if (chatboxBoundsChanged)
{
chatboxBounds = chatbox != null ? chatbox.getBounds() : new Rectangle();
changed = true;
}
final boolean chatboxHiddenChanged = chatboxHidden != (chatbox == null || chatbox.isHidden());
if (chatboxHiddenChanged) if (chatboxHiddenChanged)
{ {
chatboxHidden = widget != null && widget.isHidden(); chatboxHidden = chatbox == null || chatbox.isHidden();
return true; changed = true;
} }
final boolean viewportChanged = !client.getViewportWidget().getBounds().equals(viewportBounds); final boolean viewportChanged = !client.getViewportWidget().getBounds().equals(viewportBounds);
@@ -520,34 +530,36 @@ public class OverlayRenderer extends MouseListener implements KeyListener
if (viewportChanged) if (viewportChanged)
{ {
viewportBounds = client.getViewportWidget().getBounds(); viewportBounds = client.getViewportWidget().getBounds();
return true; changed = true;
} }
return false; return changed;
} }
private OverlayBounds buildSnapCorners() private OverlayBounds buildSnapCorners()
{ {
final Client client = clientProvider.get();
final Rectangle bounds = viewportBounds != null
? viewportBounds
: new Rectangle(0, 0, client.getCanvas().getWidth(), client.getCanvas().getHeight());
final Widget chatbox = client.getWidget(WidgetInfo.CHATBOX_MESSAGES);
final Rectangle chatboxBounds = chatbox != null
? chatbox.getBounds() : new Rectangle(0, bounds.height, 519, 165);
final Point topLeftPoint = new Point( final Point topLeftPoint = new Point(
isResizeable ? BORDER_LEFT_RESIZABLE : BORDER_LEFT_FIXED, isResizeable ? BORDER_LEFT_RESIZABLE : BORDER_LEFT_FIXED,
isResizeable ? BORDER_TOP_RESIZABLE : BORDER_TOP_FIXED); isResizeable ? BORDER_TOP_RESIZABLE : BORDER_TOP_FIXED);
final Point topRightPoint = new Point(bounds.x + bounds.width - BORDER_RIGHT, BORDER_TOP_FIXED);
final Point bottomLeftPoint = new Point(isResizeable ? BORDER_LEFT_RESIZABLE : BORDER_LEFT_FIXED, bounds.y + bounds.height - BORDER_BOTTOM); final Point topRightPoint = new Point(
final Point bottomRightPoint = new Point(bounds.x + bounds.width - BORDER_RIGHT, bounds.y + bounds.height - BORDER_BOTTOM); viewportBounds.x + viewportBounds.width - BORDER_RIGHT,
final Point rightChatboxPoint = new Point(bounds.x + chatboxBounds.width - BORDER_RIGHT, bounds.y + bounds.height - BORDER_BOTTOM); BORDER_TOP_FIXED);
final Point bottomLeftPoint = new Point(
isResizeable ? BORDER_LEFT_RESIZABLE : BORDER_LEFT_FIXED,
viewportBounds.y + viewportBounds.height - BORDER_BOTTOM);
final Point bottomRightPoint = new Point(
viewportBounds.x + viewportBounds.width - BORDER_RIGHT,
viewportBounds.y + viewportBounds.height - BORDER_BOTTOM);
final Point rightChatboxPoint = new Point(
viewportBounds.x + chatboxBounds.width - BORDER_RIGHT,
viewportBounds.y + viewportBounds.height - BORDER_BOTTOM);
// Check to see if chat box is minimized // Check to see if chat box is minimized
if (chatbox != null && isResizeable && chatboxHidden) if (isResizeable && chatboxHidden)
{ {
rightChatboxPoint.y += chatboxBounds.height; rightChatboxPoint.y += chatboxBounds.height;
bottomLeftPoint.y += chatboxBounds.height; bottomLeftPoint.y += chatboxBounds.height;