Skip to content

Commit

Permalink
Dynamic Field Colors + Dark Mode Listener (#102)
Browse files Browse the repository at this point in the history
* allow for dynamic field colors

* fix checkstyle errors
  • Loading branch information
Rdeisenroth authored Jun 17, 2024
1 parent 7885d9d commit 7a6ea3c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
32 changes: 24 additions & 8 deletions src/main/java/fopbot/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.awt.Color;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Supplier;

/**
* A single field in a 2D world where field entities can be placed.
Expand All @@ -18,7 +19,8 @@ public class Field {
*/
private final List<FieldEntity> entities;

private @Nullable Color fieldColor;
// Lambda that provides color
private Supplier<@Nullable Color> fieldColorSupplier = () -> null;

private final KarelWorld world;

Expand All @@ -30,10 +32,10 @@ public class Field {
* Constructs a field with the given coordinate and no entities on it.
*
* @param world the world
* @param x the x-coordinate
* @param y the x-coordinate
* @param x the x-coordinate
* @param y the x-coordinate
*/
public Field(KarelWorld world, int x, int y) {
public Field(final KarelWorld world, final int x, final int y) {
entities = new LinkedList<>();
this.world = world;
this.x = x;
Expand All @@ -48,7 +50,7 @@ public Field(KarelWorld world, int x, int y) {
* @param y the x-coordinate
* @param entities the entities
*/
public Field(KarelWorld world, int x, int y, List<FieldEntity> entities) {
public Field(final KarelWorld world, final int x, final int y, final List<FieldEntity> entities) {
this(world, x, y);
this.entities.addAll(entities);
}
Expand All @@ -62,6 +64,21 @@ public List<FieldEntity> getEntities() {
return entities;
}

/**
* Sets the background color of this {@link Field} to the color provided by the specified
* {@link Supplier}.
* <p>If the specified {@link Supplier} returns {@code null}, the background color of this {@link Field}
* will be reset to the default color.</p>
* <p>Use this method to set the background color of this {@link Field} dynamically.</p>
*
* @param fieldColorSupplier the {@link Supplier} that provides the background color of this {@link Field}
* or {@code null} to reset the background color to the default color
*/
public void setFieldColor(final Supplier<@Nullable Color> fieldColorSupplier) {
this.fieldColorSupplier = fieldColorSupplier;
world.getGuiPanel().updateGui();
}

/**
* Sets the background color of this {@link Field} to the specified color.
* <p>If the specified color is {@code null}, the background color of this {@link Field}
Expand All @@ -70,8 +87,7 @@ public List<FieldEntity> getEntities() {
* @param fieldColor the new background color of this {@link Field}
*/
public void setFieldColor(final @Nullable Color fieldColor) {
this.fieldColor = fieldColor;
world.getGuiPanel().updateGui();
setFieldColor(() -> fieldColor);
}

/**
Expand All @@ -80,7 +96,7 @@ public void setFieldColor(final @Nullable Color fieldColor) {
* @return the background color of this {@link Field}
*/
public @Nullable Color getFieldColor() {
return fieldColor;
return fieldColorSupplier.get();
}

/**
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/fopbot/GuiPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -28,7 +30,6 @@
import java.util.Objects;
import java.util.Set;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

Expand Down Expand Up @@ -77,6 +78,11 @@ public class GuiPanel extends JPanel {
*/
private boolean darkMode = false;

/**
* Listeners for dark mode changes.
*/
private final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

/**
* Constructs and initializes graphical use interface to represent the FOP Bot world.
*
Expand Down Expand Up @@ -483,13 +489,23 @@ public boolean isDarkMode() {
return darkMode;
}

/**
* Adds a listener for dark mode changes.
*
* @param listener the listener to add
*/
public void addDarkModeChangeListener(final PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(listener);
}

/**
* Sets whether the dark mode is enabled.
*
* @param darkMode true if the dark mode is enabled, false otherwise
*/
public void setDarkMode(final boolean darkMode) {
this.darkMode = darkMode;
propertyChangeSupport.firePropertyChange("darkMode", !darkMode, darkMode);
updateGui();
}

Expand Down

0 comments on commit 7a6ea3c

Please sign in to comment.