Skip to content

Commit

Permalink
refactor: use Subscribable instead of Observer
Browse files Browse the repository at this point in the history
* this allows for property-specific updates
  • Loading branch information
jdrueckert committed Apr 7, 2024
1 parent 1f2d83c commit c1eeb55
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
import org.terasology.reflection.reflect.ReflectFactory;
import org.terasology.reflection.reflect.ReflectionReflectFactory;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.nio.ByteBuffer;
import java.util.HashSet;
import java.util.List;
Expand All @@ -91,7 +93,7 @@
* Sets up the Universe for a user. Displays a list of {@link WorldGenerator}
* for a particular game template.
*/
public class UniverseSetupScreen extends CoreScreenLayer implements UISliderOnChangeTriggeredListener, Observer<WorldConfigurator> {
public class UniverseSetupScreen extends CoreScreenLayer implements UISliderOnChangeTriggeredListener, PropertyChangeListener {
public static final ResourceUrn ASSET_URI = new ResourceUrn("engine:universeSetupScreen");

@In
Expand Down Expand Up @@ -307,11 +309,15 @@ public void update(float delta) {
}

/**
Called whenever there's a change to WorldConfigurator
Called whenever there's a change to WorldConfigurator properties
Provides information about the changed property as well as old and new value in {@link PropertyChangeEvent}
*/
@Override
public void update(WorldConfigurator layer) {
previewUpdateRequiredSince = time.getRealTimeInMs();
public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
System.out.println(propertyChangeEvent.getPropertyName());
if (propertyChangeEvent.getPropertyName() == "Trees") {
previewUpdateRequiredSince = time.getRealTimeInMs();
}
}

private void setSeed(String value) {
Expand Down Expand Up @@ -498,7 +504,7 @@ private void configureProperties() {
worldConfig = worldGenerator.getConfigurator();
universe.setWorldConfigurator(worldConfig);
}
worldConfig.addObserver(this);
worldConfig.subscribe(this);

Map<String, Component> params = worldConfig.getProperties();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
package org.terasology.engine.world.generation;

import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset;
import com.google.common.collect.SetMultimap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.engine.core.Observer;
import org.terasology.engine.world.generator.WorldConfigurator;
import org.terasology.gestalt.entitysystem.component.Component;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -23,7 +29,7 @@ public class FacetedWorldConfigurator implements WorldConfigurator {
private final Map<String, Component> properties = Maps.newHashMap();

private final List<ConfigurableFacetProvider> providers;
private Set<Observer<WorldConfigurator>> observers = new HashSet<>();
private HashMap<String, Set<PropertyChangeListener>> listeners = new HashMap<>();

public FacetedWorldConfigurator(List<ConfigurableFacetProvider> providersList) {
for (ConfigurableFacetProvider provider : providersList) {
Expand All @@ -45,10 +51,22 @@ public void setProperty(String key, Component comp) {
for (ConfigurableFacetProvider facetProvider : providers) {
if (key.equals(facetProvider.getConfigurationName())) {
facetProvider.setConfiguration(comp);
Component oldComp = properties.get(key);
properties.put(key, comp);
observers.forEach(observer -> {
observer.update(this);
});

PropertyChangeEvent event = new PropertyChangeEvent(this, key, oldComp, comp);
Set<PropertyChangeListener> allPropListeners = listeners.get("*");
if (allPropListeners != null) {
allPropListeners.forEach(listener -> {
listener.propertyChange(event);
});
}
Set<PropertyChangeListener> specPropListeners = listeners.get(key);
if (specPropListeners != null) {
specPropListeners.forEach(listener -> {
listener.propertyChange(event);
});
}
return;
}
}
Expand All @@ -57,12 +75,30 @@ public void setProperty(String key, Component comp) {
}

@Override
public void addObserver(Observer<WorldConfigurator> observer) {
observers.add(observer);
public void subscribe(PropertyChangeListener changeListener) {
Set<PropertyChangeListener> ls = listeners.computeIfAbsent("*", k -> new HashSet<>());
ls.add(changeListener);
}

@Override
public void unsubscribe(PropertyChangeListener changeListener) {
listeners.computeIfPresent("*", (k, v) -> {
v.remove(changeListener);
return v;
});
}

@Override
public void subscribe(String propertyName, PropertyChangeListener changeListener) {
Set<PropertyChangeListener> ls = listeners.computeIfAbsent(propertyName, k -> new HashSet<>());
ls.add(changeListener);
}

@Override
public void removeObserver(Observer<WorldConfigurator> observer) {
observers.remove(observer);
public void unsubscribe(String propertyName, PropertyChangeListener changeListener) {
listeners.computeIfPresent(propertyName, (k, v) -> {
v.remove(changeListener);
return v;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package org.terasology.engine.world.generator;

import org.terasology.engine.core.Observer;
import org.terasology.engine.utilities.subscribables.Subscribable;
import org.terasology.gestalt.entitysystem.component.Component;

import java.util.Map;
Expand All @@ -11,7 +12,7 @@
/**
* Allows for configuration of world generators.
*/
public interface WorldConfigurator {
public interface WorldConfigurator extends Subscribable {

/**
* The values are supposed to be annotated with {@link org.terasology.nui.properties.Property}
Expand All @@ -25,8 +26,4 @@ public interface WorldConfigurator {
* @param comp the configuration component
*/
void setProperty(String key, Component comp);

void addObserver(Observer<WorldConfigurator> observer);

void removeObserver(Observer<WorldConfigurator> observer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.terasology.engine.core.Observer;
import org.terasology.gestalt.entitysystem.component.Component;

import java.beans.PropertyChangeListener;
import java.util.Collections;
import java.util.Map;

Expand All @@ -24,12 +25,22 @@ public void setProperty(String key, Component comp) {
}

@Override
public void addObserver(Observer<WorldConfigurator> observer) {
public void subscribe(PropertyChangeListener changeListener) {
// simply ignore
}

@Override
public void removeObserver(Observer<WorldConfigurator> observer) {
public void unsubscribe(PropertyChangeListener changeListener) {
// simply ignore
}

@Override
public void subscribe(String propertyName, PropertyChangeListener changeListener) {
// simply ignore
}

@Override
public void unsubscribe(String propertyName, PropertyChangeListener changeListener) {
// simply ignore
}
}

0 comments on commit c1eeb55

Please sign in to comment.