Skip to content

Commit

Permalink
Merge pull request rvandoosselaer#15 from rvandoosselaer/feature-page…
Browse files Browse the repository at this point in the history
…rlistener

add a listener that gets triggered when a page is attached, detached and updated.
  • Loading branch information
rvandoosselaer authored Jan 3, 2020
2 parents 9cf80f5 + 557f723 commit bccc30e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.rvandoosselaer.blocks.Block;
import com.rvandoosselaer.blocks.BlockIds;
import com.rvandoosselaer.blocks.BlocksConfig;
Expand All @@ -19,6 +20,7 @@
import com.rvandoosselaer.blocks.ChunkManager;
import com.rvandoosselaer.blocks.ChunkManagerState;
import com.rvandoosselaer.blocks.ChunkPagerState;
import com.rvandoosselaer.blocks.PagerListener;
import com.simsilica.lemur.GuiGlobals;
import com.simsilica.lemur.style.BaseStyles;
import com.simsilica.mathd.Vec3i;
Expand Down Expand Up @@ -76,6 +78,7 @@ public void simpleInitApp() {
.build();

stateManager.attachAll(new ChunkManagerState(chunkManager), new ChunkPagerState(rootNode, chunkManager));
stateManager.getState(ChunkPagerState.class).getChunkPager().addListener(new PagerListenerOutput());

hideCursor();

Expand All @@ -95,6 +98,25 @@ private void hideCursor() {
inputManager.setCursorVisible(false);
}

private static class PagerListenerOutput implements PagerListener<Node> {

@Override
public void onPageDetached(Vec3i location, Node page) {
System.out.println("Chunk detached - " + location + " - " + page);
}

@Override
public void onPageAttached(Vec3i location, Node page) {
System.out.println("Chunk attached - " + location + " - " + page);
}

@Override
public void onPageUpdated(Vec3i location, Node oldPage, Node newPage) {
System.out.println("Chunk updated - " + location + " - " + " old page: " + oldPage + ", new page: " + newPage);
}

}

private static class ChunkNoiseGenerator implements ChunkGenerator {

private final long seed;
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/rvandoosselaer/blocks/Pager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArrayList;

/**
* An abstract implementation of a 3D pager. Based on the given center location ({@link #setLocation(Vector3f)} the pages
Expand Down Expand Up @@ -59,6 +61,7 @@ public abstract class Pager<T> {
@Setter
protected Vec3i gridUpperBounds = new Vec3i(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
private ChunkManagerListener listener = new ChunkPagerListener();
protected List<PagerListener<T>> pagerListeners = new CopyOnWriteArrayList<>();

public void initialize() {
if (log.isTraceEnabled()) {
Expand Down Expand Up @@ -95,6 +98,14 @@ public void cleanup() {
listener = null;
}

public void addListener(@NonNull PagerListener<T> listener) {
pagerListeners.add(listener);
}

public void removeListener(@NonNull PagerListener<T> listener) {
pagerListeners.remove(listener);
}

/**
* Calculates and returns the pages (chunks) in the grid, based on the gridSize and centerPageLocation in the grid.
*
Expand Down Expand Up @@ -210,6 +221,12 @@ private void detachPageAtLocation(Vec3i pageLocation) {
// cause problems when a chunk that is attached to the scenegraph is removed from the cache. By manually
// evicting a chunk that is safely detached, we try to counter this behaviour.
chunkManager.removeChunk(pageLocation);

notifyListenersPageDetached(pageLocation, page);
}

private void notifyListenersPageDetached(Vec3i pageLocation, T page) {
pagerListeners.forEach(listener -> listener.onPageDetached(pageLocation, page));
}

/**
Expand Down Expand Up @@ -240,6 +257,12 @@ private void attachPageAtLocation(Vec3i pageLocation) {

attachPage(page);
attachedPages.put(pageLocation, page);

notifyListenersPageAttached(pageLocation, page);
}

private void notifyListenersPageAttached(Vec3i pageLocation, T page) {
pagerListeners.forEach(listener -> listener.onPageAttached(pageLocation, page));
}

private void requestPage(Vec3i pageLocation) {
Expand Down Expand Up @@ -287,6 +310,12 @@ private void updatePages() {

attachPage(newPage);
attachedPages.put(pageLocation, newPage);

notifyListenersPageUpdated(pageLocation, oldPage, newPage);
}

private void notifyListenersPageUpdated(Vec3i pageLocation, T oldPage, T newPage) {
pagerListeners.forEach(listener -> listener.onPageUpdated(pageLocation, oldPage, newPage));
}

private void setCenterPage(Vec3i centerPage) {
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/rvandoosselaer/blocks/PagerListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.rvandoosselaer.blocks;

import com.simsilica.mathd.Vec3i;

/**
* A listener that can be registered to the {@link Pager}. Use this to get notified when pages are attached, detached
* or updated.
*
* @author: rvandoosselaer
*/
public interface PagerListener<T> {

void onPageDetached(Vec3i location, T page);

void onPageAttached(Vec3i location, T page);

void onPageUpdated(Vec3i location, T oldPage, T newPage);

}

0 comments on commit bccc30e

Please sign in to comment.