Skip to content

Commit

Permalink
Resolves issue of components being placed on layers of layers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Valkryst committed May 15, 2018
1 parent 923a8cb commit 6a75016
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions src/com/valkryst/VTerminal/component/Layer.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public void addComponent(final Component component) {

// todo Recursive check to ensure layer isn't a child of the layer being added.

if (component instanceof Layer) {
((Layer) component).setRootScreen(rootScreen);
addLayerComponent((Layer) component, new Point(0, 0));
}

// Add the component
componentsLock.writeLock().lock();
super.tiles.addChild(component.getTiles());
Expand All @@ -105,23 +110,36 @@ public void addComponent(final Component component) {

// Add the component's event listeners
super.eventListeners.addAll(component.getEventListeners());
}

// If the Layer has already been added to a screen, then we need to do
// some special steps to ensure that new components are correctly set-up
// to work with the screen.
if (rootScreen != null) {
// Set the component to use the offset of this Layer
component.setBoundingBoxOffset(super.getBoundingBoxOffset());

// Set the component's redraw function
component.setRedrawFunction(() -> rootScreen.draw());

// Create the component's event listeners
component.createEventListeners(rootScreen);

// Add component's event listeners to root screen.
for (final EventListener listener : component.getEventListeners()) {
rootScreen.addListener(listener);
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();
final Point tmp = new Point(x, y);

component.setBoundingBoxOffset(tmp);
((Layer) component).setRootScreen(rootScreen);
addLayerComponent((Layer) component, tmp);
} else {
// Set the component to use the offset of this Layer
final int x = this.getTiles().getXPosition() + boundingBoxOffset.x + component.getTiles().getXPosition() + layer.getTiles().getXPosition();
final int y = this.getTiles().getYPosition() + boundingBoxOffset.y + component.getTiles().getYPosition() + layer.getTiles().getYPosition();
component.setBoundingBoxOffset(new Point(x, y));

if (rootScreen != null) {
// Set the component's redraw function
component.setRedrawFunction(() -> rootScreen.draw());

// Create the component's event listeners
component.createEventListeners(rootScreen);

// Add component's event listeners to root screen.
for (final EventListener listener : component.getEventListeners()) {
rootScreen.addListener(listener);
}
}
}
}
}
Expand Down

0 comments on commit 6a75016

Please sign in to comment.