From c681b0a1e0763eada67b4a48008dbcd6fb6bc735 Mon Sep 17 00:00:00 2001 From: Valkryst Date: Mon, 14 May 2018 11:42:55 -0300 Subject: [PATCH] Fixes issue where components on layers weren't working correctly with the intersects function of the component class. --- src/com/valkryst/VTerminal/Screen.java | 31 +++++++++++++++++-- .../VTerminal/component/Component.java | 11 ++++--- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/com/valkryst/VTerminal/Screen.java b/src/com/valkryst/VTerminal/Screen.java index cd26b053f7..2c9a70520b 100644 --- a/src/com/valkryst/VTerminal/Screen.java +++ b/src/com/valkryst/VTerminal/Screen.java @@ -365,6 +365,9 @@ public void draw() { /** * Adds a component to the screen. * + * This function should never be called when the parameter is a layer within a layer. It will throw-off + * the algorithm that adjusts the bounding box offsets for all of the components. + * * @param component * The component. */ @@ -374,9 +377,7 @@ public void addComponent(final Component component) { } if (component instanceof Layer) { - for (final Component layerComponent : ((Layer) component).getComponents()) { - addComponent(layerComponent); - } + addLayerComponent((Layer) component, new Point(0, 0)); } // Add the component @@ -396,6 +397,30 @@ public void addComponent(final Component component) { } } + /** + * Adds the components of a layer to the screen. + * + * @param layer + * The layer. + * + * @param boundingBoxOffset + * The offset to apply to each of the layer's components, so their intersection functions work + * correctly. + */ + private void addLayerComponent(final Layer layer, final Point boundingBoxOffset) { + for (final Component component : layer.getComponents()) { + if (component instanceof Layer) { + final int x = boundingBoxOffset.x + component.getTiles().getXPosition(); + final int y = boundingBoxOffset.y + component.getTiles().getYPosition(); + + addLayerComponent((Layer) component, new Point(x, y)); + } else { + component.setBoundingBoxOffset(boundingBoxOffset); + addComponent(component); + } + } + } + /** * Removes a component from the screen. * diff --git a/src/com/valkryst/VTerminal/component/Component.java b/src/com/valkryst/VTerminal/component/Component.java index dd72ef6314..897f66b1b6 100644 --- a/src/com/valkryst/VTerminal/component/Component.java +++ b/src/com/valkryst/VTerminal/component/Component.java @@ -18,6 +18,9 @@ public class Component { /** The tiles. */ @Getter protected final TileGrid tiles; + // todo Java Doc + @Getter @Setter private Point boundingBoxOffset = new Point(0, 0); + /** The event listeners. */ protected final List eventListeners = new LinkedList<>(); @@ -67,10 +70,10 @@ public boolean intersects(final Point point) { return false; } - boolean intersects = point.x >= tiles.getXPosition(); - intersects &= point.x < (tiles.getWidth() + tiles.getXPosition()); - intersects &= point.y >= tiles.getYPosition(); - intersects &= point.y < (tiles.getHeight() + tiles.getYPosition()); + boolean intersects = point.x >= (tiles.getXPosition() + boundingBoxOffset.x); + intersects &= point.x < (tiles.getWidth() + tiles.getXPosition() + boundingBoxOffset.x); + intersects &= point.y >= (tiles.getYPosition() + boundingBoxOffset.y); + intersects &= point.y < (tiles.getHeight() + tiles.getYPosition() + boundingBoxOffset.y); return intersects; }