Skip to content

Commit

Permalink
Fixes issue where components on layers weren't working correctly with…
Browse files Browse the repository at this point in the history
… the intersects function of the component class.
  • Loading branch information
Valkryst committed May 14, 2018
1 parent 50e4b68 commit c681b0a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
31 changes: 28 additions & 3 deletions src/com/valkryst/VTerminal/Screen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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
Expand All @@ -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.
*
Expand Down
11 changes: 7 additions & 4 deletions src/com/valkryst/VTerminal/component/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<EventListener> eventListeners = new LinkedList<>();

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit c681b0a

Please sign in to comment.